List of usage examples for com.mongodb QueryBuilder start
public static QueryBuilder start(final String key)
From source file:org.entcore.timeline.services.impl.DefaultTimelineMailerService.java
License:Open Source License
private void getRecipientsUsers(Date from, final Handler<JsonArray> handler) { final JsonObject aggregation = new JsonObject(); JsonArray pipeline = new fr.wseduc.webutils.collections.JsonArray(); aggregation.put("aggregate", "timeline").put("allowDiskUse", true).put("pipeline", pipeline); JsonObject matcher = MongoQueryBuilder.build(QueryBuilder.start("date").greaterThanEquals(from)); JsonObject grouper = new JsonObject( "{ \"_id\" : \"notifiedUsers\", \"recipients\" : {\"$addToSet\" : \"$recipients.userId\"}}"); pipeline.add(new JsonObject().put("$match", matcher)); pipeline.add(new JsonObject().put("$unwind", "$recipients")); pipeline.add(new JsonObject().put("$group", grouper)); mongo.command(aggregation.toString(), new Handler<Message<JsonObject>>() { @Override//from www .j a v a 2s . c om public void handle(Message<JsonObject> event) { if ("error".equals(event.body().getString("status", "error"))) { handler.handle(new fr.wseduc.webutils.collections.JsonArray()); } else { JsonArray r = event.body().getJsonObject("result", new JsonObject()).getJsonArray("result"); if (r != null && r.size() > 0) { handler.handle(r.getJsonObject(0).getJsonArray("recipients", new fr.wseduc.webutils.collections.JsonArray())); } else { handler.handle(new fr.wseduc.webutils.collections.JsonArray()); } } } }); }
From source file:org.entcore.timeline.services.impl.DefaultTimelineMailerService.java
License:Open Source License
/** * Retrieves an aggregated list of notifications from mongodb for a single user. * * Notifications are grouped by type & event-type. * @param userId : Userid/*from www.j a v a 2s. c o m*/ * @param from : Starting date in the past * @param handler: Handles the notifications */ private void getAggregatedUserNotifications(String userId, Date from, final Handler<JsonArray> handler) { final JsonObject aggregation = new JsonObject(); JsonArray pipeline = new fr.wseduc.webutils.collections.JsonArray(); aggregation.put("aggregate", "timeline").put("allowDiskUse", true).put("pipeline", pipeline); JsonObject matcher = MongoQueryBuilder.build(QueryBuilder.start("recipients") .elemMatch(QueryBuilder.start("userId").is(userId).get()).and("date").greaterThanEquals(from)); JsonObject grouper = new JsonObject( "{ \"_id\" : { \"type\": \"$type\", \"event-type\": \"$event-type\"}, \"count\": { \"$sum\": 1 } }"); JsonObject transformer = new JsonObject( "{ \"type\": \"$_id.type\", \"event-type\": \"$_id.event-type\", \"count\": 1, \"_id\": 0 }"); pipeline.add(new JsonObject().put("$match", matcher)); pipeline.add(new JsonObject().put("$group", grouper)); pipeline.add(new JsonObject().put("$project", transformer)); mongo.command(aggregation.toString(), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if ("error".equals(event.body().getString("status", "error"))) { handler.handle(new fr.wseduc.webutils.collections.JsonArray()); } else { handler.handle(event.body().getJsonObject("result", new JsonObject()).getJsonArray("result", new fr.wseduc.webutils.collections.JsonArray())); } } }); }
From source file:org.entcore.timeline.services.impl.FlashMsgServiceMongoImpl.java
License:Open Source License
@Override public void deleteMultiple(List<String> ids, Handler<Either<String, JsonObject>> handler) { QueryBuilder q = QueryBuilder.start("_id").in(new fr.wseduc.webutils.collections.JsonArray(ids)); mongo.delete(collection, MongoQueryBuilder.build(q), validActionResultHandler(handler)); }
From source file:org.entcore.timeline.services.impl.FlashMsgServiceMongoImpl.java
License:Open Source License
@Override public void listForUser(UserInfos user, String lang, String domain, Handler<Either<String, JsonArray>> handler) { String profile = user.getType(); String now = mongoFormat.format(new Date()); QueryBuilder query = QueryBuilder.start("contents." + lang).exists(true); query.put("contents." + lang).notEquals(""); query.put("profiles").is(profile); query.put("markedAsRead").notEquals(user.getUserId()); query.put("startDate").lessThanEquals(now); query.put("endDate").greaterThan(now); query.put("domain").is(domain); JsonObject sort = new JsonObject().put("modified", -1); JsonObject keys = new JsonObject().put("contents", 1).put("color", 1).put("customColor", 1); //Eventually add a limit. mongo.find(collection, MongoQueryBuilder.build(query), sort, keys, MongoDbResult.validResultsHandler(handler)); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
public void getParentRights(final String parentName, final String parentFolder, final String owner, final Handler<Either<String, JsonArray>> result) { QueryBuilder parentFolderQuery = QueryBuilder.start("owner").is(owner).and("name").is(parentName) .and("folder").is(parentFolder); mongo.findOne(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(parentFolderQuery), new Handler<Message<JsonObject>>() { @Override/*from w ww. j av a2 s . co m*/ public void handle(Message<JsonObject> event) { if ("ok".equals(event.body().getString("status")) && event.body().containsKey("result")) { JsonObject parent = event.body().getJsonObject("result"); JsonArray parentSharedRights = parent != null ? parent.getJsonArray("shared", null) : null; result.handle(new Either.Right<String, JsonArray>(parentSharedRights)); } else { result.handle(new Either.Left<String, JsonArray>("workspace.folder.not.found")); } } }); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
@Override public void create(String name, final String path, String application, final UserInfos owner, final Handler<Either<String, JsonObject>> result) { if (owner == null) { result.handle(new Either.Left<String, JsonObject>("workspace.invalid.user")); return;/*www. j a v a2s.com*/ } if (name == null || name.trim().isEmpty()) { result.handle(new Either.Left<String, JsonObject>("workspace.invalid.folder.name")); return; } final JsonObject doc = new JsonObject(); String now = MongoDb.formatDate(new Date()); doc.put("created", now); doc.put("modified", now); doc.put("owner", owner.getUserId()); doc.put("ownerName", owner.getUsername()); doc.put("name", name); String folder; if (path != null && !path.trim().isEmpty()) { folder = path + "_" + name; } else { folder = name; } doc.put("folder", folder); doc.put("application", getOrElse(application, WorkspaceService.WORKSPACE_NAME)); QueryBuilder alreadyExist = QueryBuilder.start("owner").is(owner.getUserId()).put("folder").is(folder); mongo.count(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(alreadyExist), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if ("ok".equals(event.body().getString("status")) && event.body().getInteger("count") == 0) { if (path == null || path.trim().isEmpty()) { mongo.save(DOCUMENTS_COLLECTION, doc, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> res) { result.handle(Utils.validResult(res)); } }); } else { //If the folder has a parent folder, replicate sharing rights String[] splittedPath = path.split("_"); String parentName = splittedPath[splittedPath.length - 1]; String parentFolder = path; getParentRights(parentName, parentFolder, owner, new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { final JsonArray parentSharedRights = event.right() == null || event.isLeft() ? null : event.right().getValue(); if (parentSharedRights != null) doc.put("shared", parentSharedRights); mongo.save(DOCUMENTS_COLLECTION, doc, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> res) { result.handle(Utils.validResult(res)); } }); } }); } } else { result.handle(new Either.Left<String, JsonObject>("workspace.folder.already.exists")); } } }); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
@Override public void move(String id, final String path, final UserInfos owner, final Handler<Either<String, JsonObject>> result) { if (owner == null) { result.handle(new Either.Left<String, JsonObject>("workspace.invalid.user")); return;/*from ww w . j av a2s . c o m*/ } if (id == null || id.trim().isEmpty()) { result.handle(new Either.Left<String, JsonObject>("workspace.folder.not.found")); return; } QueryBuilder query = QueryBuilder.start("_id").is(id).put("owner").is(owner.getUserId()); JsonObject keys = new JsonObject().put("folder", 1).put("name", 1); mongo.findOne(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(query), keys, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { final String folder = event.body().getJsonObject("result", new JsonObject()) .getString("folder"); final String name = event.body().getJsonObject("result", new JsonObject()) .getString("name"); if ("ok".equals(event.body().getString("status")) && folder != null && !folder.trim().isEmpty() && name != null && !name.trim().isEmpty()) { if (path != null && path.startsWith(folder)) { result.handle(new Either.Left<String, JsonObject>( "workspace.forbidden.move.folder.in.itself")); } else { String dest; if (path != null && !path.trim().isEmpty()) { dest = path + "_" + name; } else { dest = name; } QueryBuilder q = QueryBuilder.start("owner").is(owner.getUserId()).put("folder") .is(dest); mongo.count(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(q), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if ("ok".equals(event.body().getString("status")) && event.body().getInteger("count") == 0) { recursiveMove(folder, name, owner.getUserId(), path, result); } else { result.handle(new Either.Left<String, JsonObject>( "workspace.folder.already.exists")); } } }); } } else { result.handle(new Either.Left<String, JsonObject>("workspace.folder.not.found")); } } }); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
private void recursiveMove(final String folder, final String name, final String owner, final String path, final Handler<Either<String, JsonObject>> result) { final String folderAttr = "Trash".equals(path) ? "old-folder" : "folder"; final QueryBuilder q = QueryBuilder.start("owner").is(owner).put(folderAttr) .regex(Pattern.compile("^" + Pattern.quote(folder) + "($|_)")); //If the folder has a parent folder, replicate sharing rights String[] splittedPath = path.split("_"); String parentName = splittedPath[splittedPath.length - 1]; String parentFolder = path;//from w w w. j a v a 2 s . c o m getParentRights(parentName, parentFolder, owner, new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { final JsonArray parentSharedRights = event.right() == null || event.isLeft() ? null : event.right().getValue(); mongo.distinct(DOCUMENTS_COLLECTION, folderAttr, MongoQueryBuilder.build(q), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> d) { JsonArray directories = d.body().getJsonArray("values", new fr.wseduc.webutils.collections.JsonArray()); if ("ok".equals(d.body().getString("status")) && directories.size() > 0) { final AtomicInteger remaining = new AtomicInteger(directories.size()); final AtomicInteger count = new AtomicInteger(0); String dest; if (path != null && !path.trim().isEmpty()) { dest = path + "_" + name; } else { dest = name; } for (Object o : directories) { if (!(o instanceof String)) continue; String dir = (String) o; QueryBuilder qf = QueryBuilder.start("owner").is(owner).put(folderAttr) .is(dir); MongoUpdateBuilder modifier = new MongoUpdateBuilder(); modifier.set("folder", dir.replaceFirst("^" + Pattern.quote(folder), Matcher.quoteReplacement(dest))); if ("Trash".equals(path) || parentSharedRights == null) modifier.unset("shared"); else modifier.set("shared", parentSharedRights); mongo.update(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(qf), modifier.build(), false, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> res) { count.getAndAdd(res.body().getInteger("number", 0)); if (remaining.decrementAndGet() == 0) { res.body().put("number", count.get()); result.handle(Utils.validResult(res)); } } }); } } else { result.handle( new Either.Left<String, JsonObject>("workspace.folder.not.found")); } } }); } }); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
@Override public void copy(final String id, final String n, final String p, final UserInfos owner, final long emptySize, final Handler<Either<String, JsonArray>> result) { if (owner == null) { result.handle(new Either.Left<String, JsonArray>("workspace.invalid.user")); return;// w w w . j a va 2s. c o m } if (id == null || id.trim().isEmpty()) { result.handle(new Either.Left<String, JsonArray>("workspace.folder.not.found")); return; } final String path = getOrElse(p, ""); //If the folder has a parent folder, replicate sharing rights String[] splittedPath = path.split("_"); String parentName = splittedPath[splittedPath.length - 1]; String parentFolder = path; getParentRights(parentName, parentFolder, owner, new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { final JsonArray parentSharedRights = event.right() == null || event.isLeft() ? null : event.right().getValue(); QueryBuilder query = QueryBuilder.start("_id").is(id).put("owner").is(owner.getUserId()); JsonObject keys = new JsonObject().put("folder", 1).put("name", 1); mongo.findOne(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(query), keys, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { final String folder = event.body().getJsonObject("result", new JsonObject()) .getString("folder"); final String n1 = event.body().getJsonObject("result", new JsonObject()) .getString("name"); if ("ok".equals(event.body().getString("status")) && folder != null && !folder.trim().isEmpty() && n1 != null && !n1.trim().isEmpty()) { final String folderAttr = "folder"; QueryBuilder q = QueryBuilder.start("owner").is(owner.getUserId()) .put(folderAttr) .regex(Pattern.compile("^" + Pattern.quote(folder) + "($|_)")); mongo.find(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(q), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> src) { final JsonArray origs = src.body().getJsonArray("results", new fr.wseduc.webutils.collections.JsonArray()); if ("ok".equals(src.body().getString("status")) && origs.size() > 0) { long size = 0; for (Object o : origs) { if (!(o instanceof JsonObject)) continue; JsonObject metadata = ((JsonObject) o) .getJsonObject("metadata"); if (metadata != null) { size += metadata.getLong("size", 0l); } } if (size > emptySize) { result.handle(new Either.Left<String, JsonArray>( "files.too.large")); return; } final AtomicInteger number = new AtomicInteger( origs.size()); final JsonArray insert = new fr.wseduc.webutils.collections.JsonArray(); String name = (n != null && !n.trim().isEmpty()) ? n : n1; final String destFolderName = (path != null && !path.trim().isEmpty()) ? path + "_" + name : name; QueryBuilder alreadyExist = QueryBuilder.start("owner") .is(owner.getUserId()).put("folder") .is(destFolderName); mongo.count(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(alreadyExist), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { String destFolder; if ("ok".equals( event.body().getString("status")) && event.body() .getInteger("count") == 0) { destFolder = destFolderName; } else { destFolder = destFolderName + "-" + format.format(new Date()); } for (Object o : origs) { if (!(o instanceof JsonObject)) continue; JsonObject orig = (JsonObject) o; final JsonObject dest = orig.copy(); String now = MongoDb .formatDate(new Date()); dest.remove("_id"); dest.put("created", now); dest.put("modified", now); dest.put("folder", dest .getString("folder", "") .replaceFirst( "^" + Pattern .quote(folder), Matcher.quoteReplacement( destFolder))); dest.put("shared", parentSharedRights); insert.add(dest); String filePath = orig .getString("file"); if (filePath != null) { storage.copyFile(filePath, new Handler<JsonObject>() { @Override public void handle( JsonObject event) { if (event != null && "ok".equals( event.getString( "status"))) { dest.put("file", event.getString( "_id")); persist(insert, number.decrementAndGet()); } } }); } else { persist(insert, number.decrementAndGet()); } } } }); } else { result.handle(new Either.Left<String, JsonArray>( "workspace.folder.not.found")); } } private void persist(final JsonArray insert, int remains) { if (remains == 0) { mongo.insert(DOCUMENTS_COLLECTION, insert, new Handler<Message<JsonObject>>() { @Override public void handle( Message<JsonObject> inserted) { if ("ok".equals(inserted.body() .getString("status"))) { result.handle( new Either.Right<String, JsonArray>( insert)); } else { result.handle( new Either.Left<String, JsonArray>( inserted.body() .getString( "message"))); } } }); } } }); } else { result.handle(new Either.Left<String, JsonArray>("workspace.folder.not.found")); } } }); } }); }
From source file:org.entcore.workspace.service.impl.DefaultFolderService.java
License:Open Source License
@Override public void trash(String id, final UserInfos owner, final Handler<Either<String, JsonObject>> result) { if (owner == null) { result.handle(new Either.Left<String, JsonObject>("workspace.invalid.user")); return;/*from www . j a v a2 s. com*/ } if (id == null || id.trim().isEmpty()) { result.handle(new Either.Left<String, JsonObject>("workspace.folder.not.found")); return; } final QueryBuilder resourceQuery = QueryBuilder.start("_id").is(id); final List<DBObject> groups = new ArrayList<>(); groups.add(QueryBuilder.start("userId").is(owner.getUserId()).get()); for (String gpId : owner.getGroupsIds()) { groups.add(QueryBuilder.start("groupId").is(gpId).get()); } final QueryBuilder rightQuery = new QueryBuilder() .or(QueryBuilder.start("owner").is(owner.getUserId()).get(), QueryBuilder.start("shared") .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()).get()); final QueryBuilder query = new QueryBuilder().and(resourceQuery.get(), rightQuery.get()); JsonObject keys = new JsonObject().put("folder", 1).put("name", 1).put("owner", 1); mongo.findOne(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(query), keys, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { final String folder = event.body().getJsonObject("result", new JsonObject()) .getString("folder"); final String name = event.body().getJsonObject("result", new JsonObject()) .getString("name"); final String owner = event.body().getJsonObject("result", new JsonObject()) .getString("owner"); if ("ok".equals(event.body().getString("status")) && folder != null && !folder.trim().isEmpty()) { QueryBuilder q = QueryBuilder.start("owner").is(owner).put("folder") .regex(Pattern.compile("^" + Pattern.quote(folder) + "($|_)")); MongoUpdateBuilder modifier = new MongoUpdateBuilder(); modifier.rename("folder", "old-folder"); mongo.update(DOCUMENTS_COLLECTION, MongoQueryBuilder.build(q), modifier.build(), false, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> e) { if ("ok".equals(e.body().getString("status"))) { recursiveMove(folder, name, owner, "Trash", result); } else { result.handle(new Either.Left<String, JsonObject>( "workspace.trash.error")); } } }); } else { result.handle(new Either.Left<String, JsonObject>("workspace.folder.not.found")); } } }); }