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.timelinegenerator.services.impl.EventServiceMongoImpl.java

License:Open Source License

@Override
public void delete(String timelineId, String eventId, UserInfos user,
        Handler<Either<String, JsonObject>> handler) {
    QueryBuilder query = QueryBuilder.start("_id").is(eventId);
    query.put("timeline").is(timelineId);
    mongo.delete(this.collection, MongoQueryBuilder.build(query), validActionResultHandler(handler));

}

From source file:net.atos.entng.timelinegenerator.TimelineGeneratorRepositoryEvents.java

License:Open Source License

@Override
public void deleteGroups(JsonArray groups) {
    if (groups == null || groups.size() == 0) {
        log.warn("[TimelineGeneratorRepositoryEvents][deleteGroups] JsonArray groups is null or empty");
        return;//from  w  w w.ja va  2  s .co  m
    }

    final String[] groupIds = new String[groups.size()];
    for (int i = 0; i < groups.size(); i++) {
        JsonObject j = groups.get(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)));
    // remove all the shares with groups
    mongo.update(TimelineGenerator.TIMELINE_GENERATOR_COLLECTION, matcher, modifier.build(), false, true,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        log.info(
                                "[TimelineGeneratorRepositoryEvents][deleteGroups] All groups shares are removed");
                    } else {
                        log.error(
                                "[TimelineGeneratorRepositoryEvents][deleteGroups] Error removing groups shares. Message : "
                                        + event.left().getValue());
                    }
                }
            }));
}

From source file:net.atos.entng.timelinegenerator.TimelineGeneratorRepositoryEvents.java

License:Open Source License

/**
 * Remove the shares of categories with a list of users if OK, Call prepareCleanCategories()
 * @param usersIds users identifiers/*w  w w .  j  av  a2 s.  c o m*/
 */
private void removeSharesTimelines(final String[] usersIds) {
    final JsonObject criteria = MongoQueryBuilder.build(QueryBuilder.start("shared.userId").in(usersIds));
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    modifier.pull("shared", MongoQueryBuilder.build(QueryBuilder.start("userId").in(usersIds)));

    // Remove Categories shares with these users
    mongo.update(TimelineGenerator.TIMELINE_GENERATOR_COLLECTION, criteria, modifier.build(), false, true,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        log.info(
                                "[TimelineGeneratorRepositoryEvents][removeSharesTimelines] All calendars shares with users are removed");
                        prepareCleanTimelines(usersIds);
                    } else {
                        log.error(
                                "[TimelineGeneratorRepositoryEvents][removeSharesTimelines] Error removing calendars shares with users. Message : "
                                        + event.left().getValue());
                    }
                }
            }));
}

From source file:net.atos.entng.timelinegenerator.TimelineGeneratorRepositoryEvents.java

License:Open Source License

/**
 * Prepare a list of categories identifiers if OK, Call cleanCategories()
 * @param usersIds users identifiers//from ww  w .j a  v  a2  s  . c o  m
 */
private void prepareCleanTimelines(final String[] usersIds) {
    DBObject deletedUsers = new BasicDBObject();
    // users currently deleted
    deletedUsers.put("owner.userId", new BasicDBObject("$in", usersIds));
    // users who have already been deleted
    DBObject ownerIsDeleted = new BasicDBObject("owner.deleted", true);
    // no manager found
    JsonObject matcher = MongoQueryBuilder
            .build(QueryBuilder.start("shared." + TimelineGenerator.MANAGE_RIGHT_ACTION).notEquals(true)
                    .or(deletedUsers, ownerIsDeleted));
    // return only calendar identifiers
    JsonObject projection = new JsonObject().putNumber("_id", 1);

    mongo.find(TimelineGenerator.TIMELINE_GENERATOR_COLLECTION, matcher, null, projection,
            MongoDbResult.validResultsHandler(new Handler<Either<String, JsonArray>>() {
                @Override
                public void handle(Either<String, JsonArray> event) {
                    if (event.isRight()) {
                        JsonArray timelines = event.right().getValue();
                        if (timelines == null || timelines.size() == 0) {
                            log.info(
                                    "[TimelineGeneratorRepositoryEvents][prepareCleanTimelines] No timelines to delete");
                            return;
                        }
                        final String[] timelineIds = new String[timelines.size()];
                        for (int i = 0; i < timelines.size(); i++) {
                            JsonObject j = timelines.get(i);
                            timelineIds[i] = j.getString("_id");
                        }
                        cleanTimelines(usersIds, timelineIds);
                    } else {
                        log.error(
                                "[TimelineGeneratorRepositoryEvents][prepareCleanTimelines] Error retreving the timelines created by users. Message : "
                                        + event.left().getValue());
                    }
                }
            }));
}

From source file:net.atos.entng.timelinegenerator.TimelineGeneratorRepositoryEvents.java

License:Open Source License

/**
 * Delete timelines by identifier if OK, call cleanEvents() and tagUsersAsDeleted()
 * @param usersIds users identifiers, used for tagUsersAsDeleted()
 * @param timelineIds timelines identifiers
 *///  w  ww.ja v  a 2 s  .  c o  m
private void cleanTimelines(final String[] usersIds, final String[] timelineIds) {
    JsonObject matcher = MongoQueryBuilder.build(QueryBuilder.start("_id").in(timelineIds));

    mongo.delete(TimelineGenerator.TIMELINE_GENERATOR_COLLECTION, matcher,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        log.info(
                                "[TimelineGeneratorRepositoryEvents][cleanTimelines] The timelines created by users are deleted");
                        cleanEvents(timelineIds);
                        tagUsersAsDeleted(usersIds);
                    } else {
                        log.error(
                                "[TimelineGeneratorRepositoryEvents][cleanTimelines] Error deleting the timelines created by users. Message : "
                                        + event.left().getValue());
                    }
                }
            }));
}

From source file:net.atos.entng.timelinegenerator.TimelineGeneratorRepositoryEvents.java

License:Open Source License

/**
 * Delete events by timeline identifier//from ww w .  ja  va  2  s . c  o  m
 * @param timelineIds timeline identifiers
 */
private void cleanEvents(final String[] timelineIds) {
    JsonObject matcher = MongoQueryBuilder.build(QueryBuilder.start("timeline").in(timelineIds));

    mongo.delete(TimelineGenerator.TIMELINE_GENERATOR_EVENT_COLLECTION, matcher,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        log.info(
                                "[TimelineGeneratorRepositoryEvents][cleanEvents] The events created by users are deleted");
                    } else {
                        log.error(
                                "[TimelineGeneratorRepositoryEvents][cleanEvents] Error deleting the events created by users. Message : "
                                        + event.left().getValue());
                    }
                }
            }));
}

From source file:net.atos.entng.timelinegenerator.TimelineGeneratorRepositoryEvents.java

License:Open Source License

/**
 * Tag as deleted a list of users in their own calendars
 * @param usersIds users identifiers/*from  www  . j ava2 s. co  m*/
 */
private void tagUsersAsDeleted(final String[] usersIds) {
    final JsonObject criteria = MongoQueryBuilder.build(QueryBuilder.start("owner.userId").in(usersIds));
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    modifier.set("owner.deleted", true);

    mongo.update(TimelineGenerator.TIMELINE_GENERATOR_COLLECTION, criteria, modifier.build(), false, true,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                @Override
                public void handle(Either<String, JsonObject> event) {
                    if (event.isRight()) {
                        log.info(
                                "[TimelineGeneratorRepositoryEvents][tagUsersAsDeleted] users are tagged as deleted in their own timelines");
                    } else {
                        log.error(
                                "[TimelineGeneratorRepositoryEvents][tagUsersAsDeleted] Error tagging as deleted users. Message : "
                                        + event.left().getValue());
                    }
                }
            }));
}

From source file:net.atos.entng.wiki.filters.OwnerAuthorOrShared.java

License:Open Source License

@Override
public void authorize(HttpServerRequest request, Binding binding, UserInfos user, Handler<Boolean> handler) {

    String wikiId = request.params().get(conf.getResourceIdLabel());
    String pageId = request.params().get("idpage");
    String commentId = request.params().get("idcomment");

    if (isValidId(wikiId) && isValidId(pageId) && isValidId(commentId)) {
        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   ww  w  .  ja v  a  2 s  .  co  m

        BasicDBObject commentMatch = new BasicDBObject("_id", commentId);
        commentMatch.put("author", user.getUserId());
        DBObject pageMatch = QueryBuilder.start("_id").is(pageId).put("comments").elemMatch(commentMatch).get();

        // Authorize if current user is the wiki's owner, the comment's author or if the serviceMethod has been shared
        QueryBuilder query = QueryBuilder.start("_id").is(wikiId).or(
                QueryBuilder.start("owner.userId").is(user.getUserId()).get(),
                QueryBuilder.start("pages").elemMatch(pageMatch).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:net.atos.entng.wiki.service.WikiServiceMongoImpl.java

License:Open Source License

@Override
public void listWikis(UserInfos user, Handler<Either<String, JsonArray>> handler) {
    // Query : return wikis visible by current user only (i.e. owner or
    // shared)/*from www  .  j  av  a 2 s  .com*/
    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());
    }

    QueryBuilder 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());

    // Projection
    JsonObject projection = new JsonObject();
    projection.putNumber("pages", 0).putNumber("created", 0);

    JsonObject sort = new JsonObject().putNumber("modified", -1);

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

From source file:net.atos.entng.wiki.service.WikiServiceMongoImpl.java

License:Open Source License

@Override
public void listPages(String idWiki, Handler<Either<String, JsonObject>> handler) {
    QueryBuilder query = QueryBuilder.start("_id").is(idWiki);

    JsonObject projection = new JsonObject();
    projection.putNumber("pages.content", 0).putNumber("created", 0);

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