2016-02-08 02:51:02 +01:00
|
|
|
package net.frozenorb.apiv3.collections;
|
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2016-02-08 03:06:41 +01:00
|
|
|
import io.vertx.core.AsyncResult;
|
|
|
|
import io.vertx.core.Handler;
|
|
|
|
import lombok.Getter;
|
2016-02-08 02:52:03 +01:00
|
|
|
import lombok.ToString;
|
2016-02-08 02:54:27 +01:00
|
|
|
import net.frozenorb.apiv3.LiteFullJson;
|
2016-02-08 03:06:41 +01:00
|
|
|
import net.frozenorb.apiv3.utils.MongoUtils;
|
2016-02-12 01:10:46 +01:00
|
|
|
import org.bson.Document;
|
2016-02-08 03:06:41 +01:00
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
import java.time.Instant;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
2016-02-08 02:52:03 +01:00
|
|
|
|
|
|
|
@ToString
|
2016-02-08 02:54:27 +01:00
|
|
|
public final class Grant implements LiteFullJson {
|
2016-02-08 02:51:02 +01:00
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
private static final String COLLECTION_NAME = "grant";
|
2016-02-08 02:51:02 +01:00
|
|
|
|
2016-02-08 03:06:41 +01:00
|
|
|
@Getter private String id;
|
2016-02-12 01:10:46 +01:00
|
|
|
@Getter private UUID target;
|
|
|
|
@Getter private String reason;
|
|
|
|
@Getter private Set<String> scopes;
|
|
|
|
@Getter private String rank;
|
|
|
|
@Getter private UUID addedBy;
|
|
|
|
@Getter private Instant addedAt;
|
|
|
|
|
|
|
|
@Getter private UUID removedBy;
|
|
|
|
@Getter private Instant removedAt;
|
|
|
|
@Getter private String removalReason;
|
|
|
|
|
|
|
|
public static void findAll(Handler<AsyncResult<Collection<Grant>>> callback) {
|
|
|
|
MongoUtils.findAndTransform(COLLECTION_NAME, new Document(), Grant::new, callback);
|
2016-02-08 03:06:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void findById(String id, Handler<AsyncResult<Grant>> callback) {
|
2016-02-12 01:10:46 +01:00
|
|
|
MongoUtils.findOneAndTransform(COLLECTION_NAME, new Document("_id", id), Grant::new, callback);
|
2016-02-08 03:06:41 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
public static void findByTarget(UUID target, Handler<AsyncResult<Collection<Grant>>> callback) {
|
|
|
|
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("target", target.toString()), Grant::new, callback);
|
2016-02-08 03:06:41 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
public static void findByAddedBy(UUID addedBy, Handler<AsyncResult<Collection<Grant>>> callback) {
|
|
|
|
MongoUtils.findAndTransform(COLLECTION_NAME, new Document("addedBy", addedBy.toString()), Grant::new, callback);
|
2016-02-08 03:06:41 +01:00
|
|
|
}
|
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
private Grant(Document json) {
|
|
|
|
this.id = json.getString("_id");
|
|
|
|
this.target = UUID.fromString(json.getString("target"));
|
|
|
|
this.reason = json.getString("reason");
|
|
|
|
this.scopes = ImmutableSet.copyOf((Collection) json.get("scopes")); // This is a safe cast, the collection's type is always String
|
|
|
|
this.rank = json.getString("rank");
|
|
|
|
this.addedBy = UUID.fromString(json.getString("addedBy"));
|
|
|
|
this.addedAt = (Instant) json.get("addedAt");
|
|
|
|
|
|
|
|
if (json.containsKey("removedBy")) {
|
|
|
|
this.removedBy = UUID.fromString(json.getString("removedBy"));
|
|
|
|
this.removedAt = (Instant) json.get("removedAt");
|
|
|
|
this.removalReason = json.getString("removalReason");
|
|
|
|
}
|
|
|
|
}
|
2016-02-08 03:06:41 +01:00
|
|
|
|
2016-02-12 01:10:46 +01:00
|
|
|
public Document toLiteJson() {
|
|
|
|
Document json = new Document();
|
|
|
|
|
|
|
|
json.put("id", id);
|
|
|
|
json.put("target", target.toString());
|
|
|
|
json.put("reason", reason);
|
|
|
|
json.put("scopes", scopes);
|
|
|
|
json.put("rank", rank);
|
|
|
|
json.put("addedBy", addedBy.toString());
|
|
|
|
json.put("addedAt", addedAt.toString());
|
|
|
|
|
|
|
|
if (removedBy != null) {
|
|
|
|
json.put("removedBy", removedBy.toString());
|
|
|
|
json.put("removedAt", removedAt.toString());
|
|
|
|
json.put("removalReason", removalReason);
|
|
|
|
}
|
2016-02-08 03:06:41 +01:00
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2016-02-08 02:51:02 +01:00
|
|
|
}
|