QueryResult<Cohort> insert(long studyId, Cohort cohort, List<VariableSet> variableSetList, QueryOptions options) throws CatalogDBException;
default boolean exists(long cohortId) throws CatalogDBException { return count(new Query(QueryParams.UID.key(), cohortId)).first() > 0; }
default void checkId(long cohortId) throws CatalogDBException { if (cohortId < 0) { throw CatalogDBException.newInstance("Cohort id '{}' is not valid: ", cohortId); } if (!exists(cohortId)) { throw CatalogDBException.newInstance("Cohort id '{}' does not exist", cohortId); } }
@Override public DBIterator<Cohort> iterator(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { options = ParamUtils.defaultObject(options, QueryOptions::new); query = ParamUtils.defaultObject(query, Query::new); String userId = userManager.getUserId(sessionId); Study study = studyManager.resolveId(studyStr, userId); fixQueryObject(study, query, sessionId); Query myQuery = new Query(query).append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); return cohortDBAdaptor.iterator(myQuery, options, userId); }
@Override Cohort smartResolutor(long studyUid, String entry, String user) throws CatalogException { Query query = new Query() .append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), studyUid); if (UUIDUtils.isOpenCGAUUID(entry)) { query.put(CohortDBAdaptor.QueryParams.UUID.key(), entry); } else { query.put(CohortDBAdaptor.QueryParams.ID.key(), entry); } QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( CohortDBAdaptor.QueryParams.UUID.key(), CohortDBAdaptor.QueryParams.UID.key(), CohortDBAdaptor.QueryParams.STUDY_UID.key(), CohortDBAdaptor.QueryParams.ID.key(), CohortDBAdaptor.QueryParams.RELEASE.key(), CohortDBAdaptor.QueryParams.SAMPLES.key(), CohortDBAdaptor.QueryParams.STATUS.key())); QueryResult<Cohort> cohortQueryResult = cohortDBAdaptor.get(query, options, user); if (cohortQueryResult.getNumResults() == 0) { cohortQueryResult = cohortDBAdaptor.get(query, options); if (cohortQueryResult.getNumResults() == 0) { throw new CatalogException("Cohort " + entry + " not found"); } else { throw new CatalogAuthorizationException("Permission denied. " + user + " is not allowed to see the cohort " + entry); } } else if (cohortQueryResult.getNumResults() > 1) { throw new CatalogException("More than one cohort found based on " + entry); } else { return cohortQueryResult.first(); } }
threads.add(new Thread(() -> { try { dbAdaptorFactory.getCatalogCohortDBAdaptor().insert(studyId, new Cohort(cohortName, Study.Type.COLLECTION, "", "", Collections.emptyList(), 1, null), null); } catch (CatalogException ignore) { List<Cohort> cohorts = dbAdaptorFactory.getCatalogCohortDBAdaptor().get( new Query(CohortDBAdaptor.QueryParams.STUDY_UID.key(), studyId), null).getResult(); assertEquals(numCohorts, cohorts.size());
finalQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); iterator = cohortDBAdaptor.iterator(finalQuery, QueryOptions.empty(), userId); .append(CohortDBAdaptor.QueryParams.STATUS_NAME.key(), Status.DELETED) .append(CohortDBAdaptor.QueryParams.ID.key(), cohort.getId() + suffixName); QueryResult<Long> update = cohortDBAdaptor.update(updateQuery, updateParams, QueryOptions.empty()); if (update.first() > 0) { numModified += 1;
public Long getStudyId(long cohortId) throws CatalogException { return cohortDBAdaptor.getStudyId(cohortId); }
for (String line = br.readLine(); line != null; line = br.readLine()) { Map<String, Object> file = objectMapper.readValue(line, Map.class); cohortDBAdaptor.nativeInsert(file, owner);
.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), studyIds) .append(CohortDBAdaptor.QueryParams.RELEASE.key(), "<=" + release); dbIterator = cohortDBAdaptor.nativeIterator(query, QueryOptions.empty()); exportToFile(dbIterator, outputDir.resolve("cohorts.json").toFile(), objectMapper, "cohorts");
@Override public QueryResult groupBy(@Nullable String studyStr, Query query, List<String> fields, QueryOptions options, String sessionId) throws CatalogException { query = ParamUtils.defaultObject(query, Query::new); options = ParamUtils.defaultObject(options, QueryOptions::new); ParamUtils.checkObj(fields, "fields"); String userId = userManager.getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); AnnotationUtils.fixQueryOptionAnnotation(options); // Add study id to the query query.put(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult queryResult = cohortDBAdaptor.groupBy(query, fields, options, userId); return ParamUtils.defaultObject(queryResult, QueryResult::new); }
public QueryResult<VariableSetSummary> getVariableSetSummary(String studyStr, String variableSetStr, String sessionId) throws CatalogException { MyResourceId resource = getVariableSetId(variableSetStr, studyStr, sessionId); String userId = resource.getUser(); QueryResult<VariableSet> variableSet = studyDBAdaptor.getVariableSet(resource.getResourceId(), new QueryOptions(), userId); if (variableSet.getNumResults() == 0) { logger.error("getVariableSetSummary: Could not find variable set id {}. {} results returned", variableSetStr, variableSet.getNumResults()); throw new CatalogDBException("Variable set " + variableSetStr + " not found."); } int dbTime = 0; VariableSetSummary variableSetSummary = new VariableSetSummary(resource.getResourceId(), variableSet.first().getId()); QueryResult<VariableSummary> annotationSummary = sampleDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setSamples(annotationSummary.getResult()); annotationSummary = cohortDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setCohorts(annotationSummary.getResult()); annotationSummary = individualDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setIndividuals(annotationSummary.getResult()); annotationSummary = familyDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); dbTime += annotationSummary.getDbTime(); variableSetSummary.setFamilies(annotationSummary.getResult()); return new QueryResult<>("Variable set summary", dbTime, 1, 1, "", "", Arrays.asList(variableSetSummary)); }
.append(CohortDBAdaptor.QueryParams.SAMPLE_UIDS.key(), sample.getUid()) .append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), studyId); DBIterator<Cohort> cohortIterator = cohortDBAdaptor.iterator(query, QueryOptions.empty()); List<String> errorCohorts = new ArrayList<>(); boolean associatedToDefaultCohort = false;
@Override public QueryResult<Cohort> search(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { query = ParamUtils.defaultObject(query, Query::new); options = ParamUtils.defaultObject(options, QueryOptions::new); String userId = userManager.getUserId(sessionId); Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, query); AnnotationUtils.fixQueryOptionAnnotation(options); fixQueryObject(study, query, sessionId); query.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult<Cohort> queryResult = cohortDBAdaptor.get(query, options, userId); // authorizationManager.filterCohorts(userId, studyId, queryResultAux.getResult()); return queryResult; }
private Boolean indexCohort(CatalogSolrManager catalogSolrManager, Study study) throws CatalogException, IOException { Query query = new Query() .append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()) .append(CohortDBAdaptor.QueryParams.STATUS_NAME.key(), Constants.ALL_STATUS); QueryOptions cohortQueryOptions = new QueryOptions() .append(QueryOptions.INCLUDE, Arrays.asList(CohortDBAdaptor.QueryParams.ID.key(), CohortDBAdaptor.QueryParams.NAME.key(), CohortDBAdaptor.QueryParams.CREATION_DATE.key(), CohortDBAdaptor.QueryParams.STATUS.key(), CohortDBAdaptor.QueryParams.RELEASE.key(), CohortDBAdaptor.QueryParams.ANNOTATION_SETS.key(), CohortDBAdaptor.QueryParams.SAMPLE_UIDS.key(), CohortDBAdaptor.QueryParams.TYPE.key())) .append(DBAdaptor.INCLUDE_ACLS, true) .append(Constants.FLATTENED_ANNOTATIONS, true); catalogSolrManager.insertCatalogCollection(this.cohortDBAdaptor.iterator(query, cohortQueryOptions), new CatalogCohortToSolrCohortConverter(study), CatalogSolrManager.COHORT_SOLR_COLLECTION); return true; }
.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), resource.getStudy().getUid()) .append(CohortDBAdaptor.QueryParams.UID.key(), resource.getResource().getUid()); QueryResult<Cohort> cohortQueryResult = cohortDBAdaptor.get(query, new QueryOptions(QueryOptions.INCLUDE, CohortDBAdaptor.QueryParams.SAMPLES.key()));
@Test @Ignore // TODO: This should be tested in the sample manager, not here !!! public void deleteSampleFail3Test() throws Exception { long studyId = user3.getProjects().get(0).getStudies().get(0).getUid(); Sample hg0097 = new Sample("HG0097", "1000g", new Individual(), "A description", 1); QueryResult<Sample> createResult = dbAdaptorFactory.getCatalogSampleDBAdaptor().insert(studyId, hg0097, null); dbAdaptorFactory.getCatalogCohortDBAdaptor().insert(studyId, new Cohort("Cohort", Study.Type.COLLECTION, "", "", Collections.singletonList(createResult.first()), 1, null), null); thrown.expect(CatalogDBException.class); dbAdaptorFactory.getCatalogSampleDBAdaptor().delete(createResult.first().getUid()); }
@Override public QueryResult<Cohort> count(String studyStr, Query query, String sessionId) throws CatalogException { query = ParamUtils.defaultObject(query, Query::new); String userId = userManager.getUserId(sessionId); Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, query); fixQueryObject(study, query, sessionId); query.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult<Long> queryResultAux = cohortDBAdaptor.count(query, userId, StudyAclEntry.StudyPermissions.VIEW_COHORTS); return new QueryResult<>("count", queryResultAux.getDbTime(), 0, queryResultAux.first(), queryResultAux.getWarningMsg(), queryResultAux.getErrorMsg(), Collections.emptyList()); }
.append(CohortDBAdaptor.QueryParams.SAMPLE_UIDS.key(), sample.getUid()) .append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); DBIterator<Cohort> cohortIterator = cohortDBAdaptor.iterator(auxQuery, QueryOptions.empty()); while (cohortIterator.hasNext()) { Cohort cohort = cohortIterator.next();
@Override public QueryResult<Cohort> get(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { query = ParamUtils.defaultObject(query, Query::new); options = ParamUtils.defaultObject(options, QueryOptions::new); String userId = userManager.getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, query); AnnotationUtils.fixQueryOptionAnnotation(options); fixQueryObject(study, query, sessionId); query.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult<Cohort> cohortQueryResult = cohortDBAdaptor.get(query, options, userId); if (cohortQueryResult.getNumResults() == 0 && query.containsKey(CohortDBAdaptor.QueryParams.UID.key())) { List<Long> idList = query.getAsLongList(CohortDBAdaptor.QueryParams.UID.key()); for (Long myId : idList) { authorizationManager.checkCohortPermission(study.getUid(), myId, userId, CohortAclEntry.CohortPermissions.VIEW); } } return cohortQueryResult; }