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.events.ForumSearchingEvents.java

License:Open Source License

@Override
public void searchResource(List<String> appFilters, String userId, JsonArray groupIds,
        final JsonArray searchWords, final Integer page, final Integer limit, final JsonArray columnsHeader,
        final String locale, final Handler<Either<String, JsonArray>> handler) {
    if (appFilters.contains(ForumSearchingEvents.class.getSimpleName())) {

        final List<String> groupIdsLst = groupIds.toList();
        final List<DBObject> groups = new ArrayList<DBObject>();
        groups.add(QueryBuilder.start("userId").is(userId).get());
        for (String gpId : groupIdsLst) {
            groups.add(QueryBuilder.start("groupId").is(gpId).get());
        }/*w ww.  j a v  a  2s.  c  om*/

        final QueryBuilder rightsQuery = new QueryBuilder().or(
                QueryBuilder.start("visibility").is(VisibilityFilter.PUBLIC.name()).get(),
                QueryBuilder.start("visibility").is(VisibilityFilter.PROTECTED.name()).get(),
                QueryBuilder.start("owner.userId").is(userId).get(),
                QueryBuilder.start("shared")
                        .elemMatch(new QueryBuilder().or(groups.toArray(new DBObject[groups.size()])).get())
                        .get());

        JsonObject sort = new JsonObject().putNumber("modified", -1);
        final JsonObject projection = new JsonObject();
        projection.putNumber("name", 1);
        //search all category of user
        mongo.find(Forum.CATEGORY_COLLECTION, MongoQueryBuilder.build(rightsQuery), sort, projection,
                new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> event) {
                        final Either<String, JsonArray> ei = validResults(event);
                        if (ei.isRight()) {
                            final JsonArray categoryResult = ei.right().getValue();

                            final Map<String, String> mapIdName = new HashMap<String, String>();
                            for (int i = 0; i < categoryResult.size(); i++) {
                                final JsonObject j = categoryResult.get(i);
                                mapIdName.put(j.getString("_id"), j.getString("name"));
                            }

                            //search subject for the catagories found
                            searchSubject(page, limit, searchWords.toList(), mapIdName,
                                    new Handler<Either<String, JsonArray>>() {
                                        @Override
                                        public void handle(Either<String, JsonArray> event) {
                                            if (event.isRight()) {
                                                if (log.isDebugEnabled()) {
                                                    log.debug(
                                                            "[ForumSearchingEvents][searchResource] The resources searched by user are finded");
                                                }
                                                final JsonArray res = formatSearchResult(
                                                        event.right().getValue(), columnsHeader,
                                                        searchWords.toList(), mapIdName, locale);
                                                handler.handle(new Right<String, JsonArray>(res));
                                            } else {
                                                handler.handle(new Either.Left<String, JsonArray>(
                                                        event.left().getValue()));
                                            }
                                        }
                                    });
                        } else {
                            handler.handle(new Either.Left<String, JsonArray>(ei.left().getValue()));
                        }
                    }
                });
    } else {
        handler.handle(new Right<String, JsonArray>(new JsonArray()));
    }
}

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

License:Open Source License

@Override
public void retrieve(String id, UserInfos user, Handler<Either<String, JsonObject>> handler) {
    // Query/*from  w w  w . j  a va 2  s  .  c o  m*/
    QueryBuilder builder = QueryBuilder.start("_id").is(id);
    mongo.findOne(categories_collection, MongoQueryBuilder.build(builder), null, validResultHandler(handler));
}

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

License:Open Source License

@Override
public void delete(String id, UserInfos user, Handler<Either<String, JsonObject>> handler) {
    // Delete the category
    QueryBuilder builder = QueryBuilder.start("_id").is(id);
    mongo.delete(categories_collection, MongoQueryBuilder.build(builder), validResultHandler(handler));
}

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

License:Open Source License

@Override
public void deleteSubjects(String id, UserInfos user, Handler<Either<String, JsonObject>> handler) {
    // Delete all subjects of the category
    QueryBuilder query = QueryBuilder.start("category").is(id);
    mongo.delete(subjects_collection, MongoQueryBuilder.build(query), validResultHandler(handler));
}

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

License:Open Source License

@Override
public void list(final String categoryId, final String subjectId, final UserInfos user,
        final Handler<Either<String, JsonArray>> handler) {
    // Prepare Query
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId).put("category").is(categoryId);

    // Projection
    JsonObject projection = new JsonObject();
    projection.putNumber("messages", 1);

    mongo.findOne(subjects_collection, MongoQueryBuilder.build(query), projection,
            validResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override//  w w  w .j  a v  a2s.co m
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        try {
                            // Extract messages
                            JsonObject subject = event.right().getValue();
                            if (subject.containsField("messages")) {
                                handler.handle(
                                        new Either.Right<String, JsonArray>(subject.getArray("messages")));
                            } else {
                                handler.handle(new Either.Right<String, JsonArray>(new JsonArray()));
                            }
                        } catch (Exception e) {
                            handler.handle(new Either.Left<String, JsonArray>(
                                    "Malformed response : " + e.getClass().getName() + " : " + e.getMessage()));
                        }
                    } else {
                        handler.handle(new Either.Left<String, JsonArray>(event.left().getValue()));
                    }
                }
            }));
}

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

License:Open Source License

@Override
public void create(final String categoryId, final String subjectId, final JsonObject body, final UserInfos user,
        final Handler<Either<String, JsonObject>> handler) {
    // Prepare Message object
    final ObjectId newId = new ObjectId();
    JsonObject now = MongoDb.now();//from   w  w w.  ja v  a2  s  .c o m
    body.putString("_id", newId.toStringMongod())
            .putObject("owner",
                    new JsonObject().putString("userId", user.getUserId()).putString("displayName",
                            user.getUsername()))
            .putString("contentPlain", StringUtils.stripHtmlTag(body.getString("content", "")))
            .putObject("created", now).putObject("modified", now);

    // Prepare Query
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId);
    query.put("category").is(categoryId);
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    modifier.push("messages", body);
    modifier.inc("nbMessages", 1);
    modifier.set("modified", MongoDb.now());

    // Execute query
    mongo.update(subjects_collection, MongoQueryBuilder.build(query), modifier.build(),
            validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        try {
                            if (event.right().getValue().getNumber("number").intValue() == 1) {
                                // Respond with created message Id
                                JsonObject created = new JsonObject();
                                created.putString("_id", newId.toStringMongod());
                                handler.handle(new Either.Right<String, JsonObject>(created));
                            } else {
                                handler.handle(new Either.Left<String, JsonObject>("Subject not found"));
                            }
                        } catch (Exception e) {
                            handler.handle(new Either.Left<String, JsonObject>(
                                    "Malformed response : " + e.getClass().getName() + " : " + e.getMessage()));
                        }
                    } else {
                        handler.handle(event);
                    }
                }
            }));
}

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

License:Open Source License

@Override
public void retrieve(final String categoryId, final String subjectId, final String messageId,
        final UserInfos user, final Handler<Either<String, JsonObject>> handler) {
    // Prepare Query
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId).put("category").is(categoryId).put("messages")
            .elemMatch(new BasicDBObject("_id", messageId));

    // Projection
    JsonObject idMatch = new JsonObject();
    idMatch.putString("_id", messageId);
    JsonObject elemMatch = new JsonObject();
    elemMatch.putObject("$elemMatch", idMatch);
    JsonObject projection = new JsonObject();
    projection.putObject("messages", elemMatch);

    mongo.findOne(subjects_collection, MongoQueryBuilder.build(query), projection,
            validResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override//from  ww  w .ja v a  2 s.  com
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        try {
                            // Extract message
                            JsonObject subject = event.right().getValue();
                            if (subject.containsField("messages")) {
                                JsonArray messages = subject.getArray("messages");
                                JsonObject extractedMessage = messages.get(0);
                                handler.handle(new Either.Right<String, JsonObject>(extractedMessage));
                            } else {
                                handler.handle(new Either.Right<String, JsonObject>(null));
                            }
                        } catch (Exception e) {
                            handler.handle(new Either.Left<String, JsonObject>(
                                    "Malformed response : " + e.getClass().getName() + " : " + e.getMessage()));
                        }
                    } else {
                        handler.handle(event);
                    }
                }
            }));
}

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

License:Open Source License

@Override
public void update(final String categoryId, final String subjectId, final String messageId,
        final JsonObject body, final UserInfos user, final Handler<Either<String, JsonObject>> handler) {
    // Prepare Query
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId).put("category").is(categoryId).put("messages")
            .elemMatch(new BasicDBObject("_id", messageId));

    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    // Prepare Message object update
    body.removeField("_id");
    body.removeField("owner");

    for (String attr : body.getFieldNames()) {
        modifier.set("messages.$." + attr, body.getValue(attr));
    }/*w ww .j  av  a2s .c om*/
    modifier.set("messages.$.modified", MongoDb.now());
    modifier.set("messages.$.contentPlain", StringUtils.stripHtmlTag(body.getString("content", "")));

    // Prepare Subject update
    modifier.set("modified", MongoDb.now());

    // Execute query
    mongo.update(subjects_collection, MongoQueryBuilder.build(query), modifier.build(),
            validActionResultHandler(handler));
}

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

License:Open Source License

@Override
public void delete(final String categoryId, final String subjectId, final String messageId,
        final UserInfos user, final Handler<Either<String, JsonObject>> handler) {
    // Prepare Query
    QueryBuilder query = QueryBuilder.start("_id").is(subjectId).put("category").is(categoryId).put("messages")
            .elemMatch(new BasicDBObject("_id", messageId));

    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    // Prepare Message delete
    JsonObject messageMatcher = new JsonObject();
    modifier.pull("messages", messageMatcher.putString("_id", messageId));
    modifier.inc("nbMessages", -1);
    modifier.set("modified", MongoDb.now());

    // Execute query
    mongo.update(subjects_collection, MongoQueryBuilder.build(query), modifier.build(),
            validActionResultHandler(handler));
}

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

License:Open Source License

protected void prepareIsSharedMethodQuery(final QueryBuilder query, final UserInfos user, final String threadId,
        final String sharedMethod) {
    // ThreadId//from  ww  w  . j a v a  2  s  . com
    query.put("_id").is(threadId);

    // Permissions
    List<DBObject> groups = new ArrayList<>();
    groups.add(QueryBuilder.start("userId").is(user.getUserId()).put(sharedMethod).is(true).get());
    for (String gpId : user.getProfilGroupsIds()) {
        groups.add(QueryBuilder.start("groupId").is(gpId).put(sharedMethod).is(true).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());
}