List of usage examples for com.mongodb.client.model Filters eq
public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value)
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoProjectDBAdaptor.java
License:Apache License
@Override public QueryResult<AclEntry> getProjectAcl(long projectId, String userId) throws CatalogDBException { long startTime = startQuery(); /*/* w w w. ja va 2 s . com*/ DBObject match1 = new BasicDBObject("$match", new BasicDBObject("projects.id", projectId)); DBObject project = new BasicDBObject("$project", BasicDBObjectBuilder .start("_id", false) .append("projects.acl", true) .append("projects.id", true).get()); DBObject unwind1 = new BasicDBObject("$unwind", "$projects"); DBObject match2 = new BasicDBObject("$match", new BasicDBObject("projects.id", projectId)); DBObject unwind2 = new BasicDBObject("$unwind", "$projects.acl"); DBObject match3 = new BasicDBObject("$match", new BasicDBObject("projects.acl.userId", userId)); */ Bson match1 = Aggregates.match(Filters.eq("projects.id", projectId)); // Bson project = Projections.fields(Projections.excludeId(), Projections.include("projects.acl", "projects.id")); Bson project = Aggregates.project( Projections.fields(Projections.excludeId(), Projections.include("projects.acl", "projects.id"))); Bson unwind1 = Aggregates.unwind("$projects"); Bson match2 = Aggregates.match(Filters.eq("projects.id", projectId)); Bson unwind2 = Aggregates.unwind("$projects.acl"); Bson match3 = Aggregates.match(Filters.eq("projects.acl.userId", userId)); List<Bson> operations = new LinkedList<>(); operations.add(match1); operations.add(project); operations.add(unwind1); operations.add(match2); operations.add(unwind2); operations.add(match3); QueryResult aggregate = userCollection.aggregate(operations, null); List<AclEntry> acls = new LinkedList<>(); if (aggregate.getNumResults() != 0) { // DBObject aclObject = (DBObject) ((DBObject) ((DBObject) aggregate.getResult().get(0)).get("projects")).get("acl"); Document aclObject = (Document) ((Document) ((Document) aggregate.getResult().get(0)).get("projects")) .get("acl"); AclEntry acl = parseObject(aclObject, AclEntry.class); acls.add(acl); } return endQuery("get project ACL", startTime, acls); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoProjectDBAdaptor.java
License:Apache License
@Override public QueryResult setProjectAcl(long projectId, AclEntry newAcl) throws CatalogDBException { long startTime = startQuery(); String userId = newAcl.getUserId(); if (!dbAdaptorFactory.getCatalogUserDBAdaptor().userExists(userId)) { throw new CatalogDBException("Can not set ACL to non-existent user: " + userId); }/* www. j a v a 2 s. c o m*/ Document newAclObject = getMongoDBDocument(newAcl, "ACL"); //DBObject newAclObject = getDbObject(newAcl, "ACL"); List<AclEntry> projectAcls = getProjectAcl(projectId, userId).getResult(); Bson query = new Document("projects.id", projectId); Bson push = Updates.push("projects.$.acl", newAclObject); if (!projectAcls.isEmpty()) { // ensure that there is no acl for that user in that project. pull Bson pull = Updates.pull("projects.$.acl", Filters.eq("userId", userId)); userCollection.update(query, pull, null); } /* DBObject query = new BasicDBObject("projects.id", projectId); BasicDBObject push = new BasicDBObject("$push", new BasicDBObject("projects.$.acl", newAclObject)); if (!projectAcls.isEmpty()) { // ensure that there is no acl for that user in that project. pull DBObject pull = new BasicDBObject("$pull", new BasicDBObject("projects.$.acl", new BasicDBObject("userId", userId))); userCollection.update(query, pull, null); } */ //Put study QueryResult pushResult = userCollection.update(query, push, null); return endQuery("Set project acl", startTime, pushResult); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
private long getDiskUsageByStudy(int studyId) { /*//from w w w. java 2 s. c om List<DBObject> operations = Arrays.<DBObject>asList( new BasicDBObject( "$match", new BasicDBObject( PRIVATE_STUDY_ID, studyId //new BasicDBObject("$in",studyIds) ) ), new BasicDBObject( "$group", BasicDBObjectBuilder .start("_id", "$" + PRIVATE_STUDY_ID) .append("diskUsage", new BasicDBObject( "$sum", "$diskUsage" )).get() ) );*/ List<Bson> operations = new ArrayList<>(); operations.add(Aggregates.match(Filters.eq(PRIVATE_STUDY_ID, studyId))); operations.add(Aggregates.group("$" + PRIVATE_STUDY_ID, Accumulators.sum("diskUsage", "$diskUsage"))); // Bson match = Aggregates.match(Filters.eq(PRIVATE_STUDY_ID, studyId)); // Aggregates.group() QueryResult<Document> aggregate = dbAdaptorFactory.getCatalogFileDBAdaptor().getFileCollection() .aggregate(operations, null); if (aggregate.getNumResults() == 1) { Object diskUsage = aggregate.getResult().get(0).get("diskUsage"); if (diskUsage instanceof Integer) { return ((Integer) diskUsage).longValue(); } else if (diskUsage instanceof Long) { return ((Long) diskUsage); } else { return Long.parseLong(diskUsage.toString()); } } else { return 0; } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<Group> getGroup(long studyId, @Nullable String groupId, List<String> userIds) throws CatalogDBException { long startTime = startQuery(); checkStudyId(studyId);// www . ja v a2 s. c o m for (String userId : userIds) { dbAdaptorFactory.getCatalogUserDBAdaptor().checkUserExists(userId); } if (groupId != null && groupId.length() > 0 && !groupExists(studyId, groupId)) { throw new CatalogDBException("Group \"" + groupId + "\" does not exist in study " + studyId); } /* * List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(), Filters.eq(VariableSetParams.ID.key(), variableSetId)))); aggregation.add(Aggregates.project(Projections.include(QueryParams.VARIABLE_SET.key()))); aggregation.add(Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key())); aggregation.add(Aggregates.match(Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId))); QueryResult<VariableSet> queryResult = studyCollection.aggregate(aggregation, variableSetConverter, new QueryOptions()); * */ List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.eq(PRIVATE_ID, studyId))); aggregation.add(Aggregates.project(Projections.include(QueryParams.GROUPS.key()))); aggregation.add(Aggregates.unwind("$" + QueryParams.GROUPS.key())); // Document query = new Document(PRIVATE_ID, studyId); // List<Bson> groupQuery = new ArrayList<>(); if (userIds.size() > 0) { aggregation.add(Aggregates.match(Filters.in(QueryParams.GROUP_USER_IDS.key(), userIds))); // groupQuery.add(Filters.in("userIds", userIds)); } if (groupId != null && groupId.length() > 0) { aggregation.add(Aggregates.match(Filters.eq(QueryParams.GROUP_NAME.key(), groupId))); // groupQuery.add(Filters.eq("id", groupId)); } // Bson projection = new Document(QueryParams.GROUPS.key(), new Document("$elemMatch", groupQuery)); QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, null); // QueryResult<Document> queryResult = studyCollection.find(query, projection, null); List<Study> studies = CatalogMongoDBUtils.parseStudies(queryResult); List<Group> groups = new ArrayList<>(); studies.stream().filter(study -> study.getGroups() != null) .forEach(study -> groups.addAll(study.getGroups())); return endQuery("getGroup", startTime, groups); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Deprecated @Override//from ww w . j a v a2 s . com public QueryResult<Role> getRole(long studyId, String userId, String groupId, String roleId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); Bson query = new Document(PRIVATE_ID, studyId); List<Bson> roleQuery = new ArrayList<>(); List<String> userIds = new ArrayList<>(); if (userId != null) { userIds.add(userId); } if (groupId != null) { userIds.add(groupId); } if (userIds.size() > 0) { roleQuery.add(Filters.in("userIds", userIds)); } if (roleId != null) { roleQuery.add(Filters.eq("id", roleId)); } Bson projection = Projections.elemMatch(QueryParams.ROLES.key(), Filters.and(roleQuery)); QueryResult<Document> queryResult = studyCollection.find(query, projection, filterOptions(options, FILTER_ROUTE_STUDIES + QueryParams.ROLES.key() + ".")); List<Study> studies = CatalogMongoDBUtils.parseStudies(queryResult); List<Role> roles = new ArrayList<>(1); // studies.stream().filter(study -> study.getRoles() != null).forEach(study -> { // roles.addAll(study.getRoles()); // }); return endQuery("getRole", startTime, roles); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public void removeUsersFromGroup(long studyId, String groupId, List<String> members) throws CatalogDBException { for (String member : members) { dbAdaptorFactory.getCatalogUserDBAdaptor().checkUserExists(member); }//from w w w.java 2 s.c o m Bson and = Filters.and(Filters.eq(PRIVATE_ID, studyId), Filters.eq("groups.name", groupId)); Bson pull = Updates.pullAll("groups.$.userIds", members); QueryResult<UpdateResult> update = studyCollection.update(and, pull, null); if (update.first().getModifiedCount() != 1) { throw new CatalogDBException("Unable to remove members from group " + groupId); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public Long variableSetExists(long variableSetId) { List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(), Filters.eq(VariableSetParams.ID.key(), variableSetId)))); aggregation.add(Aggregates.project(Projections.include(QueryParams.VARIABLE_SET.key()))); aggregation.add(Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key())); aggregation.add(Aggregates.match(Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId))); QueryResult<VariableSet> queryResult = studyCollection.aggregate(aggregation, variableSetConverter, new QueryOptions()); return (long) queryResult.getResult().size(); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> createVariableSet(long studyId, VariableSet variableSet) throws CatalogDBException { long startTime = startQuery(); if (variableSetExists(variableSet.getName(), studyId) > 0) { throw new CatalogDBException("VariableSet { name: '" + variableSet.getName() + "'} already exists."); }//from www.j a v a 2s .c o m long variableSetId = getNewId(); variableSet.setId(variableSetId); Document object = getMongoDBDocument(variableSet, "VariableSet"); Bson bsonQuery = Filters.eq(PRIVATE_ID, studyId); Bson update = Updates.push("variableSets", object); QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException( "createVariableSet: Could not create a new variable set in study " + studyId); } return endQuery("createVariableSet", startTime, getVariableSet(variableSetId, null)); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> addFieldToVariableSet(long variableSetId, Variable variable) throws CatalogDBException { long startTime = startQuery(); checkVariableSetExists(variableSetId); checkVariableNotInVariableSet(variableSetId, variable.getName()); Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId); Bson update = Updates.push(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), getMongoDBDocument(variable, "variable")); QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw CatalogDBException.updateError("VariableSet", variableSetId); }/*from w w w.j a v a 2 s.c o m*/ if (variable.isRequired()) { dbAdaptorFactory.getCatalogSampleDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogCohortDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogIndividualDBAdaptor().addVariableToAnnotations(variableSetId, variable); } return endQuery("Add field to variable set", startTime, getVariableSet(variableSetId, null)); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> renameFieldVariableSet(long variableSetId, String oldName, String newName) throws CatalogDBException { long startTime = startQuery(); checkVariableSetExists(variableSetId); checkVariableInVariableSet(variableSetId, oldName); checkVariableNotInVariableSet(variableSetId, newName); // The field can be changed if we arrive to this point. // 1. we obtain the variable Variable variable = getVariable(variableSetId, oldName); // 2. we take it out from the array. Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId); Bson update = Updates.pull(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), Filters.eq("name", oldName)); QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException( "VariableSet {id: " + variableSetId + "} - Could not rename the field " + oldName); }//from w w w . ja va2s .c o m if (queryResult.first().getModifiedCount() > 1) { throw new CatalogDBException("VariableSet {id: " + variableSetId + "} - An unexpected error happened when extracting the " + "variable from the variableSet to do the rename. Please, report this error to the OpenCGA developers."); } // 3. we change the name in the variable object and push it again in the array. variable.setName(newName); update = Updates.push(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), getMongoDBDocument(variable, "Variable")); queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() != 1) { throw new CatalogDBException("VariableSet {id: " + variableSetId + "} - A critical error happened when trying to rename one " + "of the variables of the variableSet object. Please, report this error to the OpenCGA developers."); } // 4. Change the field id in the annotations dbAdaptorFactory.getCatalogSampleDBAdaptor().renameAnnotationField(variableSetId, oldName, newName); dbAdaptorFactory.getCatalogCohortDBAdaptor().renameAnnotationField(variableSetId, oldName, newName); return endQuery("Rename field in variableSet", startTime, getVariableSet(variableSetId, null)); }