fixComplexQueryParam(QueryParams.ATTRIBUTES.key(), query); fixComplexQueryParam(QueryParams.BATTRIBUTES.key(), query); fixComplexQueryParam(QueryParams.NATTRIBUTES.key(), query); QueryParams queryParam = QueryParams.getParam(entry.getKey()) != null ? QueryParams.getParam(entry.getKey()) : QueryParams.getParam(key); if (queryParam == null) { throw new CatalogDBException("Unexpected parameter " + entry.getKey() + ". The parameter does not exist or cannot be " switch (queryParam) { case ID: addAutoOrQuery(PRIVATE_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case ATTRIBUTES: addAutoOrQuery(entry.getKey(), entry.getKey(), query, queryParam.type(), andBsonList); break; case BATTRIBUTES: String mongoKey = entry.getKey().replace(QueryParams.BATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; case NATTRIBUTES: mongoKey = entry.getKey().replace(QueryParams.NATTRIBUTES.key(), QueryParams.ATTRIBUTES.key()); addAutoOrQuery(mongoKey, entry.getKey(), query, queryParam.type(), andBsonList); break; case STATUS_NAME: query.put(queryParam.key(), Status.getPositiveStatus(User.UserStatus.STATUS_LIST, query.getString(queryParam.key()))); addAutoOrQuery(queryParam.key(), queryParam.key(), query, queryParam.type(), andBsonList);
Bson countQuery = Filters.and(Filters.eq(UserDBAdaptor.QueryParams.ID.key(), userId), Filters.eq(UserDBAdaptor.QueryParams.PROJECTS_ID.key(), project.getId())); QueryResult<Long> count = userCollection.count(countQuery); if (count.getResult().get(0) != 0) { Bson query = Filters.and(Filters.eq(UserDBAdaptor.QueryParams.ID.key(), userId), Filters.ne(UserDBAdaptor.QueryParams.PROJECTS_ID.key(), project.getId())); QueryResult<UpdateResult> queryResult = userCollection.update(query, update, null);
QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.CONFIGS.key()); QueryResult<User> userQueryResult = userDBAdaptor.get(userId, options, ""); if (userQueryResult.getNumResults() == 0) {
QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.CONFIGS.key()); QueryResult<User> userQueryResult = userDBAdaptor.get(userId, options, ""); if (userQueryResult.getNumResults() == 0) {
public QueryResult<User> delete(String id, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); checkId(id); // Check the user is active or banned Query query = new Query(QueryParams.ID.key(), id) .append(QueryParams.STATUS_NAME.key(), User.UserStatus.READY + "," + User.UserStatus.BANNED); if (count(query).first() == 0) { query.put(QueryParams.STATUS_NAME.key(), User.UserStatus.DELETED); QueryOptions options = new QueryOptions(MongoDBCollection.INCLUDE, QueryParams.STATUS_NAME.key()); User user = get(query, options).first(); throw new CatalogDBException("The user {" + id + "} was already " + user.getStatus().getName()); } // If we don't find the force parameter, we check first if the user does not have an active project. if (!queryOptions.containsKey(FORCE) || !queryOptions.getBoolean(FORCE)) { checkCanDelete(id); } if (queryOptions.containsKey(FORCE) && queryOptions.getBoolean(FORCE)) { // Delete the active projects (if any) query = new Query(ProjectDBAdaptor.QueryParams.USER_ID.key(), id); dbAdaptorFactory.getCatalogProjectDbAdaptor().delete(query, queryOptions); } // Change the status of the user to deleted setStatus(id, User.UserStatus.DELETED); query = new Query(QueryParams.ID.key(), id) .append(QueryParams.STATUS_NAME.key(), User.UserStatus.DELETED); return endQuery("Delete user", startTime, get(query, queryOptions)); }
/** * Retrieves all the user filters. * * @param userId user id having the filters. * @param sessionId session id of the user fetching the filters. * @return the filters. * @throws CatalogException if the user corresponding to the session id is not the same as the provided user id. */ public QueryResult<User.Filter> getAllFilters(String userId, String sessionId) throws CatalogException { ParamUtils.checkParameter(userId, "userId"); ParamUtils.checkParameter(sessionId, "sessionId"); String userIdAux = getUserId(sessionId); userDBAdaptor.checkId(userId); if (!userId.equals(userIdAux)) { throw new CatalogException("User " + userIdAux + " is not authorised to get filters from user " + userId); } Query query = new Query() .append(UserDBAdaptor.QueryParams.ID.key(), userId); QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.CONFIGS.key()); QueryResult<User> userQueryResult = userDBAdaptor.get(query, queryOptions); if (userQueryResult.getNumResults() != 1) { throw new CatalogException("Internal error: User " + userId + " not found."); } List<User.Filter> filters = userQueryResult.first().getConfigs().getFilters(); return new QueryResult<>("Get filters", 0, filters.size(), filters.size(), "", "", filters); }
@Override public QueryResult<Long> updateFilter(String userId, String name, ObjectMap params) throws CatalogDBException { long startTime = startQuery(); if (params.isEmpty()) { throw new CatalogDBException("Nothing to be updated. No parameters were passed."); } final String prefixUpdate = CONFIGS_FILTERS.key() + ".$."; Document parameters = new Document(); if (params.get(FilterParams.DESCRIPTION.key()) != null) { parameters.put(prefixUpdate + FilterParams.DESCRIPTION.key(), params.get(FilterParams.DESCRIPTION.key())); } if (params.get(FilterParams.BIOFORMAT.key()) != null) { parameters.put(prefixUpdate + FilterParams.BIOFORMAT.key(), params.get(FilterParams.BIOFORMAT.key()).toString()); } if (params.get(FilterParams.QUERY.key()) != null) { parameters.put(prefixUpdate + FilterParams.QUERY.key(), getMongoDBDocument(params.get(FilterParams.QUERY.key()), "Query")); } if (params.get(FilterParams.OPTIONS.key()) != null) { parameters.put(prefixUpdate + FilterParams.OPTIONS.key(), getMongoDBDocument(params.get(FilterParams.OPTIONS.key()), "Options")); } if (parameters.isEmpty()) { throw new CatalogDBException("Nothing to be updated. Parameters were not recognised."); } Query query = new Query() .append(ID.key(), userId) .append(CONFIGS_FILTERS_NAME.key(), name); QueryResult<UpdateResult> update = userCollection.update(parseQuery(query), new Document("$set", parameters), null); return endQuery("Update filter", startTime, Arrays.asList(update.first().getModifiedCount())); }
@Override public QueryResult nativeGet(Query query, QueryOptions options) throws CatalogDBException { if (!query.containsKey(QueryParams.STATUS_NAME.key())) { query.append(QueryParams.STATUS_NAME.key(), "!=" + Status.DELETED); } Bson bson = parseQuery(query); QueryResult<Document> queryResult = userCollection.find(bson, options); for (Document user : queryResult.getResult()) { ArrayList<Document> projects = (ArrayList<Document>) user.get("projects"); if (projects.size() > 0) { List<Document> projectsTmp = new ArrayList<>(projects.size()); for (Document project : projects) { Query query1 = new Query(ProjectDBAdaptor.QueryParams.UID.key(), project.get(ProjectDBAdaptor .QueryParams.UID.key())); QueryResult<Document> queryResult1 = dbAdaptorFactory.getCatalogProjectDbAdaptor().nativeGet(query1, options); projectsTmp.add(queryResult1.first()); } user.remove("projects"); user.append("projects", projectsTmp); } } return queryResult; }
@Override public QueryResult<User.Filter> addFilter(String userId, User.Filter filter) throws CatalogDBException { long startTime = startQuery(); // Insert the filter Bson bsonQuery = Filters.and( Filters.eq(QueryParams.ID.key(), userId), Filters.ne(QueryParams.CONFIGS_FILTERS_NAME.key(), filter.getName()) ); Bson filterDocument = getMongoDBDocument(filter, "Filter"); Bson update = Updates.push(QueryParams.CONFIGS_FILTERS.key(), filterDocument); QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() != 1) { if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Internal error: The filter 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."); } } return endQuery("addFilter", startTime, Arrays.asList(filter)); }
@Override public QueryResult resetPassword(String userId) throws CatalogException { ParamUtils.checkParameter(userId, "userId"); userDBAdaptor.updateUserLastModified(userId); String newPassword = StringUtils.randomString(6); String newCryptPass = cypherPassword(newPassword); QueryResult<User> user = userDBAdaptor.get(userId, new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.EMAIL.key()), ""); if (user == null || user.getNumResults() != 1) { throw new CatalogException("Could not retrieve the user e-mail."); } String email = user.first().getEmail(); QueryResult queryResult = userDBAdaptor.resetPassword(userId, email, newCryptPass); String mailUser = this.configuration.getEmail().getFrom(); String mailPassword = this.configuration.getEmail().getPassword(); String mailHost = this.configuration.getEmail().getHost(); String mailPort = this.configuration.getEmail().getPort(); MailUtils.sendResetPasswordMail(email, newPassword, mailUser, mailPassword, mailHost, mailPort); return queryResult; } }
@Override public QueryResult<User> get(Query query, QueryOptions options) throws CatalogDBException { if (!query.containsKey(QueryParams.STATUS_NAME.key())) { query.append(QueryParams.STATUS_NAME.key(), "!=" + Status.DELETED); } Bson bson = parseQuery(query); QueryResult<User> userQueryResult = userCollection.find(bson, null, userConverter, options); for (User user : userQueryResult.getResult()) { if (user.getProjects() != null) { List<Project> projects = new ArrayList<>(user.getProjects().size()); for (Project project : user.getProjects()) { Query query1 = new Query(ProjectDBAdaptor.QueryParams.UID.key(), project.getUid()); QueryResult<Project> projectQueryResult = dbAdaptorFactory.getCatalogProjectDbAdaptor().get(query1, options); projects.add(projectQueryResult.first()); } user.setProjects(projects); } } return userQueryResult; }
@Override public void editId(String owner, long projectUid, String oldId, String newId) throws CatalogDBException { if (!exists(projectUid)) { logger.error("Project {} not found", projectUid); throw new CatalogDBException("Project not found."); } Bson query = Filters.and( Filters.eq(UserDBAdaptor.QueryParams.PROJECTS_UID.key(), projectUid), Filters.ne(UserDBAdaptor.QueryParams.PROJECTS_ID.key(), newId) ); Bson update = new Document("$set", new Document() .append("projects.$." + QueryParams.ID.key(), newId) .append("projects.$." + QueryParams.FQN.key(), owner + "@" + newId) ); QueryResult<UpdateResult> result = userCollection.update(query, update, null); if (result.getResult().get(0).getModifiedCount() == 0) { //Check if the the project id was modified throw new CatalogDBException("Project {id:\"" + newId + "\"} already exists"); } // Update all the internal project ids stored in the study documents dbAdaptorFactory.getCatalogStudyDBAdaptor().updateProjectId(projectUid, newId); }
@Override public QueryResult<Long> deleteConfig(String userId, String name) throws CatalogDBException { long startTime = startQuery(); // Insert the config Bson bsonQuery = Filters.and( Filters.eq(QueryParams.ID.key(), userId), Filters.exists(QueryParams.CONFIGS.key() + "." + name) ); Bson update = Updates.unset(QueryParams.CONFIGS.key() + "." + name); QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Could not delete " + name + " configuration "); } QueryResult<User> userQueryResult = get(userId, new QueryOptions(), ""); if (userQueryResult.getNumResults() == 0) { throw new CatalogDBException("Internal error: Could not retrieve user " + userId + " information"); } return endQuery("Delete config", startTime, Arrays.asList(queryResult.first().getModifiedCount())); }
@Override public long getId(final String userId, final String projectIdStr) throws CatalogDBException { String projectId = projectIdStr; if (projectId.contains("@")) { projectId = projectId.split("@", 2)[1]; } QueryResult<Document> queryResult = userCollection.find( new BsonDocument(UserDBAdaptor.QueryParams.PROJECTS_ID.key(), new BsonString(projectId)) .append(UserDBAdaptor.QueryParams.ID.key(), new BsonString(userId)), Projections.fields(Projections.include(UserDBAdaptor.QueryParams.PROJECTS_UID.key()), Projections.elemMatch("projects", Filters.eq(QueryParams.ID.key(), projectId))), null); User user = parseUser(queryResult); if (user == null || user.getProjects().isEmpty()) { return -1; } else { return user.getProjects().get(0).getUid(); } }