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 UID: addAutoOrQuery(PRIVATE_UID, queryParam.key(), query, queryParam.type(), andBsonList); break; case PROJECT_UID: addAutoOrQuery(PRIVATE_PROJECT_UID, queryParam.key(), query, queryParam.type(), andBsonList); break; case PROJECT_ID: addAutoOrQuery(PRIVATE_PROJECT_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case PROJECT_UUID: addAutoOrQuery(PRIVATE_PROJECT_UUID, 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;
new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); if (studyQueryResult.getNumResults() == 0) { throw new CatalogException("Unexpected error: Study " + studyId + " not found");
Query query = new Query(StudyDBAdaptor.QueryParams.VARIABLE_SET_UID.key(), variableSetId); QueryResult<Study> studyQueryResult = studyDBAdaptor.get(query, new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.UID.key())); if (studyQueryResult.getNumResults() == 0) { throw new CatalogException("Variable set " + variableStr + " not found");
.append(QueryOptions.INCLUDE, Arrays.asList(StudyDBAdaptor.QueryParams.UID.key(), StudyDBAdaptor.QueryParams.ID.key(), StudyDBAdaptor.QueryParams.FQN.key(), StudyDBAdaptor.QueryParams.VARIABLE_SET.key())) .append(DBAdaptor.INCLUDE_ACLS, true); QueryResult<Study> studyQueryResult = studyDBAdaptor.get(query, options);
.GROUP_USER_IDS.key(), "user2"), null, sessionIdUser).getResult().stream().map(Study::getId) .collect(Collectors.toSet())); sessionIdUser); assertEquals(new HashSet<>(Arrays.asList("study_4")), catalogManager.getStudyManager().get(new Query(StudyDBAdaptor.QueryParams .GROUP_USER_IDS.key(), "user3"), null, sessionIdUser).getResult().stream().map(Study::getId) .collect(Collectors.toSet())); catalogManager.getStudyManager().get(new Query(StudyDBAdaptor.QueryParams.PROJECT_ID.key(), projectId), null, sessionIdUser) .getResult().stream().map(Study::getId).collect(Collectors.toSet())); assertEquals(new HashSet<>(Arrays.asList("phase1", "phase3", "study_1", "study_2", "study_3", "study_4")), .collect(Collectors.toSet())); assertEquals(new HashSet<>(Arrays.asList("study_1", "study_2", "study_3", "study_4")), catalogManager.getStudyManager().get(new Query(StudyDBAdaptor.QueryParams.ID.key(), "~^study"), null, sessionIdUser).getResult().stream() .map(Study::getId).collect(Collectors.toSet())); assertEquals(Collections.singleton("s1"), catalogManager.getStudyManager().get(new Query(), null, sessionIdUser2).getResult()
@Test public void testCreateFolder() throws Exception { Query query = new Query(StudyDBAdaptor.QueryParams.OWNER.key(), "user2"); Study study = catalogManager.getStudyManager().get(query, QueryOptions.empty(), sessionIdUser2).first(); Set<String> paths = catalogManager.getFileManager().get(study.getFqn(), new Query("type", File.Type.DIRECTORY), new
/** * Job methods * *************************** */ @Test public void testCreateJob() throws CatalogException, IOException { Query query = new Query(StudyDBAdaptor.QueryParams.OWNER.key(), "user"); String studyId = catalogManager.getStudyManager().get(query, null, sessionIdUser).first().getId(); File outDir = catalogManager.getFileManager().createFolder(studyId, Paths.get("jobs", "myJob").toString(), null, true, null, QueryOptions.empty(), sessionIdUser).first(); catalogManager.getJobManager().create(studyId,new Job().setId("myJob").setToolId("samtool").setDescription("description") .setOutDir(outDir).setExecution("echo \"Hello world!\"").setStatus(new Job.JobStatus(Job.JobStatus.PREPARED)), null, sessionIdUser); catalogManager.getJobManager().create(studyId, new Job().setId("myReadyJob").setToolId("samtool").setDescription("description") .setOutDir(outDir).setExecution("echo \"Hello world!\"").setStatus(new Job.JobStatus(Job.JobStatus.READY)), null, sessionIdUser); catalogManager.getJobManager().create(studyId, new Job().setId("myQueuedJob").setToolId("samtool").setDescription("description") .setOutDir(outDir).setExecution("echo \"Hello world!\"").setStatus(new Job.JobStatus(Job.JobStatus.QUEUED)), null, sessionIdUser); catalogManager.getJobManager().create(studyId, new Job().setId("myErrorJob").setToolId("samtool").setDescription("description") .setOutDir(outDir).setExecution("echo \"Hello world!\"").setStatus(new Job.JobStatus(Job.JobStatus.ERROR)), null, sessionIdUser); query = new Query() .append(JobDBAdaptor.QueryParams.STATUS_NAME.key(), Arrays.asList(Job.JobStatus.PREPARED, Job.JobStatus.QUEUED, Job.JobStatus.RUNNING, Job.JobStatus.DONE)); QueryResult<Job> unfinishedJobs = catalogManager.getJobManager().get(String.valueOf(studyId), query, null, sessionIdUser); assertEquals(2, unfinishedJobs.getNumResults()); QueryResult<Job> allJobs = catalogManager.getJobManager().get(String.valueOf(studyId), (Query) null, null, sessionIdUser); assertEquals(4, allJobs.getNumResults()); }
Query query = new Query(QueryParams.UID.key(), id).append(QueryParams.STATUS_NAME.key(), Status.READY); if (count(query).first() == 0) { query.put(QueryParams.STATUS_NAME.key(), Status.DELETED); QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, QueryParams.STATUS_NAME.key()); Study study = get(query, options).first(); throw new CatalogDBException("The study {" + id + "} was already " + study.getStatus().getName()); query = new Query(QueryParams.UID.key(), id).append(QueryParams.STATUS_NAME.key(), Status.DELETED);
@Deprecated @Override public QueryResult<Project> delete(long id, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); checkId(id); // Check the project is active Query query = new Query(QueryParams.UID.key(), id).append(QueryParams.STATUS_NAME.key(), Status.READY); if (count(query).first() == 0) { query.put(QueryParams.STATUS_NAME.key(), Status.DELETED); QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, QueryParams.STATUS_NAME.key()); Project project = get(query, options).first(); throw new CatalogDBException("The project {" + id + "} was already " + project.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 studies (if any) query = new Query(StudyDBAdaptor.QueryParams.PROJECT_ID.key(), id); dbAdaptorFactory.getCatalogStudyDBAdaptor().delete(query, queryOptions); } // Change the status of the project to deleted setStatus(id, Status.DELETED); query = new Query(QueryParams.UID.key(), id) .append(QueryParams.STATUS_NAME.key(), Status.DELETED); return endQuery("Delete project", startTime, get(query, queryOptions)); }
/** * Checks if the study is empty or has more active information. * * @param studyId Id of the study. * @throws CatalogDBException when there exists active files, samples, cohorts... */ private void checkEmptyStudy(long studyId) throws CatalogDBException { Query query = new Query(PRIVATE_STUDY_ID, studyId) .append(QueryParams.STATUS_NAME.key(), "!=" + Status.DELETED); // Check files if (dbAdaptorFactory.getCatalogFileDBAdaptor().count(query).first() > 0) { throw new CatalogDBException("Cannot delete study " + studyId + ". There are files being used."); } // Check samples if (dbAdaptorFactory.getCatalogSampleDBAdaptor().count(query).first() > 0) { throw new CatalogDBException("Cannot delete study " + studyId + ". There are samples being used."); } // Check individuals if (dbAdaptorFactory.getCatalogIndividualDBAdaptor().count(query).first() > 0) { throw new CatalogDBException("Cannot delete study " + studyId + ". There are individuals being used."); } // Check cohorts if (dbAdaptorFactory.getCatalogCohortDBAdaptor().count(query).first() > 0) { throw new CatalogDBException("Cannot delete study " + studyId + ". There are cohorts being used."); } }
@Override public QueryResult<Project> get(Query query, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); List<Project> documentList = new ArrayList<>(); QueryResult<Project> queryResult; try (DBIterator<Project> dbIterator = iterator(query, options)) { while (dbIterator.hasNext()) { documentList.add(dbIterator.next()); } } queryResult = endQuery("Get", startTime, documentList); if (options == null || !options.containsKey(QueryOptions.EXCLUDE) || (!options.getAsStringList(QueryOptions.EXCLUDE).contains("projects.studies") && !options.getAsStringList(QueryOptions.EXCLUDE).contains("studies"))) { for (Project project : queryResult.getResult()) { Query studyQuery = new Query(StudyDBAdaptor.QueryParams.PROJECT_UID.key(), project.getUid()); try { QueryResult<Study> studyQueryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().get(studyQuery, options); project.setStudies(studyQueryResult.getResult()); } catch (CatalogDBException e) { logger.error("{}", e.getMessage(), e); } } } return queryResult; }
@Override public QueryResult<Project> get(Query query, QueryOptions options, String user) throws CatalogDBException, CatalogAuthorizationException { long startTime = startQuery(); List<Project> documentList = new ArrayList<>(); QueryResult<Project> queryResult; try (DBIterator<Project> dbIterator = iterator(query, options, user)) { while (dbIterator.hasNext()) { documentList.add(dbIterator.next()); } } queryResult = endQuery("Get", startTime, documentList); if (options == null || !options.containsKey(QueryOptions.EXCLUDE) || (!options.getAsStringList(QueryOptions.EXCLUDE).contains("projects.studies") && !options.getAsStringList(QueryOptions.EXCLUDE).contains("studies"))) { for (Project project : queryResult.getResult()) { Query studyQuery = new Query(StudyDBAdaptor.QueryParams.PROJECT_UID.key(), project.getUid()); try { QueryResult<Study> studyQueryResult = dbAdaptorFactory.getCatalogStudyDBAdaptor().get(studyQuery, options, user); project.setStudies(studyQueryResult.getResult()); } catch (CatalogDBException e) { logger.error("{}", e.getMessage(), e); } } } return queryResult; }
public QueryResult<Group> syncGroupWith(String studyStr, String groupId, Group.Sync syncedFrom, String sessionId) throws CatalogException { ParamUtils.checkObj(syncedFrom, "sync"); String userId = catalogManager.getUserManager().getUserId(sessionId); Study study = resolveId(studyStr, userId); if (StringUtils.isEmpty(groupId)) { throw new CatalogException("Missing group name parameter"); } // Fix the groupId if (!groupId.startsWith("@")) { groupId = "@" + groupId; } authorizationManager.checkSyncGroupPermissions(study.getUid(), userId, groupId); QueryResult<Group> group = studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); if (group.first().getSyncedFrom() != null && StringUtils.isNotEmpty(group.first().getSyncedFrom().getAuthOrigin()) && StringUtils.isNotEmpty(group.first().getSyncedFrom().getRemoteGroup())) { throw new CatalogException("Cannot modify already existing sync information."); } // Check the group exists Query query = new Query() .append(StudyDBAdaptor.QueryParams.UID.key(), study.getUid()) .append(StudyDBAdaptor.QueryParams.GROUP_NAME.key(), groupId); if (studyDBAdaptor.count(query).first() == 0) { throw new CatalogException("The group " + groupId + " does not exist."); } studyDBAdaptor.syncGroup(study.getUid(), groupId, syncedFrom); return studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); }
@Override public void markDeletedPermissionRule(long studyId, Study.Entity entry, String permissionRuleId, PermissionRule.DeleteAction deleteAction) throws CatalogDBException { if (entry == null) { throw new CatalogDBException("Missing entry parameter"); } String newPermissionRuleId = permissionRuleId + INTERNAL_DELIMITER + "DELETE_" + deleteAction.name(); Document query = new Document() .append(PRIVATE_UID, studyId) .append(QueryParams.PERMISSION_RULES.key() + "." + entry + ".id", permissionRuleId); // Change permissionRule id Document update = new Document("$set", new Document(QueryParams.PERMISSION_RULES.key() + "." + entry + ".$.id", newPermissionRuleId)); logger.debug("Mark permission rule for deletion: Query {}, Update {}", query.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()), update.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); QueryResult<UpdateResult> updateQueryResult = studyCollection.update(query, update, QueryOptions.empty()); if (updateQueryResult.first().getMatchedCount() == 0) { throw new CatalogDBException("Permission rule " + permissionRuleId + " not found"); } if (updateQueryResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Unexpected error: Permission rule " + permissionRuleId + " could not be marked for deletion"); } }
@Override public void createPermissionRule(long studyId, Study.Entity entry, PermissionRule permissionRule) throws CatalogDBException { if (entry == null) { throw new CatalogDBException("Missing entry parameter"); } // Get permission rules from study QueryResult<PermissionRule> permissionRulesResult = getPermissionRules(studyId, entry); List<Document> permissionDocumentList = new ArrayList<>(); if (permissionRulesResult.getNumResults() > 0) { for (PermissionRule rule : permissionRulesResult.getResult()) { // We add all the permission rules with different id if (!rule.getId().equals(permissionRule.getId())) { permissionDocumentList.add(getMongoDBDocument(rule, "PermissionRules")); } else { throw new CatalogDBException("Permission rule " + permissionRule.getId() + " already exists."); } } } permissionDocumentList.add(getMongoDBDocument(permissionRule, "PermissionRules")); // We update the study document to contain the new permission rules Query query = new Query(QueryParams.UID.key(), studyId); Document update = new Document("$set", new Document(QueryParams.PERMISSION_RULES.key() + "." + entry, permissionDocumentList)); QueryResult<UpdateResult> updateResult = studyCollection.update(parseQuery(query, true), update, QueryOptions.empty()); if (updateResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Unexpected error occurred when adding new permission rules to study"); } }
/** * Study methods * *************************** */ @Test public void testModifyStudy() throws Exception { Query query = new Query(StudyDBAdaptor.QueryParams.OWNER.key(), "user"); String studyId = catalogManager.getStudyManager().get(query, null, sessionIdUser).first().getId(); String newName = "Phase 1 " + StringUtils.randomString(20); String newDescription = StringUtils.randomString(500); ObjectMap parameters = new ObjectMap(); parameters.put("name", newName); parameters.put("description", newDescription); BasicDBObject attributes = new BasicDBObject("key", "value"); parameters.put("attributes", attributes); catalogManager.getStudyManager().update(studyId, parameters, null, sessionIdUser); QueryResult<Study> result = catalogManager.getStudyManager().get(studyId, null, sessionIdUser); System.out.println(result); Study study = result.first(); assertEquals(study.getName(), newName); assertEquals(study.getDescription(), newDescription); for (Map.Entry<String, Object> entry : attributes.entrySet()) { assertEquals(study.getAttributes().get(entry.getKey()), entry.getValue()); } }
@Override public void updateProjectId(long projectUid, String newProjectId) throws CatalogDBException { Query query = new Query(QueryParams.PROJECT_UID.key(), projectUid); QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( QueryParams.FQN.key(), QueryParams.UID.key() )); QueryResult<Study> studyQueryResult = get(query, options); for (Study study : studyQueryResult.getResult()) { String[] split = study.getFqn().split("@"); String[] split1 = split[1].split(":"); String newFqn = split[0] + "@" + newProjectId + ":" + split1[1]; // Update the internal project id and fqn Bson update = new Document("$set", new Document() .append(QueryParams.FQN.key(), newFqn) .append(PRIVATE_PROJECT_ID, newProjectId) ); Bson bsonQuery = Filters.eq(QueryParams.UID.key(), study.getUid()); QueryResult<UpdateResult> result = studyCollection.update(bsonQuery, update, null); if (result.getResult().get(0).getModifiedCount() == 0) { //Check if the the project id was modified throw new CatalogDBException("CRITICAL: Could not update new project id references in study " + study.getFqn()); } } }
@Override public QueryResult<VariableSet> removeFieldFromVariableSet(long variableSetId, String name, String user) throws CatalogDBException, CatalogAuthorizationException { long startTime = startQuery(); QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, new QueryOptions(), user); checkVariableInVariableSet(variableSet.first(), name); Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_UID.key(), variableSetId); Bson update = Updates.pull(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), Filters.eq("id", 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); dbAdaptorFactory.getCatalogIndividualDBAdaptor().removeAnnotationField(variableSetId, name); dbAdaptorFactory.getCatalogFamilyDBAdaptor().removeAnnotationField(variableSetId, name); dbAdaptorFactory.getCatalogFileDBAdaptor().removeAnnotationField(variableSetId, name); return endQuery("Remove field from Variable Set", startTime, getVariableSet(variableSetId, null)); }
@Override public QueryResult<VariableSet> addFieldToVariableSet(long variableSetId, Variable variable, String user) throws CatalogDBException, CatalogAuthorizationException { long startTime = startQuery(); QueryResult<VariableSet> variableSet = getVariableSet(variableSetId, new QueryOptions(), user); checkVariableNotInVariableSet(variableSet.first(), variable.getId()); Bson bsonQuery = Filters.eq(QueryParams.VARIABLE_SET_UID.key(), variableSetId); Bson update = Updates.push(QueryParams.VARIABLE_SET.key() + ".$." + VariableSetParams.VARIABLE.key(), getMongoDBDocument(variable, "variable")); QueryResult<UpdateResult> queryResult = studyCollection.update(bsonQuery, update, null); if (queryResult.first().getModifiedCount() == 0) { throw CatalogDBException.updateError("VariableSet", variableSetId); } if (variable.isRequired()) { dbAdaptorFactory.getCatalogSampleDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogCohortDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogIndividualDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogFamilyDBAdaptor().addVariableToAnnotations(variableSetId, variable); dbAdaptorFactory.getCatalogFileDBAdaptor().addVariableToAnnotations(variableSetId, variable); } return endQuery("Add field to variable set", startTime, getVariableSet(variableSetId, null)); }