List of usage examples for com.mongodb QueryBuilder start
public static QueryBuilder start(final String key)
From source file:org.entcore.common.http.filter.ShareAndOwner.java
License:Open Source License
@Override public void authorize(HttpServerRequest request, Binding binding, UserInfos user, Handler<Boolean> handler) { String id = request.params().get(conf.getResourceIdLabel()); if (id != null && !id.trim().isEmpty()) { List<DBObject> groups = new ArrayList<>(); String sharedMethod = binding.getServiceMethod().replaceAll("\\.", "-"); groups.add(QueryBuilder.start("userId").is(user.getUserId()).put(sharedMethod).is(true).get()); for (String gpId : user.getGroupsIds()) { groups.add(QueryBuilder.start("groupId").is(gpId).put(sharedMethod).is(true).get()); }//from www . j a v a 2s . com QueryBuilder query = QueryBuilder.start("_id").is(id).or( QueryBuilder.start("owner.userId").is(user.getUserId()).get(), QueryBuilder.start("shared") .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()) .get()); MongoAppFilter.executeCountQuery(request, conf.getCollection(), MongoQueryBuilder.build(query), 1, handler); } else { handler.handle(false); } }
From source file:org.entcore.common.notification.TimelineMailer.java
License:Open Source License
/** * Retrieves all timeline notifications from mongodb for a single user, from a specific date in the past. * * @param userId : Userid/* w w w . java2 s . co m*/ * @param from : The starting date * @param handler : Handles the notifications */ private void getUserNotifications(String userId, Date from, final Handler<JsonArray> handler) { JsonObject matcher = MongoQueryBuilder.build(QueryBuilder.start("recipients") .elemMatch(QueryBuilder.start("userId").is(userId).get()).and("date").greaterThanEquals(from)); mongo.find("timeline", matcher, new Handler<Message<JsonObject>>() { public void handle(Message<JsonObject> event) { if ("error".equals(event.body().getString("status", "error"))) { handler.handle(new JsonArray()); } else { handler.handle(event.body().getArray("results")); } } }); }
From source file:org.entcore.common.notification.TimelineMailer.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 w w w. j ava 2 s . c om * @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 JsonArray(); aggregation.putString("aggregate", "timeline").putBoolean("allowDiskUse", true).putArray("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.addObject(new JsonObject().putObject("$match", matcher)); pipeline.add(new JsonObject().putObject("$group", grouper)); pipeline.add(new JsonObject().putObject("$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 JsonArray()); } else { handler.handle( event.body().getObject("result", new JsonObject()).getArray("result", new JsonArray())); } } }); }
From source file:org.entcore.common.service.impl.MongoDbCrudService.java
License:Open Source License
@Override public void retrieve(String id, Handler<Either<String, JsonObject>> handler) { QueryBuilder builder = QueryBuilder.start("_id").is(id); mongo.findOne(collection, MongoQueryBuilder.build(builder), defaultRetrieveProjection, validResultHandler(handler)); }
From source file:org.entcore.common.service.impl.MongoDbCrudService.java
License:Open Source License
@Override public void retrieve(String id, UserInfos user, Handler<Either<String, JsonObject>> handler) { QueryBuilder builder = QueryBuilder.start("_id").is(id); if (user == null) { builder.put("visibility").is(VisibilityFilter.PUBLIC.name()); }/* w ww . j a v a 2 s. c om*/ mongo.findOne(collection, MongoQueryBuilder.build(builder), defaultRetrieveProjection, validResultHandler(handler)); }
From source file:org.entcore.common.service.impl.MongoDbCrudService.java
License:Open Source License
@Override public void update(String id, JsonObject data, UserInfos user, Handler<Either<String, JsonObject>> handler) { QueryBuilder query = QueryBuilder.start("_id").is(id); addPlainField(data);/*from w w w .j av a 2s. c om*/ MongoUpdateBuilder modifier = new MongoUpdateBuilder(); for (String attr : data.fieldNames()) { modifier.set(attr, data.getValue(attr)); } modifier.set("modified", MongoDb.now()); mongo.update(collection, MongoQueryBuilder.build(query), modifier.build(), validActionResultHandler(handler)); }
From source file:org.entcore.common.service.impl.MongoDbCrudService.java
License:Open Source License
@Override public void delete(String id, UserInfos user, Handler<Either<String, JsonObject>> handler) { QueryBuilder q = QueryBuilder.start("_id").is(id); mongo.delete(collection, MongoQueryBuilder.build(q), validActionResultHandler(handler)); }
From source file:org.entcore.common.service.impl.MongoDbCrudService.java
License:Open Source License
@Override public void list(VisibilityFilter filter, UserInfos user, Handler<Either<String, JsonArray>> handler) { QueryBuilder query;/*from w w w. ja v a2s .c o m*/ if (user != null) { List<DBObject> groups = new ArrayList<>(); groups.add(QueryBuilder.start("userId").is(user.getUserId()).get()); for (String gpId : user.getGroupsIds()) { groups.add(QueryBuilder.start("groupId").is(gpId).get()); } switch (filter) { case OWNER: query = QueryBuilder.start("owner.userId").is(user.getUserId()); break; case OWNER_AND_SHARED: query = new QueryBuilder().or(QueryBuilder.start("owner.userId").is(user.getUserId()).get(), QueryBuilder.start("shared") .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()) .get()); break; case SHARED: query = QueryBuilder.start("shared") .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()); break; case PROTECTED: query = QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()); break; case PUBLIC: query = QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()); break; default: query = new QueryBuilder().or( QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()).get(), QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(), QueryBuilder.start("owner.userId").is(user.getUserId()).get(), QueryBuilder.start("shared") .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()) .get()); break; } } else { query = QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()); } JsonObject sort = new JsonObject().put("modified", -1); mongo.find(collection, MongoQueryBuilder.build(query), sort, defaultListProjection, validResultsHandler(handler)); }
From source file:org.entcore.common.service.impl.MongoDbCrudService.java
License:Open Source License
public void isOwner(String id, UserInfos user, final Handler<Boolean> handler) { QueryBuilder query = QueryBuilder.start("_id").is(id).put("owner.userId").is(user.getUserId()); mongo.count(collection, MongoQueryBuilder.build(query), new Handler<Message<JsonObject>>() { @Override//w w w. j a v a2 s . c o m public void handle(Message<JsonObject> event) { JsonObject res = event.body(); handler.handle(res != null && "ok".equals(res.getString("status")) && 1 == res.getInteger("count")); } }); }
From source file:org.entcore.common.service.impl.MongoDbRepositoryEvents.java
License:Open Source License
@Override public void deleteGroups(JsonArray groups) { if (groups == null || groups.size() == 0) { return;/*from ww w. j a v a2 s . co m*/ } final String[] groupIds = new String[groups.size()]; for (int i = 0; i < groups.size(); i++) { JsonObject j = groups.getJsonObject(i); groupIds[i] = j.getString("group"); } final JsonObject matcher = MongoQueryBuilder.build(QueryBuilder.start("shared.groupId").in(groupIds)); MongoUpdateBuilder modifier = new MongoUpdateBuilder(); modifier.pull("shared", MongoQueryBuilder.build(QueryBuilder.start("groupId").in(groupIds))); final String collection = MongoDbConf.getInstance().getCollection(); if (collection == null || collection.trim().isEmpty()) { log.error("Error deleting groups : invalid collection " + collection + " in class " + this.getClass().getName()); return; } mongo.update(collection, matcher, modifier.build(), false, true, new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if (!"ok".equals(event.body().getString("status"))) { log.error("Error deleting groups in collection " + collection + " : " + event.body().getString("message")); } } }); }