/** * Delete the entries satisfying the query. * * @param query Query of the objects to be deleted. * @param options Deleting options. * @param sessionId sessionId. * @return A list with the deleted objects. * @throws CatalogException CatalogException * @throws IOException IOException. */ public List<QueryResult<User>> delete(Query query, QueryOptions options, String sessionId) throws CatalogException, IOException { QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.ID.key()); QueryResult<User> userQueryResult = userDBAdaptor.get(query, queryOptions); List<String> userIds = userQueryResult.getResult().stream().map(User::getId).collect(Collectors.toList()); String userIdStr = StringUtils.join(userIds, ","); return delete(userIdStr, options, sessionId); }
@Override public boolean exists(long projectId) { QueryResult<Long> count = userCollection.count(new Document(UserDBAdaptor.QueryParams.PROJECTS_UID.key(), projectId)); return count.getResult().get(0) != 0; }
default void checkIds(List<String> userIds) throws CatalogDBException { if (userIds == null || userIds.isEmpty()) { throw CatalogDBException.newInstance("No users to be checked."); } Query query = new Query(QueryParams.ID.key(), userIds); if (count(query).first() < userIds.size()) { throw CatalogDBException.newInstance("Some users do not exist."); } }
@Override public QueryResult<Long> update(Query query, ObjectMap parameters, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); Map<String, Object> userParameters = new HashMap<>(); final String[] acceptedParams = {QueryParams.NAME.key(), QueryParams.EMAIL.key(), QueryParams.ORGANIZATION.key(), QueryParams.LAST_MODIFIED.key(), }; filterStringParams(parameters, userParameters, acceptedParams); if (parameters.containsKey(QueryParams.STATUS_NAME.key())) { userParameters.put(QueryParams.STATUS_NAME.key(), parameters.get(QueryParams.STATUS_NAME.key())); userParameters.put(QueryParams.STATUS_DATE.key(), TimeUtils.getTime()); } final String[] acceptedLongParams = {QueryParams.QUOTA.key(), QueryParams.SIZE.key()}; filterLongParams(parameters, userParameters, acceptedLongParams); final String[] acceptedMapParams = {QueryParams.ATTRIBUTES.key()}; filterMapParams(parameters, userParameters, acceptedMapParams); if (!userParameters.isEmpty()) { QueryResult<UpdateResult> update = userCollection.update(parseQuery(query), new Document("$set", userParameters), null); return endQuery("Update user", startTime, Arrays.asList(update.getNumTotalResults())); } return endQuery("Update user", startTime, new QueryResult<>()); }
/*** * Removes completely the user from the database. * @param id User id to be removed from the database. * @return a QueryResult object with the user removed. * @throws CatalogDBException when there is any problem during the removal. */ public QueryResult<User> clean(String id) throws CatalogDBException { long startTime = startQuery(); Query query = new Query(QueryParams.ID.key(), id); Bson bson = parseQuery(query); QueryResult<User> userQueryResult = get(query, new QueryOptions()); QueryResult<DeleteResult> remove = userCollection.remove(bson, new QueryOptions()); if (remove.first().getDeletedCount() == 0) { throw CatalogDBException.idNotFound("User", query.getString(QueryParams.ID.key())); } else { return endQuery("Clean user", startTime, userQueryResult); } }
private User.Filter getFilter(String userId, String name) throws CatalogException { 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."); } for (User.Filter filter : userQueryResult.first().getConfigs().getFilters()) { if (name.equals(filter.getName())) { return filter; } } return null; }
@Override public QueryResult setConfig(String userId, String name, Map<String, Object> config) throws CatalogDBException { long startTime = startQuery(); // Set the config Bson bsonQuery = Filters.eq(QueryParams.ID.key(), userId); Bson filterDocument = getMongoDBDocument(config, "Config"); Bson update = Updates.set(QueryParams.CONFIGS.key() + "." + name, filterDocument); QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Could not create " + 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("Set config", startTime, Arrays.asList(userQueryResult.first().getConfigs().get(name))); }
@Override public String getOwnerId(long projectId) throws CatalogDBException { // DBObject query = new BasicDBObject(UserDBAdaptor.QueryParams.PROJECTS_UID.key(), projectId); Bson query = Filters.eq(UserDBAdaptor.QueryParams.PROJECTS_UID.key(), projectId); // DBObject projection = new BasicDBObject(UserDBAdaptor.QueryParams.ID.key(), "true"); Bson projection = Projections.include(UserDBAdaptor.QueryParams.ID.key()); // QueryResult<DBObject> result = userCollection.find(query, projection, null); QueryResult<Document> result = userCollection.find(query, projection, null); if (result.getResult().isEmpty()) { throw CatalogDBException.uidNotFound("Project", projectId); } else { return result.getResult().get(0).get(UserDBAdaptor.QueryParams.ID.key()).toString(); } }
public QueryResult<User> setStatus(String userId, String status) throws CatalogDBException { return update(userId, new ObjectMap(QueryParams.STATUS_NAME.key(), status)); }
QueryResult<Long> setStatus(Query query, String status) throws CatalogDBException { return update(query, new ObjectMap(QueryParams.STATUS_NAME.key(), status), QueryOptions.empty()); }
default boolean exists(String userId) throws CatalogDBException { return count(new Query(QueryParams.ID.key(), userId)).getResult().get(0) > 0; }
@Override public QueryResult deleteFilter(String userId, String name) throws CatalogDBException { long startTime = startQuery(); // Delete the filter Bson bsonQuery = Filters.and( Filters.eq(QueryParams.ID.key(), userId), Filters.eq(QueryParams.CONFIGS_FILTERS_NAME.key(), name) ); Bson update = Updates.pull(QueryParams.CONFIGS_FILTERS.key(), new Document(FilterParams.NAME.key(), name)); QueryResult<UpdateResult> queryResult = userCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Internal error: Filter " + name + " could not be removed"); } return endQuery("Delete filter", startTime, Arrays.asList(queryResult.first().getModifiedCount())); }
@Override public QueryResult resetPassword(String userId, String email, String newCryptPass) throws CatalogDBException { long startTime = startQuery(); Query query = new Query(QueryParams.ID.key(), userId); query.append(QueryParams.EMAIL.key(), email); Bson bson = parseQuery(query); Bson set = Updates.set("password", new Document("password", newCryptPass)); QueryResult<UpdateResult> update = userCollection.update(bson, set, null); if (update.getResult().get(0).getModifiedCount() == 0) { //0 query matches. throw new CatalogDBException("Bad user or email"); } return endQuery("Reset Password", startTime, Arrays.asList("Password successfully changed")); }
@Override public QueryResult changePassword(String userId, String oldPassword, String newPassword) throws CatalogDBException { long startTime = startQuery(); Query query = new Query(QueryParams.ID.key(), userId); query.append(QueryParams.PASSWORD.key(), oldPassword); Bson bson = parseQuery(query); Bson set = Updates.set("password", newPassword); QueryResult<UpdateResult> update = userCollection.update(bson, set, null); if (update.getResult().get(0).getModifiedCount() == 0) { //0 query matches. throw new CatalogDBException("Bad user or password"); } return endQuery("Change Password", startTime, update); }
public QueryResult<User> restore(String id, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); checkId(id); Query query = new Query(QueryParams.ID.key(), id) .append(QueryParams.STATUS_NAME.key(), Status.DELETED); if (count(query).first() == 0) { throw new CatalogDBException("The user {" + id + "} is not deleted"); } setStatus(id, Status.READY); query = new Query(QueryParams.ID.key(), id); return endQuery("Restore user", startTime, get(query, null)); }
@Override public QueryResult<Long> restore(Query query, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); query.put(QueryParams.STATUS_NAME.key(), Status.DELETED); return endQuery("Restore users", startTime, setStatus(query, Status.READY)); }
public QueryResult<User> update(String userId, ObjectMap parameters) throws CatalogDBException { long startTime = startQuery(); checkId(userId); Query query = new Query(QueryParams.ID.key(), userId); QueryResult<Long> update = update(query, parameters, QueryOptions.empty()); if (update.getResult().isEmpty() || update.first() != 1) { throw new CatalogDBException("Could not update user " + userId); } return endQuery("Update user", startTime, get(query, null)); }
@Override public void delete(long id) throws CatalogDBException { Query query = new Query(QueryParams.ID.key(), id); delete(query); }
@Override public QueryResult<User> get(String userId, QueryOptions options, String lastModified) throws CatalogDBException { checkId(userId); Query query = new Query(QueryParams.ID.key(), userId).append(QueryParams.STATUS_NAME.key(), "!=" + Status.DELETED); if (lastModified != null && !lastModified.isEmpty()) { query.append(QueryParams.LAST_MODIFIED.key(), "!=" + lastModified); } return get(query, options); }
@Override public void nativeInsert(Map<String, Object> project, String userId) throws CatalogDBException { Bson query = Filters.and(Filters.eq(UserDBAdaptor.QueryParams.ID.key(), userId), Filters.ne(UserDBAdaptor.QueryParams.PROJECTS_ID.key(), project.get(QueryParams.ID.key()))); Bson update = Updates.push("projects", getMongoDBDocument(project, "project")); //Update object 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 {" + project.get(QueryParams.ID.key()) + "\"} already exists for this user"); } }