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.commons.datastore.mongodb.MongoDBQueryUtils.java
License:Apache License
/** * Generates a date filter./*from www . j ava2s . co m*/ * * @param mongoDbField Mongo field. * @param dateValues List of 1 or 2 strings (dates). Only one will be expected when something like the following is passed: * =20171210, 20171210, >=20171210, >20171210, <20171210, <=20171210 * When 2 strings are passed, we will expect it to be a range such as: 20171201-20171210 * @param comparator Comparator value. * @param type Type of parameter. Expecting one of {@link QueryParam.Type#DATE} or {@link QueryParam.Type#TIMESTAMP} * @return the Bson query. */ private static Bson createDateFilter(String mongoDbField, List<String> dateValues, ComparisonOperator comparator, QueryParam.Type type) { Bson filter = null; Object date = null; if (QueryParam.Type.DATE.equals(type)) { date = convertStringToDate(dateValues.get(0)); } else if (QueryParam.Type.TIMESTAMP.equals(type)) { date = convertStringToDate(dateValues.get(0)).getTime(); } if (date != null) { switch (comparator) { case BETWEEN: if (dateValues.size() == 2) { Date to = convertStringToDate(dateValues.get(1)); if (QueryParam.Type.DATE.equals(type)) { filter = new Document(mongoDbField, new Document().append("$gte", date).append("$lt", to)); } else if (QueryParam.Type.TIMESTAMP.equals(type)) { filter = new Document(mongoDbField, new Document().append("$gte", date).append("$lt", to.getTime())); } } break; case EQUALS: filter = Filters.eq(mongoDbField, date); break; case GREATER_THAN: filter = Filters.gt(mongoDbField, date); break; case GREATER_THAN_EQUAL: filter = Filters.gte(mongoDbField, date); break; case LESS_THAN: filter = Filters.lt(mongoDbField, date); break; case LESS_THAN_EQUAL: filter = Filters.lte(mongoDbField, date); break; default: break; } } return filter; }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoMetaDBAdaptor.java
License:Apache License
public long getNewAutoIncrementId(String field) { //, MongoDBCollection metaCollection // QueryResult<BasicDBObject> result = metaCollection.findAndModify( // new BasicDBObject("_id", CatalogMongoDBAdaptor.METADATA_OBJECT_ID), //Query // new BasicDBObject(field, true), //Fields // null, // new BasicDBObject("$inc", new BasicDBObject(field, 1)), //Update // new QueryOptions("returnNew", true), // BasicDBObject.class // );/*from w w w. ja va2 s.co m*/ Bson query = Filters.eq(PRIVATE_ID, CatalogMongoDBAdaptorFactory.METADATA_OBJECT_ID); Document projection = new Document(field, true); Bson inc = Updates.inc(field, 1L); QueryOptions queryOptions = new QueryOptions("returnNew", true); QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions); // return (int) Float.parseFloat(result.getResult().get(0).get(field).toString()); return result.getResult().get(0).getLong(field); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoMetaDBAdaptor.java
License:Apache License
public void checkAdmin(String password) throws CatalogException { Bson query = Filters.eq("admin.password", CatalogAuthenticationManager.cypherPassword(password)); if (metaCollection.count(query).getResult().get(0) == 0) { throw new CatalogDBException("The admin password is incorrect."); }//w ww . j a v a2s . c o m }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoMetaDBAdaptor.java
License:Apache License
@Override public String getAdminPassword() throws CatalogDBException { Bson query = Filters.eq(PRIVATE_ID, "METADATA"); QueryResult<Document> queryResult = metaCollection.find(query, new QueryOptions(QueryOptions.INCLUDE, "admin")); return parseObject((Document) queryResult.first().get("admin"), Admin.class).getPassword(); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoMetaDBAdaptor.java
License:Apache License
@Override public QueryResult<StudyAclEntry> getDaemonAcl(List<String> members) throws CatalogDBException { long startTime = startQuery(); Bson match = Aggregates.match(Filters.eq(PRIVATE_ID, "METADATA")); Bson unwind = Aggregates.unwind("$" + CatalogCohortDBAdaptor.QueryParams.ACL.key()); Bson match2 = Aggregates.match(Filters.in(CatalogCohortDBAdaptor.QueryParams.ACL_MEMBER.key(), members)); Bson project = Aggregates.project(Projections.include(CatalogCohortDBAdaptor.QueryParams.ID.key(), CatalogCohortDBAdaptor.QueryParams.ACL.key())); QueryResult<Document> aggregate = metaCollection.aggregate(Arrays.asList(match, unwind, match2, project), null);/*from w ww . jav a 2s .c om*/ StudyAclEntry result = null; if (aggregate.getNumResults() == 1) { result = parseObject(((Document) aggregate.getResult().get(0).get("acl")), StudyAclEntry.class); } return endQuery("get daemon Acl", startTime, Arrays.asList(result)); }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoMetaDBAdaptor.java
License:Apache License
@Override public QueryResult<AuthenticationOrigin> getAuthenticationOrigin(String authId) throws CatalogDBException { long startTime = startQuery(); Bson match = Aggregates.match(Filters.eq(PRIVATE_ID, "METADATA")); Bson unwind = Aggregates.unwind("$authenticationOrigins"); Bson match2 = Aggregates.match(Filters.in("authenticationOrigins.id", authId)); Bson project = Aggregates.project(Projections.include("authenticationOrigins")); QueryResult<Document> aggregate = metaCollection.aggregate(Arrays.asList(match, unwind, match2, project), null);/*w ww . j a va2 s . c o m*/ AuthenticationOrigin result = null; if (aggregate.getNumResults() == 1) { result = parseObject(((Document) aggregate.getResult().get(0).get("authenticationOrigins")), AuthenticationOrigin.class); } return endQuery("get authenticationOrigin", startTime, Arrays.asList(result)); }
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 www . 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); ///*ww w .ja va 2s . c o m*/ // 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.CatalogMongoProjectDBAdaptor.java
License:Apache License
@Override public long getProjectId(String userId, String projectAlias) throws CatalogDBException { QueryResult<Document> queryResult = userCollection.find( new BsonDocument("projects.alias", new BsonString(projectAlias)).append("id", new BsonString(userId)), Projections.fields(Projections.include("projects.id"), Projections.elemMatch("projects", Filters.eq("alias", projectAlias))), null);/*from www .ja v a2 s. c o m*/ /* QueryResult<DBObject> queryResult = userCollection.find( BasicDBObjectBuilder .start("projects.alias", projectAlias) .append("id", userId).get(), BasicDBObjectBuilder.start("projects.id", true) .append("projects", new BasicDBObject("$elemMatch", new BasicDBObject("alias", projectAlias))).get(), null );*/ User user = parseUser(queryResult); if (user == null || user.getProjects().isEmpty()) { return -1; } else { return user.getProjects().get(0).getId(); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoProjectDBAdaptor.java
License:Apache License
@Override public String getProjectOwnerId(long projectId) throws CatalogDBException { // DBObject query = new BasicDBObject("projects.id", projectId); Bson query = Filters.eq("projects.id", projectId); // DBObject projection = new BasicDBObject("id", "true"); Bson projection = Projections.include("id"); // QueryResult<DBObject> result = userCollection.find(query, projection, null); QueryResult<Document> result = userCollection.find(query, projection, null); if (result.getResult().isEmpty()) { throw CatalogDBException.idNotFound("Project", projectId); } else {// w w w. ja v a 2s. c o m return result.getResult().get(0).get("id").toString(); } }