Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

From source file:net.atos.entng.forum.services.impl.MongoDbMessageService.java

License:Open Source License

protected void prepareIsSharedAnyQuery(final QueryBuilder query, final UserInfos user, final String threadId) {
    // ThreadId//w  w w .  j a v a 2 s .  co  m
    query.put("_id").is(threadId);

    // Permissions
    List<DBObject> groups = new ArrayList<>();
    groups.add(QueryBuilder.start("userId").is(user.getUserId()).get());
    for (String gpId : user.getProfilGroupsIds()) {
        groups.add(QueryBuilder.start("groupId").is(gpId).get());
    }
    query.or(QueryBuilder.start("owner.userId").is(user.getUserId()).get(),
            QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()).get(),
            QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(),
            QueryBuilder.start("shared")
                    .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get()).get());
}

From source file:net.atos.entng.forum.services.impl.MongoDbSubjectService.java

License:Open Source License

@Override
public void list(final String categoryId, final UserInfos user,
        final Handler<Either<String, JsonArray>> handler) {
    // Query/*from   ww  w.j a  v a  2 s . com*/
    QueryBuilder query = QueryBuilder.start("category").is(categoryId);
    JsonObject sort = new JsonObject().putNumber("modified", -1);

    // Projection
    JsonObject projection = new JsonObject();
    JsonObject slice = new JsonObject();
    slice.putNumber("$slice", -1);
    projection.putObject("messages", slice);

    mongo.find(subjects_collection, MongoQueryBuilder.build(query), sort, projection,
            validResultsHandler(handler));
}

From source file:net.atos.entng.forum.services.impl.MongoDbSubjectService.java

License:Open Source License

@Override
public void retrieve(String categoryId, String subjectId, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    // Query/*from ww w .ja v a 2  s .com*/
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId);
    query.put("category").is(categoryId);

    // Projection
    JsonObject projection = new JsonObject();
    JsonObject slice = new JsonObject();
    slice.putNumber("$slice", -1);
    projection.putObject("messages", slice);

    mongo.findOne(subjects_collection, MongoQueryBuilder.build(query), projection, validResultHandler(handler));
}

From source file:net.atos.entng.forum.services.impl.MongoDbSubjectService.java

License:Open Source License

@Override
public void update(String categoryId, String subjectId, JsonObject body, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    // Query/*w w  w. ja v a 2  s .  co  m*/
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId);
    query.put("category").is(categoryId);

    // Clean data
    body.removeField("_id");
    body.removeField("category");
    body.removeField("messages");

    // Modifier
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    for (String attr : body.getFieldNames()) {
        modifier.set(attr, body.getValue(attr));
    }
    modifier.set("modified", MongoDb.now());
    mongo.update(subjects_collection, MongoQueryBuilder.build(query), modifier.build(),
            validActionResultHandler(handler));
}

From source file:net.atos.entng.forum.services.impl.MongoDbSubjectService.java

License:Open Source License

@Override
public void delete(String categoryId, String subjectId, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId);
    query.put("category").is(categoryId);
    mongo.delete(subjects_collection, MongoQueryBuilder.build(query), validActionResultHandler(handler));
}

From source file:net.atos.entng.forum.services.impl.MongoDbSubjectService.java

License:Open Source License

@Override
public void getSubjectTitle(String categoryId, String subjectId, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId);
    query.put("category").is(categoryId);
    // Projection
    JsonObject projection = new JsonObject();
    projection.putNumber("title", 1);
    mongo.findOne(subjects_collection, MongoQueryBuilder.build(query), projection,
            validActionResultHandler(handler));
}

From source file:net.atos.entng.statistics.services.StatisticsServiceMongoImpl.java

License:Open Source License

private void getStatistics(final List<String> schoolIds, final JsonObject params,
        final Handler<Either<String, JsonArray>> handler, boolean isExport) {
    if (schoolIds == null || schoolIds.isEmpty()) {
        throw new IllegalArgumentException("schoolIds is null or empty");
    }/*from  w  ww . j  a va2  s .c o m*/

    String indicator = params.getString(PARAM_INDICATOR);
    Long start = (Long) params.getNumber(PARAM_START_DATE);
    Long end = (Long) params.getNumber(PARAM_END_DATE);

    boolean isActivatedAccountsIndicator = STATS_FIELD_ACTIVATED_ACCOUNTS.equals(indicator);
    boolean isAccessIndicator = TRACE_TYPE_SVC_ACCESS.equals(indicator);
    String groupedBy = isAccessIndicator ? "module/structures/profil" : "structures/profil";
    final QueryBuilder criteriaQuery = QueryBuilder.start(STATS_FIELD_GROUPBY).is(groupedBy)
            .and(STATS_FIELD_DATE).greaterThanEquals(formatTimestamp(start)).lessThan(formatTimestamp(end))
            .and(indicator).exists(true);

    String module = params.getString(PARAM_MODULE);
    boolean moduleIsEmpty = module == null || module.trim().isEmpty();
    boolean isAccessAllModules = isAccessIndicator && moduleIsEmpty;
    if (isAccessIndicator && !moduleIsEmpty) {
        criteriaQuery.and(MODULE_ID).is(module);
    }

    if (schoolIds.size() > 1) {
        criteriaQuery.and(STRUCTURES_ID).in(schoolIds);
    } else {
        criteriaQuery.and(STRUCTURES_ID).is(schoolIds.get(0));

        // When getting data for only one module, a "find" is enough (no need to aggregate data)
        if (!isExport && !isAccessAllModules) {
            JsonObject projection = new JsonObject();
            projection.putNumber("_id", 0).putNumber(indicator, 1).putNumber(PROFILE_ID, 1)
                    .putNumber(STATS_FIELD_DATE, 1);
            if (isActivatedAccountsIndicator) {
                projection.putNumber(STATS_FIELD_ACCOUNTS, 1);
            }

            mongo.find(collection, MongoQueryBuilder.build(criteriaQuery), sortByDateProfile, projection,
                    MongoDbResult.validResultsHandler(handler));
            return;
        }
    }

    // Aggregate data
    final JsonObject aggregation = new JsonObject();
    JsonArray pipeline = new JsonArray();
    aggregation.putString("aggregate", collection).putBoolean("allowDiskUse", true).putArray("pipeline",
            pipeline);

    pipeline.addObject(new JsonObject().putObject("$match", MongoQueryBuilder.build(criteriaQuery)));

    JsonObject id = new JsonObject().putString(PROFILE_ID, "$" + PROFILE_ID);
    if (isAccessAllModules && !isExport) {
        // Case : get JSON data for indicator "access to all modules"
        id.putString(MODULE_ID, "$" + MODULE_ID);
    } else {
        id.putString(STATS_FIELD_DATE, "$" + STATS_FIELD_DATE);
    }

    JsonObject group = new JsonObject().putObject("_id", id).putObject(indicator,
            new JsonObject().putString("$sum", "$" + indicator));
    if (isActivatedAccountsIndicator) {
        group.putObject(STATS_FIELD_ACCOUNTS, new JsonObject().putString("$sum", "$" + STATS_FIELD_ACCOUNTS));
    }
    JsonObject groupBy = new JsonObject().putObject("$group", group);

    pipeline.addObject(groupBy);

    QueryBuilder projection = QueryBuilder.start("_id").is(0).and(PROFILE_ID).is("$_id." + PROFILE_ID);
    if (isActivatedAccountsIndicator) {
        projection.and(STATS_FIELD_ACCOUNTS).is(1);
    }

    if (!isExport) {
        projection.and(indicator).is(1);
        if (isAccessAllModules) {
            projection.and(MODULE_ID).is("$_id." + MODULE_ID);
        } else {
            projection.and(STATS_FIELD_DATE).is("$_id." + STATS_FIELD_DATE);
        }

        // Sum stats for all structure_ids
        pipeline.addObject(new JsonObject().putObject("$project", MongoQueryBuilder.build(projection)));
    } else {
        // Projection : keep 'yyyy-MM' from 'yyyy-MM-dd HH:mm.ss.SSS'
        DBObject dateSubstring = new BasicDBObject();
        BasicDBList dbl = new BasicDBList();
        dbl.add("$_id." + STATS_FIELD_DATE);
        dbl.add(0);
        dbl.add(7);
        dateSubstring.put("$substr", dbl);

        projection.and(STATS_FIELD_DATE).is(dateSubstring).and("indicatorValue").is("$" + indicator); // Replace indicatorName by label "indicatorValue", so that the mustache template can be generic

        JsonObject sort = sortByStructureDateProfile;

        // Export stats for each structure_id
        id.putString(STRUCTURES_ID, "$" + STRUCTURES_ID);
        projection.and(STRUCTURES_ID).is("$_id." + STRUCTURES_ID);

        if (isAccessIndicator) {
            if (isAccessAllModules) {
                sort = sort.copy().putNumber(MODULE_ID, 1);
            }

            // Export stats for each module_id
            id.putString(MODULE_ID, "$" + MODULE_ID);
            projection.and(MODULE_ID).is("$_id." + MODULE_ID);
        }

        pipeline.addObject(new JsonObject().putObject("$project", MongoQueryBuilder.build(projection)));
        pipeline.addObject(new JsonObject().putObject("$sort", sort));
    }

    mongo.command(aggregation.toString(), new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> message) {
            if ("ok".equals(message.body().getString("status"))
                    && message.body().getObject("result", new JsonObject()).getInteger("ok") == 1) {
                JsonArray result = message.body().getObject("result").getArray("result");
                handler.handle(new Either.Right<String, JsonArray>(result));
            } else {
                String error = message.body().toString();
                handler.handle(new Either.Left<String, JsonArray>(error));
            }
        }
    });
}

From source file:net.atos.entng.timelinegenerator.services.impl.EventServiceMongoImpl.java

License:Open Source License

@Override
public void list(String timelineId, UserInfos user, final Handler<Either<String, JsonArray>> handler) {
    // Query//from  w w w  .  j a v a 2s  .  co  m
    QueryBuilder query = QueryBuilder.start("timeline").is(timelineId);
    JsonObject sort = new JsonObject().putNumber("modified", -1);

    // Projection
    JsonObject projection = new JsonObject();

    mongo.find(this.collection, MongoQueryBuilder.build(query), sort, projection, validResultsHandler(handler));
}

From source file:net.atos.entng.timelinegenerator.services.impl.EventServiceMongoImpl.java

License:Open Source License

@Override
public void retrieve(String timelineId, String eventId, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    // Query/* w  ww . ja  v a  2  s . co m*/
    QueryBuilder query = QueryBuilder.start("_id").is(eventId);
    query.put("category").is(timelineId);

    // Projection
    JsonObject projection = new JsonObject();

    mongo.findOne(this.collection, MongoQueryBuilder.build(query), projection, validResultHandler(handler));
}

From source file:net.atos.entng.timelinegenerator.services.impl.EventServiceMongoImpl.java

License:Open Source License

@Override
public void update(String timelineId, String eventId, JsonObject body, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    // Query/* w  ww  .  ja  v a  2s .  co  m*/
    QueryBuilder query = QueryBuilder.start("_id").is(eventId);
    query.put("timeline").is(timelineId);

    // Clean data
    body.removeField("_id");
    body.removeField("timeline");

    // Modifier
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    for (String attr : body.getFieldNames()) {
        modifier.set(attr, body.getValue(attr));
    }
    modifier.set("modified", MongoDb.now());
    mongo.update(this.collection, MongoQueryBuilder.build(query), modifier.build(),
            validActionResultHandler(handler));

}