List of usage examples for com.mongodb.client.model Filters ne
public static <TItem> Bson ne(final String fieldName, @Nullable final TItem value)
From source file:org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.java
License:Apache License
public static <T> Bson createFilter(String mongoDbField, T queryValue, ComparisonOperator comparator) { Bson filter = null;/*from www .ja v a 2s .c om*/ if (queryValue != null) { if (queryValue instanceof String) { switch (comparator) { case EQUALS: filter = Filters.eq(mongoDbField, queryValue); break; case NOT_EQUALS: filter = Filters.ne(mongoDbField, queryValue); break; case EQUAL_IGNORE_CASE: filter = Filters.regex(mongoDbField, queryValue.toString(), "i"); break; case STARTS_WITH: filter = Filters.regex(mongoDbField, "^" + queryValue + "*"); break; case ENDS_WITH: filter = Filters.regex(mongoDbField, "*" + queryValue + "$"); break; case REGEX: filter = Filters.regex(mongoDbField, queryValue.toString()); break; case TEXT: filter = Filters.text(String.valueOf(queryValue)); break; default: break; } } else { switch (comparator) { case EQUALS: filter = Filters.eq(mongoDbField, queryValue); break; case NOT_EQUALS: filter = Filters.ne(mongoDbField, queryValue); break; case GREATER_THAN: filter = Filters.gt(mongoDbField, queryValue); break; case GREATER_THAN_EQUAL: filter = Filters.gte(mongoDbField, queryValue); break; case LESS_THAN: filter = Filters.lt(mongoDbField, queryValue); break; case LESS_THAN_EQUAL: filter = Filters.lte(mongoDbField, queryValue); break; default: break; } } } return filter; }
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 .j a va2 s . 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 renameProjectAlias(long projectId, String newProjectAlias) throws CatalogDBException { long startTime = startQuery(); // String projectOwner = getProjectOwner(projectId); ////from w ww .ja va 2 s . com // int collisionProjectId = getProjectId(projectOwner, newProjectAlias); // if (collisionProjectId != -1) { // throw new CatalogManagerException("Couldn't rename project alias, alias already used in the same user"); // } QueryResult<Project> projectResult = getProject(projectId, null); // if projectId doesn't exist, an exception is raised Project project = projectResult.getResult().get(0); //String oldAlias = project.getAlias(); project.setAlias(newProjectAlias); /* DBObject query = BasicDBObjectBuilder .start("projects.id", projectId) .append("projects.alias", new BasicDBObject("$ne", newProjectAlias)) // check that any other project in the user has // the new name .get(); DBObject update = new BasicDBObject("$set", new BasicDBObject("projects.$.alias", newProjectAlias)); */ Bson query = Filters.and(Filters.eq("projects.id", projectId), Filters.ne("projects.alias", newProjectAlias)); Bson update = Updates.set("projects.$.alias", newProjectAlias); QueryResult<UpdateResult> result = userCollection.update(query, update, null); if (result.getResult().get(0).getModifiedCount() == 0) { //Check if the the study has been inserted throw new CatalogDBException("Project {alias:\"" + newProjectAlias + "\"} already exists"); } return endQuery("rename project alias", startTime, result); }
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 ww . j a v a 2 s. 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.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(); }/*from w w w. j a v a 2 s . 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() .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.ProjectMongoDBAdaptor.java
License:Apache License
@Override public QueryResult renameAlias(long projectId, String newProjectAlias) throws CatalogDBException { long startTime = startQuery(); // String projectOwner = getProjectOwner(projectId); ////ww w.j a va 2s .c om // int collisionProjectId = getProjectId(projectOwner, newProjectAlias); // if (collisionProjectId != -1) { // throw new CatalogManagerException("Couldn't rename project alias, alias already used in the same user"); // } QueryResult<Project> projectResult = get(projectId, null); // if projectId doesn't exist, an exception is raised Project project = projectResult.getResult().get(0); //String oldAlias = project.getAlias(); project.setAlias(newProjectAlias); /* DBObject query = BasicDBObjectBuilder .start("projects.id", projectId) .append("projects.alias", new BasicDBObject("$ne", newProjectAlias)) // check that any other project in the user has // the new name .get(); DBObject update = new BasicDBObject("$set", new BasicDBObject("projects.$.alias", newProjectAlias)); */ Bson query = Filters.and(Filters.eq("projects.id", projectId), Filters.ne("projects.alias", newProjectAlias)); Bson update = Updates.set("projects.$.alias", newProjectAlias); QueryResult<UpdateResult> result = userCollection.update(query, update, null); if (result.getResult().get(0).getModifiedCount() == 0) { //Check if the the study has been inserted throw new CatalogDBException("Project {alias:\"" + newProjectAlias + "\"} already exists"); } return endQuery("rename project alias", startTime, result); }
From source file:org.opencb.opencga.catalog.db.mongodb.UserMongoDBAdaptor.java
License:Apache License
@Override public void addQueryFilter(String userId, QueryFilter queryFilter) throws CatalogDBException { // Check if there exists a filter for that user with the same id Bson checkFilterExists = Filters.and(Filters.eq(QueryParams.ID.key(), userId), Filters.eq(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", queryFilter.getId())); QueryResult<Long> count = userCollection.count(checkFilterExists); if (count.getResult().get(0) != 0) { throw new CatalogDBException( "There already exists a filter with name " + queryFilter.getId() + " for user " + userId); }//from ww w.jav a2s. com // Insert the filter Bson query = Filters.and(Filters.eq(QueryParams.ID.key(), userId), Filters.ne(QueryParams.CONFIG_OPENCGA_FILTERS.key() + ".id", queryFilter.getId())); Bson filterDocument = getMongoDBDocument(queryFilter, "Filter"); Bson update = Updates.push(QueryParams.CONFIG_OPENCGA_FILTERS.key(), filterDocument); QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null); if (queryResult.first().getModifiedCount() != 1) { if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException( "User: There was an error when trying to store the filter. It could not be stored."); } else { // This error should NEVER be raised. throw new CatalogDBException( "User: There was a critical error when storing the filter. Is has been inserted " + queryResult.first().getModifiedCount() + " times."); } } }
From source file:org.seadpdt.impl.RepoServicesImpl.java
License:Apache License
@GET @Path("/{id}/researchobjects") @Produces(MediaType.APPLICATION_JSON)// w w w . j a v a2s . co m public Response getROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) { MongoCollection<Document> publicationsCollection = null; publicationsCollection = db.getCollection(MongoDB.researchObjects); FindIterable<Document> iter; if (purpose != null && purpose.equals("Production")) { iter = publicationsCollection.find( Filters.and(Filters.eq("Repository", id), Filters.ne("Preferences.Purpose", "Testing-Only"))); } else if (purpose != null && purpose.equals("Testing-Only")) { iter = publicationsCollection .find(Filters.and(Filters.eq("Repository", id), Filters.eq("Preferences.Purpose", purpose))); } else if (purpose != null) { return Response.status(Status.BAD_REQUEST) .entity(new JSONObject() .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString()) .build(); } else { iter = publicationsCollection.find(Filters.eq("Repository", id)); } iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1) .append("Repository", 1).append("Status", 1).append("_id", 0)); MongoCursor<Document> cursor = iter.iterator(); Set<Document> array = new HashSet<Document>(); while (cursor.hasNext()) { array.add(cursor.next()); } return Response.ok(array).cacheControl(control).build(); }
From source file:org.seadpdt.impl.RepoServicesImpl.java
License:Apache License
@GET @Path("/{id}/researchobjects/new") @Produces(MediaType.APPLICATION_JSON)/* w ww .j a v a 2s.c om*/ public Response getNewROsByRepository(@PathParam("id") String id, @QueryParam("Purpose") final String purpose) { MongoCollection<Document> publicationsCollection = null; publicationsCollection = db.getCollection(MongoDB.researchObjects); //Match ROs with no status from any repo Document match = new Document("Repository", id); Document reporter = new Document("reporter", id); Document elem = new Document("$elemMatch", reporter); Document not = new Document("$not", elem); match.put("Status", not); FindIterable<Document> iter; if (purpose != null && purpose.equals("Production")) { iter = publicationsCollection .find(Filters.and(match, Filters.ne("Preferences.Purpose", "Testing-Only"))); } else if (purpose != null && purpose.equals("Testing-Only")) { iter = publicationsCollection.find(Filters.and(match, Filters.eq("Preferences.Purpose", purpose))); } else if (purpose != null) { return Response.status(Status.BAD_REQUEST) .entity(new JSONObject() .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString()) .build(); } else { iter = publicationsCollection.find(match); } iter.projection(new Document("Aggregation.Identifier", 1).append("Aggregation.Title", 1) .append("Repository", 1).append("Status", 1).append("_id", 0)); MongoCursor<Document> cursor = iter.iterator(); Set<Document> array = new HashSet<Document>(); while (cursor.hasNext()) { Document nextDoc = cursor.next(); array.add(nextDoc); } return Response.ok(array).cacheControl(control).build(); }
From source file:org.seadpdt.impl.ROServicesImpl.java
License:Apache License
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON)// w w w . j a v a2 s. c o m public Response getROsList(@QueryParam("Purpose") final String purpose) { FindIterable<Document> iter; if (purpose != null && purpose.equals("Production")) { iter = publicationsCollection.find(Filters.ne("Preferences.Purpose", "Testing-Only")); } else if (purpose != null && purpose.equals("Testing-Only")) { iter = publicationsCollection.find(Filters.eq("Preferences.Purpose", purpose)); } else if (purpose != null) { return Response.status(ClientResponse.Status.BAD_REQUEST) .entity(new JSONObject() .put("Error", "'" + purpose + "' is not an acceptable value for 'Purpose'").toString()) .build(); } else { iter = publicationsCollection.find(); } iter.projection(new Document("Status", 1).append("Repository", 1).append("Aggregation.Identifier", 1) .append("Aggregation.Title", 1).append("_id", 0)); MongoCursor<Document> cursor = iter.iterator(); JSONArray array = new JSONArray(); while (cursor.hasNext()) { array.put(JSON.parse(cursor.next().toJson())); } return Response.ok(array.toString()).cacheControl(control).build(); }