List of usage examples for com.mongodb.client.model Updates push
public static <TItem> Bson push(final String fieldName, @Nullable final TItem value)
From source file:com.exorath.exodata.impl.IExoDocument.java
License:Apache License
@Override public Observable<UpdateResult> push(String key, Object value) { return update(Updates.push(key, value), true); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoMetaDBAdaptor.java
License:Apache License
@Override public QueryResult<ObjectMap> addAdminSession(Session session) throws CatalogDBException { long startTime = startQuery(); Bson query = new Document(PRIVATE_ID, "METADATA"); Bson updates = Updates.push("admin.sessions", new Document("$each", Arrays.asList(getMongoDBDocument(session, "Session"))).append("$slice", -5)); QueryResult<UpdateResult> update = metaCollection.update(query, updates, null); if (update.first().getModifiedCount() == 0) { throw new CatalogDBException("An internal error occurred when logging the admin"); }/* w w w . j a va 2 s. c om*/ ObjectMap resultObjectMap = new ObjectMap(); resultObjectMap.put("sessionId", session.getId()); resultObjectMap.put("userId", "admin"); return endQuery("Login", startTime, Collections.singletonList(resultObjectMap)); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoProjectDBAdaptor.java
License:Apache License
@Override public QueryResult<Project> createProject(String userId, Project project, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); List<Study> studies = project.getStudies(); if (studies == null) { studies = Collections.emptyList(); }//from w w w .ja v a 2s . c o m project.setStudies(Collections.<Study>emptyList()); // Check if project.alias already exists. // DBObject countQuery = BasicDBObjectBuilder // .start("id", userId) // .append("projects.alias", project.getAlias()) // .get(); Bson countQuery = Filters.and(Filters.eq("id", userId), Filters.eq("projects.alias", project.getAlias())); QueryResult<Long> count = userCollection.count(countQuery); if (count.getResult().get(0) != 0) { throw new CatalogDBException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user"); } // if(getProjectId(userId, project.getAlias()) >= 0){ // throw new CatalogManagerException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user"); // } //Generate json // int projectId = CatalogMongoDBUtils.getNewAutoIncrementId(metaCollection); long projectId = dbAdaptorFactory.getCatalogMetaDBAdaptor().getNewAutoIncrementId(); project.setId(projectId); // DBObject query = new BasicDBObject("id", userId); // query.put("projects.alias", new BasicDBObject("$ne", project.getAlias())); Bson query = Filters.and(Filters.eq("id", userId), Filters.ne("projects.alias", project.getAlias())); Document projectDocument = projectConverter.convertToStorageType(project); // DBObject update = new BasicDBObject("$push", new BasicDBObject("projects", projectDBObject)); Bson update = Updates.push("projects", projectDocument); //Update object // QueryResult<WriteResult> queryResult = userCollection.update(query, update, null); QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null); if (queryResult.getResult().get(0).getModifiedCount() == 0) { // Check if the project has been inserted throw new CatalogDBException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user"); } String errorMsg = ""; for (Study study : studies) { String studyErrorMsg = dbAdaptorFactory.getCatalogStudyDBAdaptor() .createStudy(project.getId(), study, options).getErrorMsg(); if (studyErrorMsg != null && !studyErrorMsg.isEmpty()) { errorMsg += ", " + study.getAlias() + ":" + studyErrorMsg; } } List<Project> result = getProject(project.getId(), null).getResult(); return endQuery("Create Project", startTime, result, errorMsg, null); }
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); }//from w w w . j a v a 2 s . com 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
@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."); }/*w ww .java 2 s .co 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 ww .j av a2 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 www. ja v a 2s. 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)); }
From source file:org.opencb.opencga.catalog.db.mongodb.ProjectMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<Project> insert(Project project, String userId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); List<Study> studies = project.getStudies(); if (studies == null) { studies = Collections.emptyList(); }//w w w .j a v a 2 s . c om project.setStudies(Collections.<Study>emptyList()); // Check if project.alias already exists. // DBObject countQuery = BasicDBObjectBuilder // .start("id", userId) // .append("projects.alias", project.getAlias()) // .get(); Bson countQuery = Filters.and(Filters.eq("id", userId), Filters.eq("projects.alias", project.getAlias())); QueryResult<Long> count = userCollection.count(countQuery); if (count.getResult().get(0) != 0) { throw new CatalogDBException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user"); } // if(getProjectId(userId, project.getAlias()) >= 0){ // throw new CatalogManagerException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user"); // } //Generate json // int projectId = CatalogMongoDBUtils.getNewAutoIncrementId(metaCollection); long projectId = dbAdaptorFactory.getCatalogMetaDBAdaptor().getNewAutoIncrementId(); project.setId(projectId); // DBObject query = new BasicDBObject("id", userId); // query.put("projects.alias", new BasicDBObject("$ne", project.getAlias())); Bson query = Filters.and(Filters.eq("id", userId), Filters.ne("projects.alias", project.getAlias())); Document projectDocument = projectConverter.convertToStorageType(project); // DBObject update = new BasicDBObject("$push", new BasicDBObject("projects", projectDBObject)); Bson update = Updates.push("projects", projectDocument); //Update object // QueryResult<WriteResult> queryResult = userCollection.update(query, update, null); QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null); if (queryResult.getResult().get(0).getModifiedCount() == 0) { // Check if the project has been inserted throw new CatalogDBException( "Project {alias:\"" + project.getAlias() + "\"} already exists in this user"); } String errorMsg = ""; for (Study study : studies) { String studyErrorMsg = dbAdaptorFactory.getCatalogStudyDBAdaptor() .insert(project.getId(), study, options).getErrorMsg(); if (studyErrorMsg != null && !studyErrorMsg.isEmpty()) { errorMsg += ", " + study.getAlias() + ":" + studyErrorMsg; } } List<Project> result = get(project.getId(), null).getResult(); return endQuery("Create Project", startTime, result, errorMsg, null); }
From source file:org.opencb.opencga.catalog.db.mongodb.SampleMongoDBAdaptor.java
License:Apache License
@Override @Deprecated/*from w w w . jav a 2 s .c o m*/ public QueryResult<AnnotationSet> annotate(long sampleId, AnnotationSet annotationSet, boolean overwrite) throws CatalogDBException { long startTime = startQuery(); /*QueryResult<Long> count = sampleCollection.count( new BasicDBObject("annotationSets.id", annotationSet.getId()).append(PRIVATE_ID, sampleId));*/ QueryResult<Long> count = sampleCollection .count(new Document("annotationSets.name", annotationSet.getName()).append(PRIVATE_ID, sampleId)); if (overwrite) { if (count.getResult().get(0) == 0) { throw CatalogDBException.idNotFound("AnnotationSet", annotationSet.getName()); } } else { if (count.getResult().get(0) > 0) { throw CatalogDBException.alreadyExists("AnnotationSet", "name", annotationSet.getName()); } } /*DBObject object = getDbObject(annotationSet, "AnnotationSet"); DBObject query = new BasicDBObject(PRIVATE_ID, sampleId);*/ Document object = getMongoDBDocument(annotationSet, "AnnotationSet"); Bson query = new Document(PRIVATE_ID, sampleId); if (overwrite) { ((Document) query).put("annotationSets.name", annotationSet.getName()); } else { ((Document) query).put("annotationSets.name", new Document("$ne", annotationSet.getName())); } /* DBObject update; if (overwrite) { update = new BasicDBObject("$set", new BasicDBObject("annotationSets.$", object)); } else { update = new BasicDBObject("$push", new BasicDBObject("annotationSets", object)); } */ Bson update; if (overwrite) { update = Updates.set("annotationSets.$", object); } else { update = Updates.push("annotationSets", object); } QueryResult<UpdateResult> queryResult = sampleCollection.update(query, update, null); if (queryResult.first().getModifiedCount() != 1) { throw CatalogDBException.alreadyExists("AnnotationSet", "name", annotationSet.getName()); } return endQuery("", startTime, Collections.singletonList(annotationSet)); }
From source file:org.opencb.opencga.catalog.db.mongodb.UserMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<ObjectMap> addSession(String userId, Session session) throws CatalogDBException { long startTime = startQuery(); Bson query = new Document(QueryParams.ID.key(), userId); Bson updates = Updates.push("sessions", new Document("$each", Arrays.asList(getMongoDBDocument(session, "Session")))); // .append("$slice", -50)); QueryResult<UpdateResult> update = userCollection.update(query, updates, null); if (update.first().getModifiedCount() == 0) { throw new CatalogDBException("An internal error occurred when logging the user" + userId); }//from w w w . j ava 2 s.c o m ObjectMap resultObjectMap = new ObjectMap(); resultObjectMap.put("sessionId", session.getId()); resultObjectMap.put("userId", userId); return endQuery("Login", startTime, Collections.singletonList(resultObjectMap)); }