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.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> removeFieldFromVariableSet(long variableSetId, String name) throws CatalogDBException { long startTime = startQuery(); try {/*from w w w. j av a2 s . co m*/ checkVariableInVariableSet(variableSetId, name); } catch (CatalogDBException e) { checkVariableSetExists(variableSetId); throw e; } Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_ID.key(), variableSetId); Bson update = Updates.pull(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), Filters.eq("name", name)); QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() != 1) { throw new CatalogDBException("Remove field from Variable Set. Could not remove the field " + name + " from the variableSet id " + variableSetId); } // Remove all the annotations from that field dbAdaptorFactory.getCatalogSampleDBAdaptor().removeAnnotationField(variableSetId, name); dbAdaptorFactory.getCatalogCohortDBAdaptor().removeAnnotationField(variableSetId, name); return endQuery("Remove field from Variable Set", startTime, getVariableSet(variableSetId, null)); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
/** * The method will return the variable object given variableSetId and the variableId. * @param variableSetId Id of the variableSet. * @param variableId Id of the variable inside the variableSet. * @return the variable object./*w w w .j a va 2s . c o m*/ */ private Variable getVariable(long variableSetId, String variableId) throws CatalogDBException { 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))); aggregation.add( Aggregates.unwind("$" + QueryParams.VARIABLE_SET.key() + "." + VariableSetParams.VARIABLE.key())); aggregation.add(Aggregates.match(Filters .eq(QueryParams.VARIABLE_SET.key() + "." + VariableSetParams.VARIABLE_NAME.key(), variableId))); QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, new QueryOptions()); Document variableSetDocument = (Document) queryResult.first().get(QueryParams.VARIABLE_SET.key()); VariableSet variableSet = variableSetConverter.convertToDataModelType(variableSetDocument); Iterator<Variable> iterator = variableSet.getVariables().iterator(); if (iterator.hasNext()) { return iterator.next(); } else { // This error should never be raised. throw new CatalogDBException( "VariableSet {id: " + variableSetId + "} - Could not obtain variable object."); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
/** * Checks if the variable given is present in the variableSet. * @param variableSetId Identifier of the variableSet where it will be checked. * @param variableId VariableId that will be checked. * @throws CatalogDBException when the variableId is not present in the variableSet. */// w w w.j ava 2 s . co m private void checkVariableInVariableSet(long variableSetId, String variableId) throws CatalogDBException { List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(), Filters.and(Filters.eq(VariableSetParams.ID.key(), variableSetId), Filters.eq(VariableSetParams.VARIABLE_NAME.key(), variableId))))); if (studyCollection.aggregate(aggregation, new QueryOptions()).getNumResults() == 0) { throw new CatalogDBException("VariableSet {id: " + variableSetId + "}. The variable {id: " + variableId + "} does not exist."); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
/** * Checks if the variable given is not present in the variableSet. * @param variableSetId Identifier of the variableSet where it will be checked. * @param variableId VariableId that will be checked. * @throws CatalogDBException when the variableId is present in the variableSet. *///from w w w . jav a 2s .c o m private void checkVariableNotInVariableSet(long variableSetId, String variableId) throws CatalogDBException { List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.elemMatch(QueryParams.VARIABLE_SET.key(), Filters.and(Filters.eq(VariableSetParams.ID.key(), variableSetId), Filters.ne(VariableSetParams.VARIABLE_NAME.key(), variableId))))); if (studyCollection.aggregate(aggregation, new QueryOptions()).getNumResults() == 0) { throw new CatalogDBException("VariableSet {id: " + variableSetId + "}. The variable {id: " + variableId + "} already exists."); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> getVariableSet(long variableSetId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); Query query = new Query(QueryParams.VARIABLE_SET_ID.key(), variableSetId); Bson projection = Projections.elemMatch("variableSets", Filters.eq("id", variableSetId)); if (options == null) { options = new QueryOptions(); }// www . j a v a2 s .com QueryOptions qOptions = new QueryOptions(options); qOptions.put(MongoDBCollection.ELEM_MATCH, projection); QueryResult<Study> studyQueryResult = get(query, qOptions); if (studyQueryResult.getResult().isEmpty() || studyQueryResult.first().getVariableSets().isEmpty()) { throw new CatalogDBException("VariableSet {id: " + variableSetId + "} does not exist."); } /* Bson query = Filters.eq("variableSets.id", variableSetId); Bson projection = Projections.elemMatch("variableSets", Filters.eq("id", variableSetId)); QueryOptions filteredOptions = filterOptions(options, FILTER_ROUTE_STUDIES); QueryResult<Document> queryResult = studyCollection.find(query, projection, filteredOptions); List<Study> studies = parseStudies(queryResult); if (studies.isEmpty() || studies.get(0).getVariableSets().isEmpty()) { throw new CatalogDBException("VariableSet {id: " + variableSetId + "} does not exist"); } */ return endQuery("", startTime, studyQueryResult.first().getVariableSets()); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> getAllVariableSets(long studyId, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); List<Bson> mongoQueryList = new LinkedList<>(); for (Map.Entry<String, Object> entry : options.entrySet()) { String key = entry.getKey().split("\\.")[0]; try {/*w w w . j av a2 s. c o m*/ if (isDataStoreOption(key) || isOtherKnownOption(key)) { continue; //Exclude DataStore options } CatalogStudyDBAdaptor.VariableSetParams option = CatalogStudyDBAdaptor.VariableSetParams .getParam(key) != null ? CatalogStudyDBAdaptor.VariableSetParams.getParam(key) : CatalogStudyDBAdaptor.VariableSetParams.getParam(entry.getKey()); switch (option) { case STUDY_ID: addCompQueryFilter(option, option.name(), PRIVATE_ID, options, mongoQueryList); break; default: String optionsKey = "variableSets." + entry.getKey().replaceFirst(option.name(), option.key()); addCompQueryFilter(option, entry.getKey(), optionsKey, options, mongoQueryList); break; } } catch (IllegalArgumentException e) { throw new CatalogDBException(e); } } /* QueryResult<DBObject> queryResult = studyCollection.aggregate(Arrays.<DBObject>asList( new BasicDBObject("$match", new BasicDBObject(PRIVATE_ID, studyId)), new BasicDBObject("$project", new BasicDBObject("variableSets", 1)), new BasicDBObject("$unwind", "$variableSets"), new BasicDBObject("$match", new BasicDBObject("$and", mongoQueryList)) ), filterOptions(options, FILTER_ROUTE_STUDIES)); */ List<Bson> aggregation = new ArrayList<>(); aggregation.add(Aggregates.match(Filters.eq(PRIVATE_ID, studyId))); aggregation.add(Aggregates.project(Projections.include("variableSets"))); aggregation.add(Aggregates.unwind("$variableSets")); if (mongoQueryList.size() > 0) { aggregation.add(Aggregates.match(Filters.and(mongoQueryList))); } QueryResult<Document> queryResult = studyCollection.aggregate(aggregation, filterOptions(options, FILTER_ROUTE_STUDIES)); List<VariableSet> variableSets = parseObjects(queryResult, Study.class).stream() .map(study -> study.getVariableSets().get(0)).collect(Collectors.toList()); return endQuery("", startTime, variableSets); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public QueryResult<VariableSet> deleteVariableSet(long variableSetId, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); checkVariableSetInUse(variableSetId); long studyId = getStudyIdByVariableSetId(variableSetId); QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, queryOptions); Bson query = Filters.eq(PRIVATE_ID, studyId); Bson operation = Updates.pull("variableSets", Filters.eq("id", variableSetId)); QueryResult<UpdateResult> update = studyCollection.update(query, operation, null); if (update.first().getModifiedCount() == 0) { throw CatalogDBException.idNotFound("VariableSet", variableSetId); }//w w w. j a v a 2 s . c o m return endQuery("Delete VariableSet", startTime, variableSet); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoStudyDBAdaptor.java
License:Apache License
@Override public long getStudyIdByVariableSetId(long variableSetId) throws CatalogDBException { // DBObject query = new BasicDBObject("variableSets.id", variableSetId); Bson query = Filters.eq("variableSets.id", variableSetId); Bson projection = Projections.include("id"); // QueryResult<DBObject> queryResult = studyCollection.find(query, new BasicDBObject("id", true), null); QueryResult<Document> queryResult = studyCollection.find(query, projection, null); if (!queryResult.getResult().isEmpty()) { Object id = queryResult.getResult().get(0).get("id"); return id instanceof Number ? ((Number) id).intValue() : (int) Double.parseDouble(id.toString()); } else {/*www . ja v a 2s . com*/ throw CatalogDBException.idNotFound("VariableSet", variableSetId); } }
From source file:org.opencb.opencga.catalog.db.mongodb.IndividualMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<AnnotationSet> annotate(long individualId, AnnotationSet annotationSet, boolean overwrite) throws CatalogDBException { long startTime = startQuery(); QueryResult<Long> count = individualCollection.count( new Document("annotationSets.name", annotationSet.getName()).append(PRIVATE_ID, individualId)); if (overwrite) { if (count.first() == 0) { throw CatalogDBException.idNotFound("AnnotationSet", annotationSet.getName()); }// w w w . j a va2 s . c om } else { if (count.first() > 0) { throw CatalogDBException.alreadyExists("AnnotationSet", "name", annotationSet.getName()); } } Document document = getMongoDBDocument(annotationSet, "AnnotationSet"); Bson query; Bson individualQuery = Filters.eq(PRIVATE_ID, individualId); if (overwrite) { // query.put("annotationSets.id", annotationSet.getId()); query = Filters.and(individualQuery, Filters.eq("annotationSets.name", annotationSet.getName())); } else { // query.put("annotationSets.id", new BasicDBObject("$ne", annotationSet.getId())); query = Filters.and(individualQuery, Filters.eq("annotationSets.name", new Document("$ne", annotationSet.getName()))); } Bson update; if (overwrite) { update = new Document("$set", new Document("annotationSets.$", document)); } else { update = new Document("$push", new Document("annotationSets", document)); } QueryResult<UpdateResult> queryResult = individualCollection.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.IndividualMongoDBAdaptor.java
License:Apache License
@Override public QueryResult<AnnotationSet> deleteAnnotation(long individualId, String annotationId) throws CatalogDBException { long startTime = startQuery(); Individual individual = get(individualId, new QueryOptions("include", "projects.studies.individuals.annotationSets")).first(); AnnotationSet annotationSet = null;/*from w w w.j a va2s .co m*/ for (AnnotationSet as : individual.getAnnotationSets()) { if (as.getName().equals(annotationId)) { annotationSet = as; break; } } if (annotationSet == null) { throw CatalogDBException.idNotFound("AnnotationSet", annotationId); } // DBObject query = new BasicDBObject(PRIVATE_ID, individualId); // DBObject update = new BasicDBObject("$pull", new BasicDBObject("annotationSets", new BasicDBObject("id", annotationId))); // QueryResult<WriteResult> resultQueryResult = individualCollection.update(query, update, null); // if (resultQueryResult.first().getN() < 1) { // throw CatalogDBException.idNotFound("AnnotationSet", annotationId); // } Bson eq = Filters.eq(PRIVATE_ID, individualId); Bson pull = Updates.pull("annotationSets", new Document("name", annotationId)); QueryResult<UpdateResult> update = individualCollection.update(eq, pull, null); if (update.first().getModifiedCount() < 1) { throw CatalogDBException.idNotFound("AnnotationSet", annotationId); } return endQuery("Delete annotation", startTime, Collections.singletonList(annotationSet)); }