fixComplexQueryParam(QueryParams.ATTRIBUTES.key(), query); fixComplexQueryParam(QueryParams.BATTRIBUTES.key(), query); fixComplexQueryParam(QueryParams.NATTRIBUTES.key(), query); switch (queryParam) { case UID: addAutoOrQuery(PRIVATE_UID, queryParam.key(), query, queryParam.type(), andBsonList); break; case STUDY_UID: addAutoOrQuery(PRIVATE_STUDY_ID, queryParam.key(), query, queryParam.type(), andBsonList); break; case ATTRIBUTES: 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 CREATION_DATE: addAutoOrQuery(PRIVATE_CREATION_DATE, queryParam.key(), query, queryParam.type(), andBsonList); break; case MODIFICATION_DATE: addAutoOrQuery(PRIVATE_MODIFICATION_DATE, queryParam.key(), query, queryParam.type(), andBsonList); break;
@Override public QueryResult<Interpretation> 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 = catalogManager.getUserManager().getUserId(sessionId); Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); query.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); QueryResult<Interpretation> queryResult = interpretationDBAdaptor.get(query, options, userId); List<Interpretation> results = new ArrayList<>(queryResult.getResult().size()); for (Interpretation interpretation : queryResult.getResult()) { if (StringUtils.isNotEmpty(interpretation.getInterpretation().getClinicalAnalysisId())) { try { catalogManager.getClinicalAnalysisManager().smartResolutor(study.getUid(), interpretation.getInterpretation().getClinicalAnalysisId(), userId); results.add(interpretation); } catch (CatalogException e) { logger.debug("Removing interpretation " + interpretation.getUuid() + " from results. User " + userId + " does not have " + "proper permissions"); } } } queryResult.setResult(results); queryResult.setNumTotalResults(results.size()); queryResult.setNumResults(results.size()); return queryResult; }
@Override public QueryResult<Interpretation> insert(long studyId, Interpretation interpretation, QueryOptions options) throws CatalogDBException { long startTime = startQuery(); dbAdaptorFactory.getCatalogStudyDBAdaptor().checkId(studyId); List<Bson> filterList = new ArrayList<>(); filterList.add(Filters.eq(QueryParams.ID.key(), interpretation.getInterpretation().getId())); filterList.add(Filters.eq(PRIVATE_STUDY_ID, studyId)); filterList.add(Filters.eq(QueryParams.STATUS.key(), Status.READY)); Bson bson = Filters.and(filterList); QueryResult<Long> count = interpretationCollection.count(bson); if (count.getResult().get(0) > 0) { throw new CatalogDBException("Cannot create interpretation. An interpretation with { id: '" + interpretation.getInterpretation().getId() + "'} already exists."); } long interpretationUid = getNewId(); interpretation.setUid(interpretationUid); interpretation.setStudyUid(studyId); if (StringUtils.isEmpty(interpretation.getUuid())) { interpretation.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.INTERPRETATION)); } Document interpretationObject = interpretationConverter.convertToStorageType(interpretation); if (StringUtils.isNotEmpty(interpretation.getInterpretation().getCreationDate())) { interpretationObject.put(PRIVATE_CREATION_DATE, TimeUtils.toDate(interpretation.getInterpretation().getCreationDate())); } else { interpretationObject.put(PRIVATE_CREATION_DATE, TimeUtils.getDate()); } interpretationCollection.insert(interpretationObject, null); return endQuery("createInterpretation", startTime, get(interpretationUid, options)); }
.append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) .append(QueryParams.STUDY_UID.key(), studyId); QueryResult<Long> count = count(tmpQuery); if (count.getResult().get(0) > 0) { throw new CatalogDBException("Cannot set id for interpretation. A interpretation with { id: '" + parameters.get(QueryParams.ID.key()) + "'} already exists."); document.getSet().put(QueryParams.MODIFICATION_DATE.key(), time); document.getSet().put(PRIVATE_MODIFICATION_DATE, date);
@Override Interpretation smartResolutor(long studyUid, String entry, String user) throws CatalogException { Query query = new Query() .append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), studyUid); if (UUIDUtils.isOpenCGAUUID(entry)) { query.put(InterpretationDBAdaptor.QueryParams.UUID.key(), entry); } else { query.put(InterpretationDBAdaptor.QueryParams.ID.key(), entry); InterpretationDBAdaptor.QueryParams.UUID.key(), InterpretationDBAdaptor.QueryParams.CLINICAL_ANALYSIS.key(), InterpretationDBAdaptor.QueryParams.UID.key(), InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), InterpretationDBAdaptor.QueryParams.ID.key(), InterpretationDBAdaptor.QueryParams.STATUS.key())); QueryResult<Interpretation> interpretationQueryResult = interpretationDBAdaptor.get(query, options, user); if (interpretationQueryResult.getNumResults() == 0) {
/** * Creates a new version for all the interpretations matching the query. * * @param query Query object. */ private void createNewVersion(Query query) throws CatalogDBException { QueryResult<Document> queryResult = nativeGet(query, new QueryOptions(QueryOptions.EXCLUDE, "_id")); for (Document document : queryResult.getResult()) { Document updateOldVersion = new Document(); updateOldVersion.put(LAST_OF_VERSION, false); // Perform the update on the previous version Document queryDocument = new Document() .append(PRIVATE_STUDY_ID, document.getLong(PRIVATE_STUDY_ID)) .append(QueryParams.VERSION.key(), document.getInteger(QueryParams.VERSION.key())) .append(PRIVATE_UID, document.getLong(PRIVATE_UID)); QueryResult<UpdateResult> updateResult = interpretationCollection.update(queryDocument, new Document("$set", updateOldVersion), null); if (updateResult.first().getModifiedCount() == 0) { throw new CatalogDBException("Internal error: Could not update interpretation"); } // We update the information for the new version of the document document.put(LAST_OF_VERSION, true); document.put(QueryParams.VERSION.key(), document.getInteger(QueryParams.VERSION.key()) + 1); // Insert the new version document interpretationCollection.insert(document, QueryOptions.empty()); } }
@Override public QueryResult<Interpretation> update(long id, ObjectMap parameters, QueryOptions queryOptions) throws CatalogDBException { long startTime = startQuery(); Query query = new Query(QueryParams.UID.key(), id); UpdateDocument updateDocument = parseAndValidateUpdateParams(parameters, query, queryOptions); if (queryOptions.getBoolean(Constants.INCREMENT_VERSION)) { createNewVersion(query); } Document updateOperation = updateDocument.toFinalUpdateDocument(); if (!updateOperation.isEmpty()) { Bson bsonQuery = Filters.eq(PRIVATE_UID, id); logger.debug("Update interpretation. Query: {}, Update: {}", bsonQuery.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry()), updateDocument); QueryResult<UpdateResult> update = interpretationCollection.update(bsonQuery, updateOperation, null); if (update.getResult().isEmpty() || update.getResult().get(0).getMatchedCount() == 0) { throw CatalogDBException.uidNotFound("Interpretation", id); } } return endQuery("Modify interpretation", startTime, get(id, queryOptions)); }
default boolean exists(long interpretationId) throws CatalogDBException { return count(new Query(QueryParams.UID.key(), interpretationId)).first() > 0; }