diff --git a/opencga-analysis/src/test/java/org/opencb/opencga/analysis/variant/metadata/CatalogStorageMetadataSynchronizerTest.java b/opencga-analysis/src/test/java/org/opencb/opencga/analysis/variant/metadata/CatalogStorageMetadataSynchronizerTest.java index ebbde37158d..e9fd8439538 100644 --- a/opencga-analysis/src/test/java/org/opencb/opencga/analysis/variant/metadata/CatalogStorageMetadataSynchronizerTest.java +++ b/opencga-analysis/src/test/java/org/opencb/opencga/analysis/variant/metadata/CatalogStorageMetadataSynchronizerTest.java @@ -158,7 +158,7 @@ public static File create(String resourceName, boolean indexed) throws IOExcepti catalogManager.getFileManager().updateFileInternalVariantIndex(file, FileInternalVariantIndex.init().setStatus(new VariantIndexStatus(InternalStatus.READY)), sessionId); indexedFiles.add(file.getName()); - List samples = catalogManager.getCohortManager().getSamples(studyId, cohortId, sessionId).getResults().stream().map(Sample::getId).collect(Collectors.toList()); + List samples = catalogManager.getCohortManager().get(studyId, cohortId, QueryOptions.empty(), sessionId).first().getSamples().stream().map(Sample::getId).collect(Collectors.toList()); samples.addAll(file.getSampleIds()); List sampleReferenceParams = samples.stream().map(s -> new SampleReferenceParam().setId(s)).collect(Collectors.toList()); catalogManager.getCohortManager().update(studyId, cohortId, @@ -172,8 +172,9 @@ public void updateCatalogFromStorageTest() throws Exception { StudyMetadata sm = studyConfigurationFactory.getStudyMetadata(studyId); - List samples = catalogManager.getCohortManager().getSamples(studyId, cohortId, sessionId) - .getResults() + List samples = catalogManager.getCohortManager().get(studyId, cohortId, QueryOptions.empty(), sessionId) + .first() + .getSamples() .stream() .map(Sample::getId) .collect(Collectors.toList()); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/CoreDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/CoreDBAdaptor.java index 48e8e5376ce..ed69e817333 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/CoreDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/CoreDBAdaptor.java @@ -28,7 +28,7 @@ DBIterator iterator(long studyUid, Query query, QueryOptions options, String DBIterator nativeIterator(long studyUid, Query query, QueryOptions options, String user) throws CatalogDBException, CatalogAuthorizationException, CatalogParameterException; - OpenCGAResult count(Query query, String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException; + OpenCGAResult count(Query query, String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException; OpenCGAResult groupBy(Query query, List fields, QueryOptions options, String user) throws CatalogDBException, CatalogAuthorizationException, CatalogParameterException; diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/DBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/DBAdaptor.java index 2b0347446ea..a5d8ffc69d3 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/DBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/api/DBAdaptor.java @@ -45,11 +45,11 @@ public interface DBAdaptor extends Iterable { @Deprecated String FORCE = "force"; - default OpenCGAResult count() throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + default OpenCGAResult count() throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(new Query()); } - OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException; + OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException; default OpenCGAResult stats() { return stats(new Query()); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ClinicalAnalysisMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ClinicalAnalysisMongoDBAdaptor.java index 6b52a53aefc..3791a1601b8 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ClinicalAnalysisMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ClinicalAnalysisMongoDBAdaptor.java @@ -148,23 +148,24 @@ public MongoDBCollection getClinicalCollection() { } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) + throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query); return new OpenCGAResult<>(clinicalCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(final Query query, final String user) + public OpenCGAResult count(final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query, user); } - OpenCGAResult count(ClientSession clientSession, final Query query, final String user) + OpenCGAResult count(ClientSession clientSession, final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, user); logger.debug("Clinical count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); @@ -300,7 +301,7 @@ UpdateDocument parseAndValidateUpdateParams(ObjectMap parameters, List count = count(tmpQuery); + OpenCGAResult count = count(tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot set id for clinical analysis. A clinical analysis with { id: '" + parameters.get(QueryParams.ID.key()) + "'} already exists."); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/CohortMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/CohortMongoDBAdaptor.java index ce8b4e24f64..b1f9fff6db7 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/CohortMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/CohortMongoDBAdaptor.java @@ -191,23 +191,23 @@ public OpenCGAResult unmarkPermissionRule(long studyId, String permissionRuleId) } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - private OpenCGAResult count(ClientSession clientSession, Query query) + private OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { long startTime = startQuery(); return endQuery(startTime, cohortCollection.count(clientSession, parseQuery(query))); } @Override - public OpenCGAResult count(final Query query, final String user) + public OpenCGAResult count(final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query, user); } - private OpenCGAResult count(ClientSession clientSession, final Query query, final String user) + private OpenCGAResult count(ClientSession clientSession, final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, user); logger.debug("Cohort count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); @@ -441,8 +441,8 @@ private UpdateDocument parseAndValidateUpdateParams(ClientSession clientSession, tmpQuery = new Query() .append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) - .append(STUDY_UID.key(), studyId); - OpenCGAResult count = count(clientSession, tmpQuery); + .append(QueryParams.STUDY_UID.key(), studyId); + OpenCGAResult count = count(clientSession, tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot update the " + QueryParams.ID.key() + ". Cohort " + parameters.get(QueryParams.ID.key()) + " already exists."); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FamilyMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FamilyMongoDBAdaptor.java index 24933e45e52..1b7b0776c71 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FamilyMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FamilyMongoDBAdaptor.java @@ -46,6 +46,7 @@ import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; +import org.opencb.opencga.core.models.clinical.ClinicalAnalysis; import org.opencb.opencga.core.models.cohort.Cohort; import org.opencb.opencga.core.models.common.AnnotationSet; import org.opencb.opencga.core.models.common.Enums; @@ -230,23 +231,23 @@ private void createMissingIndividual(ClientSession clientSession, long studyUid, } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query); return new OpenCGAResult<>(familyCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(final Query query, final String user) + public OpenCGAResult count(final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query, user); } - public OpenCGAResult count(ClientSession clientSession, final Query query, final String user) + public OpenCGAResult count(ClientSession clientSession, final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, user); logger.debug("Family count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); @@ -654,7 +655,7 @@ UpdateDocument parseAndValidateUpdateParams(ClientSession clientSession, ObjectM tmpQuery = new Query() .append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) .append(QueryParams.STUDY_UID.key(), familyDataResult.first().getStudyUid()); - OpenCGAResult count = count(clientSession, tmpQuery); + OpenCGAResult count = count(clientSession, tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot set '" + QueryParams.ID.key() + "' for family. A family with { '" + QueryParams.ID.key() + "': '" + parameters.get(QueryParams.ID.key()) + "'} already exists."); @@ -731,7 +732,7 @@ OpenCGAResult privateDelete(ClientSession clientSession, Document family Query queryCheck = new Query() .append(ClinicalAnalysisDBAdaptor.QueryParams.FAMILY_UID.key(), familyUid) .append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), studyUid); - OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, queryCheck); + OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, queryCheck); if (count.getNumMatches() > 0) { throw new CatalogDBException("Could not delete family. Family is in use in " + count.getNumMatches() + " cases"); } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FileMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FileMongoDBAdaptor.java index fdf7b132be3..c2834fef6d2 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FileMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/FileMongoDBAdaptor.java @@ -965,18 +965,18 @@ public OpenCGAResult restore(long id, QueryOptions queryOptions) throws CatalogD } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query); return new OpenCGAResult<>(fileCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(final Query query, final String user) + public OpenCGAResult count(final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, user); logger.debug("File count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/IndividualMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/IndividualMongoDBAdaptor.java index de95e128668..b6e96f331ec 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/IndividualMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/IndividualMongoDBAdaptor.java @@ -275,23 +275,24 @@ public OpenCGAResult unmarkPermissionRule(long studyId, String permissionRuleId) } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) + throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - public OpenCGAResult count(ClientSession clientSession, Query query) + public OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query); return new OpenCGAResult<>(individualCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(Query query, String user) + public OpenCGAResult count(Query query, String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query, user); } - OpenCGAResult count(ClientSession clientSession, Query query, String user) + OpenCGAResult count(ClientSession clientSession, Query query, String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, user); logger.debug("Individual count: query : {}, dbTime: {}", bson.toBsonDocument(Document.class, @@ -653,7 +654,7 @@ private void checkInUseInClinicalAnalysis(ClientSession clientSession, Document Query query = new Query() .append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), studyUid) .append(ClinicalAnalysisDBAdaptor.QueryParams.INDIVIDUAL.key(), individualUid); - OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, query); + OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, query); if (count.getNumMatches() > 0) { throw new CatalogDBException("Could not delete individual '" + individualId + "'. Individual is in use in " + count.getNumMatches() + " cases"); @@ -675,7 +676,7 @@ UpdateDocument parseAndValidateUpdateParams(ClientSession clientSession, ObjectM Query tmpQuery = new Query() .append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) .append(QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult count = count(clientSession, tmpQuery); + OpenCGAResult count = count(clientSession, tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot set id for individual. An individual with { id: '" + parameters.get(QueryParams.ID.key()) + "'} already exists."); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/InterpretationMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/InterpretationMongoDBAdaptor.java index 408ede70d6e..66fcef9e345 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/InterpretationMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/InterpretationMongoDBAdaptor.java @@ -325,18 +325,18 @@ public OpenCGAResult updateProjectRelease(long studyId, int release) } @Override - public OpenCGAResult count(Query query) throws CatalogDBException { + public OpenCGAResult count(Query query) throws CatalogDBException { return count(null, query); } - public OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException { + public OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException { Bson bson = parseQuery(query); logger.debug("Interpretation count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); return new OpenCGAResult<>(interpretationCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(Query query, String user) + public OpenCGAResult count(Query query, String user) throws CatalogDBException { return count(query); } @@ -415,7 +415,7 @@ private UpdateDocument parseAndValidateUpdateParams(ClientSession clientSession, tmpQuery = new Query() .append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) .append(QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult count = count(clientSession, tmpQuery); + OpenCGAResult count = count(clientSession, tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot set id for interpretation. A interpretation with { id: '" + parameters.get(QueryParams.ID.key()) + "'} already exists."); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/JobMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/JobMongoDBAdaptor.java index c0a59316a23..035ea5e3ea5 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/JobMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/JobMongoDBAdaptor.java @@ -190,11 +190,11 @@ public OpenCGAResult unmarkPermissionRule(long studyId, String permissionRuleId) } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bsonDocument = parseQuery(query, QueryOptions.empty()); return new OpenCGAResult<>(jobCollection.count(clientSession, bsonDocument)); @@ -202,10 +202,10 @@ OpenCGAResult count(ClientSession clientSession, Query query) @Override - public OpenCGAResult count(Query query, String user) + public OpenCGAResult count(Query query, String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, QueryOptions.empty(), user); - logger.debug("Job count: query : {}, dbTime: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); + logger.debug("Job count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); return new OpenCGAResult<>(jobCollection.count(bson)); } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/PanelMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/PanelMongoDBAdaptor.java index a0b4d33435a..ba1f5e8bc0d 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/PanelMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/PanelMongoDBAdaptor.java @@ -39,6 +39,7 @@ import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; +import org.opencb.opencga.core.models.clinical.ClinicalAnalysis; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.common.InternalStatus; import org.opencb.opencga.core.models.panel.Panel; @@ -249,23 +250,23 @@ public long getStudyId(long panelUid) throws CatalogDBException { } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query); return new OpenCGAResult<>(panelCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(final Query query, final String user) + public OpenCGAResult count(final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query, user); } - OpenCGAResult count(ClientSession clientSession, final Query query, final String user) + OpenCGAResult count(ClientSession clientSession, final Query query, final String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query, user); logger.debug("Panel count: query : {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); @@ -426,7 +427,7 @@ private Document parseAndValidateUpdateParams(ClientSession clientSession, Objec tmpQuery = new Query() .append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) .append(QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult count = count(clientSession, tmpQuery); + OpenCGAResult count = count(clientSession, tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot update the " + QueryParams.ID.key() + ". Panel " + parameters.get(QueryParams.ID.key()) + " already exists."); @@ -505,7 +506,7 @@ private OpenCGAResult privateDelete(ClientSession clientSession, Documen Query queryCheck = new Query() .append(ClinicalAnalysisDBAdaptor.QueryParams.PANELS_UID.key(), panelUid) .append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), studyUid); - OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, queryCheck); + OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, queryCheck); if (count.getNumMatches() > 0) { throw new CatalogDBException("Could not delete panel. Panel is in use in " + count.getNumMatches() + " cases"); } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ProjectMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ProjectMongoDBAdaptor.java index acfaabf7664..ba5dfd6d8cc 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ProjectMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/ProjectMongoDBAdaptor.java @@ -81,7 +81,7 @@ public ProjectMongoDBAdaptor(MongoDBCollection userCollection, MongoDBCollection @Override public boolean exists(long projectId) { - DataResult count = userCollection.count(new Document(UserDBAdaptor.QueryParams.PROJECTS_UID.key(), projectId)); + DataResult count = userCollection.count(new Document(UserDBAdaptor.QueryParams.PROJECTS_UID.key(), projectId)); return count.getNumMatches() != 0; } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/SampleMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/SampleMongoDBAdaptor.java index 282006cb2ad..f319cf9f2ab 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/SampleMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/SampleMongoDBAdaptor.java @@ -49,6 +49,7 @@ import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; +import org.opencb.opencga.core.models.clinical.ClinicalAnalysis; import org.opencb.opencga.core.models.common.*; import org.opencb.opencga.core.models.individual.Individual; import org.opencb.opencga.core.models.sample.Sample; @@ -63,7 +64,7 @@ import java.util.function.Consumer; import java.util.function.UnaryOperator; -import static org.opencb.opencga.catalog.db.api.ClinicalAnalysisDBAdaptor.QueryParams.*; +import static org.opencb.opencga.catalog.db.api.ClinicalAnalysisDBAdaptor.QueryParams.MODIFICATION_DATE; import static org.opencb.opencga.catalog.db.mongodb.AuthorizationMongoDBUtils.filterAnnotationSets; import static org.opencb.opencga.catalog.db.mongodb.AuthorizationMongoDBUtils.getQueryForAuthorisedEntries; import static org.opencb.opencga.catalog.db.mongodb.MongoDBUtils.*; @@ -508,7 +509,7 @@ private void checkInUseInClinicalAnalysis(ClientSession clientSession, Document Query query = new Query() .append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), studyUid) .append(ClinicalAnalysisDBAdaptor.QueryParams.SAMPLE.key(), sampleUid); - OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, query); + OpenCGAResult count = dbAdaptorFactory.getClinicalAnalysisDBAdaptor().count(clientSession, query); if (count.getNumMatches() > 0) { throw new CatalogDBException("Could not delete sample '" + sampleId + "'. Sample is in use in " + count.getNumMatches() + " cases"); @@ -603,7 +604,7 @@ UpdateDocument parseAndValidateUpdateParams(ClientSession clientSession, ObjectM tmpQuery = new Query() .append(QueryParams.ID.key(), parameters.get(QueryParams.ID.key())) .append(QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult count = count(clientSession, tmpQuery); + OpenCGAResult count = count(clientSession, tmpQuery); if (count.getNumMatches() > 0) { throw new CatalogDBException("Cannot update the " + QueryParams.ID.key() + ". Sample " + parameters.get(QueryParams.ID.key()) + " already exists."); @@ -789,18 +790,18 @@ public OpenCGAResult setRgaIndexes(long studyUid, List sampleUids, } @Override - public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { + public OpenCGAResult count(Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Bson bson = parseQuery(query); return new OpenCGAResult<>(sampleCollection.count(clientSession, bson)); } @Override - public OpenCGAResult count(Query query, String user) + public OpenCGAResult count(Query query, String user) throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException { Query finalQuery = new Query(query); @@ -831,7 +832,7 @@ public OpenCGAResult count(Query query, String user) DataResult aggregate = sampleCollection.aggregate(Arrays.asList(match, lookup, individualMatch, count), QueryOptions.empty()); long numResults = aggregate.getNumResults() == 0 ? 0 : ((int) aggregate.first().get("count")); - return new OpenCGAResult<>(aggregate.getTime(), Collections.emptyList(), 1, Collections.singletonList(numResults), 1); + return new OpenCGAResult(aggregate.getTime(), Collections.emptyList(), 0, Collections.emptyList(), numResults); } else { logger.debug("Sample count query: {}", bson.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); return new OpenCGAResult<>(sampleCollection.count(bson)); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java index 9ba9d3c91a4..57e91a0baea 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/UserMongoDBAdaptor.java @@ -284,11 +284,11 @@ public OpenCGAResult deleteFilter(String userId, String name) throws CatalogDBEx } @Override - public OpenCGAResult count(Query query) throws CatalogDBException { + public OpenCGAResult count(Query query) throws CatalogDBException { return count(null, query); } - OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException { + OpenCGAResult count(ClientSession clientSession, Query query) throws CatalogDBException { Bson bsonDocument = parseQuery(query); logger.debug("User count: {}", bsonDocument.toBsonDocument(Document.class, MongoClient.getDefaultCodecRegistry())); return new OpenCGAResult<>(userCollection.count(clientSession, bsonDocument)); diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AbstractManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AbstractManager.java index 5f12e37db83..e22b854553c 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AbstractManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AbstractManager.java @@ -17,10 +17,13 @@ package org.opencb.opencga.catalog.managers; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.StopWatch; import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; import org.opencb.commons.datastore.core.QueryParam; +import org.opencb.commons.datastore.core.result.Error; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; import org.opencb.opencga.catalog.db.api.*; @@ -29,16 +32,25 @@ import org.opencb.opencga.catalog.exceptions.CatalogException; import org.opencb.opencga.catalog.exceptions.CatalogParameterException; import org.opencb.opencga.catalog.models.InternalGetDataResult; +import org.opencb.opencga.catalog.utils.UuidUtils; import org.opencb.opencga.core.api.ParamConstants; +import org.opencb.opencga.core.common.GitRepositoryState; +import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.AuthenticationOrigin; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.IPrivateStudyUid; +import org.opencb.opencga.core.models.audit.AuditRecord; +import org.opencb.opencga.core.models.common.Enums; +import org.opencb.opencga.core.models.common.ReferenceParam; import org.opencb.opencga.core.models.study.Group; +import org.opencb.opencga.core.models.study.Study; import org.opencb.opencga.core.response.OpenCGAResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.Function; /** @@ -48,7 +60,7 @@ public abstract class AbstractManager { protected final Logger logger; protected final AuthorizationManager authorizationManager; - protected final AuditManager auditManager; + private final AuditManager auditManager; protected final CatalogManager catalogManager; protected Configuration configuration; @@ -100,6 +112,121 @@ public abstract class AbstractManager { logger = LoggerFactory.getLogger(this.getClass()); } + public interface ExecuteOperation { + T execute(Study study, String userId, ReferenceParam rp, QueryOptions queryOptions) throws CatalogException; + } + + public interface ExecuteBatchOperation { + T execute(Study study, String userId, QueryOptions queryOptions, String auditOperationUuid) throws CatalogException, IOException; + } + + protected T run(ObjectMap params, Enums.Action action, Enums.Resource resource, String studyStr, String token, + S options, ExecuteOperation body) throws CatalogException { + return run(params, action, resource, studyStr, token, options, Collections.emptyList(), body); + } + + protected T run(ObjectMap params, Enums.Action action, Enums.Resource resource, String studyStr, String token, + S options, List studyIncludeList, ExecuteOperation body) throws CatalogException { + String userId = null; + if (StringUtils.isNotEmpty(token)) { + userId = catalogManager.getUserManager().getUserId(token); + } + Study study = null; + if (StringUtils.isNotEmpty(studyStr)) { + QueryOptions studyOptions = StudyManager.INCLUDE_BASE; + if (CollectionUtils.isNotEmpty(studyIncludeList)) { + studyOptions = keepFieldsInQueryOptions(studyOptions, studyIncludeList); + } + study = catalogManager.getStudyManager().resolveId(studyStr, userId, studyOptions); + } + String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); + return run(params, action, resource, operationUuid, study, userId, options, body); + } + + protected T run(ObjectMap params, Enums.Action action, Enums.Resource resource, String operationUuid, + Study study, String userId, S options, ExecuteOperation body) throws CatalogException { + StopWatch totalStopWatch = StopWatch.createStarted(); + Exception exception = null; + ReferenceParam referenceParam = new ReferenceParam(); + try { + QueryOptions queryOptions = options != null ? new QueryOptions(options) : new QueryOptions(); + return body.execute(study, userId, referenceParam, queryOptions); + } catch (Exception e) { + exception = e; + throw e; + } finally { + try { + String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); + AuditRecord auditRecord = new AuditRecord(operationId, operationUuid, userId, GitRepositoryState.get().getBuildVersion(), + action, resource, referenceParam.getId(), referenceParam.getUuid(), study.getId(), study.getUuid(), params, + new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), TimeUtils.getDate(), + new ObjectMap("totalTimeMillis", totalStopWatch.getTime(TimeUnit.MILLISECONDS))); + if (exception != null) { + auditRecord.setStatus(new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(1, exception.getMessage(), + ""))); + auditRecord.getAttributes() + .append("errorType", exception.getClass()) + .append("errorMessage", exception.getMessage()); + } + auditManager.audit(auditRecord); + } catch (Exception e2) { + if (exception != null) { + exception.addSuppressed(e2); + } else { + throw e2; + } + } + } + } + + protected T runBatch(ObjectMap params, Enums.Action action, Enums.Resource resource, String studyStr, String token, + QueryOptions options, ExecuteBatchOperation body) throws CatalogException { + StopWatch totalStopWatch = StopWatch.createStarted(); + String userId = catalogManager.getUserManager().getUserId(token); + Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, StudyManager.INCLUDE_BASE); + String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); + auditManager.initAuditBatch(operationUuid); + Exception exception = null; + try { + QueryOptions queryOptions = options != null ? new QueryOptions(options) : new QueryOptions(); + return body.execute(study, userId, queryOptions, operationUuid); + } catch (IOException e) { + exception = new CatalogException(e); + ObjectMap auditAttributes = new ObjectMap() + .append("totalTimeMillis", totalStopWatch.getTime(TimeUnit.MILLISECONDS)) + .append("errorType", e.getClass()) + .append("errorMessage", e.getMessage()); + AuditRecord.Status status = new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage())); + AuditRecord auditRecord = new AuditRecord(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT), operationUuid, userId, + GitRepositoryState.get().getBuildVersion(), action, resource, "", "", study.getId(), study.getUuid(), params, + status, TimeUtils.getDate(), auditAttributes); + auditManager.audit(auditRecord); + throw (CatalogException) exception; + } catch (Exception e) { + exception = e; + ObjectMap auditAttributes = new ObjectMap() + .append("totalTimeMillis", totalStopWatch.getTime(TimeUnit.MILLISECONDS)) + .append("errorType", e.getClass()) + .append("errorMessage", e.getMessage()); + AuditRecord.Status status = new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage())); + AuditRecord auditRecord = new AuditRecord(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT), operationUuid, userId, + GitRepositoryState.get().getBuildVersion(), action, resource, "", "", study.getId(), study.getUuid(), params, + status, TimeUtils.getDate(), auditAttributes); + auditManager.audit(auditRecord); + throw e; + } finally { + try { + auditManager.finishAuditBatch(operationUuid); + } catch (Exception e2) { + if (exception != null) { + exception.addSuppressed(e2); + } else { + throw e2; + } + } + } + } + protected void fixQueryObject(Query query) { changeQueryId(query, ParamConstants.INTERNAL_STATUS_PARAM, "internal.status"); } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AdminManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AdminManager.java index 3bffa773511..8d14deb28cd 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AdminManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/AdminManager.java @@ -3,13 +3,11 @@ import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; import org.opencb.opencga.catalog.db.api.UserDBAdaptor; import org.opencb.opencga.catalog.exceptions.CatalogException; import org.opencb.opencga.catalog.io.CatalogIOManager; -import org.opencb.opencga.catalog.utils.ParamUtils; import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.common.Enums; @@ -30,46 +28,23 @@ public class AdminManager extends AbstractManager { this.catalogIOManager = catalogIOManager; } - public OpenCGAResult userSearch(Query query, QueryOptions options, String token) - throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - + public OpenCGAResult userSearch(Query query, QueryOptions options, String token) throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("query", query) .append("options", options) .append("token", token); - String userId = catalogManager.getUserManager().getUserId(token); - try { + + return run(auditParams, Enums.Action.SEARCH, Enums.Resource.USER, "", token, options, (study, userId, rp, queryOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); authorizationManager.checkIsInstallationAdministrator(userId); - // Fix query object - if (query.containsKey(ParamConstants.USER)) { - query.put(UserDBAdaptor.QueryParams.ID.key(), query.get(ParamConstants.USER)); - query.remove(ParamConstants.USER); - } - if (query.containsKey(ParamConstants.USER_ACCOUNT_TYPE)) { - query.put(UserDBAdaptor.QueryParams.ACCOUNT_TYPE.key(), query.get(ParamConstants.USER_ACCOUNT_TYPE)); - query.remove(ParamConstants.USER_ACCOUNT_TYPE); - } - if (query.containsKey(ParamConstants.USER_AUTHENTICATION_ORIGIN)) { - query.put(UserDBAdaptor.QueryParams.ACCOUNT_AUTHENTICATION_ID.key(), query.get(ParamConstants.USER_AUTHENTICATION_ORIGIN)); - query.remove(ParamConstants.USER_AUTHENTICATION_ORIGIN); - } - if (query.containsKey(ParamConstants.USER_CREATION_DATE)) { - query.put(UserDBAdaptor.QueryParams.ACCOUNT_CREATION_DATE.key(), query.get(ParamConstants.USER_CREATION_DATE)); - query.remove(ParamConstants.USER_CREATION_DATE); - } + changeQueryId(myQuery, ParamConstants.USER, UserDBAdaptor.QueryParams.ID.key()); + changeQueryId(myQuery, ParamConstants.USER_ACCOUNT_TYPE, UserDBAdaptor.QueryParams.ACCOUNT_TYPE.key()); + changeQueryId(myQuery, ParamConstants.USER_AUTHENTICATION_ORIGIN, UserDBAdaptor.QueryParams.ACCOUNT_AUTHENTICATION_ID.key()); + changeQueryId(myQuery, ParamConstants.USER_CREATION_DATE, UserDBAdaptor.QueryParams.ACCOUNT_CREATION_DATE.key()); - OpenCGAResult userDataResult = userDBAdaptor.get(query, options); - auditManager.auditSearch(userId, Enums.Resource.USER, "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return userDataResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.USER, "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return userDBAdaptor.get(myQuery, queryOptions); + }); } } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ClinicalAnalysisManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ClinicalAnalysisManager.java index 949028dde58..180ba809352 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ClinicalAnalysisManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ClinicalAnalysisManager.java @@ -19,13 +19,15 @@ import com.fasterxml.jackson.core.JsonProcessingException; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.opencb.biodata.models.clinical.*; +import org.opencb.biodata.models.clinical.ClinicalAnalyst; +import org.opencb.biodata.models.clinical.ClinicalAudit; +import org.opencb.biodata.models.clinical.ClinicalComment; +import org.opencb.biodata.models.clinical.Disorder; import org.opencb.biodata.models.common.Status; import org.opencb.commons.datastore.core.Event; import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -42,7 +44,6 @@ import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; import org.opencb.opencga.core.models.AclParams; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.clinical.*; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.common.FlagAnnotation; @@ -71,6 +72,8 @@ import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; import static org.opencb.opencga.core.common.JacksonUtils.getUpdateObjectMapper; +import static org.opencb.opencga.core.models.common.Enums.Resource.CLINICAL_ANALYSIS; +import static org.opencb.opencga.core.models.common.Enums.Resource.STUDY; /** * Created by pfurio on 05/06/17. @@ -100,19 +103,15 @@ public class ClinicalAnalysisManager extends ResourceManager { ClinicalAnalysisDBAdaptor.QueryParams.SECONDARY_INTERPRETATIONS.key(), ClinicalAnalysisDBAdaptor.QueryParams.FLAGS.key(), ClinicalAnalysisDBAdaptor.QueryParams.TYPE.key())); protected static Logger logger = LoggerFactory.getLogger(ClinicalAnalysisManager.class); - private UserManager userManager; - private StudyManager studyManager; ClinicalAnalysisManager(AuthorizationManager authorizationManager, AuditManager auditManager, CatalogManager catalogManager, DBAdaptorFactory catalogDBAdaptorFactory, Configuration configuration) { super(authorizationManager, auditManager, catalogManager, catalogDBAdaptorFactory, configuration); - this.userManager = catalogManager.getUserManager(); - this.studyManager = catalogManager.getStudyManager(); } @Override - Enums.Resource getEntity() { - return Enums.Resource.CLINICAL_ANALYSIS; + Enums.Resource getResource() { + return CLINICAL_ANALYSIS; } // @Override @@ -198,18 +197,19 @@ InternalGetDataResult internalGet(long studyUid, List } @Override - public DBIterator iterator(String studyStr, Query query, QueryOptions options, String sessionId) + public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) 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); - - fixQueryObject(study, query, userId, sessionId); - query.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - return clinicalDBAdaptor.iterator(study.getUid(), query, options, userId); + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("query", query) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.ITERATE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, rp, qOptions) -> { + Query myQuery = ParamUtils.defaultObject(query, Query::new); + fixQueryObject(study, myQuery, userId, token); + myQuery.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return clinicalDBAdaptor.iterator(study.getUid(), myQuery, qOptions, userId); + }); } @Override @@ -221,16 +221,14 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis public OpenCGAResult create(String studyStr, ClinicalAnalysis clinicalAnalysis, Boolean skipCreateDefaultInterpretation, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysis", clinicalAnalysis) .append("skipCreateDefaultInterpretation", skipCreateDefaultInterpretation) .append("options", options) .append("token", token); - try { + + return run(auditParams, Enums.Action.CREATE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, rp, queryOptions) -> { if (study.getInternal() == null || study.getInternal().getConfiguration() == null || study.getInternal().getConfiguration().getClinical() == null) { throw new CatalogException("Unexpected error: ClinicalConfiguration is null"); @@ -239,7 +237,6 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_CLINICAL_ANALYSIS); - options = ParamUtils.defaultObject(options, QueryOptions::new); ParamUtils.checkObj(clinicalAnalysis, "clinicalAnalysis"); ParamUtils.checkIdentifier(clinicalAnalysis.getId(), "id"); ParamUtils.checkObj(clinicalAnalysis.getType(), "type"); @@ -247,6 +244,7 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis List events = new LinkedList<>(); + clinicalAnalysis.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.CLINICAL)); clinicalAnalysis.setStatus(ParamUtils.defaultObject(clinicalAnalysis.getStatus(), Status::new)); clinicalAnalysis.setInternal(ClinicalAnalysisInternal.init()); clinicalAnalysis.setDisorder(ParamUtils.defaultObject(clinicalAnalysis.getDisorder(), @@ -259,6 +257,9 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis ClinicalAnalysisQualityControl::new)); clinicalAnalysis.setPanels(ParamUtils.defaultObject(clinicalAnalysis.getPanels(), Collections.emptyList())); + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); + if (clinicalAnalysis.getQualityControl().getComments() != null) { for (ClinicalComment comment : clinicalAnalysis.getQualityControl().getComments()) { comment.setDate(TimeUtils.getTime()); @@ -546,7 +547,6 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis List clinicalAuditList = new ArrayList<>(); - clinicalAnalysis.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.CLINICAL)); if (clinicalAnalysis.getInterpretation() == null && (skipCreateDefaultInterpretation == null || !skipCreateDefaultInterpretation)) { clinicalAnalysis.setInterpretation(ParamUtils.defaultObject(clinicalAnalysis.getInterpretation(), Interpretation::new)); @@ -561,13 +561,11 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.CREATE_CLINICAL_ANALYSIS, "Create ClinicalAnalysis '" + clinicalAnalysis.getId() + "'", TimeUtils.getTime())); - OpenCGAResult insert = clinicalDBAdaptor.insert(study.getUid(), clinicalAnalysis, clinicalAuditList, options); + OpenCGAResult insert = clinicalDBAdaptor.insert(study.getUid(), clinicalAnalysis, clinicalAuditList, + queryOptions); insert.addEvents(events); - auditManager.auditCreate(userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), clinicalAnalysis.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + if (queryOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch updated clinical analysis OpenCGAResult queryResult = clinicalDBAdaptor.get(study.getUid(), clinicalAnalysis.getId(), QueryOptions.empty()); @@ -575,11 +573,7 @@ public OpenCGAResult create(String studyStr, ClinicalAnalysis } return insert; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } private void validateStatusParameter(ClinicalAnalysis clinicalAnalysis, ClinicalAnalysisStudyConfiguration clinicalConfiguration) @@ -844,120 +838,6 @@ private void validateFiles(Study study, ClinicalAnalysis clinicalAnalysis, Strin // } } - private Family getFullValidatedFamily(Family family, Study study, String sessionId) throws CatalogException { - if (family == null) { - return null; - } - - if (StringUtils.isEmpty(family.getId())) { - throw new CatalogException("Missing family id"); - } - - // List of members relevant for the clinical analysis - List selectedMembers = family.getMembers(); - - OpenCGAResult familyDataResult = catalogManager.getFamilyManager().get(study.getFqn(), family.getId(), new QueryOptions(), - sessionId); - if (familyDataResult.getNumResults() == 0) { - throw new CatalogException("Family " + family.getId() + " not found"); - } - Family finalFamily = familyDataResult.first(); - - if (ListUtils.isNotEmpty(selectedMembers)) { - if (ListUtils.isEmpty(finalFamily.getMembers())) { - throw new CatalogException("Family " + family.getId() + " does not have any members associated"); - } - - Map memberMap = new HashMap<>(); - for (Individual member : finalFamily.getMembers()) { - memberMap.put(member.getId(), member); - } - - List finalMembers = new ArrayList<>(selectedMembers.size()); - for (Individual selectedMember : selectedMembers) { - Individual fullMember = memberMap.get(selectedMember.getId()); - if (fullMember == null) { - throw new CatalogException("Member " + selectedMember.getId() + " does not belong to family " + family.getId()); - } - fullMember.setSamples(selectedMember.getSamples()); - finalMembers.add(getFullValidatedMember(fullMember, study, sessionId)); - } - - finalFamily.setMembers(finalMembers); - } else { - if (ListUtils.isNotEmpty(finalFamily.getMembers())) { - Query query = new Query() - .append(IndividualDBAdaptor.QueryParams.UID.key(), finalFamily.getMembers().stream() - .map(Individual::getUid).collect(Collectors.toList())) - .append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult individuals = individualDBAdaptor.get(study.getUid(), query, QueryOptions.empty(), - catalogManager.getUserManager().getUserId(sessionId)); - finalFamily.setMembers(individuals.getResults()); - } - } - - return finalFamily; - } - - private Individual getFullValidatedMember(Individual member, Study study, String sessionId) throws CatalogException { - if (member == null) { - return null; - } - - if (StringUtils.isEmpty(member.getId())) { - throw new CatalogException("Missing member id"); - } - - Individual finalMember; - - // List of samples relevant for the clinical analysis - List samples = member.getSamples(); - - if (member.getUid() <= 0) { - OpenCGAResult individualDataResult = catalogManager.getIndividualManager().get(study.getFqn(), member.getId(), - new QueryOptions(), sessionId); - if (individualDataResult.getNumResults() == 0) { - throw new CatalogException("Member " + member.getId() + " not found"); - } - - finalMember = individualDataResult.first(); - } else { - finalMember = member; - if (ListUtils.isNotEmpty(samples) && StringUtils.isEmpty(samples.get(0).getUuid())) { - // We don't have the full sample information... - OpenCGAResult individualDataResult = catalogManager.getIndividualManager().get(study.getFqn(), - finalMember.getId(), new QueryOptions(QueryOptions.INCLUDE, IndividualDBAdaptor.QueryParams.SAMPLES.key()), - sessionId); - if (individualDataResult.getNumResults() == 0) { - throw new CatalogException("Member " + finalMember.getId() + " not found"); - } - - finalMember.setSamples(individualDataResult.first().getSamples()); - } - } - - if (ListUtils.isNotEmpty(finalMember.getSamples())) { - List finalSampleList = null; - if (ListUtils.isNotEmpty(samples)) { - - Map sampleMap = new HashMap<>(); - for (Sample sample : finalMember.getSamples()) { - sampleMap.put(sample.getId(), sample); - } - - finalSampleList = new ArrayList<>(samples.size()); - - // We keep only the original list of samples passed - for (Sample sample : samples) { - finalSampleList.add(sampleMap.get(sample.getId())); - } - } - finalMember.setSamples(finalSampleList); - } - - return finalMember; - } - public OpenCGAResult update(String studyStr, Query query, ClinicalAnalysisUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { return update(studyStr, query, updateParams, false, options, token); @@ -965,11 +845,6 @@ public OpenCGAResult update(String studyStr, Query query, Clin public OpenCGAResult update(String studyStr, Query query, ClinicalAnalysisUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -985,51 +860,39 @@ public OpenCGAResult update(String studyStr, Query query, Clin .append("options", options) .append("token", token); - DBIterator iterator; - try { + return runBatch(auditParams, Enums.Action.UPDATE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { fixQueryObject(study, query, userId, token); query.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = clinicalDBAdaptor.iterator(study.getUid(), query, new QueryOptions(), userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - ClinicalAnalysis clinicalAnalysis = iterator.next(); - try { - OpenCGAResult queryResult = update(study, clinicalAnalysis, updateParams, userId, options); - result.append(queryResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, clinicalAnalysis.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + try (DBIterator iterator = clinicalDBAdaptor.iterator(study.getUid(), query, new QueryOptions(), userId)) { + OpenCGAResult result = OpenCGAResult.empty(ClinicalAnalysis.class); + while (iterator.hasNext()) { + ClinicalAnalysis clinicalAnalysis = iterator.next(); + + try { + OpenCGAResult tmpResult = run(auditParams, Enums.Action.UPDATE, CLINICAL_ANALYSIS, operationUuid, + study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); + return update(study, clinicalAnalysis, updateParams, userId, qOptions); + }); + result.append(tmpResult); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, clinicalAnalysis.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update clinical analysis {}: {}", clinicalAnalysis.getId(), e.getMessage()); + } + } - logger.error("Could not update clinical analysis {}: {}", clinicalAnalysis.getId(), e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyStr, String clinicalId, ClinicalAnalysisUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -1044,37 +907,18 @@ public OpenCGAResult update(String studyStr, String clinicalId .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String clinicalUuid = ""; - try { + return run(auditParams, Enums.Action.UPDATE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, rp, queryOptions) -> { + rp.setId(clinicalId); OpenCGAResult internalResult = internalGet(study.getUid(), clinicalId, QueryOptions.empty(), userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Clinical analysis '" + clinicalId + "' not found"); } ClinicalAnalysis clinicalAnalysis = internalResult.first(); + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); - // We set the proper values for the audit - clinicalId = clinicalAnalysis.getId(); - clinicalUuid = clinicalAnalysis.getUuid(); - - OpenCGAResult updateResult = update(study, clinicalAnalysis, updateParams, userId, options); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, clinicalId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update clinical analysis {}: {}", clinicalId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalId, clinicalUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - return result; + return update(study, clinicalAnalysis, updateParams, userId, options); + }); } /** @@ -1096,11 +940,6 @@ public OpenCGAResult update(String studyStr, List clin public OpenCGAResult update(String studyStr, List clinicalIds, ClinicalAnalysisUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -1112,46 +951,43 @@ public OpenCGAResult update(String studyStr, List clin .append("study", studyStr) .append("clinicalIds", clinicalIds) .append("updateParams", updateMap) - .append("options", options) .append("ignoreException", ignoreException) + .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - - auditManager.initAuditBatch(operationId); - for (String id : clinicalIds) { - String clinicalAnalysisId = id; - String clinicalAnalysisUuid = ""; - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Clinical analysis '" + id + "' not found"); + return runBatch(auditParams, Enums.Action.UPDATE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(ClinicalAnalysis.class); + + for (String id : clinicalIds) { + try { + OpenCGAResult tmpResult = run(auditParams, Enums.Action.UPDATE, CLINICAL_ANALYSIS, operationUuid, + study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), + userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Clinical analysis '" + id + "' not found"); + } + ClinicalAnalysis clinicalAnalysis = internalResult.first(); + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); + + return update(study, clinicalAnalysis, updateParams, userId, qOptions); + }); + result.append(tmpResult); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.error("Could not update clinical analysis {}: {}", id, e.getMessage()); } - ClinicalAnalysis clinicalAnalysis = internalResult.first(); - - // We set the proper values for the audit - clinicalAnalysisId = clinicalAnalysis.getId(); - clinicalAnalysisUuid = clinicalAnalysis.getUuid(); + } - OpenCGAResult updateResult = update(study, clinicalAnalysis, updateParams, userId, options); - result.append(updateResult); + return endResult(result, ignoreException); + }); - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - logger.error("Could not update clinical analysis {}: {}", clinicalAnalysisId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysisId, clinicalAnalysisUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); } private OpenCGAResult update(Study study, ClinicalAnalysis clinicalAnalysis, @@ -1490,45 +1326,38 @@ private boolean sortMembersFromFamily(ClinicalAnalysis clinicalAnalysis) { } public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - - fixQueryObject(study, query, userId, token); - query.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - return clinicalDBAdaptor.get(study.getUid(), query, options, userId); + ObjectMap auditParams = new ObjectMap() + .append("study", studyId) + .append("query", query) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.SEARCH, CLINICAL_ANALYSIS, studyId, token, options, (study, userId, rp, queryOptions) -> { + Query myQuery = ParamUtils.defaultObject(query, Query::new); + fixQueryObject(study, myQuery, userId, token); + myQuery.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return clinicalDBAdaptor.get(study.getUid(), myQuery, queryOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId, token); - - query.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = clinicalDBAdaptor.distinct(study.getUid(), field, query, userId); - auditManager.auditDistinct(userId, Enums.Resource.CLINICAL_ANALYSIS, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.DISTINCT, CLINICAL_ANALYSIS, studyId, token, null, (study, userId, rp, qo) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + ClinicalAnalysisDBAdaptor.QueryParams param = ClinicalAnalysisDBAdaptor.QueryParams.getParam(field); + if (param == null) { + throw new CatalogException("Unknown '" + field + "' parameter."); + } - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.CLINICAL_ANALYSIS, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + fixQueryObject(study, myQuery, userId, token); + myQuery.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return clinicalDBAdaptor.distinct(study.getUid(), field, myQuery, userId); + }); } protected void fixQueryObject(Study study, Query query, String user, String token) throws CatalogException { @@ -1688,29 +1517,18 @@ protected void fixQueryObject(Study study, Query query, String user, String toke public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", new Query(query)) .append("token", token); - try { - fixQueryObject(study, query, userId, token); - query.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - OpenCGAResult queryResultAux = clinicalDBAdaptor.count(query, userId); - auditManager.auditCount(userId, Enums.Resource.CLINICAL_ANALYSIS, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.COUNT, CLINICAL_ANALYSIS, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, myQuery, userId, token); + myQuery.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.CLINICAL_ANALYSIS, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return clinicalDBAdaptor.count(myQuery, userId); + }); } @Override @@ -1721,15 +1539,6 @@ public OpenCGAResult delete(String studyStr, List clinicalAnalysisIds, Q public OpenCGAResult delete(String studyStr, List clinicalAnalysisIds, QueryOptions options, boolean ignoreException, String token) throws CatalogException { - if (CollectionUtils.isEmpty(clinicalAnalysisIds)) { - throw new CatalogException("Missing list of Clinical Analysis ids"); - } - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysisIds", clinicalAnalysisIds) @@ -1737,71 +1546,63 @@ public OpenCGAResult delete(String studyStr, List clinicalAnalysisIds, Q .append("ignoreException", ignoreException) .append("token", token); - options = ParamUtils.defaultObject(options, QueryOptions::new); + return runBatch(auditParams, Enums.Action.DELETE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + if (CollectionUtils.isEmpty(clinicalAnalysisIds)) { + throw new CatalogException("Missing list of Clinical Analysis ids"); + } - boolean checkPermissions; - try { // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : clinicalAnalysisIds) { - String clinicalId = id; - String clinicalUuid = ""; - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, - keepFieldsInQueryOptions(INCLUDE_CLINICAL_INTERPRETATION_IDS, Arrays.asList( - ClinicalAnalysisDBAdaptor.QueryParams.INTERPRETATION.key() + "." - + InterpretationDBAdaptor.QueryParams.PRIMARY_FINDINGS_ID.key(), - ClinicalAnalysisDBAdaptor.QueryParams.SECONDARY_INTERPRETATIONS.key() + "." - + InterpretationDBAdaptor.QueryParams.PRIMARY_FINDINGS_ID.key())), - userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Clinical Analysis '" + id + "' not found"); - } - ClinicalAnalysis clinicalAnalysis = internalResult.first(); - - // We set the proper values for the audit - clinicalId = clinicalAnalysis.getId(); - clinicalUuid = clinicalAnalysis.getUuid(); - - if (checkPermissions) { - authorizationManager.checkClinicalAnalysisPermission(study.getUid(), clinicalAnalysis.getUid(), userId, - ClinicalAnalysisPermissions.DELETE); - } + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(ClinicalAnalysis.class); + for (String id : clinicalAnalysisIds) { + try { + OpenCGAResult tmpResult = run(auditParams, Enums.Action.DELETE, CLINICAL_ANALYSIS, operationUuid, study, userId, + qOptions, (s, u, rp, qo) -> { + rp.setId(id); + + OpenCGAResult internalResult = internalGet(study.getUid(), id, + keepFieldsInQueryOptions(INCLUDE_CLINICAL_INTERPRETATION_IDS, Arrays.asList( + ClinicalAnalysisDBAdaptor.QueryParams.INTERPRETATION.key() + "." + + InterpretationDBAdaptor.QueryParams.PRIMARY_FINDINGS_ID.key(), + ClinicalAnalysisDBAdaptor.QueryParams.SECONDARY_INTERPRETATIONS.key() + "." + + InterpretationDBAdaptor.QueryParams.PRIMARY_FINDINGS_ID.key())), + userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Clinical Analysis '" + id + "' not found"); + } + ClinicalAnalysis clinicalAnalysis = internalResult.first(); + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); - // Check if the ClinicalAnalysis can be deleted - checkClinicalAnalysisCanBeDeleted(clinicalAnalysis, options); + if (checkPermissions) { + authorizationManager.checkClinicalAnalysisPermission(study.getUid(), clinicalAnalysis.getUid(), userId, + ClinicalAnalysisPermissions.DELETE); + } - ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.DELETE_CLINICAL_ANALYSIS, - "Delete Clinical Analysis '" + clinicalId + "'", TimeUtils.getTime()); + // Check if the ClinicalAnalysis can be deleted + checkClinicalAnalysisCanBeDeleted(clinicalAnalysis, qOptions); - result.append(clinicalDBAdaptor.delete(clinicalAnalysis, Collections.singletonList(clinicalAudit))); + ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.DELETE_CLINICAL_ANALYSIS, + "Delete Clinical Analysis '" + clinicalAnalysis.getId() + "'", TimeUtils.getTime()); - auditManager.auditDelete(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete Clinical Analysis " + clinicalId + ": " + e.getMessage(); + return clinicalDBAdaptor.delete(clinicalAnalysis, Collections.singletonList(clinicalAudit)); + }); + result.append(tmpResult); + } catch (CatalogException e) { + String errorMsg = "Cannot delete Clinical Analysis " + id + ": " + e.getMessage(); - Event event = new Event(Event.Type.ERROR, clinicalId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error(errorMsg); - auditManager.auditDelete(operationId, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalId, clinicalUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error(errorMsg); + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private void checkClinicalAnalysisCanBeDeleted(ClinicalAnalysis clinicalAnalysis, QueryOptions options) throws CatalogException { @@ -1831,16 +1632,6 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("query", new Query(query)) @@ -1848,62 +1639,54 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, .append("ignoreException", ignoreException) .append("token", token); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; - - // We try to get an iterator containing all the ClinicalAnalyses to be deleted - DBIterator iterator; - try { - fixQueryObject(study, finalQuery, userId, token); - finalQuery.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - iterator = clinicalDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_CLINICAL_INTERPRETATION_IDS, userId); + return runBatch(auditParams, Enums.Action.DELETE, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + OpenCGAResult result = OpenCGAResult.empty(ClinicalAnalysis.class); // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.CLINICAL_ANALYSIS, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - while (iterator.hasNext()) { - ClinicalAnalysis clinicalAnalysis = iterator.next(); - - try { - if (checkPermissions) { - authorizationManager.checkClinicalAnalysisPermission(study.getUid(), clinicalAnalysis.getUid(), userId, - ClinicalAnalysisPermissions.DELETE); - } - - // Check if the sample can be deleted - checkClinicalAnalysisCanBeDeleted(clinicalAnalysis, options); - - ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.DELETE_CLINICAL_ANALYSIS, - "Delete Clinical Analysis '" + clinicalAnalysis.getId() + "'", TimeUtils.getTime()); + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + fixQueryObject(study, myQuery, userId, token); + myQuery.append(ClinicalAnalysisDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + + try (DBIterator iterator = clinicalDBAdaptor.iterator(study.getUid(), myQuery, + INCLUDE_CLINICAL_INTERPRETATION_IDS, userId)) { + while (iterator.hasNext()) { + ClinicalAnalysis clinicalAnalysis = iterator.next(); + try { + OpenCGAResult tmpResult = run(auditParams, Enums.Action.DELETE, CLINICAL_ANALYSIS, operationUuid, study, userId, + qOptions, (s, u, rp, qo) -> { + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); + + if (checkPermissions) { + authorizationManager.checkClinicalAnalysisPermission(study.getUid(), clinicalAnalysis.getUid(), + userId, ClinicalAnalysisPermissions.DELETE); + } - result.append(clinicalDBAdaptor.delete(clinicalAnalysis, Collections.singletonList(clinicalAudit))); + // Check if the sample can be deleted + checkClinicalAnalysisCanBeDeleted(clinicalAnalysis, options); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete Clinical Analysis " + clinicalAnalysis.getId() + ": " + e.getMessage(); + ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.DELETE_CLINICAL_ANALYSIS, + "Delete Clinical Analysis '" + clinicalAnalysis.getId() + "'", TimeUtils.getTime()); + return clinicalDBAdaptor.delete(clinicalAnalysis, Collections.singletonList(clinicalAudit)); + }); + result.append(tmpResult); + } catch (CatalogException e) { + String errorMsg = "Cannot delete Clinical Analysis " + clinicalAnalysis.getId() + ": " + e.getMessage(); - Event event = new Event(Event.Type.ERROR, clinicalAnalysis.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + Event event = new Event(Event.Type.ERROR, clinicalAnalysis.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error(errorMsg); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.CLINICAL_ANALYSIS, clinicalAnalysis.getId(), - clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error(errorMsg); + } + } } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -1913,24 +1696,29 @@ public OpenCGAResult rank(String studyStr, Query query, String field, int numRes } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - if (fields == null || fields.size() == 0) { - throw new CatalogException("Empty fields parameter."); - } + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); - String userId = catalogManager.getUserManager().getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + return run(auditParams, Enums.Action.GROUP_BY, CLINICAL_ANALYSIS, studyStr, token, options, (study, userId, rp, queryOptions) -> { + if (fields == null || fields.size() == 0) { + throw new CatalogException("Empty fields parameter."); + } - fixQueryObject(study, query, userId, sessionId); + Query myQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, myQuery, userId, token); - // Add study id to the query - query.put(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + // Add study id to the query + myQuery.put(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = clinicalDBAdaptor.groupBy(query, fields, options, userId); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + OpenCGAResult queryResult = clinicalDBAdaptor.groupBy(myQuery, fields, options, userId); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } // ************************** ACLs ******************************** // @@ -1941,10 +1729,6 @@ public OpenCGAResult> getAcls( public OpenCGAResult> getAcls(String studyId, List clinicalList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("clinicalList", clinicalList) @@ -1952,10 +1736,10 @@ public OpenCGAResult> getAcls(String s .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> clinicalAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, CLINICAL_ANALYSIS, studyId, token, null, (study, user, qOptions, + operationUuid) -> { + OpenCGAResult> clinicalAcls; + Map missingMap = null; InternalGetDataResult queryResult = internalGet(study.getUid(), clinicalList, INCLUDE_CLINICAL_IDS, user, ignoreException); @@ -1966,62 +1750,45 @@ public OpenCGAResult> getAcls(String s List clinicalUids = queryResult.getResults().stream().map(ClinicalAnalysis::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - clinicalAcls = authorizationManager.getAcl(user, study.getUid(), clinicalUids, members, Enums.Resource.CLINICAL_ANALYSIS, + clinicalAcls = authorizationManager.getAcl(user, study.getUid(), clinicalUids, members, CLINICAL_ANALYSIS, ClinicalAnalysisPermissions.class); } else { - clinicalAcls = authorizationManager.getAcl(user, study.getUid(), clinicalUids, Enums.Resource.CLINICAL_ANALYSIS, + clinicalAcls = authorizationManager.getAcl(user, study.getUid(), clinicalUids, CLINICAL_ANALYSIS, ClinicalAnalysisPermissions.class); } - // Include non-existing samples to the result list + // Include non-existing cases to the result list List> resultList = new ArrayList<>(clinicalList.size()); List eventList = new ArrayList<>(missingMap.size()); int counter = 0; for (String clinicalId : clinicalList) { if (!missingMap.containsKey(clinicalId)) { ClinicalAnalysis clinical = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, CLINICAL_ANALYSIS, operationUuid, study, user, qOptions, (s, u, rp, qo) -> { + rp.setId(clinical.getId()); + rp.setUuid(clinical.getUuid()); + return null; + }); resultList.add(clinicalAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.CLINICAL_ANALYSIS, clinical.getId(), - clinical.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); counter++; } else { + if (!ignoreException) { + throw new CatalogException(missingMap.get(clinicalId).getErrorMsg()); + } resultList.add(new AclEntryList<>()); eventList.add(new Event(Event.Type.ERROR, clinicalId, missingMap.get(clinicalId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.CLINICAL_ANALYSIS, clinicalId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(clinicalId).getErrorMsg())), new ObjectMap()); } } clinicalAcls.setResults(resultList); clinicalAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String caseId : clinicalList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.CLINICAL_ANALYSIS, caseId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String caseId : clinicalList) { - Event event = new Event(Event.Type.ERROR, caseId, e.getMessage()); - clinicalAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return clinicalAcls; + return clinicalAcls; + }); } - public OpenCGAResult> updateAcl( - String studyStr, List clinicalList, String memberIds, AclParams clinicalAclParams, ParamUtils.AclAction action, - boolean propagate, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, user); - + public OpenCGAResult> updateAcl(String studyStr, List clinicalList, String memberIds, + AclParams clinicalAclParams, ParamUtils.AclAction action, + boolean propagate, String token) throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyStr) .append("clinicalList", clinicalList) @@ -2030,10 +1797,9 @@ public OpenCGAResult> updateAcl( .append("action", action) .append("token", token); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.CLINICAL); - - try { - auditManager.initAuditBatch(operationUuid); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, CLINICAL_ANALYSIS, studyStr, token, null, (study, userId, qOptions, + operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); if (clinicalList == null || clinicalList.isEmpty()) { throw new CatalogException("Update ACL: Missing 'clinicalAnalysis' parameter"); @@ -2049,9 +1815,7 @@ public OpenCGAResult> updateAcl( checkPermissions(permissions, ClinicalAnalysisPermissions::valueOf); } - OpenCGAResult queryResult = internalGet(study.getUid(), clinicalList, INCLUDE_CATALOG_DATA, user, false); - - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), user); + OpenCGAResult queryResult = internalGet(study.getUid(), clinicalList, INCLUDE_CATALOG_DATA, userId, false); // Validate that the members are actually valid members List members; @@ -2065,7 +1829,7 @@ public OpenCGAResult> updateAcl( List clinicalUidList = queryResult.getResults().stream().map(ClinicalAnalysis::getUid).collect(Collectors.toList()); List aclParamsList = new LinkedList<>(); - AuthorizationManager.CatalogAclParams.addToList(clinicalUidList, permissions, Enums.Resource.CLINICAL_ANALYSIS, aclParamsList); + AuthorizationManager.CatalogAclParams.addToList(clinicalUidList, permissions, CLINICAL_ANALYSIS, aclParamsList); if (propagate) { // Obtain the whole list of implicity permissions @@ -2149,41 +1913,31 @@ public OpenCGAResult> updateAcl( throw new CatalogException("Unexpected error occurred. No valid action found."); } - queryResults = authorizationManager.getAcls(study.getUid(), clinicalUidList, members, Enums.Resource.CLINICAL_ANALYSIS, + queryResults = authorizationManager.getAcls(study.getUid(), clinicalUidList, members, CLINICAL_ANALYSIS, ClinicalAnalysisPermissions.class); - for (ClinicalAnalysis clinicalAnalysis : queryResult.getResults()) { - auditManager.audit(operationUuid, user, Enums.Action.UPDATE_ACLS, Enums.Resource.CLINICAL_ANALYSIS, - clinicalAnalysis.getId(), clinicalAnalysis.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + run(auditParams, Enums.Action.UPDATE_ACLS, CLINICAL_ANALYSIS, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(clinicalAnalysis.getId()); + rp.setUuid(clinicalAnalysis.getUuid()); + return null; + }); } return queryResults; - } catch (CatalogException e) { - if (clinicalList != null) { - for (String clinicalId : clinicalList) { - auditManager.audit(operationUuid, user, Enums.Action.UPDATE_ACLS, Enums.Resource.CLINICAL_ANALYSIS, clinicalId, "", - study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - } - throw e; - } finally { - auditManager.finishAuditBatch(operationUuid); - } + }); } public OpenCGAResult configureStudy(String studyStr, ClinicalAnalysisStudyConfiguration clinicalConfiguration, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyStr) .append("clinicalConfiguration", clinicalConfiguration) .append("token", token); - try { + return run(auditParams, Enums.Action.UPDATE, STUDY, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); ParamUtils.checkObj(clinicalConfiguration, "ClinicalConfiguration"); ParamUtils.checkObj(clinicalConfiguration.getFlags(), "flags"); @@ -2203,16 +1957,8 @@ public OpenCGAResult configureStudy(String studyStr, ClinicalAnalysisStudyConfig throw new CatalogException("Jackson casting error: " + e.getMessage(), e); } - OpenCGAResult updateResult = studyDBAdaptor.update(study.getUid(), update, QueryOptions.empty()); - auditManager.auditUpdate(userId, Enums.Resource.STUDY, study.getId(), study.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return updateResult; - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.STUDY, study.getId(), study.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return studyDBAdaptor.update(study.getUid(), update, QueryOptions.empty()); + }); } private void validateClinicalStatus(Map> status, String field) diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/CohortManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/CohortManager.java index 0f8892915f1..52fbd39e8b0 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/CohortManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/CohortManager.java @@ -23,11 +23,13 @@ import org.opencb.biodata.models.common.Status; import org.opencb.biodata.models.variant.StudyEntry; import org.opencb.commons.datastore.core.*; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; -import org.opencb.opencga.catalog.db.api.*; +import org.opencb.opencga.catalog.db.api.CohortDBAdaptor; +import org.opencb.opencga.catalog.db.api.DBIterator; +import org.opencb.opencga.catalog.db.api.IndividualDBAdaptor; +import org.opencb.opencga.catalog.db.api.SampleDBAdaptor; import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException; import org.opencb.opencga.catalog.exceptions.CatalogException; import org.opencb.opencga.catalog.exceptions.CatalogParameterException; @@ -41,7 +43,6 @@ import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; import org.opencb.opencga.core.models.AclParams; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.cohort.*; import org.opencb.opencga.core.models.common.AnnotationSet; import org.opencb.opencga.core.models.common.Enums; @@ -56,12 +57,13 @@ import org.slf4j.LoggerFactory; import javax.annotation.Nullable; -import java.io.IOException; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; +import static org.opencb.opencga.core.models.common.Enums.Action.*; +import static org.opencb.opencga.core.models.common.Enums.Resource.COHORT; /** * Created by pfurio on 06/07/16. @@ -89,8 +91,8 @@ public class CohortManager extends AnnotationSetManager { } @Override - Enums.Resource getEntity() { - return Enums.Resource.COHORT; + Enums.Resource getResource() { + return COHORT; } @Override @@ -150,15 +152,6 @@ private OpenCGAResult getCohort(long studyUid, String cohortUuid, QueryO public OpenCGAResult create(String studyStr, CohortCreateParams cohortParams, String variableSetId, String variableId, QueryOptions options, String token) throws CatalogException { - ParamUtils.checkObj(cohortParams, "CohortCreateParams"); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_COHORTS); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("cohortParams", cohortParams) @@ -166,13 +159,16 @@ public OpenCGAResult create(String studyStr, CohortCreateParams cohortPa .append("variableId", variableId) .append("options", options) .append("token", token); + return runBatch(auditParams, Enums.Action.CREATE, COHORT, studyStr, token, options, (study, user, myOptions, operationUuid) -> { + ParamUtils.checkObj(cohortParams, "CohortCreateParams"); + authorizationManager.checkStudyPermission(study.getUid(), user, StudyPermissions.Permissions.WRITE_COHORTS); - List cohorts = new LinkedList<>(); - try { + // Generate list of cohorts to create + List cohorts = new LinkedList<>(); if (StringUtils.isNotEmpty(variableId) && CollectionUtils.isNotEmpty(cohortParams.getSamples())) { - throw new CatalogParameterException("Can only create a cohort given list of sampleIds or a categorical variable name"); + throw new CatalogParameterException("Can only create a cohort given list of sampleIds or a categorical variable " + + "name"); } - ParamUtils.checkIdentifier(cohortParams.getId(), "id"); if (CollectionUtils.isNotEmpty(cohortParams.getSamples())) { @@ -187,10 +183,10 @@ public OpenCGAResult create(String studyStr, CohortCreateParams cohortPa } } List sampleList = catalogManager.getSampleManager().internalGet(study.getUid(), sampleIds, - SampleManager.INCLUDE_SAMPLE_IDS, userId, false).getResults(); + SampleManager.INCLUDE_SAMPLE_IDS, user, false).getResults(); cohorts.add(new Cohort(cohortParams.getId(), cohortParams.getName(), cohortParams.getType(), - cohortParams.getCreationDate(), cohortParams.getModificationDate(), cohortParams.getDescription(), sampleList, 0, - cohortParams.getAnnotationSets(), 1, + cohortParams.getCreationDate(), cohortParams.getModificationDate(), cohortParams.getDescription(), + sampleList, 0, cohortParams.getAnnotationSets(), 1, cohortParams.getStatus() != null ? cohortParams.getStatus().toStatus() : new Status(), null, cohortParams.getAttributes())); @@ -217,11 +213,12 @@ public OpenCGAResult create(String studyStr, CohortCreateParams cohortPa } } if (variable == null) { - throw new CatalogException("Variable '" + variableId + "' does not exist in VariableSet " + variableSet.getId()); + throw new CatalogException("Variable '" + variableId + "' does not exist in VariableSet " + + variableSet.getId()); } if (variable.getType() != Variable.VariableType.CATEGORICAL) { - throw new CatalogException("Variable '" + variableId + "' is not a categorical variable. Please, choose a categorical " - + "variable"); + throw new CatalogException("Variable '" + variableId + "' is not a categorical variable. Please, choose a " + + "categorical variable"); } for (String value : variable.getAllowedValues()) { OpenCGAResult sampleResults = catalogManager.getSampleManager().search(study.getFqn(), @@ -240,100 +237,81 @@ public OpenCGAResult create(String studyStr, CohortCreateParams cohortPa cohortParams.getCreationDate(), cohortParams.getModificationDate(), cohortParams.getDescription(), Collections.emptyList(), cohortParams.getAnnotationSets(), -1, null)); } - } catch (CatalogException e) { - auditManager.audit(operationId, userId, Enums.Action.CREATE, Enums.Resource.COHORT, "", "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - auditManager.initAuditBatch(operationId); - OpenCGAResult insertResult = OpenCGAResult.empty(Cohort.class); - StopWatch stopWatch = new StopWatch(); - stopWatch.start(); - for (Cohort cohort : cohorts) { - try { - validateNewCohort(study, cohort); - OpenCGAResult tmpResult = cohortDBAdaptor.insert(study.getUid(), cohort, study.getVariableSets(), options); - insertResult.append(tmpResult); - auditManager.audit(operationId, userId, Enums.Action.CREATE, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, cohort.getId(), e.getMessage()); - insertResult.getEvents().add(event); - - logger.error("Could not create cohort {}: {}", cohort.getId(), e.getMessage(), e); - auditManager.audit(operationId, userId, Enums.Action.CREATE, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + // Create cohorts + OpenCGAResult insertResult = OpenCGAResult.empty(Cohort.class); + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + for (Cohort cohort : cohorts) { + try { + run(auditParams, Enums.Action.CREATE, COHORT, operationUuid, study, user, myOptions, (s, u, rp, qOptions) -> { + validateNewCohort(study, cohort); + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); + OpenCGAResult tmpResult = cohortDBAdaptor.insert(study.getUid(), cohort, study.getVariableSets(), + myOptions); + insertResult.append(tmpResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, cohort.getId(), e.getMessage()); + insertResult.getEvents().add(event); + logger.warn("Could not create cohort {}", cohort.getId(), e); + } } - } - - auditManager.finishAuditBatch(operationId); - stopWatch.stop(); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { - // Fetch created cohort(s) - Query query = new Query(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()) - .append(CohortDBAdaptor.QueryParams.UID.key(), cohorts.stream() - .map(Cohort::getUid) - .filter(uid -> uid > 0) - .collect(Collectors.toList())); - OpenCGAResult result = cohortDBAdaptor.get(study.getUid(), query, options, userId); - insertResult.setResults(result.getResults()); - } - - return insertResult; + stopWatch.stop(); + if (myOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + // Fetch created cohort(s) + Query query = new Query(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()) + .append(CohortDBAdaptor.QueryParams.UID.key(), cohorts.stream() + .map(Cohort::getUid) + .filter(uid -> uid > 0) + .collect(Collectors.toList())); + OpenCGAResult result = cohortDBAdaptor.get(study.getUid(), query, myOptions, user); + insertResult.setResults(result.getResults()); + } + return insertResult; + }); } public OpenCGAResult generate(String studyStr, Query sampleQuery, Cohort cohort, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_COHORTS); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyStr) .append("query", new Query(sampleQuery)) .append("cohort", cohort) .append("options", options) .append("token", token); + return run(auditParams, Enums.Action.GENERATE, COHORT, studyStr, token, options, (study, userId, rp, qOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_COHORTS); - options = ParamUtils.defaultObject(options, QueryOptions::new); - try { // Fix sample query object and search for samples catalogManager.getSampleManager().fixQueryObject(study, sampleQuery, userId); - AnnotationUtils.fixQueryOptionAnnotation(options); + AnnotationUtils.fixQueryOptionAnnotation(qOptions); sampleQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = sampleDBAdaptor.get(study.getUid(), sampleQuery, options, userId); + OpenCGAResult result = sampleDBAdaptor.get(study.getUid(), sampleQuery, qOptions, userId); // Add samples to provided cohort object cohort.setSamples(result.getResults()); + OpenCGAResult openCGAResult = privateCreate(study, cohort, qOptions, userId); + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); - OpenCGAResult cohortResult = privateCreate(study, cohort, options, userId); - auditManager.audit(userId, Enums.Action.GENERATE, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return cohortResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.GENERATE, Enums.Resource.COHORT, cohort.getId(), "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return openCGAResult; + }); } @Override public OpenCGAResult create(String studyStr, Cohort cohort, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_COHORTS); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("cohort", cohort) .append("options", options) .append("token", token); - try { + return run(auditParams, Enums.Action.CREATE, COHORT, studyStr, token, options, (study, userId, rp, queryOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_COHORTS); + if (CollectionUtils.isNotEmpty(cohort.getSamples())) { // Look for the samples InternalGetDataResult sampleResult = catalogManager.getSampleManager().internalGet(study.getUid(), @@ -342,16 +320,11 @@ public OpenCGAResult create(String studyStr, Cohort cohort, QueryOptions cohort.setSamples(sampleResult.getResults()); } - OpenCGAResult cohortResult = privateCreate(study, cohort, options, userId); - auditManager.auditCreate(userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - + OpenCGAResult cohortResult = privateCreate(study, cohort, queryOptions, userId); + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); return cohortResult; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.COHORT, cohort.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } OpenCGAResult privateCreate(Study study, Cohort cohort, QueryOptions options, String userId) throws CatalogException { @@ -407,114 +380,65 @@ public Long getStudyId(long cohortId) throws CatalogException { return cohortDBAdaptor.getStudyId(cohortId); } - /** - * Fetch all the samples from a cohort. - * - * @param studyStr Study id in string format. Could be one of [id|user@aliasProject:aliasStudy|aliasProject:aliasStudy|aliasStudy]. - * @param cohortStr Cohort id or name. - * @param sessionId Token of the user logged in. - * @return a OpenCGAResult containing all the samples belonging to the cohort. - * @throws CatalogException if there is any kind of error (permissions or invalid ids). - */ - public OpenCGAResult getSamples(String studyStr, String cohortStr, String sessionId) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - OpenCGAResult cohortDataResult = internalGet(study.getUid(), cohortStr, - new QueryOptions(QueryOptions.INCLUDE, CohortDBAdaptor.QueryParams.SAMPLES.key()), userId); - - if (cohortDataResult == null || cohortDataResult.getNumResults() == 0) { - throw new CatalogException("No cohort " + cohortStr + " found in study " + studyStr); - } - if (cohortDataResult.first().getSamples().size() == 0) { - return OpenCGAResult.empty(); - } - - return new OpenCGAResult<>(cohortDataResult.getTime(), cohortDataResult.getEvents(), cohortDataResult.first().getSamples().size(), - cohortDataResult.first().getSamples(), cohortDataResult.first().getSamples().size()); - } - @Override - public DBIterator 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); - - // Fix query if it contains any annotation - Query finalQuery = new Query(query); - AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); - fixQueryObject(study, finalQuery, userId); - finalQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("studyId", studyStr) + .append("query", new Query(query)) + .append("options", options) + .append("token", token); + Query myQuery = query != null ? new Query(query) : new Query(); + return run(auditParams, Enums.Action.ITERATE, COHORT, studyStr, token, options, (study, userId, rp, qOptions) -> { + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, myQuery); + AnnotationUtils.fixQueryOptionAnnotation(qOptions); + fixQueryObject(study, myQuery, userId); + myQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return cohortDBAdaptor.iterator(study.getUid(), finalQuery, options, userId); + return cohortDBAdaptor.iterator(study.getUid(), myQuery, qOptions, userId); + }); } @Override public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", new Query(query)) .append("options", options) .append("token", token); - try { + return run(auditParams, Enums.Action.SEARCH, COHORT, studyId, token, options, (study, userId, rp, queryOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); + AnnotationUtils.fixQueryAnnotationSearch(study, myQuery); AnnotationUtils.fixQueryOptionAnnotation(options); - fixQueryObject(study, query, userId); - query.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - OpenCGAResult queryResult = cohortDBAdaptor.get(study.getUid(), query, options, userId); - - auditManager.auditSearch(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + fixQueryObject(study, myQuery, userId); + myQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return cohortDBAdaptor.get(study.getUid(), myQuery, options, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("field", new Query(query)) .append("query", new Query(query)) .append("token", token); - try { - fixQueryObject(study, query, userId); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); - - query.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = cohortDBAdaptor.distinct(study.getUid(), field, query, userId); + return run(auditParams, Enums.Action.DISTINCT, COHORT, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); - auditManager.auditDistinct(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + CohortDBAdaptor.QueryParams param = CohortDBAdaptor.QueryParams.getParam(field); + if (param == null) { + throw new CatalogException("Unknown '" + field + "' parameter."); + } - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + fixQueryObject(study, myQuery, userId); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, myQuery); + myQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return cohortDBAdaptor.distinct(study.getUid(), field, myQuery, userId); + }); } private void fixQueryObject(Study study, Query query, String userId) throws CatalogException { @@ -522,8 +446,6 @@ private void fixQueryObject(Study study, Query query, String userId) throws Cata changeQueryId(query, ParamConstants.COHORT_INTERNAL_STATUS_PARAM, CohortDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key()); if (query.containsKey(ParamConstants.COHORT_SAMPLES_PARAM)) { - QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, SampleDBAdaptor.QueryParams.UID.key()); - // First look for the sample ids. List sampleList = catalogManager.getSampleManager().internalGet(study.getUid(), query.getAsStringList(ParamConstants.COHORT_SAMPLES_PARAM), SampleManager.INCLUDE_SAMPLE_IDS, userId, true) @@ -542,34 +464,19 @@ private void fixQueryObject(Study study, Query query, String userId) throws Cata @Override public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", new Query(query)) .append("token", token); - try { - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); - fixQueryObject(study, query, userId); - - query.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResultAux = cohortDBAdaptor.count(query, userId); - - auditManager.auditCount(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.COUNT, COHORT, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, myQuery); + fixQueryObject(study, myQuery, userId); + myQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return cohortDBAdaptor.count(myQuery, userId); + }); } @Override @@ -579,72 +486,48 @@ public OpenCGAResult delete(String studyStr, List cohortIds, QueryOption public OpenCGAResult delete(String studyStr, List cohortIds, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - if (cohortIds == null || ListUtils.isEmpty(cohortIds)) { - throw new CatalogException("Missing list of cohort ids"); - } - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("cohortIds", cohortIds) .append("params", params) .append("ignoreException", ignoreException) .append("token", token); - - boolean checkPermissions; - try { - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationId, userId, Enums.Resource.COHORT, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - OpenCGAResult result = OpenCGAResult.empty(); - auditManager.initAuditBatch(operationId); - for (String id : cohortIds) { - - String cohortId = id; - String cohortUuid = ""; - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_COHORT_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Cohort '" + id + "' not found"); - } - - // We set the proper values for the audit - cohortId = internalResult.first().getId(); - cohortUuid = internalResult.first().getUuid(); - - if (checkPermissions) { - authorizationManager.checkCohortPermission(study.getUid(), internalResult.first().getUid(), userId, - CohortPermissions.DELETE); + return runBatch(auditParams, DELETE, COHORT, studyStr, token, null, (study, userId, queryOptions, auditOperationUuid) -> { + if (cohortIds == null || ListUtils.isEmpty(cohortIds)) { + throw new CatalogException("Missing list of cohort ids"); + } + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Cohort.class); + for (String cohortId : cohortIds) { + try { + run(auditParams, DELETE, COHORT, auditOperationUuid, study, userId, queryOptions, (s, u, rp, q) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), cohortId, INCLUDE_COHORT_IDS, + userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Cohort '" + cohortId + "' not found"); + } + rp.setId(internalResult.first().getId()); + rp.setUuid(internalResult.first().getUuid()); + + if (checkPermissions) { + authorizationManager.checkCohortPermission(study.getUid(), internalResult.first().getUid(), userId, + CohortPermissions.DELETE); + } + OpenCGAResult deleteResult = cohortDBAdaptor.delete(internalResult.first()); + result.append(deleteResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, cohortId, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not delete cohort {}", cohortId, e); } - OpenCGAResult deleteResult = cohortDBAdaptor.delete(internalResult.first()); - result.append(deleteResult); - - auditManager.auditDelete(operationId, userId, Enums.Resource.COHORT, internalResult.first().getId(), - internalResult.first().getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot delete cohort {}: {}", cohortId, e.getMessage()); - auditManager.auditDelete(operationId, userId, Enums.Resource.COHORT, cohortId, cohortUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -654,15 +537,6 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("query", new Query(query)) @@ -670,52 +544,47 @@ public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, bool .append("ignoreException", ignoreException) .append("token", token); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; + return runBatch(auditParams, DELETE, COHORT, studyStr, token, null, (study, userId, q, operationUuid) -> { + Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); + OpenCGAResult result = OpenCGAResult.empty(Cohort.class); - // We try to get an iterator containing all the cohorts to be deleted - DBIterator iterator; - try { + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + boolean checkPermissions; + + // We try to get an iterator containing all the cohorts to be deleted AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); fixQueryObject(study, finalQuery, userId); finalQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - iterator = cohortDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_COHORT_IDS, userId); - - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.COHORT, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - while (iterator.hasNext()) { - Cohort cohort = iterator.next(); - try { - if (checkPermissions) { - authorizationManager.checkCohortPermission(study.getUid(), cohort.getUid(), userId, - CohortPermissions.DELETE); + try (DBIterator iterator = cohortDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_COHORT_IDS, userId)) { + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + while (iterator.hasNext()) { + Cohort cohort = iterator.next(); + try { + run(auditParams, DELETE, COHORT, operationUuid, study, userId, q, (study1, userId1, rp, qOptions) -> { + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); + + if (checkPermissions) { + authorizationManager.checkCohortPermission(study.getUid(), cohort.getUid(), userId, + CohortPermissions.DELETE); + } + OpenCGAResult tmpResult = cohortDBAdaptor.delete(cohort); + result.append(tmpResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, cohort.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not delete cohort {}", cohort.getId(), e); + } } - OpenCGAResult tmpResult = cohortDBAdaptor.delete(cohort); - result.append(tmpResult); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, cohort.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot delete cohort {}: {}", cohort.getId(), e.getMessage()); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } public OpenCGAResult updateAnnotationSet(String studyStr, String cohortStr, List annotationSetList, @@ -786,18 +655,12 @@ public OpenCGAResult update(String studyStr, String cohortId, CohortUpda public OpenCGAResult update(String studyStr, String cohortId, CohortUpdateParams updateParams, boolean allowModifyCohortAll, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse CohortUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("cohortId", cohortId) @@ -805,38 +668,17 @@ public OpenCGAResult update(String studyStr, String cohortId, CohortUpda .append("allowModifyCohortAll", allowModifyCohortAll) .append("options", options) .append("token", token); - - OpenCGAResult result = OpenCGAResult.empty(); - String cohortUuid = ""; - - try { + return run(auditParams, UPDATE, COHORT, studyStr, token, options, (study, userId, rp, queryOptions) -> { OpenCGAResult internalResult = internalGet(study.getUid(), cohortId, INCLUDE_COHORT_STATUS, userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Cohort '" + cohortId + "' not found"); } Cohort cohort = internalResult.first(); + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); - // We set the proper values for the audit - cohortId = cohort.getId(); - cohortUuid = cohort.getUuid(); - - OpenCGAResult updateResult = update(study, cohort, updateParams, allowModifyCohortAll, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, cohortId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update cohort {}: {}", cohortId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, cohortId, cohortUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - return result; + return update(study, cohort, updateParams, allowModifyCohortAll, options, userId); + }); } public OpenCGAResult update(String studyStr, List cohortIds, CohortUpdateParams updateParams, QueryOptions options, @@ -861,18 +703,12 @@ public OpenCGAResult update(String studyStr, List cohortIds, Coh public OpenCGAResult update(String studyStr, List cohortIds, CohortUpdateParams updateParams, boolean allowModifyCohortAll, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse CohortUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("cohortIds", cohortIds) @@ -881,42 +717,33 @@ public OpenCGAResult update(String studyStr, List cohortIds, Coh .append("ignoreException", ignoreException) .append("options", options) .append("token", token); - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : cohortIds) { - String cohortId = id; - String cohortUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_COHORT_STATUS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Cohort '" + id + "' not found"); + return runBatch(auditParams, UPDATE, COHORT, studyStr, token, options, (study, userId, queryOptions, operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Cohort.class); + for (String id : cohortIds) { + try { + run(auditParams, UPDATE, COHORT, operationUuid, study, userId, queryOptions, (study1, userId1, rp, queryOptions1) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_COHORT_STATUS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Cohort '" + id + "' not found"); + } + Cohort cohort = internalResult.first(); + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); + + OpenCGAResult updateResult = update(study, cohort, updateParams, allowModifyCohortAll, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not update cohort {}", id, e); } - Cohort cohort = internalResult.first(); - - // We set the proper values for the audit - cohortId = cohort.getId(); - cohortUuid = cohort.getUuid(); - - OpenCGAResult updateResult = update(study, cohort, updateParams, allowModifyCohortAll, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, cohortId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update cohort {}: {}", cohortId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, cohortId, cohortUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } /** @@ -938,20 +765,12 @@ public OpenCGAResult update(String studyStr, Query query, CohortUpdatePa public OpenCGAResult update(String studyStr, Query query, CohortUpdateParams updateParams, boolean allowModifyCohortAll, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse CohortUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("query", query) @@ -959,56 +778,40 @@ public OpenCGAResult update(String studyStr, Query query, CohortUpdatePa .append("allowModifyCohortAll", allowModifyCohortAll) .append("options", options) .append("token", token); + return runBatch(auditParams, UPDATE, COHORT, studyStr, token, options, (study, userId, queryOptions, operationUuid) -> { + Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - DBIterator iterator; - try { AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); fixQueryObject(study, finalQuery, userId); finalQuery.append(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = cohortDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_COHORT_STATUS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + OpenCGAResult result = OpenCGAResult.empty(Cohort.class); + try (DBIterator iterator = cohortDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_COHORT_STATUS, userId)) { + while (iterator.hasNext()) { + Cohort cohort = iterator.next(); + try { + run(auditParams, UPDATE, COHORT, operationUuid, study, userId, queryOptions, (study1, userId1, rp, qOptions) -> { + OpenCGAResult queryResult = update(study, cohort, updateParams, allowModifyCohortAll, qOptions, userId); + result.append(queryResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, cohort.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not update cohort {}", cohort.getId(), e); + } + } - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Cohort cohort = iterator.next(); - try { - OpenCGAResult queryResult = update(study, cohort, updateParams, allowModifyCohortAll, options, userId); - result.append(queryResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, cohort.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update cohort {}: {}", cohort.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, Cohort cohort, CohortUpdateParams updateParams, boolean allowModifyCohortAll, QueryOptions options, String userId) throws CatalogException { options = ParamUtils.defaultObject(options, QueryOptions::new); - if (StringUtils.isNotEmpty(updateParams.getCreationDate())) { - ParamUtils.checkDateFormat(updateParams.getCreationDate(), CohortDBAdaptor.QueryParams.CREATION_DATE.key()); - } - if (StringUtils.isNotEmpty(updateParams.getModificationDate())) { - ParamUtils.checkDateFormat(updateParams.getModificationDate(), CohortDBAdaptor.QueryParams.MODIFICATION_DATE.key()); - } - ObjectMap parameters = new ObjectMap(); if (updateParams != null) { try { @@ -1019,6 +822,13 @@ private OpenCGAResult update(Study study, Cohort cohort, CohortUpdatePar } ParamUtils.checkUpdateParametersMap(parameters); + if (updateParams != null && StringUtils.isNotEmpty(updateParams.getCreationDate())) { + ParamUtils.checkDateFormat(updateParams.getCreationDate(), CohortDBAdaptor.QueryParams.CREATION_DATE.key()); + } + if (updateParams != null && StringUtils.isNotEmpty(updateParams.getModificationDate())) { + ParamUtils.checkDateFormat(updateParams.getModificationDate(), CohortDBAdaptor.QueryParams.MODIFICATION_DATE.key()); + } + if (parameters.containsKey(SampleDBAdaptor.QueryParams.ANNOTATION_SETS.key())) { Map actionMap = options.getMap(Constants.ACTIONS, new HashMap<>()); if (!actionMap.containsKey(AnnotationSetManager.ANNOTATION_SETS) && !actionMap.containsKey(AnnotationSetManager.ANNOTATIONS)) { @@ -1103,47 +913,43 @@ private OpenCGAResult update(Study study, Cohort cohort, CohortUpdatePar } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) 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); + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); + return run(auditParams, GROUP_BY, COHORT, studyStr, token, options, (study, userId, rp, queryOptions) -> { + ParamUtils.checkObj(fields, "fields"); + Query myQuery = query != null ? new Query(query) : new Query(); - // Add study id to the query - query.put(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, myQuery, authorizationManager); + AnnotationUtils.fixQueryOptionAnnotation(queryOptions); - OpenCGAResult queryResult = cohortDBAdaptor.groupBy(query, fields, options, userId); + // Add study id to the query + myQuery.put(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + OpenCGAResult queryResult = cohortDBAdaptor.groupBy(myQuery, fields, queryOptions, userId); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } public void setStatus(String studyStr, String cohortId, String status, String message, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("cohortId", cohortId) .append("status", status) .append("message", message) .append("token", token); - Cohort cohort; - try { - cohort = internalGet(study.getUid(), cohortId, INCLUDE_COHORT_IDS, userId).first(); - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.COHORT, cohortId, "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - try { + run(auditParams, UPDATE, COHORT, studyStr, token, null, (study, userId, rp, queryOptions) -> { + Cohort cohort = internalGet(study.getUid(), cohortId, INCLUDE_COHORT_IDS, userId).first(); + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); + authorizationManager.checkCohortPermission(study.getUid(), cohort.getUid(), userId, CohortPermissions.WRITE); if (status != null && !CohortStatus.isValid(status)) { @@ -1154,39 +960,39 @@ public void setStatus(String studyStr, String cohortId, String status, String me parameters.putIfNotNull(CohortDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key(), status); parameters.putIfNotNull(CohortDBAdaptor.QueryParams.INTERNAL_STATUS_DESCRIPTION.key(), message); - cohortDBAdaptor.update(cohort.getUid(), parameters, new QueryOptions()); - - auditManager.auditUpdate(userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return cohortDBAdaptor.update(cohort.getUid(), parameters, new QueryOptions()); + }); } @Override - public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String sessionId) + public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_COHORTS); - - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = cohortDBAdaptor.rank(query, field, numResults, asc); - } + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); + return run(auditParams, RANK, COHORT, studyStr, token, null, (study, userId, rp, queryOptions) -> { + ParamUtils.checkObj(field, "field"); + Query myQuery = query != null ? new Query(query) : new Query(); + + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_COHORTS); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, myQuery, authorizationManager); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = cohortDBAdaptor.rank(myQuery, field, numResults, asc); + } + + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } // ************************** ACLs ******************************** // @@ -1197,23 +1003,17 @@ public OpenCGAResult> getAcls(String studyId, Li public OpenCGAResult> getAcls(String studyId, List cohortList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("cohortList", cohortList) .append("members", members) .append("ignoreException", ignoreException) .append("token", token); - - OpenCGAResult> cohortAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), cohortList, INCLUDE_COHORT_IDS, user, ignoreException); - + return runBatch(auditParams, FETCH_ACLS, COHORT, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult> cohortAcls = OpenCGAResult.empty(); + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), cohortList, INCLUDE_COHORT_IDS, userId, + ignoreException); if (queryResult.getMissing() != null) { missingMap = queryResult.getMissing().stream() .collect(Collectors.toMap(InternalGetDataResult.Missing::getId, Function.identity())); @@ -1221,10 +1021,9 @@ public OpenCGAResult> getAcls(String studyId, Li List cohortUids = queryResult.getResults().stream().map(Cohort::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - cohortAcls = authorizationManager.getAcl(user, study.getUid(), cohortUids, members, Enums.Resource.COHORT, - CohortPermissions.class); + cohortAcls = authorizationManager.getAcl(userId, study.getUid(), cohortUids, members, COHORT, CohortPermissions.class); } else { - cohortAcls = authorizationManager.getAcl(user, study.getUid(), cohortUids, Enums.Resource.COHORT, CohortPermissions.class); + cohortAcls = authorizationManager.getAcl(userId, study.getUid(), cohortUids, COHORT, CohortPermissions.class); } // Include non-existing cohorts to the result list @@ -1234,49 +1033,31 @@ public OpenCGAResult> getAcls(String studyId, Li for (String cohortId : cohortList) { if (!missingMap.containsKey(cohortId)) { Cohort cohort = queryResult.getResults().get(counter); + run(auditParams, FETCH_ACLS, COHORT, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); + return null; + }); resultList.add(cohortAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.COHORT, cohort.getId(), cohort.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), - new ObjectMap()); counter++; } else { - resultList.add(new AclEntryList<>()); + if (!ignoreException) { + throw new CatalogException(missingMap.get(cohortId).getErrorMsg()); + } eventList.add(new Event(Event.Type.ERROR, cohortId, missingMap.get(cohortId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.COHORT, cohortId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(cohortId).getErrorMsg())), new ObjectMap()); + resultList.add(new AclEntryList<>()); } } cohortAcls.setResults(resultList); cohortAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String cohortId : cohortList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.COHORT, cohortId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String cohortId : cohortList) { - Event event = new Event(Event.Type.ERROR, cohortId, e.getMessage()); - cohortAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return cohortAcls; + return cohortAcls; + }); } - public OpenCGAResult> updateAcl(String studyId, List cohortStrList, - String memberList, AclParams aclParams, - ParamUtils.AclAction action, String token) + public OpenCGAResult> updateAcl(String studyId, List cohortStrList, String memberList, + AclParams aclParams, ParamUtils.AclAction action, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, StudyManager.INCLUDE_STUDY_UID); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("cohortStrList", cohortStrList) @@ -1284,10 +1065,9 @@ public OpenCGAResult> updateAcl(String studyId, .append("aclParams", aclParams) .append("action", action) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, UPDATE_ACLS, COHORT, studyId, token, null, (study, userId, queryOptions, operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); if (cohortStrList == null || cohortStrList.isEmpty()) { throw new CatalogException("Missing cohort parameter"); @@ -1305,8 +1085,6 @@ public OpenCGAResult> updateAcl(String studyId, List cohortList = internalGet(study.getUid(), cohortStrList, INCLUDE_COHORT_IDS, userId, false).getResults(); - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); - // Validate that the members are actually valid members List members; if (memberList != null && !memberList.isEmpty()) { @@ -1320,7 +1098,7 @@ public OpenCGAResult> updateAcl(String studyId, List cohortUids = cohortList.stream().map(Cohort::getUid).collect(Collectors.toList()); AuthorizationManager.CatalogAclParams catalogAclParams = new AuthorizationManager.CatalogAclParams(cohortUids, permissions, - Enums.Resource.COHORT); + COHORT); OpenCGAResult> queryResultList; switch (action) { @@ -1341,67 +1119,40 @@ public OpenCGAResult> updateAcl(String studyId, throw new CatalogException("Unexpected error occurred. No valid action found."); } - queryResultList = authorizationManager.getAcls(study.getUid(), cohortUids, members, Enums.Resource.COHORT, - CohortPermissions.class); - for (Cohort cohort : cohortList) { - auditManager.audit(operationId, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.COHORT, cohort.getId(), - cohort.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + run(auditParams, UPDATE_ACLS, COHORT, operationUuid, study, userId, queryOptions, (s, u, rp, qo) -> { + rp.setId(cohort.getId()); + rp.setUuid(cohort.getUuid()); + return null; + }); } - return queryResultList; - } catch (CatalogException e) { - if (cohortStrList != null) { - for (String cohortId : cohortStrList) { - auditManager.audit(operationId, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.COHORT, cohortId, "", - study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - } - throw e; - } finally { - auditManager.finishAuditBatch(operationId); - } + + return authorizationManager.getAcls(study.getUid(), cohortUids, members, COHORT, + CohortPermissions.class); + }); } public DataResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) - throws CatalogException, IOException { - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - // We need to add variableSets and groups to avoid additional queries as it will be used in the catalogSolrManager - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()))); - + throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("defaultStats", defaultStats) .append("token", token); - try { + return run(auditParams, FACET, COHORT, studyId, token, options, (study, userId, rp, queryOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + if (defaultStats || StringUtils.isEmpty(options.getString(QueryOptions.FACET))) { String facet = options.getString(QueryOptions.FACET); options.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); } - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); - + AnnotationUtils.fixQueryAnnotationSearch(study, userId, myQuery, authorizationManager); try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { - - DataResult result = catalogSolrManager.facetedQuery(study, CatalogSolrManager.COHORT_SOLR_COLLECTION, query, - options, userId); - auditManager.auditFacet(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; + return catalogSolrManager.facetedQuery(study, CatalogSolrManager.COHORT_SOLR_COLLECTION, myQuery, options, userId); } - } catch (CatalogException e) { - auditManager.auditFacet(userId, Enums.Resource.COHORT, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage()))); - throw e; - } + }); } } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FamilyManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FamilyManager.java index 641c0298885..5a730dbc3c2 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FamilyManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FamilyManager.java @@ -31,7 +31,6 @@ import org.opencb.biodata.models.pedigree.IndividualProperty; import org.opencb.biodata.tools.pedigree.ModeOfInheritance; import org.opencb.commons.datastore.core.*; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -47,7 +46,6 @@ import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.clinical.ClinicalAnalysis; import org.opencb.opencga.core.models.common.AnnotationSet; import org.opencb.opencga.core.models.common.Enums; @@ -70,6 +68,7 @@ import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; import static org.opencb.opencga.core.common.JacksonUtils.getDefaultObjectMapper; +import static org.opencb.opencga.core.models.common.Enums.Resource.FAMILY; /** * Created by pfurio on 02/05/17. @@ -127,8 +126,8 @@ public static Pedigree getPedigreeFromFamily(Family family, String probandId) { } @Override - Enums.Resource getEntity() { - return Enums.Resource.FAMILY; + Enums.Resource getResource() { + return FAMILY; } @Override @@ -202,21 +201,22 @@ private OpenCGAResult getFamily(long studyUid, String familyUuid, QueryO @Override public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { - ParamUtils.checkObj(token, "sessionId"); - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("query", query) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.ITERATE, FAMILY, studyStr, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - Query finalQuery = new Query(query); - fixQueryObject(study, finalQuery, token); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); - finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + fixQueryObject(study, finalQuery, token); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + AnnotationUtils.fixQueryOptionAnnotation(queryOptions); + finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return familyDBAdaptor.iterator(study.getUid(), finalQuery, options, userId); + return familyDBAdaptor.iterator(study.getUid(), finalQuery, queryOptions, userId); + }); } @Override @@ -226,17 +226,13 @@ public OpenCGAResult create(String studyStr, Family family, QueryOptions public OpenCGAResult create(String studyStr, Family family, List members, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("family", family) .append("members", members) .append("options", options) .append("token", token); - try { + return run(auditParams, Enums.Action.CREATE, FAMILY, studyStr, token, options, (study, userId, rp, queryOptions) -> { authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_FAMILIES); ParamUtils.checkObj(family, "family"); @@ -272,95 +268,66 @@ public OpenCGAResult create(String studyStr, Family family, List validateFamily(family, existingMembers); validatePhenotypes(family, existingMembers); validateDisorders(family, existingMembers); - - options = ParamUtils.defaultObject(options, QueryOptions::new); family.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.FAMILY)); + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); + OpenCGAResult insert = familyDBAdaptor.insert(study.getUid(), family, existingMembers, study.getVariableSets(), - options); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + queryOptions); + if (queryOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch updated family - OpenCGAResult queryResult = getFamily(study.getUid(), family.getUuid(), options); + OpenCGAResult queryResult = getFamily(study.getUid(), family.getUuid(), queryOptions); insert.setResults(queryResult.getResults()); } - - auditManager.auditCreate(userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return insert; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.FAMILY, family.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - Query finalQuery = new Query(query); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", new Query(query)) .append("options", options) .append("token", token); - try { - fixQueryObject(study, finalQuery, token); + return run(auditParams, Enums.Action.SEARCH, FAMILY, studyId, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + + fixQueryObject(study, finalQuery, token); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); + AnnotationUtils.fixQueryOptionAnnotation(queryOptions); finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - OpenCGAResult queryResult = familyDBAdaptor.get(study.getUid(), finalQuery, options, userId); - - auditManager.auditSearch(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return familyDBAdaptor.get(study.getUid(), finalQuery, queryOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); - query.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = familyDBAdaptor.distinct(study.getUid(), field, query, userId); + return run(auditParams, Enums.Action.DISTINCT, FAMILY, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - auditManager.auditDistinct(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + FamilyDBAdaptor.QueryParams param = FamilyDBAdaptor.QueryParams.getParam(field); + if (param == null) { + throw new CatalogException("Unknown '" + field + "' parameter."); + } + Class clazz = getTypeClass(param.type()); - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + fixQueryObject(study, finalQuery, userId); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + + finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return familyDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } private void fixQueryObject(Study study, Query query, String sessionId) throws CatalogException { @@ -417,35 +384,20 @@ private void fixQueryObject(Study study, Query query, String sessionId) throws C } public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - Query finalQuery = new Query(query); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", query) .append("token", token); - try { + + return run(auditParams, Enums.Action.COUNT, FAMILY, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); fixQueryObject(study, finalQuery, token); finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResultAux = familyDBAdaptor.count(finalQuery, userId); - - auditManager.auditCount(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return familyDBAdaptor.count(finalQuery, userId); + }); } @Override @@ -453,74 +405,54 @@ public OpenCGAResult delete(String studyStr, List familyIds, QueryOption return delete(studyStr, familyIds, options, false, token); } - public OpenCGAResult delete(String studyStr, List familyIds, ObjectMap params, boolean ignoreException, String token) + public OpenCGAResult delete(String studyStr, List familyIds, QueryOptions options, boolean ignoreException, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("familyIds", familyIds) - .append("params", params) + .append("options", options) .append("ignoreException", ignoreException) .append("token", token); - boolean checkPermissions; - try { + return runBatch(auditParams, Enums.Action.DELETE, FAMILY, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Family.class); + for (String familyId : familyIds) { + try { + run(auditParams, Enums.Action.DELETE, FAMILY, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), familyId, INCLUDE_FAMILY_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Family '" + familyId + "' not found"); + } - auditManager.initAuditBatch(operationUuid); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : familyIds) { - String familyId = id; - String familyUuid = ""; + Family family = internalResult.first(); + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_FAMILY_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Family '" + id + "' not found"); - } + if (checkPermissions) { + authorizationManager.checkFamilyPermission(study.getUid(), family.getUid(), userId, FamilyPermissions.DELETE); + } - Family family = internalResult.first(); - // We set the proper values for the audit - familyId = family.getId(); - familyUuid = family.getUuid(); + // Check family can be deleted + checkCanBeDeleted(study, family); - if (checkPermissions) { - authorizationManager.checkFamilyPermission(study.getUid(), family.getUid(), userId, - FamilyPermissions.DELETE); + // Delete the family + OpenCGAResult delete = familyDBAdaptor.delete(family); + result.append(delete); + return delete; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, familyId, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not delete family '{}'", familyId, e); } - - // Check family can be deleted - checkCanBeDeleted(study, family); - - // Delete the family - result.append(familyDBAdaptor.delete(family)); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, familyId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot delete family {}: {}", familyId, e.getMessage(), e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, familyId, familyUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private void checkCanBeDeleted(Study study, Family family) throws CatalogException { @@ -539,77 +471,58 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, return delete(studyStr, query, options, false, token); } - public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, boolean ignoreException, String token) + public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("query", new Query(query)) - .append("params", params) + .append("options", options) .append("ignoreException", ignoreException) .append("token", token); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; + return runBatch(auditParams, Enums.Action.DELETE, FAMILY, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - // We try to get an iterator containing all the families to be deleted - DBIterator iterator; - try { // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); fixQueryObject(study, finalQuery, token); finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = familyDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_FAMILY_IDS, userId); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - while (iterator.hasNext()) { - Family family = iterator.next(); + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Family.class); + try (DBIterator iterator = familyDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_FAMILY_IDS, userId)) { + while (iterator.hasNext()) { + Family family = iterator.next(); + try { + run(auditParams, Enums.Action.DELETE, FAMILY, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); + + if (checkPermissions) { + authorizationManager.checkFamilyPermission(study.getUid(), family.getUid(), userId, + FamilyPermissions.DELETE); + } - try { - if (checkPermissions) { - authorizationManager.checkFamilyPermission(study.getUid(), family.getUid(), userId, - FamilyPermissions.DELETE); + // TODO: Check if the family is used in a clinical analysis. At this point, it can be deleted no matter what. + + // Delete the family + OpenCGAResult delete = familyDBAdaptor.delete(family); + result.append(delete); + return delete; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, family.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not delete family '{}'", family.getId(), e); + } } - // TODO: Check if the family is used in a clinical analysis. At this point, it can be deleted no matter what. - - // Delete the family - result.append(familyDBAdaptor.delete(family)); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete family " + family.getId() + ": " + e.getMessage(); - - Event event = new Event(Event.Type.ERROR, family.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error(errorMsg, e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationUuid); - - return endResult(result, ignoreException); + }); } @Override @@ -619,30 +532,32 @@ public OpenCGAResult rank(String studyStr, Query query, String field, int numRes } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - if (fields == null || fields.size() == 0) { - throw new CatalogException("Empty fields parameter."); - } - - String userId = userManager.getUserId(sessionId); - Study study = studyManager.resolveId(studyStr, userId); + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); - Query finalQuery = new Query(query); - fixQueryObject(study, finalQuery, sessionId); + return run(auditParams, Enums.Action.GROUP_BY, FAMILY, studyStr, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + if (fields == null || fields.size() == 0) { + throw new CatalogException("Empty fields parameter."); + } - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); - AnnotationUtils.fixQueryOptionAnnotation(options); + fixQueryObject(study, finalQuery, token); - // Add study id to the query - finalQuery.put(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); + AnnotationUtils.fixQueryOptionAnnotation(queryOptions); - OpenCGAResult queryResult = familyDBAdaptor.groupBy(finalQuery, fields, options, userId); + // Add study id to the query + finalQuery.put(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return familyDBAdaptor.groupBy(finalQuery, fields, queryOptions, userId); + }); } public OpenCGAResult updateAnnotationSet(String studyStr, String familyStr, List annotationSetList, @@ -722,11 +637,6 @@ public OpenCGAResult update(String studyStr, Query query, FamilyUpdatePa public OpenCGAResult update(String studyStr, Query query, FamilyUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -742,56 +652,43 @@ public OpenCGAResult update(String studyStr, Query query, FamilyUpdatePa .append("options", options) .append("token", token); - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - - DBIterator iterator; - try { + return runBatch(auditParams, Enums.Action.UPDATE, FAMILY, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); fixQueryObject(study, finalQuery, token); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + AnnotationUtils.fixQueryOptionAnnotation(qOptions); finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = familyDBAdaptor.iterator(study.getUid(), finalQuery, QueryOptions.empty(), userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + try (DBIterator iterator = familyDBAdaptor.iterator(study.getUid(), finalQuery, QueryOptions.empty(), userId)) { + OpenCGAResult result = OpenCGAResult.empty(Family.class); + while (iterator.hasNext()) { + Family family = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, FAMILY, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); + OpenCGAResult queryResult = update(study, family, updateParams, options, userId); + result.append(queryResult); + return queryResult; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, family.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not update family {}", family.getId(), e); + } + } - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Family family = iterator.next(); - try { - OpenCGAResult queryResult = update(study, family, updateParams, options, userId); - result.append(queryResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, family.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update family {}: {}", family.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyStr, String familyId, FamilyUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -806,10 +703,7 @@ public OpenCGAResult update(String studyStr, String familyId, FamilyUpda .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String familyUuid = ""; - - try { + return run(auditParams, Enums.Action.UPDATE, FAMILY, studyStr, token, options, (study, userId, rp, queryOptions) -> { OpenCGAResult internalResult = internalGet(study.getUid(), familyId, QueryOptions.empty(), userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Family '" + familyId + "' not found"); @@ -817,26 +711,11 @@ public OpenCGAResult update(String studyStr, String familyId, FamilyUpda Family family = internalResult.first(); // We set the proper values for the audit - familyId = family.getId(); - familyUuid = family.getUuid(); - - OpenCGAResult updateResult = update(study, family, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, familyId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update family {}: {}", familyId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, familyId, familyUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); - return result; + return update(study, family, updateParams, options, userId); + }); } /** @@ -858,11 +737,6 @@ public OpenCGAResult update(String studyStr, List familyIds, Fam public OpenCGAResult update(String studyStr, List familyIds, FamilyUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -878,41 +752,35 @@ public OpenCGAResult update(String studyStr, List familyIds, Fam .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : familyIds) { - String familyId = id; - String familyUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), familyId, QueryOptions.empty(), userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Family '" + id + "' not found"); - } - Family family = internalResult.first(); - - // We set the proper values for the audit - familyId = family.getId(); - familyUuid = family.getUuid(); - - OpenCGAResult updateResult = update(study, family, updateParams, options, userId); - result.append(updateResult); + return runBatch(auditParams, Enums.Action.UPDATE, FAMILY, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Family.class); + for (String familyId : familyIds) { + try { + run(auditParams, Enums.Action.UPDATE, FAMILY, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), familyId, QueryOptions.empty(), userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Family '" + familyId + "' not found"); + } + Family family = internalResult.first(); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, family.getId(), family.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + // We set the proper values for the audit + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); - logger.error("Cannot update family {}: {}", familyId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FAMILY, familyId, familyUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + OpenCGAResult updateResult = update(study, family, updateParams, options, userId); + result.append(updateResult); + return updateResult; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, familyId, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + logger.warn("Could not update family {}", familyId, e); + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, Family family, FamilyUpdateParams updateParams, QueryOptions options, String userId) @@ -1100,10 +968,6 @@ public OpenCGAResult> getAcls(String studyId, Li public OpenCGAResult> getAcls(String studyId, List familyList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("familyList", familyList) @@ -1111,11 +975,11 @@ public OpenCGAResult> getAcls(String studyId, Li .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> familyAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), familyList, INCLUDE_FAMILY_IDS, user, ignoreException); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, FAMILY, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult> familyAcls; + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), familyList, INCLUDE_FAMILY_IDS, userId, + ignoreException); if (queryResult.getMissing() != null) { missingMap = queryResult.getMissing().stream() @@ -1124,73 +988,53 @@ public OpenCGAResult> getAcls(String studyId, Li List familyUids = queryResult.getResults().stream().map(Family::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - familyAcls = authorizationManager.getAcl(user, study.getUid(), familyUids, members, Enums.Resource.FAMILY, + familyAcls = authorizationManager.getAcl(userId, study.getUid(), familyUids, members, FAMILY, FamilyPermissions.class); } else { - familyAcls = authorizationManager.getAcl(user, study.getUid(), familyUids, Enums.Resource.FAMILY, + familyAcls = authorizationManager.getAcl(userId, study.getUid(), familyUids, FAMILY, FamilyPermissions.class); } - // Include non-existing samples to the result list + // Include non-existing families to the result list List> resultList = new ArrayList<>(familyList.size()); List eventList = new ArrayList<>(missingMap.size()); int counter = 0; for (String familyId : familyList) { if (!missingMap.containsKey(familyId)) { Family family = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, FAMILY, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); + return null; + }); resultList.add(familyAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.FAMILY, family.getId(), - family.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); counter++; } else { - resultList.add(new AclEntryList<>()); + if (!ignoreException) { + throw new CatalogException(missingMap.get(familyId).getErrorMsg()); + } eventList.add(new Event(Event.Type.ERROR, familyId, missingMap.get(familyId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.FAMILY, familyId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(familyId).getErrorMsg())), new ObjectMap()); + resultList.add(new AclEntryList<>()); } } familyAcls.setResults(resultList); familyAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String familyId : familyList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.FAMILY, familyId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String familyId : familyList) { - Event event = new Event(Event.Type.ERROR, familyId, e.getMessage()); - familyAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return familyAcls; + return familyAcls; + }); } public OpenCGAResult> updateAcl(String studyId, FamilyAclParams aclParams, String memberList, ParamUtils.AclAction action, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("aclParams", aclParams) .append("memberList", memberList) .append("action", action) .append("token", token); - String operationUUID = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - List familyList = null; - try { - auditManager.initAuditBatch(operationUUID); - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), user); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, FAMILY, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); int count = 0; count += aclParams.getFamily() != null && !aclParams.getFamily().isEmpty() ? 1 : 0; @@ -1220,6 +1064,7 @@ public OpenCGAResult> updateAcl(String studyId, individualStr = sampleResult.getResults().stream().map(Sample::getIndividualId).collect(Collectors.joining(",")); } + List familyList = null; if (StringUtils.isNotEmpty(individualStr)) { familyList = catalogManager.getFamilyManager().search(studyId, new Query(FamilyDBAdaptor.QueryParams.MEMBERS.key(), individualStr), FamilyManager.INCLUDE_FAMILY_MEMBERS, @@ -1246,7 +1091,7 @@ public OpenCGAResult> updateAcl(String studyId, List aclParamsList = new LinkedList<>(); List familyUids = familyList.stream().map(Family::getUid).collect(Collectors.toList()); - aclParamsList.add(new AuthorizationManager.CatalogAclParams(familyUids, permissions, Enums.Resource.FAMILY)); + aclParamsList.add(new AuthorizationManager.CatalogAclParams(familyUids, permissions, FAMILY)); if (aclParams.getPropagate() == FamilyAclParams.Propagate.YES || aclParams.getPropagate() == FamilyAclParams.Propagate.YES_AND_VARIANT_VIEW) { @@ -1302,68 +1147,42 @@ public OpenCGAResult> updateAcl(String studyId, throw new CatalogException("Unexpected error occurred. No valid action found."); } - OpenCGAResult> remainingAcls = authorizationManager.getAcls(study.getUid(), - familyUids, members, Enums.Resource.FAMILY, FamilyPermissions.class); - for (Family family : familyList) { - auditManager.audit(operationUUID, user, Enums.Action.UPDATE_ACLS, Enums.Resource.FAMILY, family.getId(), - family.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + // To audit + run(auditParams, Enums.Action.UPDATE_ACLS, FAMILY, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(family.getId()); + rp.setUuid(family.getUuid()); + return null; + }); } - return remainingAcls; - } catch (CatalogException e) { - if (familyList != null) { - for (Family family : familyList) { - auditManager.audit(operationUUID, user, Enums.Action.UPDATE_ACLS, Enums.Resource.FAMILY, family.getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - e.getError()), new ObjectMap()); - } - } - throw e; - } finally { - auditManager.finishAuditBatch(operationUUID); - } + return authorizationManager.getAcls(study.getUid(), familyUids, members, Enums.Resource.FAMILY, FamilyPermissions.class); + }); } - public DataResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) + public OpenCGAResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) throws CatalogException { - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - // We need to add variableSets and groups to avoid additional queries as it will be used in the catalogSolrManager - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()))); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("defaultStats", defaultStats) .append("token", token); - try { - if (defaultStats || StringUtils.isEmpty(options.getString(QueryOptions.FACET))) { - String facet = options.getString(QueryOptions.FACET); - options.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); - } + return run(auditParams, Enums.Action.FACET, FAMILY, studyId, token, options, (study, userId, rp, qOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + AnnotationUtils.fixQueryAnnotationSearch(study, userId, myQuery, authorizationManager); - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); + if (defaultStats || StringUtils.isEmpty(qOptions.getString(QueryOptions.FACET))) { + String facet = qOptions.getString(QueryOptions.FACET); + qOptions.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); + } try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { - DataResult result = catalogSolrManager.facetedQuery(study, CatalogSolrManager.FAMILY_SOLR_COLLECTION, query, - options, userId); - auditManager.auditFacet(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; + return new OpenCGAResult<>(catalogSolrManager.facetedQuery(study, CatalogSolrManager.FAMILY_SOLR_COLLECTION, myQuery, + qOptions, userId)); } - } catch (CatalogException e) { - auditManager.auditFacet(userId, Enums.Resource.FAMILY, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage()))); - throw e; - } + }); } /** diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FileManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FileManager.java index b0df9d484ae..70fe73770d0 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FileManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/FileManager.java @@ -24,7 +24,6 @@ import org.opencb.biodata.models.variant.VariantFileMetadata; import org.opencb.biodata.models.variant.metadata.VariantSetStats; import org.opencb.commons.datastore.core.*; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.CollectionUtils; import org.opencb.commons.utils.FileUtils; import org.opencb.commons.utils.ListUtils; @@ -48,7 +47,6 @@ import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.config.HookConfiguration; import org.opencb.opencga.core.models.AclEntryList; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.common.AnnotationSet; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.file.*; @@ -80,6 +78,7 @@ import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; import static org.opencb.opencga.core.common.JacksonUtils.getDefaultObjectMapper; import static org.opencb.opencga.core.common.JacksonUtils.getUpdateObjectMapper; +import static org.opencb.opencga.core.models.common.Enums.Resource.FILE; /** * @author Jacobo Coll <jacobo167@gmail.com> @@ -138,8 +137,8 @@ public class FileManager extends AnnotationSetManager { } @Override - Enums.Resource getEntity() { - return Enums.Resource.FILE; + Enums.Resource getResource() { + return FILE; } @Override @@ -287,15 +286,15 @@ public URI getUri(File file) throws CatalogException { } } - public Study getStudy(File file, String sessionId) throws CatalogException { + public Study getStudy(File file, String token) throws CatalogException { ParamUtils.checkObj(file, "file"); - ParamUtils.checkObj(sessionId, "session id"); + ParamUtils.checkObj(token, "session id"); if (file.getStudyUid() <= 0) { throw new CatalogException("Missing study uid field in file"); } - String user = userManager.getUserId(sessionId); + String user = userManager.getUserId(token); Query query = new Query(StudyDBAdaptor.QueryParams.UID.key(), file.getStudyUid()); OpenCGAResult studyDataResult = studyDBAdaptor.get(query, QueryOptions.empty(), user); @@ -307,8 +306,8 @@ public Study getStudy(File file, String sessionId) throws CatalogException { } } - public void matchUpVariantFiles(String studyStr, List transformedFiles, String sessionId) throws CatalogException { - String userId = userManager.getUserId(sessionId); + public void matchUpVariantFiles(String studyStr, List transformedFiles, String token) throws CatalogException { + String userId = userManager.getUserId(token); Study study = studyManager.resolveId(studyStr, userId); for (File transformedFile : transformedFiles) { @@ -428,7 +427,7 @@ public void matchUpVariantFiles(String studyStr, List transformedFiles, St new FileQualityControl().setVariant( new VariantFileQualityControl(stats, null))), new QueryOptions(), - sessionId); + token); } catch (IOException e) { throw new CatalogException("Error reading file \"" + statsFile + "\"", e); } @@ -437,61 +436,65 @@ public void matchUpVariantFiles(String studyStr, List transformedFiles, St public OpenCGAResult updateFileInternalVariantIndex(File file, FileInternalVariantIndex index, String token) throws CatalogException { - return updateFileInternalField(file, index, FileDBAdaptor.QueryParams.INTERNAL_VARIANT_INDEX.key(), token); + return updateFileInternalField(file, FileDBAdaptor.QueryParams.INTERNAL_VARIANT_INDEX.key(), index, token); } public OpenCGAResult updateFileInternalVariantAnnotationIndex(File file, FileInternalVariantAnnotationIndex index, String token) throws CatalogException { - return updateFileInternalField(file, index, FileDBAdaptor.QueryParams.INTERNAL_VARIANT_ANNOTATION_INDEX.key(), token); + return updateFileInternalField(file, FileDBAdaptor.QueryParams.INTERNAL_VARIANT_ANNOTATION_INDEX.key(), index, token); } public OpenCGAResult updateFileInternalVariantSecondaryIndex(File file, FileInternalVariantSecondaryIndex index, String token) throws CatalogException { - return updateFileInternalField(file, index, FileDBAdaptor.QueryParams.INTERNAL_VARIANT_SECONDARY_INDEX.key(), token); + return updateFileInternalField(file, FileDBAdaptor.QueryParams.INTERNAL_VARIANT_SECONDARY_INDEX.key(), index, token); } public OpenCGAResult updateFileInternalAlignmentIndex(File file, FileInternalAlignmentIndex index, String token) throws CatalogException { - return updateFileInternalField(file, index, FileDBAdaptor.QueryParams.INTERNAL_ALIGNMENT_INDEX.key(), token); + return updateFileInternalField(file, FileDBAdaptor.QueryParams.INTERNAL_ALIGNMENT_INDEX.key(), index, token); } - private OpenCGAResult updateFileInternalField(File file, Object value, String fieldKey, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyDBAdaptor.get(file.getStudyUid(), StudyManager.INCLUDE_STUDY_IDS).first(); - + private OpenCGAResult updateFileInternalField(File file, String fieldKey, Object value, String token) throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("file", file) - .append(fieldKey, value) + .append("fieldKey", fieldKey) + .append("value", value) .append("token", token); - authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.WRITE); - - ObjectMap params; - try { - params = new ObjectMap(fieldKey, new ObjectMap(getUpdateObjectMapper().writeValueAsString(value))); - } catch (JsonProcessingException e) { - throw new CatalogException("Cannot parse index object: " + e.getMessage(), e); - } - OpenCGAResult update = fileDBAdaptor.update(file.getUid(), params, QueryOptions.empty()); - auditManager.auditUpdate(userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.UPDATE, FILE, null, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.WRITE); - return new OpenCGAResult<>(update.getTime(), update.getEvents(), 1, Collections.emptyList(), 1); + ObjectMap params; + try { + params = new ObjectMap(fieldKey, new ObjectMap(getUpdateObjectMapper().writeValueAsString(value))); + } catch (JsonProcessingException e) { + throw new CatalogException("Cannot parse index object: " + e.getMessage(), e); + } + OpenCGAResult update = fileDBAdaptor.update(file.getUid(), params, QueryOptions.empty()); + return new OpenCGAResult<>(update.getTime(), update.getEvents(), 1, Collections.emptyList(), 1); + }); } @Deprecated - public OpenCGAResult getParents(long fileId, QueryOptions options, String sessionId) throws CatalogException { - OpenCGAResult fileDataResult = fileDBAdaptor.get(fileId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(FileDBAdaptor.QueryParams.PATH.key(), FileDBAdaptor.QueryParams.STUDY_UID.key()))); - - if (fileDataResult.getNumResults() == 0) { - return fileDataResult; - } + public OpenCGAResult getParents(long fileId, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("fileId", fileId) + .append("options", options) + .append("token", token); - String userId = userManager.getUserId(sessionId); - authorizationManager.checkFilePermission(fileDataResult.first().getStudyUid(), fileId, userId, FilePermissions.VIEW); + return run(auditParams, Enums.Action.INFO, FILE, null, token, options, (study, userId, rp, queryOptions) -> { + OpenCGAResult fileDataResult = fileDBAdaptor.get(fileId, new Query(), new QueryOptions(QueryOptions.INCLUDE, + Arrays.asList(FileDBAdaptor.QueryParams.PATH.key(), FileDBAdaptor.QueryParams.STUDY_UID.key())), userId); + if (fileDataResult.getNumResults() == 0) { + return fileDataResult; + } + rp.setId(fileDataResult.first().getId()); + rp.setUuid(fileDataResult.first().getUuid()); - return getParents(fileDataResult.first().getStudyUid(), fileDataResult.first().getPath(), true, options); + return getParents(fileDataResult.first().getStudyUid(), fileDataResult.first().getPath(), true, options); + }); } public OpenCGAResult createFolder(String studyStr, String path, boolean parents, String description, QueryOptions options, @@ -546,17 +549,13 @@ public OpenCGAResult create(String studyStr, File entry, QueryOptions opti public OpenCGAResult create(String studyStr, FileCreateParams createParams, boolean parents, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("createParams", createParams) .append("parents", parents) .append("token", token); - String fileId = null; - try { + return run(auditParams, Enums.Action.CREATE, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { ParamUtils.checkObj(createParams, "body"); ParamUtils.checkParameter(createParams.getPath(), "path"); ParamUtils.checkObj(createParams.getType(), "type"); @@ -588,7 +587,9 @@ public OpenCGAResult create(String studyStr, FileCreateParams createParams } OpenCGAResult parentResult = getParents(study.getUid(), path, false, FileManager.INCLUDE_FILE_URI_PATH); - // Check user can write in path + + rp.setId(path.replace("/", ":")); + // Check if user can write in path authorizationManager.checkFilePermission(study.getUid(), parentResult.first().getUid(), userId, FilePermissions.WRITE); @@ -630,7 +631,8 @@ public OpenCGAResult create(String studyStr, FileCreateParams createParams createParams.getSoftware(), null, createParams.getSampleIds(), null, createParams.getJobId(), 1, createParams.getTags(), null, null, null, createParams.getStatus() != null ? createParams.getStatus().toStatus() : null, null, null); List eventList = validateNewFile(study, file, false); - fileId = file.getId(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); if (file.getType().equals(File.Type.FILE)) { // Obtain IOManager to write in disk @@ -667,16 +669,8 @@ public OpenCGAResult create(String studyStr, FileCreateParams createParams OpenCGAResult result = register(study, file, existingSamples, nonExistingSamples, parents, QueryOptions.empty(), token); result.setEvents(eventList); - auditManager.auditCreate(userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - + }); } List validateNewFile(Study study, File file, boolean overwrite) throws CatalogException { @@ -792,8 +786,8 @@ List validateNewFile(Study study, File file, boolean overwrite) throws Ca } OpenCGAResult register(Study study, File file, List existingSamples, List nonExistingSamples, - boolean parents, QueryOptions options, String sessionId) throws CatalogException { - String userId = userManager.getUserId(sessionId); + boolean parents, QueryOptions options, String token) throws CatalogException { + String userId = userManager.getUserId(token); long studyId = study.getUid(); //Find parent. If parents == true, create folders. @@ -807,7 +801,7 @@ OpenCGAResult register(Study study, File file, List existingSample File parentFile = new File(File.Type.DIRECTORY, File.Format.NONE, File.Bioformat.NONE, parentPath, "", FileInternal.init(), 0, Collections.emptyList(), null, "", new FileQualityControl(), Collections.emptyMap(), Collections.emptyMap()); validateNewFile(study, parentFile, false); - parentFileId = register(study, parentFile, Collections.emptyList(), Collections.emptyList(), parents, options, sessionId) + parentFileId = register(study, parentFile, Collections.emptyList(), Collections.emptyList(), parents, options, token) .first().getUid(); } else { throw new CatalogDBException("Directory not found " + parentPath); @@ -828,14 +822,14 @@ OpenCGAResult register(Study study, File file, List existingSample OpenCGAResult queryResult = getFile(studyId, file.getUuid(), options); // We obtain the permissions set in the parent folder and set them to the file or folder being created OpenCGAResult> allFileAcls = authorizationManager.getAcls(studyId, parentFileId, - Enums.Resource.FILE, FilePermissions.class); + FILE, FilePermissions.class); // Propagate ACLs if (allFileAcls.getNumResults() > 0) { authorizationManager.replicateAcls(Collections.singletonList(queryResult.first().getUid()), allFileAcls.getResults().get(0), - Enums.Resource.FILE); + FILE); } - matchUpVariantFiles(study.getFqn(), queryResult.getResults(), sessionId); + matchUpVariantFiles(study.getFqn(), queryResult.getResults(), token); return queryResult; } @@ -895,14 +889,6 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, public OpenCGAResult upload(String studyStr, InputStream fileInputStream, File file, boolean overwrite, boolean parents, boolean calculateChecksum, String expectedChecksum, Long expectedSize, String token) throws CatalogException { - // Check basic parameters - ParamUtils.checkObj(fileInputStream, "fileInputStream"); - if (StringUtils.isNotEmpty(expectedChecksum)) { - calculateChecksum = true; - } - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - ObjectMap auditParams = new ObjectMap() .append("studyStr", studyStr) .append("file", file) @@ -912,8 +898,20 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, .append("expectedChecksum", expectedChecksum) .append("expectedSize", expectedSize) .append("token", token); - try { + + return run(auditParams, Enums.Action.UPLOAD, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(file.getPath().replace("/", ":")); + + // Check basic parameters + ParamUtils.checkObj(fileInputStream, "fileInputStream"); + boolean finalCalculateChecksum = calculateChecksum; + if (StringUtils.isNotEmpty(expectedChecksum)) { + finalCalculateChecksum = true; + } + validateNewFile(study, file, overwrite); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); File overwrittenFile = null; Query query = new Query() @@ -988,7 +986,7 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, } } - if (calculateChecksum) { + if (finalCalculateChecksum) { checksum = ioManager.calculateChecksum(tempFileUri); if (StringUtils.isNotEmpty(expectedChecksum)) { // Validate checksum @@ -1023,7 +1021,7 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, } else { ioManager.move(tempFileUri, file.getUri()); } - if (calculateChecksum && !checksum.equals(ioManager.calculateChecksum(file.getUri()))) { + if (finalCalculateChecksum && !checksum.equals(ioManager.calculateChecksum(file.getUri()))) { throw new CatalogIOException("Error moving file from " + tempFileUri + " to " + file.getUri()); } @@ -1050,7 +1048,7 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, if (overwrittenFile != null) { // We need to update the existing file document ObjectMap params = new ObjectMap(); - QueryOptions queryOptions = new QueryOptions(); + QueryOptions qOptions = new QueryOptions(); params.put(FileDBAdaptor.QueryParams.SIZE.key(), file.getSize()); params.put(FileDBAdaptor.QueryParams.URI.key(), file.getUri()); @@ -1064,7 +1062,7 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, // Set new samples Map actionMap = new HashMap<>(); actionMap.put(FileDBAdaptor.QueryParams.SAMPLE_IDS.key(), ParamUtils.BasicUpdateAction.SET.name()); - queryOptions.put(Constants.ACTIONS, actionMap); + qOptions.put(Constants.ACTIONS, actionMap); } if (!file.getAttributes().isEmpty()) { Map attributes = overwrittenFile.getAttributes(); @@ -1077,7 +1075,7 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, params.put(FileDBAdaptor.QueryParams.STATS.key(), stats); } - fileDBAdaptor.update(overwrittenFile.getUid(), params, null, queryOptions); + fileDBAdaptor.update(overwrittenFile.getUid(), params, null, qOptions); } else { // We need to register a new file register(study, file, existingSamples, nonExistingSamples, parents, QueryOptions.empty(), token); @@ -1092,47 +1090,38 @@ public OpenCGAResult upload(String studyStr, InputStream fileInputStream, throw new CatalogException("Upload file failed. Could not register the file in the DB", e); } - auditManager.auditCreate(userId, Enums.Action.UPLOAD, Enums.Resource.FILE, file.getId(), file.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return fileDBAdaptor.get(query, QueryOptions.empty()); - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Action.UPLOAD, Enums.Resource.FILE, file.getId(), "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** * Moves a file not yet registered in OpenCGA from origin to finalDestiny in the file system and then registers it in the study. * * @param studyStr Study to which the file will belong. - * @param fileSource Current location of the file (file system). - * @param folderDestiny Directory where the file needs to be moved (file system). - * @param path Directory in catalog where the file will be registered (catalog). + * @param source Current location of the file (file system). + * @param uriTarget Directory where the file needs to be moved (file system). + * @param pathTarget Directory in catalog where the file will be registered (catalog). * @param token Token of the user. * @return An OpenCGAResult with the file registry after moving it to the final destination. * @throws CatalogException CatalogException. */ - public OpenCGAResult moveAndRegister(String studyStr, Path fileSource, @Nullable Path folderDestiny, @Nullable String path, + public OpenCGAResult moveAndRegister(String studyStr, Path source, @Nullable Path uriTarget, @Nullable String pathTarget, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("studyStr", studyStr) - .append("fileSource", fileSource) - .append("folderDestiny", folderDestiny) - .append("path", path) + .append("fileSource", source) + .append("folderDestiny", uriTarget) + .append("path", pathTarget) .append("token", token); - - try { + return run(auditParams, Enums.Action.MOVE_AND_REGISTER, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + String path = pathTarget; + Path folderDestiny = uriTarget; try { - FileUtils.checkFile(fileSource); + FileUtils.checkFile(source); } catch (IOException e) { - throw new CatalogException("File '" + fileSource + "' not found", e); + throw new CatalogException("File '" + source + "' not found", e); } - String fileName = fileSource.toFile().getName(); + String fileName = source.toFile().getName(); if (folderDestiny == null && path == null) { throw new CatalogException("'folderDestiny' and 'path' cannot be both null."); @@ -1204,14 +1193,13 @@ public OpenCGAResult moveAndRegister(String studyStr, Path fileSource, @Nu ioManager.createDirectory(folderDestinyUri, true); } - ioManager.move(fileSource.toUri(), folderDestiny.resolve(fileName).toUri(), StandardCopyOption.REPLACE_EXISTING); + ioManager.move(source.toUri(), folderDestiny.resolve(fileName).toUri(), StandardCopyOption.REPLACE_EXISTING); } catch (CatalogIOException e) { - throw new CatalogException("Unexpected error. Could not move file from '" + fileSource + "' to '" + folderDestiny + "'", e); + throw new CatalogException("Unexpected error. Could not move file from '" + source + "' to '" + folderDestiny + "'", e); } - OpenCGAResult result; if (external) { - result = link(study.getFqn(), folderDestiny.resolve(fileName).toUri(), path, new ObjectMap("parents", true), token); + return link(study.getFqn(), folderDestiny.resolve(fileName).toUri(), path, new ObjectMap("parents", true), token); } else { CheckPath checkPath = checkPathExists(filePath, study.getUid()); if (checkPath != CheckPath.FREE_PATH) { @@ -1230,18 +1218,9 @@ public OpenCGAResult moveAndRegister(String studyStr, Path fileSource, @Nu validateNewSamples(study, file, existingSamples, nonExistingSamples, token); } - result = register(study, file, existingSamples, nonExistingSamples, true, QueryOptions.empty(), token); + return register(study, file, existingSamples, nonExistingSamples, true, QueryOptions.empty(), token); } - - auditManager.audit(userId, Enums.Action.MOVE_AND_REGISTER, Enums.Resource.FILE, result.first().getId(), - result.first().getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.MOVE_AND_REGISTER, Enums.Resource.FILE, "", "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } @Deprecated @@ -1251,46 +1230,44 @@ public OpenCGAResult get(Long fileId, QueryOptions options, String session public OpenCGAResult getTree(@Nullable String studyId, String fileId, int maxDepth, QueryOptions options, String token) throws CatalogException { - long startTime = System.currentTimeMillis(); - - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("fileId", fileId) .append("options", options) .append("maxDepth", maxDepth) .append("token", token); - try { + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.TREE, FILE, studyId, token, options, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); if (maxDepth < 1) { throw new CatalogException("Depth cannot be lower than 1"); } - if (options.containsKey(QueryOptions.INCLUDE)) { + if (queryOptions.containsKey(QueryOptions.INCLUDE)) { // Add type and path to the queryOptions - List asStringListOld = options.getAsStringList(QueryOptions.INCLUDE); + List asStringListOld = queryOptions.getAsStringList(QueryOptions.INCLUDE); Set newList = new HashSet<>(asStringListOld); newList.add(FileDBAdaptor.QueryParams.TYPE.key()); newList.add(FileDBAdaptor.QueryParams.PATH.key()); - options.put(QueryOptions.INCLUDE, new ArrayList<>(newList)); + queryOptions.put(QueryOptions.INCLUDE, new ArrayList<>(newList)); } else { - if (options.containsKey(QueryOptions.EXCLUDE)) { + if (queryOptions.containsKey(QueryOptions.EXCLUDE)) { // Avoid excluding type and path from queryoptions - List asStringListOld = options.getAsStringList(QueryOptions.EXCLUDE); + List asStringListOld = queryOptions.getAsStringList(QueryOptions.EXCLUDE); Set newList = new HashSet<>(asStringListOld); newList.remove(FileDBAdaptor.QueryParams.TYPE.key()); newList.remove(FileDBAdaptor.QueryParams.PATH.key()); if (newList.size() > 0) { - options.put(QueryOptions.EXCLUDE, new ArrayList<>(newList)); + queryOptions.put(QueryOptions.EXCLUDE, new ArrayList<>(newList)); } else { - options.remove(QueryOptions.EXCLUDE); + queryOptions.remove(QueryOptions.EXCLUDE); } } } - File file = internalGet(study.getUid(), fileId, options, userId).first(); + File file = internalGet(study.getUid(), fileId, queryOptions, userId).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); // Check if the id does not correspond to a directory if (!file.getType().equals(File.Type.DIRECTORY)) { @@ -1307,11 +1284,11 @@ public OpenCGAResult getTree(@Nullable String studyId, String fileId, pathRegex.append("[\\/]?$"); Query query = new Query(FileDBAdaptor.QueryParams.PATH.key(), "~^" + file.getPath() + pathRegex.toString()); // We want to know beforehand the number of matches we will get to be able to abort before iterating - options.put(QueryOptions.COUNT, true); + queryOptions.put(QueryOptions.COUNT, true); FileTreeBuilder treeBuilder = new FileTreeBuilder(file); int numResults; - try (DBIterator iterator = fileDBAdaptor.iterator(study.getUid(), query, options, userId)) { + try (DBIterator iterator = fileDBAdaptor.iterator(study.getUid(), query, queryOptions, userId)) { if (iterator.getNumMatches() > MAX_LIMIT) { throw new CatalogException("Please, decrease the maximum depth. More than " + MAX_LIMIT + " files found"); } @@ -1321,17 +1298,10 @@ public OpenCGAResult getTree(@Nullable String studyId, String fileId, } } FileTree fileTree = treeBuilder.toFileTree(); - int dbTime = (int) (System.currentTimeMillis() - startTime); - - auditManager.audit(userId, Enums.Action.TREE, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + int dbTime = (int) stopWatch.getTime(TimeUnit.MILLISECONDS); return new OpenCGAResult<>(dbTime, Collections.emptyList(), numResults, Collections.singletonList(fileTree), numResults); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.TREE, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult getFilesFromFolder(String folderStr, String studyStr, QueryOptions options, String sessionId) @@ -1353,86 +1323,62 @@ public OpenCGAResult getFilesFromFolder(String folderStr, String studyStr, } @Override - public DBIterator iterator(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); - - Query finalQuery = new Query(query); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); - fixQueryObject(study, finalQuery, userId); - finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.ITERATE, FILE, studyStr, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + AnnotationUtils.fixQueryOptionAnnotation(queryOptions); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return fileDBAdaptor.iterator(study.getUid(), query, options, userId); + return fileDBAdaptor.iterator(study.getUid(), finalQuery, queryOptions, userId); + }); } @Override public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - Query finalQuery = new Query(query); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", new Query(query)) .append("options", options) .append("token", token); - try { + + return run(auditParams, Enums.Action.SEARCH, FILE, studyId, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); + AnnotationUtils.fixQueryOptionAnnotation(queryOptions); fixQueryObject(study, finalQuery, userId); finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = fileDBAdaptor.get(study.getUid(), finalQuery, options, userId); - auditManager.auditSearch(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return fileDBAdaptor.get(study.getUid(), finalQuery, queryOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); - - query.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = fileDBAdaptor.distinct(study.getUid(), field, query, userId); - auditManager.auditDistinct(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.DISTINCT, FILE, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + // Fix query if it contains any annotation + fixQueryObject(study, finalQuery, userId); + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return fileDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } void fixQueryObject(Study study, Query query, String user) throws CatalogException { @@ -1483,35 +1429,22 @@ private void validateQueryPath(Query query, String key) { @Override public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", new Query(query)) .append("token", token); - try { - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); - // The samples introduced could be either ids or names. As so, we should use the smart resolutor to do this. - fixQueryObject(study, query, userId); - query.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResultAux = fileDBAdaptor.count(query, userId); + return run(auditParams, Enums.Action.COUNT, FILE, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - auditManager.auditCount(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + // The samples introduced could be either ids or names. As so, we should use the smart resolutor to do this. + fixQueryObject(study, finalQuery, userId); - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return fileDBAdaptor.count(finalQuery, userId); + }); } @Override @@ -1521,12 +1454,6 @@ public OpenCGAResult delete(String studyStr, List fileIds, QueryOptions public OpenCGAResult delete(String studyStr, List fileIds, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("fileIds", fileIds) @@ -1534,54 +1461,52 @@ public OpenCGAResult delete(String studyStr, List fileIds, ObjectMap par .append("ignoreException", ignoreException) .append("token", token); - // We need to avoid processing subfolders or subfiles of an already processed folder independently - Set processedPaths = new HashSet<>(); - boolean physicalDelete = params.getBoolean(Constants.SKIP_TRASH, false); + return runBatch(auditParams, Enums.Action.DELETE, FILE, studyStr, token, null, (study, userId, queryOptions, operationUuid) -> { + // We need to avoid processing subfolders or subfiles of an already processed folder independently + Set processedPaths = new HashSet<>(); + boolean physicalDelete = params.getBoolean(Constants.SKIP_TRASH, false); - auditManager.initAuditBatch(operationUuid); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : fileIds) { - String fileId = id; - String fileUuid = ""; + OpenCGAResult result = OpenCGAResult.empty(File.class); + for (String id : fileIds) { + try { + run(auditParams, Enums.Action.DELETE, FILE, operationUuid, study, userId, null, + (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_FILE_URI_PATH, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("File '" + id + "' not found"); + } + File file = internalResult.first(); + // We set the proper values for the audit - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_FILE_URI_PATH, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("File '" + id + "' not found"); - } - File file = internalResult.first(); - // We set the proper values for the audit - fileId = file.getId(); - fileUuid = file.getUuid(); + if (subpathInPath(file.getPath(), processedPaths)) { + // We skip this folder because it is a subfolder or subfile within an already processed folder + return null; + } - if (subpathInPath(file.getPath(), processedPaths)) { - // We skip this folder because it is a subfolder or subfile within an already processed folder - continue; - } + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + OpenCGAResult updateResult = delete(study, file, physicalDelete, userId); - OpenCGAResult updateResult = delete(study, file, physicalDelete, userId); - result.append(updateResult); + result.append(updateResult); - // We store the processed path as is - if (file.getType() == File.Type.DIRECTORY) { - processedPaths.add(file.getPath()); - } + // We store the processed path as is + if (file.getType() == File.Type.DIRECTORY) { + processedPaths.add(file.getPath()); + } - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, fileId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + return updateResult; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error("Could not delete file {}: {}", fileId, e.getMessage(), e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FILE, fileId, fileUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error("Could not delete file {}: {}", id, e.getMessage(), e); + } } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -1591,88 +1516,69 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - params = ParamUtils.defaultObject(params, ObjectMap::new); - - OpenCGAResult dataResult = OpenCGAResult.empty(); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - StopWatch watch = StopWatch.createStarted(); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) - .append("query", new Query(query)) + .append("query", query) .append("params", params) .append("ignoreException", ignoreException) .append("token", token); - // We try to get an iterator containing all the files to be deleted - DBIterator fileIterator; - try { + StopWatch watch = StopWatch.createStarted(); + return runBatch(auditParams, Enums.Action.DELETE, FILE, studyStr, token, null, (study, userId, queryOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + ObjectMap finalParams = ParamUtils.defaultObject(params, ObjectMap::new); + OpenCGAResult dataResult = OpenCGAResult.empty(File.class); + // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); fixQueryObject(study, finalQuery, userId); finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + // We try to get an iterator containing all the files to be deleted + try (DBIterator fileIterator = fileDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_FILE_URI_PATH, userId)) { - fileIterator = fileDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_FILE_URI_PATH, userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FILE, "", "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - throw e; - } - - // We need to avoid processing subfolders or subfiles of an already processed folder independently - Set processedPaths = new HashSet<>(); - boolean physicalDelete = params.getBoolean(Constants.SKIP_TRASH, false); - - long numMatches = 0; - - auditManager.initAuditBatch(operationUuid); - while (fileIterator.hasNext()) { - File file = fileIterator.next(); + // We need to avoid processing subfolders or subfiles of an already processed folder independently + Set processedPaths = new HashSet<>(); + boolean physicalDelete = finalParams.getBoolean(Constants.SKIP_TRASH, false); + while (fileIterator.hasNext()) { + File file = fileIterator.next(); - if (subpathInPath(file.getPath(), processedPaths)) { - // We skip this folder because it is a subfolder or subfile within an already processed folder - continue; - } - - try { - OpenCGAResult result = delete(study, file, physicalDelete, userId); - dataResult.append(result); - - // We store the processed path as is - if (file.getType() == File.Type.DIRECTORY) { - processedPaths.add(file.getPath()); - } + if (subpathInPath(file.getPath(), processedPaths)) { + // We skip this folder because it is a subfolder or subfile within an already processed folder + continue; + } - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg; + try { + run(auditParams, Enums.Action.DELETE, FILE, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + OpenCGAResult result = delete(study, file, physicalDelete, userId); + dataResult.append(result); + + // We store the processed path as is + if (file.getType() == File.Type.DIRECTORY) { + processedPaths.add(file.getPath()); + } + return null; + }); + } catch (CatalogException e) { + String errorMsg; + + if (file.getType() == File.Type.FILE) { + errorMsg = "Cannot delete file " + file.getPath() + ": " + e.getMessage(); + } else { + errorMsg = "Cannot delete folder " + file.getPath() + ": " + e.getMessage(); + } + dataResult.getEvents().add(new Event(Event.Type.ERROR, file.getPath(), e.getMessage())); + dataResult.setNumErrors(dataResult.getNumErrors() + 1); - if (file.getType() == File.Type.FILE) { - errorMsg = "Cannot delete file " + file.getPath() + ": " + e.getMessage(); - } else { - errorMsg = "Cannot delete folder " + file.getPath() + ": " + e.getMessage(); + logger.error(errorMsg, e); + } } - dataResult.getEvents().add(new Event(Event.Type.ERROR, file.getPath(), e.getMessage())); - dataResult.setNumErrors(dataResult.getNumErrors() + 1); - - logger.error(errorMsg, e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationUuid); - - dataResult.setTime((int) watch.getTime(TimeUnit.MILLISECONDS)); - dataResult.setNumMatches(dataResult.getNumMatches() + numMatches); - return endResult(dataResult, ignoreException); + dataResult.setTime((int) watch.getTime(TimeUnit.MILLISECONDS)); + return endResult(dataResult, ignoreException); + }); } private OpenCGAResult delete(Study study, File file, boolean physicalDelete, String userId) @@ -1719,84 +1625,93 @@ public OpenCGAResult syncUntrackedFiles(String studyId, String folderId, P public OpenCGAResult syncUntrackedFiles(String studyId, String folderId, Predicate filter, String jobId, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - - File folder = internalGet(study.getUid(), folderId, INCLUDE_FILE_URI_PATH, userId).first(); + ObjectMap auditParams = new ObjectMap() + .append("studyId", studyId) + .append("folderId", folderId) + .append("filter", filter) + .append("jobId", jobId) + .append("token", token); - if (folder.getType() == File.Type.FILE) { - throw new CatalogException("Provided folder '" + folderId + "' is actually a file"); - } + return runBatch(auditParams, Enums.Action.SYNC, FILE, studyId, token, null, (study, userId, queryOptions, operationUuid) -> { + File folder = internalGet(study.getUid(), folderId, INCLUDE_FILE_URI_PATH, userId).first(); - authorizationManager.checkFilePermission(study.getUid(), folder.getUid(), userId, FilePermissions.WRITE); + if (folder.getType() == File.Type.FILE) { + throw new CatalogException("Provided folder '" + folderId + "' is actually a file"); + } - IOManager ioManager; - try { - ioManager = ioManagerFactory.get(folder.getUri()); - } catch (IOException e) { - throw CatalogIOException.ioManagerException(folder.getUri(), e); - } - Iterator iterator = ioManager.listFilesStream(folder.getUri()).iterator(); + authorizationManager.checkFilePermission(study.getUid(), folder.getUid(), userId, FilePermissions.WRITE); - if (filter == null) { - filter = uri -> true; - } + IOManager ioManager; + try { + ioManager = ioManagerFactory.get(folder.getUri()); + } catch (IOException e) { + throw CatalogIOException.ioManagerException(folder.getUri(), e); + } + Iterator iterator = ioManager.listFilesStream(folder.getUri()).iterator(); - long numMatches = 0; + Predicate finalFilter = filter != null ? filter : uri -> true; - OpenCGAResult result = OpenCGAResult.empty(); - List fileList = new ArrayList<>(); - List eventList = new ArrayList<>(); - while (iterator.hasNext()) { - URI fileUri = iterator.next().normalize(); + long numMatches = 0; - numMatches++; + OpenCGAResult result = OpenCGAResult.empty(File.class); + List fileList = new ArrayList<>(); + List eventList = new ArrayList<>(); + while (iterator.hasNext()) { + URI fileUri = iterator.next().normalize(); - if (!filter.test(fileUri)) { - continue; - } + numMatches++; - String relativeFilePath = folder.getUri().relativize(fileUri).getPath(); - String finalCatalogPath = Paths.get(folder.getPath()).resolve(relativeFilePath).toString(); - if (relativeFilePath.endsWith("/") && !finalCatalogPath.endsWith("/")) { - finalCatalogPath += "/"; - } + if (!finalFilter.test(fileUri)) { + continue; + } - try { - File registeredFile = internalGet(study.getUid(), finalCatalogPath, INCLUDE_FILE_URI_PATH, userId).first(); - if (!registeredFile.getUri().equals(fileUri)) { - eventList.add(new Event(Event.Type.WARNING, registeredFile.getPath(), "The uri registered in Catalog '" - + registeredFile.getUri().getPath() + "' for the path does not match the uri that would have been synced '" - + fileUri.getPath() + "'")); + String relativeFilePath = folder.getUri().relativize(fileUri).getPath(); + String tmpCatalogPath = Paths.get(folder.getPath()).resolve(relativeFilePath).toString(); + String finalCatalogPath; + if (relativeFilePath.endsWith("/") && !tmpCatalogPath.endsWith("/")) { + finalCatalogPath = tmpCatalogPath + "/"; + } else { + finalCatalogPath = tmpCatalogPath; } - fileList.add(registeredFile); - } catch (CatalogException e) { - File file = registerFile(study, finalCatalogPath, fileUri, jobId, token).first(); - result.setNumInserted(result.getNumInserted() + 1); - fileList.add(file); + try { + File registeredFile = internalGet(study.getUid(), finalCatalogPath, INCLUDE_FILE_URI_PATH, userId).first(); + if (!registeredFile.getUri().equals(fileUri)) { + eventList.add(new Event(Event.Type.WARNING, registeredFile.getPath(), "The uri registered in Catalog '" + + registeredFile.getUri().getPath() + "' for the path does not match the uri that would have been synced '" + + fileUri.getPath() + "'")); + } + fileList.add(registeredFile); + } catch (CatalogException e) { + run(auditParams, Enums.Action.SYNC, FILE, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(finalCatalogPath.replace("/", ":")); + File file = registerFile(study, finalCatalogPath, fileUri, jobId, token).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + + result.setNumInserted(result.getNumInserted() + 1); + fileList.add(file); + return null; + }); + } } - } - result.setNumMatches(numMatches); - result.setEvents(eventList); - result.setResults(fileList); - result.setNumResults(fileList.size()); + result.setNumMatches(numMatches); + result.setEvents(eventList); + result.setResults(fileList); + result.setNumResults(fileList.size()); - return result; + return result; + }); } public OpenCGAResult unlink(@Nullable String studyId, String fileId, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("file", fileId) .append("token", token); - try { + return run(auditParams, Enums.Action.UNLINK, FILE, studyId, token, null, (study, userId, rp, queryOptions) -> { ParamUtils.checkParameter(fileId, "File"); - File file = internalGet(study.getUid(), fileId, QueryOptions.empty(), userId).first(); if (!file.isExternal()) { @@ -1805,17 +1720,8 @@ public OpenCGAResult unlink(@Nullable String studyId, String fileId, Strin // Check if the file or the folder plus any nested files/folders can be deleted checkCanDeleteFile(study, file.getPath(), true, Collections.singletonList(FileStatus.PENDING_DELETE), userId); - - OpenCGAResult result = unlink(file); - auditManager.audit(userId, Enums.Action.UNLINK, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.UNLINK, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return unlink(file); + }); } /** @@ -1943,18 +1849,12 @@ public OpenCGAResult update(String studyStr, Query query, FileUpdateParams public OpenCGAResult update(String studyStr, Query query, FileUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse FileUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("query", query) @@ -1963,53 +1863,41 @@ public OpenCGAResult update(String studyStr, Query query, FileUpdateParams .append("options", options) .append("token", token); - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); + return runBatch(auditParams, Enums.Action.UPDATE, FILE, studyStr, token, options, (study, userId, queryOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - DBIterator iterator; - try { // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); fixQueryObject(study, finalQuery, userId); finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = fileDBAdaptor.iterator(study.getUid(), finalQuery, EXCLUDE_FILE_ATTRIBUTES, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.FILE, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - File file = iterator.next(); - try { - OpenCGAResult updateResult = update(study, file, updateParams, options, userId, token); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, file.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update file {}: {}", file.getId(), e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + OpenCGAResult result = OpenCGAResult.empty(File.class); + try (DBIterator iterator = fileDBAdaptor.iterator(study.getUid(), finalQuery, EXCLUDE_FILE_ATTRIBUTES, userId)) { + while (iterator.hasNext()) { + File file = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, FILE, operationUuid, study, userId, queryOptions, (s, u, rp, qo) -> { + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + OpenCGAResult updateResult = update(study, file, updateParams, qo, userId, token); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, file.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot update file {}: {}", file.getId(), e.getMessage()); + } + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyStr, String fileId, FileUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -2024,35 +1912,18 @@ public OpenCGAResult update(String studyStr, String fileId, FileUpdatePara .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String fileUuid = ""; - try { + return run(auditParams, Enums.Action.UPDATE, FILE, studyStr, token, options, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); OpenCGAResult internalResult = internalGet(study.getUid(), fileId, EXCLUDE_FILE_ATTRIBUTES, userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("File '" + fileId + "' not found"); } File file = internalResult.first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); - // We set the proper values for the audit - fileId = file.getId(); - fileUuid = file.getUuid(); - - OpenCGAResult updateResult = update(study, file, updateParams, options, userId, token); - result.append(updateResult); - - auditManager.auditUpdate(userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, fileId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update file {}: {}", fileId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FILE, fileId, fileUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - return result; + return update(study, file, updateParams, queryOptions, userId, token); + }); } /** @@ -2074,11 +1945,6 @@ public OpenCGAResult update(String studyStr, List fileIds, FileUpd public OpenCGAResult update(String studyStr, List fileIds, FileUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -2094,40 +1960,36 @@ public OpenCGAResult update(String studyStr, List fileIds, FileUpd .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : fileIds) { - String fileId = id; - String fileUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), fileId, EXCLUDE_FILE_ATTRIBUTES, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("File '" + id + "' not found"); - } - File file = internalResult.first(); - - // We set the proper values for the audit - fileId = file.getId(); - fileUuid = file.getUuid(); + return runBatch(auditParams, Enums.Action.UPDATE, FILE, studyStr, token, options, (study, userId, queryOptions, operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(File.class); + for (String id : fileIds) { + try { + OpenCGAResult updateResult = run(auditParams, Enums.Action.UPDATE, FILE, operationUuid, study, userId, + queryOptions, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, EXCLUDE_FILE_ATTRIBUTES, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("File '" + id + "' not found"); + } + File file = internalResult.first(); - OpenCGAResult updateResult = update(study, file, updateParams, options, userId, token); - result.append(updateResult); + // We set the proper values for the audit + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); - auditManager.auditUpdate(userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + return update(study, file, updateParams, options, userId, token); + }); + result.append(updateResult); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error("Cannot update file {}: {}", fileId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.FILE, fileId, fileUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error("Cannot update file {}: {}", id, e.getMessage()); + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, File file, FileUpdateParams updateParams, QueryOptions options, String userId, @@ -2226,21 +2088,17 @@ private OpenCGAResult update(Study study, File file, FileUpdateParams upda @Deprecated public OpenCGAResult update(String studyStr, String entryStr, ObjectMap parameters, QueryOptions options, String token) throws CatalogException { - ParamUtils.checkObj(parameters, "Parameters"); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - File file = internalGet(study.getUid(), entryStr, QueryOptions.empty(), userId).first(); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("fileId", entryStr) .append("updateParams", parameters) .append("options", options) .append("token", token); - try { + + return run(auditParams, Enums.Action.UPDATE, FILE, studyStr, token, options, (study, userId, rp, queryOptions) -> { + ParamUtils.checkObj(parameters, "Parameters"); + File file = internalGet(study.getUid(), entryStr, QueryOptions.empty(), userId).first(); + // Check permissions... // Only check write annotation permissions if the user wants to update the annotation sets if (parameters.containsKey(FileDBAdaptor.QueryParams.ANNOTATION_SETS.key())) { @@ -2253,11 +2111,7 @@ public OpenCGAResult update(String studyStr, String entryStr, ObjectMap pa authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.WRITE); } - try { - ParamUtils.checkAllParametersExist(parameters.keySet().iterator(), (a) -> FileDBAdaptor.UpdateParams.getParam(a) != null); - } catch (CatalogParameterException e) { - throw new CatalogException("Could not update: " + e.getMessage(), e); - } + ParamUtils.checkAllParametersExist(parameters.keySet().iterator(), (a) -> FileDBAdaptor.UpdateParams.getParam(a) != null); // We make a query to check both if the samples exists and if the user has permissions to see them if (parameters.get(FileDBAdaptor.QueryParams.SAMPLE_IDS.key()) != null @@ -2272,15 +2126,8 @@ public OpenCGAResult update(String studyStr, String entryStr, ObjectMap pa rename(studyStr, file.getPath(), parameters.getString(FileDBAdaptor.QueryParams.NAME.key()), token); } - OpenCGAResult queryResult = unsafeUpdate(study, file, parameters, options, userId); - auditManager.auditUpdate(userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return queryResult; - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return unsafeUpdate(study, file, parameters, queryOptions, userId); + }); } OpenCGAResult unsafeUpdate(Study study, File file, ObjectMap parameters, QueryOptions options, String userId) @@ -2302,36 +2149,27 @@ OpenCGAResult unsafeUpdate(Study study, File file, ObjectMap parameters, Q } public OpenCGAResult link(String studyStr, FileLinkParams params, boolean parents, String token) throws CatalogException { - // We make two attempts to link to ensure the behaviour remains even if it is being called at the same time link from different - // threads - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("params", params) .append("parents", parents) .append("token", token); - try { - OpenCGAResult result = privateLink(study, params, parents, token); - auditManager.auditCreate(userId, Enums.Action.LINK, Enums.Resource.FILE, result.first().getId(), - result.first().getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { + + return run(auditParams, Enums.Action.LINK, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + // We make two attempts to link to ensure the behaviour remains even if it is being called at the same time link from different + // threads try { OpenCGAResult result = privateLink(study, params, parents, token); - auditManager.auditCreate(userId, Enums.Action.LINK, Enums.Resource.FILE, result.first().getId(), - result.first().getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + rp.setId(result.first().getId()); + rp.setUuid(result.first().getUuid()); + return result; + } catch (CatalogException e) { + OpenCGAResult result = privateLink(study, params, parents, token); + rp.setId(result.first().getId()); + rp.setUuid(result.first().getUuid()); return result; - } catch (CatalogException e2) { - auditManager.auditCreate(userId, Enums.Action.LINK, Enums.Resource.FILE, params.getUri(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", e2.getMessage()))); - throw new CatalogException(e2.getMessage(), e2); } - } + }); } @Deprecated @@ -2347,56 +2185,66 @@ public OpenCGAResult link(String studyStr, URI uriOrigin, String pathDesti } @Override - public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String sessionId) + public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(sessionId, "sessionId"); - - String userId = userManager.getUserId(sessionId); - Study study = studyManager.resolveId(studyStr, userId); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); + return run(auditParams, Enums.Action.RANK, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_FILES); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_FILES); + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(token, "token"); + Query finalQuery = query != null ? new Query(query) : new Query(); - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - query.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = fileDBAdaptor.rank(query, field, numResults, asc); - } + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + finalQuery.append(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = fileDBAdaptor.rank(finalQuery, field, numResults, asc); + } - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - if (fields == null || fields.size() == 0) { - throw new CatalogException("Empty fields parameter."); - } - - String userId = userManager.getUserId(sessionId); - Study study = studyManager.resolveId(studyStr, userId); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); - fixQueryObject(study, query, userId); + return run(auditParams, Enums.Action.GROUP_BY, FILE, studyStr, token, options, (study, userId, rp, queryOptions) -> { + if (fields == null || fields.size() == 0) { + throw new CatalogException("Empty fields parameter."); + } - // Add study id to the query - query.put(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + // Add study id to the query + finalQuery.put(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - // We do not need to check for permissions when we show the count of files - OpenCGAResult queryResult = fileDBAdaptor.groupBy(query, fields, options, userId); + // We do not need to check for permissions when we show the count of files + OpenCGAResult queryResult = fileDBAdaptor.groupBy(finalQuery, fields, queryOptions, userId); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } - OpenCGAResult rename(String studyStr, String fileStr, String newName, String sessionId) throws CatalogException { + OpenCGAResult rename(String studyStr, String fileStr, String newName, String token) throws CatalogException { ParamUtils.checkFileName(newName, "name"); - String userId = userManager.getUserId(sessionId); + String userId = userManager.getUserId(token); Study study = studyManager.resolveId(studyStr, userId); File file = internalGet(study.getUid(), fileStr, EXCLUDE_FILE_ATTRIBUTES, userId).first(); @@ -2457,11 +2305,6 @@ OpenCGAResult rename(String studyStr, String fileStr, String newName, Stri public OpenCGAResult grep(String studyId, String fileId, String pattern, boolean ignoreCase, int numLines, String token) throws CatalogException { - long startTime = System.currentTimeMillis(); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("fileId", fileId) @@ -2469,8 +2312,13 @@ public OpenCGAResult grep(String studyId, String fileId, String pat .append("ignoreCase", ignoreCase) .append("numLines", numLines) .append("token", token); - try { + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.GREP, FILE, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); File file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.VIEW_CONTENT); URI fileUri = getUri(file); @@ -2481,31 +2329,23 @@ public OpenCGAResult grep(String studyId, String fileId, String pat throw CatalogIOException.ioManagerException(fileUri, e); } - auditManager.audit(userId, Enums.Action.GREP, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), 1, + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), 1, Collections.singletonList(fileContent), 1); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.GREP, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult image(String studyStr, String fileId, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - long startTime = System.currentTimeMillis(); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("fileId", fileId) .append("token", token); - File file; - try { - file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI_PATH, userId).first(); + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.IMAGE_CONTENT, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); + File file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI_PATH, userId).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.VIEW_CONTENT); if (file.getFormat() != File.Format.IMAGE) { @@ -2520,33 +2360,26 @@ public OpenCGAResult image(String studyStr, String fileId, String t } catch (IOException e) { throw CatalogIOException.ioManagerException(fileUri, e); } - auditManager.audit(userId, Enums.Action.IMAGE_CONTENT, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), 1, + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), 1, Collections.singletonList(fileContent), 1); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.IMAGE_CONTENT, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult head(String studyStr, String fileId, long offset, int lines, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - long startTime = System.currentTimeMillis(); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("fileId", fileId) .append("offset", offset) .append("lines", lines) .append("token", token); - File file; - try { - file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.HEAD_CONTENT, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); + File file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.VIEW_CONTENT); URI fileUri = getUri(file); FileContent fileContent; @@ -2555,32 +2388,25 @@ public OpenCGAResult head(String studyStr, String fileId, long offs } catch (IOException e) { throw CatalogIOException.ioManagerException(fileUri, e); } - auditManager.audit(userId, Enums.Action.HEAD_CONTENT, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), 1, + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), 1, Collections.singletonList(fileContent), 1); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.HEAD_CONTENT, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult tail(String studyStr, String fileId, int lines, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - long startTime = System.currentTimeMillis(); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("fileId", fileId) .append("lines", lines) .append("token", token); - File file; - try { - file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.TAIL_CONTENT, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); + File file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.VIEW_CONTENT); URI fileUri = getUri(file); FileContent fileContent; @@ -2589,16 +2415,10 @@ public OpenCGAResult tail(String studyStr, String fileId, int lines } catch (IOException e) { throw CatalogIOException.ioManagerException(fileUri, e); } - auditManager.audit(userId, Enums.Action.TAIL_CONTENT, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), 1, + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), 1, Collections.singletonList(fileContent), 1); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.TAIL_CONTENT, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public DataInputStream download(String studyStr, String fileId, String token) throws CatalogException { @@ -2606,35 +2426,26 @@ public DataInputStream download(String studyStr, String fileId, String token) th } public DataInputStream download(String studyStr, String fileId, int start, int limit, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("fileId", fileId) .append("start", start) .append("limit", limit) .append("token", token); - File file; - try { - file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + + return run(auditParams, Enums.Action.DOWNLOAD, FILE, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(fileId); + File file = internalGet(study.getUid(), fileId, INCLUDE_FILE_URI, userId).first(); + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); authorizationManager.checkFilePermission(study.getUid(), file.getUid(), userId, FilePermissions.DOWNLOAD); URI fileUri = getUri(file); - DataInputStream dataInputStream; try { - dataInputStream = ioManagerFactory.get(fileUri).getFileObject(fileUri, start, limit); + return ioManagerFactory.get(fileUri).getFileObject(fileUri, start, limit); } catch (IOException e) { throw CatalogIOException.ioManagerException(fileUri, e); } - - auditManager.audit(userId, Enums.Action.DOWNLOAD, Enums.Resource.FILE, file.getId(), file.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return dataInputStream; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.DOWNLOAD, Enums.Resource.FILE, fileId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } // ************************** ACLs ******************************** // @@ -2645,10 +2456,6 @@ public OpenCGAResult> getAcls(String studyId, List public OpenCGAResult> getAcls(String studyId, List fileList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("fileList", fileList) @@ -2656,11 +2463,10 @@ public OpenCGAResult> getAcls(String studyId, List .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> fileAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), fileList, INCLUDE_FILE_IDS, user, ignoreException); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, FILE, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult> fileAcls; + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), fileList, INCLUDE_FILE_IDS, userId, ignoreException); if (queryResult.getMissing() != null) { missingMap = queryResult.getMissing().stream() @@ -2669,9 +2475,9 @@ public OpenCGAResult> getAcls(String studyId, List List fileUids = queryResult.getResults().stream().map(File::getUid).collect(Collectors.toList()); if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(members)) { - fileAcls = authorizationManager.getAcl(user, study.getUid(), fileUids, members, Enums.Resource.FILE, FilePermissions.class); + fileAcls = authorizationManager.getAcl(userId, study.getUid(), fileUids, members, FILE, FilePermissions.class); } else { - fileAcls = authorizationManager.getAcl(user, study.getUid(), fileUids, Enums.Resource.FILE, FilePermissions.class); + fileAcls = authorizationManager.getAcl(userId, study.getUid(), fileUids, FILE, FilePermissions.class); } // Include non-existing samples to the result list @@ -2681,49 +2487,32 @@ public OpenCGAResult> getAcls(String studyId, List for (String fileId : fileList) { if (!missingMap.containsKey(fileId)) { File file = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, FILE, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + return null; + }); resultList.add(fileAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.FILE, file.getId(), - file.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); counter++; } else { + if (!ignoreException) { + throw new CatalogException(missingMap.get(fileId).getErrorMsg()); + } resultList.add(new AclEntryList<>()); eventList.add(new Event(Event.Type.ERROR, fileId, missingMap.get(fileId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.FILE, fileId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(fileId).getErrorMsg())), new ObjectMap()); } } fileAcls.setResults(resultList); fileAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String fileId : fileList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.FILE, fileId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String fileId : fileList) { - Event event = new Event(Event.Type.ERROR, fileId, e.getMessage()); - fileAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return fileAcls; + return fileAcls; + }); } public OpenCGAResult> updateAcl(String studyId, List fileStrList, String memberList, FileAclParams aclParams, ParamUtils.AclAction action, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("fileStrList", fileStrList) @@ -2731,11 +2520,8 @@ public OpenCGAResult> updateAcl(String studyId, Li .append("aclParams", aclParams) .append("action", action) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, FILE, studyId, token, null, (study, userId, qOptions, operationUuid) -> { int count = 0; count += fileStrList != null && !fileStrList.isEmpty() ? 1 : 0; count += StringUtils.isNotEmpty(aclParams.getSample()) ? 1 : 0; @@ -2760,16 +2546,16 @@ public OpenCGAResult> updateAcl(String studyId, Li if (StringUtils.isNotEmpty(aclParams.getSample())) { // Obtain the sample ids OpenCGAResult sampleDataResult = catalogManager.getSampleManager().internalGet(study.getUid(), - Arrays.asList(StringUtils.split(aclParams.getSample(), ",")), SampleManager.INCLUDE_SAMPLE_IDS, user, false); + Arrays.asList(StringUtils.split(aclParams.getSample(), ",")), SampleManager.INCLUDE_SAMPLE_IDS, userId, false); Query query = new Query(FileDBAdaptor.QueryParams.SAMPLE_IDS.key(), sampleDataResult.getResults().stream().map(Sample::getId).collect(Collectors.toList())); extendedFileList = catalogManager.getFileManager().search(studyId, query, EXCLUDE_FILE_ATTRIBUTES, token).getResults(); } else { - extendedFileList = internalGet(study.getUid(), fileStrList, EXCLUDE_FILE_ATTRIBUTES, user, false).getResults(); + extendedFileList = internalGet(study.getUid(), fileStrList, EXCLUDE_FILE_ATTRIBUTES, userId, false).getResults(); } - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), user); + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); // Increase the list with the files/folders within the list of ids that correspond with folders extendedFileList = getRecursiveFilesAndFolders(study.getUid(), extendedFileList); @@ -2786,10 +2572,9 @@ public OpenCGAResult> updateAcl(String studyId, Li List fileUids = extendedFileList.stream().map(File::getUid).collect(Collectors.toList()); AuthorizationManager.CatalogAclParams catalogAclParams = new AuthorizationManager.CatalogAclParams(fileUids, permissions, - Enums.Resource.FILE); + FILE); // studyManager.membersHavePermissionsInStudy(resourceIds.getStudyId(), members); - OpenCGAResult> queryResultList; switch (action) { case SET: authorizationManager.setAcls(study.getUid(), members, catalogAclParams); @@ -2808,27 +2593,17 @@ public OpenCGAResult> updateAcl(String studyId, Li throw new CatalogException("Unexpected error occurred. No valid action found."); } - queryResultList = authorizationManager.getAcls(study.getUid(), fileUids, members, Enums.Resource.FILE, - FilePermissions.class); - for (File file : extendedFileList) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.FILE, file.getId(), - file.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + // To audit + run(auditParams, Enums.Action.UPDATE_ACLS, FILE, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(file.getId()); + rp.setUuid(file.getUuid()); + return null; + }); } - return queryResultList; - } catch (CatalogException e) { - if (fileStrList != null) { - for (String fileId : fileStrList) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.FILE, fileId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - e.getError()), new ObjectMap()); - } - } - throw e; - } finally { - auditManager.finishAuditBatch(operationId); - } + + return authorizationManager.getAcls(study.getUid(), fileUids, members, FILE, FilePermissions.class); + }); } public OpenCGAResult getParents(String studyStr, String path, boolean rootFirst, QueryOptions options, String token) @@ -3174,42 +2949,30 @@ void checkValidStatusForDeletion(File file, List expectedStatus) throws } public DataResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) - throws CatalogException, IOException { - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - // We need to add variableSets and groups to avoid additional queries as it will be used in the catalogSolrManager - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()))); - + throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("defaultStats", defaultStats) .append("token", token); - try { - if (defaultStats || StringUtils.isEmpty(options.getString(QueryOptions.FACET))) { - String facet = options.getString(QueryOptions.FACET); - options.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); - } - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); + return run(auditParams, Enums.Action.FACET, FILE, studyId, token, options, + Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()), + (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + if (defaultStats || StringUtils.isEmpty(qOptions.getString(QueryOptions.FACET))) { + String facet = qOptions.getString(QueryOptions.FACET); + qOptions.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); + } - try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { - DataResult result = catalogSolrManager.facetedQuery(study, CatalogSolrManager.FILE_SOLR_COLLECTION, query, - options, userId); - auditManager.auditFacet(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); - return result; - } - } catch (CatalogException e) { - auditManager.auditFacet(userId, Enums.Resource.FILE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage()))); - throw e; - } + try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { + return catalogSolrManager.facetedQuery(study, CatalogSolrManager.FILE_SOLR_COLLECTION, finalQuery, qOptions, + userId); + } + }); } /** @@ -3266,7 +3029,7 @@ private void createParents(Study study, String userId, URI studyURI, Path path, long parentFileId = fileDBAdaptor.getId(study.getUid(), parentPath); // We obtain the permissions set in the parent folder and set them to the file or folder being created OpenCGAResult> allFileAcls = - authorizationManager.getAcls(study.getUid(), parentFileId, Enums.Resource.FILE, FilePermissions.class); + authorizationManager.getAcls(study.getUid(), parentFileId, FILE, FilePermissions.class); URI completeURI = Paths.get(studyURI).resolve(path).toUri(); @@ -3283,7 +3046,7 @@ private void createParents(Study study, String userId, URI studyURI, Path path, // Propagate ACLs if (allFileAcls != null && allFileAcls.getNumResults() > 0) { authorizationManager.replicateAcls(Collections.singletonList(queryResult.first().getUid()), allFileAcls.getResults().get(0), - Enums.Resource.FILE); + FILE); } } @@ -3471,7 +3234,7 @@ public FileVisitResult preVisitDirectory(URI dir, BasicFileAttributes attrs) thr // We obtain the permissions set in the parent folder and set them to the file or folder being created OpenCGAResult> allFileAcls; try { - allFileAcls = authorizationManager.getAcls(study.getUid(), parentFileId, Enums.Resource.FILE, + allFileAcls = authorizationManager.getAcls(study.getUid(), parentFileId, FILE, FilePermissions.class); } catch (CatalogException e) { throw new RuntimeException(e); @@ -3493,7 +3256,7 @@ public FileVisitResult preVisitDirectory(URI dir, BasicFileAttributes attrs) thr // Propagate ACLs if (allFileAcls != null && allFileAcls.getNumResults() > 0) { authorizationManager.replicateAcls(Collections.singletonList(queryResult.first().getUid()), - allFileAcls.getResults().get(0), Enums.Resource.FILE); + allFileAcls.getResults().get(0), FILE); } } } catch (CatalogException e) { @@ -3525,7 +3288,7 @@ public FileVisitResult visitFile(URI fileUri, BasicFileAttributes attrs) throws // We obtain the permissions set in the parent folder and set them to the file or folder being created OpenCGAResult> allFileAcls; try { - allFileAcls = authorizationManager.getAcls(study.getUid(), parentFileId, Enums.Resource.FILE, + allFileAcls = authorizationManager.getAcls(study.getUid(), parentFileId, FILE, FilePermissions.class); } catch (CatalogException e) { throw new RuntimeException(e); @@ -3560,7 +3323,7 @@ public FileVisitResult visitFile(URI fileUri, BasicFileAttributes attrs) throws // Propagate ACLs if (allFileAcls != null && allFileAcls.getNumResults() > 0) { authorizationManager.replicateAcls(Collections.singletonList(subfile.getUid()), allFileAcls.getResults().get(0), - Enums.Resource.FILE); + FILE); } if (isTransformedFile(subfile.getName())) { @@ -3626,7 +3389,7 @@ OpenCGAResult registerFile(Study study, String filePath, URI fileUri, Stri File parentFile = internalGet(study.getUid(), parentPath, INCLUDE_FILE_URI_PATH, userId).first(); // We obtain the permissions set in the parent folder and set them to the file or folder being created OpenCGAResult> allFileAcls = - authorizationManager.getAcls(study.getUid(), parentFile.getUid(), Enums.Resource.FILE, FilePermissions.class); + authorizationManager.getAcls(study.getUid(), parentFile.getUid(), FILE, FilePermissions.class); File.Type type = filePath.endsWith("/") ? File.Type.DIRECTORY : File.Type.FILE; @@ -3651,7 +3414,7 @@ OpenCGAResult registerFile(Study study, String filePath, URI fileUri, Stri // Propagate ACLs if (allFileAcls != null && allFileAcls.getNumResults() > 0) { authorizationManager.replicateAcls(Collections.singletonList(subfile.getUid()), allFileAcls.getResults().get(0), - Enums.Resource.FILE); + FILE); } // If it is a transformed file, we will try to link it with the correspondent original file @@ -3935,7 +3698,7 @@ private CheckPath checkPathExists(String path, long studyId) throws CatalogExcep Query query = new Query() .append(FileDBAdaptor.QueryParams.STUDY_UID.key(), studyId) .append(FileDBAdaptor.QueryParams.PATH.key(), myPath); - OpenCGAResult fileDataResult = fileDBAdaptor.count(query); + OpenCGAResult fileDataResult = fileDBAdaptor.count(query); if (fileDataResult.getNumMatches() > 0) { return CheckPath.FILE_EXISTS; } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/IndividualManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/IndividualManager.java index 4ab8df0488f..3faf9ab9517 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/IndividualManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/IndividualManager.java @@ -19,12 +19,12 @@ import com.fasterxml.jackson.core.JsonProcessingException; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.StopWatch; import org.opencb.biodata.models.common.Status; import org.opencb.biodata.models.core.OntologyTermAnnotation; import org.opencb.biodata.models.core.SexOntologyTermAnnotation; import org.opencb.biodata.models.pedigree.IndividualProperty; import org.opencb.commons.datastore.core.*; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -41,7 +41,6 @@ import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.common.AnnotationSet; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.family.Family; @@ -56,12 +55,13 @@ import org.slf4j.LoggerFactory; import javax.annotation.Nullable; -import java.io.IOException; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; +import static org.opencb.opencga.core.models.common.Enums.Resource.INDIVIDUAL; /** * Created by hpccoll1 on 19/06/15. @@ -110,8 +110,8 @@ public class IndividualManager extends AnnotationSetManager { } @Override - Enums.Resource getEntity() { - return Enums.Resource.INDIVIDUAL; + Enums.Resource getResource() { + return INDIVIDUAL; } // @Override @@ -310,37 +310,29 @@ public OpenCGAResult create(String studyStr, Individual individual, public OpenCGAResult create(String studyStr, Individual individual, List sampleIds, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("individual", individual) .append("sampleIds", sampleIds) .append("options", options) .append("token", token); - try { + + return run(auditParams, Enums.Action.CREATE, INDIVIDUAL, studyStr, token, options, (study, userId, rp, qOptions) -> { + rp.setId(individual != null ? individual.getId() : ""); authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_INDIVIDUALS); validateNewIndividual(study, individual, sampleIds, userId, true); + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); // Create the individual - OpenCGAResult insert = individualDBAdaptor.insert(study.getUid(), individual, study.getVariableSets(), options); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + OpenCGAResult insert = individualDBAdaptor.insert(study.getUid(), individual, study.getVariableSets(), qOptions); + if (qOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch created individual - OpenCGAResult queryResult = getIndividual(study.getUid(), individual.getUuid(), options); + OpenCGAResult queryResult = getIndividual(study.getUid(), individual.getUuid(), qOptions); insert.setResults(queryResult.getResults()); } - auditManager.auditCreate(userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return insert; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.INDIVIDUAL, individual.getId(), "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } private void checkSamplesNotInUseInOtherIndividual(Set sampleIds, long studyId, Long individualId) @@ -376,131 +368,85 @@ private void checkSamplesNotInUseInOtherIndividual(Set sampleIds, long stu } @Override - public DBIterator iterator(String studyStr, Query query, QueryOptions options, String sessionId) throws CatalogException { - ParamUtils.checkObj(sessionId, "sessionId"); - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); + public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("options", options) + .append("token", token); - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + return run(auditParams, Enums.Action.ITERATE, INDIVIDUAL, studyStr, token, options, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - Query finalQuery = new Query(query); - fixQuery(study, finalQuery, userId); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); - finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + fixQuery(study, finalQuery, userId); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + AnnotationUtils.fixQueryOptionAnnotation(qOptions); + finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return individualDBAdaptor.iterator(study.getUid(), finalQuery, options, userId); + return individualDBAdaptor.iterator(study.getUid(), finalQuery, qOptions, userId); + }); } @Override public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - Query finalQuery = new Query(query); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("token", token); - try { - fixQuery(study, finalQuery, userId); + return run(auditParams, Enums.Action.SEARCH, INDIVIDUAL, studyId, token, options, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQuery(study, finalQuery, userId); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - AnnotationUtils.fixQueryOptionAnnotation(options); - + AnnotationUtils.fixQueryOptionAnnotation(qOptions); finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = individualDBAdaptor.get(study.getUid(), finalQuery, options, userId); - - auditManager.auditSearch(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return individualDBAdaptor.get(study.getUid(), finalQuery, qOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - try { - fixQuery(study, query, userId); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, query); - - query.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = individualDBAdaptor.distinct(study.getUid(), field, query, userId); - auditManager.auditDistinct(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.DISTINCT, INDIVIDUAL, studyId, token, null, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQuery(study, finalQuery, userId); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); + finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return individualDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } @Override public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - Query finalQuery = new Query(query); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", query) .append("token", token); - try { + + return run(auditParams, Enums.Action.COUNT, INDIVIDUAL, studyId, token, null, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); fixQuery(study, finalQuery, userId); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResultAux = individualDBAdaptor.count(finalQuery, userId); - - auditManager.auditCount(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return individualDBAdaptor.count(finalQuery, userId); + }); } public OpenCGAResult relatives(String studyId, String individualId, int degree, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("individualId", individualId) @@ -508,43 +454,28 @@ public OpenCGAResult relatives(String studyId, String individualId, .append("options", options) .append("token", token); - String individualUuid = individualId; - try { - long startTime = System.currentTimeMillis(); - - QueryOptions queryOptions = individualDBAdaptor.fixOptionsForRelatives(options); - + StopWatch stopWatch = StopWatch.createStarted(); + QueryOptions queryOptions = individualDBAdaptor.fixOptionsForRelatives(options); + return run(auditParams, Enums.Action.RELATIVES, INDIVIDUAL, studyId, token, null, (study, userId, rp, qo) -> { + rp.setId(individualId); if (degree < 0 || degree > 2) { throw new CatalogException("Unsupported degree value. Degree must be 0, 1 or 2"); } List individualList = new LinkedList<>(); Individual proband = internalGet(study.getUid(), individualId, queryOptions, userId).first(); + rp.setId(proband.getId()); + rp.setUuid(proband.getUuid()); individualDBAdaptor.addRelativeToList(proband, Family.FamiliarRelationship.PROBAND, 0, individualList); - individualId = proband.getId(); - individualUuid = proband.getUuid(); - if (degree == 0) { - auditManager.audit(userId, Enums.Action.RELATIVES, Enums.Resource.INDIVIDUAL, individualId, individualUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), individualList.size(), + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), individualList.size(), individualList, individualList.size()); } - individualList.addAll(individualDBAdaptor.calculateRelationship(study.getUid(), proband, degree, userId)); - - auditManager.audit(userId, Enums.Action.RELATIVES, Enums.Resource.INDIVIDUAL, individualId, individualUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), individualList.size(), + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), individualList.size(), individualList, individualList.size()); - - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.RELATIVES, Enums.Resource.INDIVIDUAL, individualId, individualUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - + }); } @Override @@ -554,12 +485,6 @@ public OpenCGAResult delete(String studyStr, List individualIds, QueryOp public OpenCGAResult delete(String studyStr, List individualIds, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("individualIds", individualIds) @@ -567,55 +492,42 @@ public OpenCGAResult delete(String studyStr, List individualIds, ObjectM .append("ignoreException", ignoreException) .append("token", token); - boolean checkPermissions; - try { + return runBatch(auditParams, Enums.Action.DELETE, INDIVIDUAL, studyStr, token, null, (study, userId, qOptions, operationUuid) -> { // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.INDIVIDUAL, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : individualIds) { - String individualId = id; - String individualUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_INDIVIDUAL_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Individual '" + id + "' not found"); + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Individual.class); + for (String id : individualIds) { + try { + run(auditParams, Enums.Action.DELETE, INDIVIDUAL, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_INDIVIDUAL_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Individual '" + id + "' not found"); + } + Individual individual = internalResult.first(); + // We set the proper values for the audit + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); + + OpenCGAResult deleteResult = delete(study, individual, params, userId, checkPermissions); + + // Add the results to the current write result + result.append(deleteResult); + return null; + }); + } catch (CatalogException e) { + String errorMsg = "Cannot delete individual " + id + ": " + e.getMessage(); + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error(errorMsg, e); } - - Individual individual = internalResult.first(); - // We set the proper values for the audit - individualId = individual.getId(); - individualUuid = individual.getUuid(); - - OpenCGAResult deleteResult = delete(study, individual, params, userId, checkPermissions); - - // Add the results to the current write result - result.append(deleteResult); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete individual " + individualId + ": " + e.getMessage(); - - Event event = new Event(Event.Type.ERROR, individualId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error(errorMsg, e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.INDIVIDUAL, individualId, individualUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -625,71 +537,55 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) - .append("query", new Query(query)) + .append("query", query) .append("params", params) .append("ignoreException", ignoreException) .append("token", token); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; + return runBatch(auditParams, Enums.Action.DELETE, INDIVIDUAL, studyStr, token, null, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + OpenCGAResult result = OpenCGAResult.empty(Individual.class); + + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - // We try to get an iterator containing all the individuals to be deleted - DBIterator iterator; - try { // Fix query if it contains any annotation fixQuery(study, finalQuery, userId); // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = individualDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_INDIVIDUAL_IDS, userId); - - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.INDIVIDUAL, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - while (iterator.hasNext()) { - Individual individual = iterator.next(); - - try { - OpenCGAResult deleteResult = delete(study, individual, params, userId, checkPermissions); - - // Add the results to the current write result - result.append(deleteResult); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete individual " + individual.getId() + ": " + e.getMessage(); - - Event event = new Event(Event.Type.ERROR, individual.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + // We try to get an iterator containing all the individuals to be deleted + try (DBIterator iterator = individualDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_INDIVIDUAL_IDS, + userId)) { + while (iterator.hasNext()) { + Individual individual = iterator.next(); + try { + run(auditParams, Enums.Action.DELETE, INDIVIDUAL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); + OpenCGAResult deleteResult = delete(study, individual, params, userId, checkPermissions); + + // Add the results to the current write result + result.append(deleteResult); + return null; + }); + } catch (CatalogException e) { + String errorMsg = "Cannot delete individual " + individual.getId() + ": " + e.getMessage(); + + Event event = new Event(Event.Type.ERROR, individual.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error(errorMsg); + } + } - logger.error(errorMsg); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationUuid); - - return endResult(result, ignoreException); + }); } private OpenCGAResult delete(Study study, Individual individual, ObjectMap params, String userId, boolean checkPermissions) @@ -801,11 +697,6 @@ public OpenCGAResult update(String studyStr, Query query, Individual public OpenCGAResult update(String studyStr, Query query, IndividualUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -821,56 +712,44 @@ public OpenCGAResult update(String studyStr, Query query, Individual .append("options", options) .append("token", token); - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); + return runBatch(auditParams, Enums.Action.UPDATE, INDIVIDUAL, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - DBIterator iterator; - try { fixQuery(study, finalQuery, userId); - // Fix query if it contains any annotation AnnotationUtils.fixQueryAnnotationSearch(study, finalQuery); - finalQuery.append(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = individualDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_INDIVIDUAL_IDS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.INDIVIDUAL, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Individual individual = iterator.next(); - try { - OpenCGAResult updateResult = update(study, individual, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, individual.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + OpenCGAResult result = OpenCGAResult.empty(Individual.class); + try (DBIterator iterator = individualDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_INDIVIDUAL_IDS, + userId)) { + while (iterator.hasNext()) { + Individual individual = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, INDIVIDUAL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); + OpenCGAResult updateResult = update(study, individual, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, individual.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot update individual {}: {}", individual.getId(), e.getMessage(), e); + } + } - logger.error("Cannot update individual {}: {}", individual.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyStr, String individualId, IndividualUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -885,10 +764,9 @@ public OpenCGAResult update(String studyStr, String individualId, In .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String individualUuid = ""; + return run(auditParams, Enums.Action.UPDATE, INDIVIDUAL, studyStr, token, options, (study, userId, rp, qOptions) -> { + rp.setId(individualId); - try { OpenCGAResult internalResult = internalGet(study.getUid(), individualId, QueryOptions.empty(), userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Individual '" + individualId + "' not found"); @@ -896,26 +774,11 @@ public OpenCGAResult update(String studyStr, String individualId, In Individual individual = internalResult.first(); // We set the proper values for the audit - individualId = individual.getId(); - individualUuid = individual.getUuid(); - - OpenCGAResult updateResult = update(study, individual, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, individualId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update individual {}: {}", individualId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INDIVIDUAL, individualId, individualUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); - return result; + return update(study, individual, updateParams, options, userId); + }); } /** @@ -937,11 +800,6 @@ public OpenCGAResult update(String studyStr, List individual public OpenCGAResult update(String studyStr, List individualIds, IndividualUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -957,41 +815,38 @@ public OpenCGAResult update(String studyStr, List individual .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : individualIds) { - String individualId = id; - String individualUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Individual '" + id + "' not found"); + return runBatch(auditParams, Enums.Action.UPDATE, INDIVIDUAL, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Individual.class); + for (String id : individualIds) { + try { + run(auditParams, Enums.Action.UPDATE, INDIVIDUAL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Individual '" + id + "' not found"); + } + Individual individual = internalResult.first(); + + // We set the proper values for the audit + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); + + OpenCGAResult updateResult = update(study, individual, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot update individual {}: {}", id, e.getMessage()); } - Individual individual = internalResult.first(); - - // We set the proper values for the audit - individualId = individual.getId(); - individualUuid = individual.getUuid(); - - OpenCGAResult updateResult = update(study, individual, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(userId, Enums.Resource.INDIVIDUAL, individual.getId(), individual.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update individual {}: {}", individualId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INDIVIDUAL, individualId, individualUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, Individual individual, IndividualUpdateParams updateParams, QueryOptions options, @@ -1123,61 +978,69 @@ private OpenCGAResult update(Study study, Individual individual, IndividualUpdat } @Override - public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String sessionId) + public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(sessionId, "sessionId"); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + return run(auditParams, Enums.Action.RANK, INDIVIDUAL, studyStr, token, null, (study, userId, rp, qOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_INDIVIDUALS); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_INDIVIDUALS); + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(token, "sessionId"); + Query finalQuery = query != null ? new Query(query) : new Query(); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; // query.append(CatalogIndividualDBAdaptor.QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = individualDBAdaptor.rank(query, field, numResults, asc); - } + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = individualDBAdaptor.rank(finalQuery, field, numResults, asc); + } - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) 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); - - Query finalQuery = new Query(query); - - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); - AnnotationUtils.fixQueryOptionAnnotation(options); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); - try { - fixQuery(study, finalQuery, userId); - } catch (CatalogException e) { - // Any of mother, father or sample ids or names do not exist or were not found - return OpenCGAResult.empty(); - } + return run(auditParams, Enums.Action.GROUP_BY, INDIVIDUAL, studyStr, token, options, (study, userId, rp, qOptions) -> { + ParamUtils.checkObj(fields, "fields"); - // Add study id to the query - finalQuery.put(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + Query finalQuery = query != null ? new Query(query) : new Query(); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); + AnnotationUtils.fixQueryOptionAnnotation(options); + try { + fixQuery(study, finalQuery, userId); + } catch (CatalogException e) { + // Any of mother, father or sample ids or names do not exist or were not found + return OpenCGAResult.empty(); + } - OpenCGAResult queryResult = individualDBAdaptor.groupBy(finalQuery, fields, options, userId); + // Add study id to the query + finalQuery.put(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + OpenCGAResult queryResult = individualDBAdaptor.groupBy(finalQuery, fields, options, userId); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } @@ -1188,13 +1051,8 @@ public OpenCGAResult> getAcls(String studyId return getAcls(studyId, individualList, Collections.singletonList(member), ignoreException, token); } - public OpenCGAResult> getAcls(String studyId, List individualList, - List members, boolean ignoreException, - String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); + public OpenCGAResult> getAcls(String studyId, List individualList, List members, + boolean ignoreException, String token) throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("individualList", individualList) @@ -1202,11 +1060,11 @@ public OpenCGAResult> getAcls(String studyId .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> individualAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), individualList, INCLUDE_INDIVIDUAL_IDS, user, + return runBatch(auditParams, Enums.Action.FETCH_ACLS, INDIVIDUAL, studyId, token, null, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult> individualAcls; + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), individualList, INCLUDE_INDIVIDUAL_IDS, userId, ignoreException); if (queryResult.getMissing() != null) { @@ -1216,10 +1074,10 @@ public OpenCGAResult> getAcls(String studyId List individualUids = queryResult.getResults().stream().map(Individual::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - individualAcls = authorizationManager.getAcl(user, study.getUid(), individualUids, members, Enums.Resource.INDIVIDUAL, + individualAcls = authorizationManager.getAcl(userId, study.getUid(), individualUids, members, INDIVIDUAL, IndividualPermissions.class); } else { - individualAcls = authorizationManager.getAcl(user, study.getUid(), individualUids, Enums.Resource.INDIVIDUAL, + individualAcls = authorizationManager.getAcl(userId, study.getUid(), individualUids, INDIVIDUAL, IndividualPermissions.class); } @@ -1230,49 +1088,32 @@ public OpenCGAResult> getAcls(String studyId for (String individualId : individualList) { if (!missingMap.containsKey(individualId)) { Individual individual = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, INDIVIDUAL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); + return null; + }); resultList.add(individualAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.INDIVIDUAL, individual.getId(), - individual.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); counter++; } else { + if (!ignoreException) { + throw new CatalogException(missingMap.get(individualId).getErrorMsg()); + } resultList.add(new AclEntryList<>()); eventList.add(new Event(Event.Type.ERROR, individualId, missingMap.get(individualId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.INDIVIDUAL, individualId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(individualId).getErrorMsg())), new ObjectMap()); } } individualAcls.setResults(resultList); individualAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String individualId : individualList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.INDIVIDUAL, individualId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String individualId : individualList) { - Event event = new Event(Event.Type.ERROR, individualId, e.getMessage()); - individualAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return individualAcls; + return individualAcls; + }); } public OpenCGAResult> updateAcl(String studyId, List individualStrList, String memberList, IndividualAclParams aclParams, ParamUtils.AclAction action, boolean propagate, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, StudyManager.INCLUDE_STUDY_UID); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("individualStrList", individualStrList) @@ -1281,10 +1122,10 @@ public OpenCGAResult> updateAcl(String study .append("action", action) .append("propagate", propagate) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, INDIVIDUAL, studyId, token, null, (study, userId, qOptions, + operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); int count = 0; count += individualStrList != null && !individualStrList.isEmpty() ? 1 : 0; @@ -1300,6 +1141,8 @@ public OpenCGAResult> updateAcl(String study throw new CatalogException("Invalid action found. Please choose a valid action to be performed."); } + List finalIndividualList = individualStrList; + List permissions = Collections.emptyList(); if (StringUtils.isNotEmpty(aclParams.getPermissions())) { permissions = Arrays.asList(aclParams.getPermissions().trim().replaceAll("\\s", "").split(",")); @@ -1311,15 +1154,13 @@ public OpenCGAResult> updateAcl(String study QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, IndividualDBAdaptor.QueryParams.ID.key()); OpenCGAResult indDataResult = catalogManager.getIndividualManager().search(studyId, query, options, token); - individualStrList = indDataResult.getResults().stream().map(Individual::getId).collect(Collectors.toList()); + finalIndividualList = indDataResult.getResults().stream().map(Individual::getId).collect(Collectors.toList()); } // Obtain the resource ids - List individualList = internalGet(study.getUid(), individualStrList, INCLUDE_INDIVIDUAL_IDS, userId, false) + List individualList = internalGet(study.getUid(), finalIndividualList, INCLUDE_INDIVIDUAL_IDS, userId, false) .getResults(); - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); - // Validate that the members are actually valid members List members; if (memberList != null && !memberList.isEmpty()) { @@ -1332,7 +1173,7 @@ public OpenCGAResult> updateAcl(String study List individualUids = individualList.stream().map(Individual::getUid).collect(Collectors.toList()); List aclParamsList = new LinkedList<>(); - aclParamsList.add(new AuthorizationManager.CatalogAclParams(individualUids, permissions, Enums.Resource.INDIVIDUAL)); + aclParamsList.add(new AuthorizationManager.CatalogAclParams(individualUids, permissions, INDIVIDUAL)); if (propagate) { List sampleUids = getSampleUidsFromIndividuals(study.getUid(), individualUids); @@ -1359,67 +1200,47 @@ public OpenCGAResult> updateAcl(String study throw new CatalogException("Unexpected error occurred. No valid action found."); } - OpenCGAResult> queryResults = authorizationManager - .getAcls(study.getUid(), individualUids, members, Enums.Resource.INDIVIDUAL, - IndividualPermissions.class); - for (Individual individual : individualList) { - auditManager.audit(operationId, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.INDIVIDUAL, individual.getId(), - individual.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + // To audit + run(auditParams, Enums.Action.UPDATE_ACLS, INDIVIDUAL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(individual.getId()); + rp.setUuid(individual.getUuid()); + return null; + }); } - return queryResults; - } catch (CatalogException e) { - if (individualStrList != null) { - for (String individualId : individualStrList) { - auditManager.audit(operationId, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.INDIVIDUAL, individualId, - "", study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - e.getError()), new ObjectMap()); - } - } - throw e; - } finally { - auditManager.finishAuditBatch(operationId); - } + + return authorizationManager.getAcls(study.getUid(), individualUids, members, INDIVIDUAL, IndividualPermissions.class); + }); } public DataResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) - throws CatalogException, IOException { - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - // We need to add variableSets and groups to avoid additional queries as it will be used in the catalogSolrManager - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()))); - + throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("defaultStats", defaultStats) .append("token", token); - try { - if (defaultStats || StringUtils.isEmpty(options.getString(QueryOptions.FACET))) { - String facet = options.getString(QueryOptions.FACET); - options.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); - } - try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); + return run(auditParams, Enums.Action.FACET, INDIVIDUAL, studyId, token, options, + Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()), + (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - DataResult result = catalogSolrManager.facetedQuery(study, CatalogSolrManager.INDIVIDUAL_SOLR_COLLECTION, query, - options, userId); - auditManager.auditFacet(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + if (defaultStats || StringUtils.isEmpty(qOptions.getString(QueryOptions.FACET))) { + String facet = qOptions.getString(QueryOptions.FACET); + qOptions.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); + } - return result; - } - } catch (CatalogException e) { - auditManager.auditFacet(userId, Enums.Resource.INDIVIDUAL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage()))); - throw e; - } + try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); + + DataResult result = catalogSolrManager.facetedQuery(study, + CatalogSolrManager.INDIVIDUAL_SOLR_COLLECTION, query, qOptions, userId); + + return result; + } + }); } // ************************** Private methods ******************************** // diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/InterpretationManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/InterpretationManager.java index 3501218ff18..2c4d6e3870d 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/InterpretationManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/InterpretationManager.java @@ -36,7 +36,6 @@ import org.opencb.opencga.catalog.db.DBAdaptorFactory; import org.opencb.opencga.catalog.db.api.*; import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException; -import org.opencb.opencga.catalog.exceptions.CatalogDBException; import org.opencb.opencga.catalog.exceptions.CatalogException; import org.opencb.opencga.catalog.models.InternalGetDataResult; import org.opencb.opencga.catalog.utils.Constants; @@ -45,7 +44,6 @@ import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.clinical.*; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.common.StatusParam; @@ -63,6 +61,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import static org.opencb.opencga.core.models.common.Enums.Resource.INTERPRETATION; + public class InterpretationManager extends ResourceManager { public static final QueryOptions INCLUDE_CLINICAL_ANALYSIS = keepFieldsInQueryOptions(ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, @@ -92,8 +92,8 @@ public InterpretationManager(AuthorizationManager authorizationManager, AuditMan } @Override - Enums.Resource getEntity() { - return Enums.Resource.INTERPRETATION; + Enums.Resource getResource() { + return INTERPRETATION; } @Override @@ -197,10 +197,6 @@ public OpenCGAResult create(String studyStr, Interpretation entr public OpenCGAResult create(String studyStr, String clinicalAnalysisStr, Interpretation interpretation, ParamUtils.SaveInterpretationAs saveInterpretationAs, QueryOptions options, String token) throws CatalogException { - // We check if the user can create interpretations in the clinical analysis - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysis", clinicalAnalysisStr) @@ -209,40 +205,35 @@ public OpenCGAResult create(String studyStr, String clinicalAnal .append("options", options) .append("token", token); - try { - QueryOptions clinicalOptions = keepFieldsInQueryOptions(ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, - Arrays.asList(ClinicalAnalysisDBAdaptor.QueryParams.PANELS.key(), - ClinicalAnalysisDBAdaptor.QueryParams.PANEL_LOCK.key(), - ClinicalAnalysisDBAdaptor.QueryParams.AUDIT.key(), - ClinicalAnalysisDBAdaptor.QueryParams.INTERPRETATION_ID.key(), - ClinicalAnalysisDBAdaptor.QueryParams.SECONDARY_INTERPRETATIONS_ID.key())); - ClinicalAnalysis clinicalAnalysis = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), clinicalAnalysisStr, - clinicalOptions, userId).first(); - - authorizationManager.checkClinicalAnalysisPermission(study.getUid(), clinicalAnalysis.getUid(), - userId, ClinicalAnalysisPermissions.WRITE); - - validateNewInterpretation(study, interpretation, clinicalAnalysis, userId); - - ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.CREATE_INTERPRETATION, - "Create interpretation '" + interpretation.getId() + "'", TimeUtils.getTime()); - OpenCGAResult result = interpretationDBAdaptor.insert(study.getUid(), interpretation, saveInterpretationAs, - Collections.singletonList(clinicalAudit)); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { - // Fetch created Interpretation - OpenCGAResult queryResult = interpretationDBAdaptor.get(study.getUid(), interpretation.getId(), - QueryOptions.empty()); - result.setResults(queryResult.getResults()); - } + return run(auditParams, Enums.Action.CREATE, INTERPRETATION, studyStr, token, options, + Collections.singletonList(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION.key()), (study, userId, rp, qOptions) -> { + QueryOptions clinicalOptions = keepFieldsInQueryOptions(ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, + Arrays.asList(ClinicalAnalysisDBAdaptor.QueryParams.PANELS.key(), + ClinicalAnalysisDBAdaptor.QueryParams.PANEL_LOCK.key(), + ClinicalAnalysisDBAdaptor.QueryParams.AUDIT.key(), + ClinicalAnalysisDBAdaptor.QueryParams.INTERPRETATION_ID.key(), + ClinicalAnalysisDBAdaptor.QueryParams.SECONDARY_INTERPRETATIONS_ID.key())); + ClinicalAnalysis clinicalAnalysis = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), + clinicalAnalysisStr, clinicalOptions, userId).first(); + + authorizationManager.checkClinicalAnalysisPermission(study.getUid(), clinicalAnalysis.getUid(), + userId, ClinicalAnalysisPermissions.WRITE); + + validateNewInterpretation(study, interpretation, clinicalAnalysis, userId); + + ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.CREATE_INTERPRETATION, + "Create interpretation '" + interpretation.getId() + "'", TimeUtils.getTime()); + OpenCGAResult result = interpretationDBAdaptor.insert(study.getUid(), interpretation, + saveInterpretationAs, Collections.singletonList(clinicalAudit)); + if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + // Fetch created Interpretation + OpenCGAResult queryResult = interpretationDBAdaptor.get(study.getUid(), interpretation.getId(), + QueryOptions.empty()); + result.setResults(queryResult.getResults()); + } - auditManager.auditCreate(userId, Enums.Resource.INTERPRETATION, interpretation.getId(), "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.INTERPRETATION, interpretation.getId(), "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return result; + }); } void validateNewInterpretation(Study study, Interpretation interpretation, ClinicalAnalysis clinicalAnalysis, String userId) @@ -388,90 +379,76 @@ void validateNewInterpretation(Study study, Interpretation interpretation, Clini public OpenCGAResult clear(String studyStr, String clinicalAnalysisId, List interpretationList, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysisId", clinicalAnalysisId) + .append("interpretationList", interpretationList) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - auditManager.initAuditBatch(operationId); - - OpenCGAResult result = OpenCGAResult.empty(); - for (String interpretationStr : interpretationList) { - String interpretationId = interpretationStr; - String interpretationUuid = ""; - try { - QueryOptions clinicalOptions = keepFieldInQueryOptions(INCLUDE_CLINICAL_ANALYSIS, - ClinicalAnalysisDBAdaptor.QueryParams.PANELS.key()); - OpenCGAResult clinicalResult = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), - clinicalAnalysisId, clinicalOptions, userId); - if (clinicalResult.getNumResults() == 0) { - throw new CatalogException("ClinicalAnalysis '" + clinicalAnalysisId + "' not found"); - } - ClinicalAnalysis clinicalAnalysis = clinicalResult.first(); - if (clinicalAnalysis.isLocked()) { - throw new CatalogException("Could not clear the Interpretation. Case is locked so no further modifications can be " - + "made to the Interpretation."); - } + return runBatch(auditParams, Enums.Action.CLEAR, INTERPRETATION, studyStr, token, null, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Interpretation.class); + for (String interpretationStr : interpretationList) { + run(auditParams, Enums.Action.CLEAR, INTERPRETATION, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(interpretationStr); + + QueryOptions clinicalOptions = keepFieldInQueryOptions(INCLUDE_CLINICAL_ANALYSIS, + ClinicalAnalysisDBAdaptor.QueryParams.PANELS.key()); + OpenCGAResult clinicalResult = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), + clinicalAnalysisId, clinicalOptions, userId); + if (clinicalResult.getNumResults() == 0) { + throw new CatalogException("ClinicalAnalysis '" + clinicalAnalysisId + "' not found"); + } + ClinicalAnalysis clinicalAnalysis = clinicalResult.first(); + if (clinicalAnalysis.isLocked()) { + throw new CatalogException("Could not clear the Interpretation. Case is locked so no further modifications can be " + + "made to the Interpretation."); + } - OpenCGAResult tmpResult = internalGet(study.getUid(), interpretationStr, INCLUDE_INTERPRETATION_IDS, - userId); - if (tmpResult.getNumResults() == 0) { - throw new CatalogException("Interpretation '" + interpretationStr + "' not found."); - } - Interpretation interpretation = tmpResult.first(); - if (interpretation.isLocked()) { - throw new CatalogException("Could not clear the Interpretation. Interpretation '" + interpretation.getId() - + " is locked. Please, unlock it first."); - } + OpenCGAResult tmpResult = internalGet(study.getUid(), interpretationStr, INCLUDE_INTERPRETATION_IDS, + userId); + if (tmpResult.getNumResults() == 0) { + throw new CatalogException("Interpretation '" + interpretationStr + "' not found."); + } + Interpretation interpretation = tmpResult.first(); + rp.setId(interpretation.getId()); + rp.setUuid(interpretation.getUuid()); + if (interpretation.isLocked()) { + throw new CatalogException("Could not clear the Interpretation. Interpretation '" + interpretation.getId() + + " is locked. Please, unlock it first."); + } - if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysisId)) { - throw new CatalogException("Interpretation '" + interpretationId + "' does not belong to ClinicalAnalysis '" - + clinicalAnalysisId + "'. It belongs to '" + interpretation.getClinicalAnalysisId() + "'."); - } + if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysisId)) { + throw new CatalogException("Interpretation '" + interpretation.getId() + "' does not belong to ClinicalAnalysis '" + + clinicalAnalysisId + "'. It belongs to '" + interpretation.getClinicalAnalysisId() + "'."); + } - interpretationId = interpretation.getId(); - interpretationUuid = interpretation.getUuid(); - - Map actionMap = new HashMap<>(); - actionMap.put(InterpretationDBAdaptor.QueryParams.PRIMARY_FINDINGS.key(), ParamUtils.BasicUpdateAction.SET); - actionMap.put(InterpretationDBAdaptor.QueryParams.SECONDARY_FINDINGS.key(), ParamUtils.BasicUpdateAction.SET); - actionMap.put(InterpretationDBAdaptor.QueryParams.METHOD.key(), ParamUtils.BasicUpdateAction.SET); - actionMap.put(InterpretationDBAdaptor.QueryParams.PANELS.key(), ParamUtils.BasicUpdateAction.SET); - QueryOptions options = new QueryOptions(Constants.ACTIONS, actionMap); - - InterpretationUpdateParams params = new InterpretationUpdateParams("", new ClinicalAnalystParam(), - InterpretationMethod.init(), null, null, Collections.emptyList(), Collections.emptyList(), - clinicalAnalysis.getPanels() != null - ? clinicalAnalysis.getPanels().stream() - .map(p -> new PanelReferenceParam().setId(p.getId())).collect(Collectors.toList()) - : null, - Collections.emptyList(), new StatusParam(), false, new ObjectMap()); - - ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.CLEAR_INTERPRETATION, - "Clear interpretation '" + interpretationId + "'", TimeUtils.getTime()); - OpenCGAResult writeResult = update(study, interpretation, params, Collections.singletonList(clinicalAudit), null, options, - userId); - result.append(writeResult); - - auditManager.audit(operationId, userId, Enums.Action.CLEAR, Enums.Resource.INTERPRETATION, interpretationId, - interpretationUuid, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); - - return result; - } catch (CatalogException e) { - auditManager.audit(operationId, userId, Enums.Action.CLEAR, Enums.Resource.INTERPRETATION, interpretationId, - interpretationUuid, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - throw e; + Map actionMap = new HashMap<>(); + actionMap.put(InterpretationDBAdaptor.QueryParams.PRIMARY_FINDINGS.key(), ParamUtils.BasicUpdateAction.SET); + actionMap.put(InterpretationDBAdaptor.QueryParams.SECONDARY_FINDINGS.key(), ParamUtils.BasicUpdateAction.SET); + actionMap.put(InterpretationDBAdaptor.QueryParams.METHOD.key(), ParamUtils.BasicUpdateAction.SET); + actionMap.put(InterpretationDBAdaptor.QueryParams.PANELS.key(), ParamUtils.BasicUpdateAction.SET); + QueryOptions options = new QueryOptions(Constants.ACTIONS, actionMap); + + InterpretationUpdateParams params = new InterpretationUpdateParams("", new ClinicalAnalystParam(), + InterpretationMethod.init(), null, null, Collections.emptyList(), Collections.emptyList(), + clinicalAnalysis.getPanels() != null + ? clinicalAnalysis.getPanels().stream() + .map(p -> new PanelReferenceParam().setId(p.getId())).collect(Collectors.toList()) + : null, + Collections.emptyList(), new StatusParam(), false, new ObjectMap()); + + ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.CLEAR_INTERPRETATION, + "Clear interpretation '" + interpretation.getId() + "'", TimeUtils.getTime()); + OpenCGAResult writeResult = update(study, interpretation, params, Collections.singletonList(clinicalAudit), null, + options, userId); + result.append(writeResult); + return null; + }); } - } - auditManager.finishAuditBatch(operationId); - return result; + return result; + }); } // public OpenCGAResult merge(String studyStr, String clinicalAnalysisId, String interpretationId, @@ -622,13 +599,6 @@ public OpenCGAResult update(String studyStr, Query query, Interp public OpenCGAResult update(String studyStr, Query query, InterpretationUpdateParams updateParams, ParamUtils.SaveInterpretationAs as, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -645,63 +615,49 @@ public OpenCGAResult update(String studyStr, Query query, Interp .append("options", options) .append("token", token); - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - fixQueryObject(study, finalQuery, userId); - - DBIterator iterator; - try { + return runBatch(auditParams, Enums.Action.UPDATE, INTERPRETATION, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); finalQuery.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = interpretationDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_INTERPRETATION_FINDING_IDS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Interpretation interpretation = iterator.next(); - try { - List clinicalAuditList = new ArrayList<>(); - clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.UPDATE_INTERPRETATION, - "Update interpretation '" + interpretation.getId() + "'", TimeUtils.getTime())); - if (as != null) { - clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.SWAP_INTERPRETATION, - "Swap interpretation '" + interpretation.getId() + "' to " + as, TimeUtils.getTime())); + OpenCGAResult result = OpenCGAResult.empty(Interpretation.class); + try (DBIterator iterator = interpretationDBAdaptor.iterator(study.getUid(), finalQuery, + INCLUDE_INTERPRETATION_FINDING_IDS, userId)) { + while (iterator.hasNext()) { + Interpretation interpretation = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, INTERPRETATION, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(interpretation.getId()); + rp.setUuid(interpretation.getUuid()); + List clinicalAuditList = new ArrayList<>(); + clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.UPDATE_INTERPRETATION, + "Update interpretation '" + interpretation.getId() + "'", TimeUtils.getTime())); + if (as != null) { + clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.SWAP_INTERPRETATION, + "Swap interpretation '" + interpretation.getId() + "' to " + as, TimeUtils.getTime())); + } + OpenCGAResult writeResult = update(study, interpretation, updateParams, clinicalAuditList, as, qo, userId); + result.append(writeResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, interpretation.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot update interpretation {}: {}", interpretation.getId(), e.getMessage(), e); + } } - OpenCGAResult writeResult = update(study, interpretation, updateParams, clinicalAuditList, as, options, userId); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, interpretation.getId(), - interpretation.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - result.append(writeResult); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, interpretation.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update interpretation {}: {}", interpretation.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, interpretation.getId(), - interpretation.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + } + }); } - public OpenCGAResult update(String studyStr, String clinicalAnalysisId, String intepretationId, + public OpenCGAResult update(String studyStr, String clinicalAnalysisId, String interpretationId, InterpretationUpdateParams updateParams, ParamUtils.SaveInterpretationAs as, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -712,35 +668,30 @@ public OpenCGAResult update(String studyStr, String clinicalAnal ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysisId", clinicalAnalysisId) - .append("intepretationId", intepretationId) + .append("interpretationId", interpretationId) .append("updateParams", updateMap) .append("as", as) .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String interpretationId = ""; - String interpretationUuid = ""; - try { + return run(auditParams, Enums.Action.UPDATE, INTERPRETATION, studyStr, token, options, (study, userId, rp, qOptions) -> { ParamUtils.checkParameter(clinicalAnalysisId, "ClinicalAnalysisId"); - ParamUtils.checkParameter(intepretationId, "InterpretationId"); + ParamUtils.checkParameter(interpretationId, "InterpretationId"); - OpenCGAResult interpretationOpenCGAResult = internalGet(study.getUid(), intepretationId, + OpenCGAResult interpretationOpenCGAResult = internalGet(study.getUid(), interpretationId, INCLUDE_INTERPRETATION_FINDING_IDS, userId); if (interpretationOpenCGAResult.getNumResults() == 0) { throw new CatalogException("Interpretation '" + interpretationId + "' not found."); } Interpretation interpretation = interpretationOpenCGAResult.first(); + rp.setId(interpretation.getId()); + rp.setUuid(interpretation.getUuid()); if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysisId)) { - throw new CatalogException("Interpretation '" + intepretationId + "' does not belong to ClinicalAnalysis '" + throw new CatalogException("Interpretation '" + interpretationId + "' does not belong to ClinicalAnalysis '" + clinicalAnalysisId + "'. It belongs to '" + interpretation.getClinicalAnalysisId() + "'."); } - // We set the proper values for the audit - interpretationId = interpretation.getId(); - interpretationUuid = interpretation.getUuid(); - List clinicalAuditList = new ArrayList<>(); clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.UPDATE_INTERPRETATION, "Update interpretation '" + interpretation.getId() + "'", TimeUtils.getTime())); @@ -748,26 +699,8 @@ public OpenCGAResult update(String studyStr, String clinicalAnal clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.SWAP_INTERPRETATION, "Swap interpretation '" + interpretation.getId() + "' to " + as, TimeUtils.getTime())); } - OpenCGAResult writeResult = update(study, interpretation, updateParams, clinicalAuditList, as, options, userId); - result.append(writeResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, interpretation.getId(), - interpretation.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - CatalogException e1 = new CatalogException("Cannot update interpretation '" + interpretationId + "' of clinical analysis '" - + clinicalAnalysisId + "': " + e.getMessage(), e); - Event event = new Event(Event.Type.ERROR, interpretationId, e1.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("{}", e1.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, interpretationId, interpretationUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e1.getError())); - throw e1; - } - - return result; + return update(study, interpretation, updateParams, clinicalAuditList, as, options, userId); + }); } /** @@ -793,13 +726,6 @@ public OpenCGAResult update(String studyStr, String clinicalAnal public OpenCGAResult update(String studyStr, String clinicalAnalysisId, List interpretationIds, InterpretationUpdateParams updateParams, ParamUtils.SaveInterpretationAs as, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -809,63 +735,58 @@ public OpenCGAResult update(String studyStr, String clinicalAnal ObjectMap auditParams = new ObjectMap() .append("study", studyStr) - .append("interpretationIds", interpretationIds) .append("clinicalAnalysisId", clinicalAnalysisId) + .append("interpretationIds", interpretationIds) .append("updateParams", updateMap) .append("as", as) .append("ignoreException", ignoreException) .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : interpretationIds) { - String interpretationId = id; - String interpretationUuid = ""; + return runBatch(auditParams, Enums.Action.UPDATE, INTERPRETATION, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Interpretation.class); + for (String id : interpretationIds) { + try { + run(auditParams, Enums.Action.UPDATE, INTERPRETATION, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult tmpResult = internalGet(study.getUid(), id, INCLUDE_INTERPRETATION_FINDING_IDS, + userId); + if (tmpResult.getNumResults() == 0) { + throw new CatalogException("Interpretation '" + id + "' not found."); + } + Interpretation interpretation = tmpResult.first(); + rp.setId(interpretation.getId()); + rp.setUuid(interpretation.getUuid()); - try { - OpenCGAResult tmpResult = internalGet(study.getUid(), interpretationId, INCLUDE_INTERPRETATION_FINDING_IDS, - userId); - if (tmpResult.getNumResults() == 0) { - throw new CatalogException("Interpretation '" + interpretationId + "' not found."); - } - Interpretation interpretation = tmpResult.first(); + if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysisId)) { + throw new CatalogException("Interpretation '" + id + "' does not belong to ClinicalAnalysis '" + + clinicalAnalysisId + "'. It belongs to '" + interpretation.getClinicalAnalysisId() + "'."); + } - if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysisId)) { - throw new CatalogException("Interpretation '" + interpretationId + "' does not belong to ClinicalAnalysis '" - + clinicalAnalysisId + "'. It belongs to '" + interpretation.getClinicalAnalysisId() + "'."); - } + List clinicalAuditList = new ArrayList<>(); + clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.UPDATE_INTERPRETATION, + "Update interpretation '" + interpretation.getId() + "'", TimeUtils.getTime())); + if (as != null) { + clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.SWAP_INTERPRETATION, + "Swap interpretation '" + interpretation.getId() + "' to " + as, TimeUtils.getTime())); + } + OpenCGAResult writeResult = update(study, interpretation, updateParams, clinicalAuditList, as, options, userId); + result.append(writeResult); - // We set the proper values for the audit - interpretationId = interpretation.getId(); - interpretationUuid = interpretation.getUuid(); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - List clinicalAuditList = new ArrayList<>(); - clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.UPDATE_INTERPRETATION, - "Update interpretation '" + interpretation.getId() + "'", TimeUtils.getTime())); - if (as != null) { - clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.SWAP_INTERPRETATION, - "Swap interpretation '" + interpretation.getId() + "' to " + as, TimeUtils.getTime())); + logger.error("Cannot update interpretation {}: {}", id, e.getMessage(), e); } - OpenCGAResult writeResult = update(study, interpretation, updateParams, clinicalAuditList, as, options, userId); - result.append(writeResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, interpretation.getId(), - interpretation.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update interpretation {}: {}", interpretationId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.INTERPRETATION, interpretationId, interpretationUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, Interpretation interpretation, InterpretationUpdateParams updateParams, @@ -1068,9 +989,6 @@ private OpenCGAResult update(Study study, Interpretation interpretation, Interpr public OpenCGAResult revert(String studyStr, String clinicalAnalysisId, String interpretationId, int version, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_CONFIGURATION); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysisId", clinicalAnalysisId) @@ -1078,8 +996,8 @@ public OpenCGAResult revert(String studyStr, String clinicalAnal .append("version", version) .append("token", token); - String interpretationUuid = ""; - try { + return run(auditParams, Enums.Action.REVERT, INTERPRETATION, studyStr, token, null, (study, userId, rp, qOptions) -> { + rp.setId(interpretationId); OpenCGAResult clinicalResult = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), clinicalAnalysisId, INCLUDE_CLINICAL_ANALYSIS, userId); if (clinicalResult.getNumResults() == 0) { @@ -1102,14 +1020,13 @@ public OpenCGAResult revert(String studyStr, String clinicalAnal throw new CatalogException("Could not find interpretation '" + interpretationId + "'"); } Interpretation interpretation = result.first(); + rp.setId(interpretation.getId()); + rp.setUuid(interpretation.getUuid()); if (interpretation.isLocked()) { throw new CatalogException("Could not revert the Interpretation. Interpretation '" + interpretation.getId() + " is locked. Please, unlock it first."); } - interpretationId = interpretation.getId(); - interpretationUuid = interpretation.getUuid(); - if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysisId)) { throw new CatalogException("Interpretation '" + interpretationId + "' does not belong to ClinicalAnalysis '" + clinicalAnalysisId + "'. It belongs to '" + interpretation.getClinicalAnalysisId() + "'."); @@ -1126,28 +1043,8 @@ public OpenCGAResult revert(String studyStr, String clinicalAnal List clinicalAuditList = new ArrayList<>(); clinicalAuditList.add(new ClinicalAudit(userId, ClinicalAudit.Action.REVERT_INTERPRETATION, "Revert interpretation '" + interpretation.getId() + "' to version '" + version + "'", TimeUtils.getTime())); - OpenCGAResult revert = interpretationDBAdaptor.revert(interpretation.getUid(), version, clinicalAuditList); - - auditManager.audit(userId, Enums.Action.REVERT, Enums.Resource.INTERPRETATION, interpretation.getId(), - interpretation.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return revert; - } catch (CatalogDBException e) { - logger.error("Could not revert interpretation {}", interpretationId, e); - auditManager.audit(userId, Enums.Action.REVERT, Enums.Resource.INTERPRETATION, interpretationId, - interpretationUuid, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - CatalogException exception = new CatalogException("Could not revert interpretation '" + interpretationId + "'"); - exception.addSuppressed(e); - throw exception; - } catch (CatalogException e) { - logger.error("Could not revert interpretation {}: {}", interpretationId, e.getMessage(), e); - auditManager.audit(userId, Enums.Action.REVERT, Enums.Resource.INTERPRETATION, interpretationId, - interpretationUuid, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw new CatalogException("Could not revert interpretation '" + interpretationId + "': " + e.getMessage()); - } + return interpretationDBAdaptor.revert(interpretation.getUid(), version, clinicalAuditList); + }); } @Override @@ -1157,75 +1054,63 @@ public DBIterator iterator(String studyStr, Query query, QueryOp } @Override - public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) - throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - - fixQueryObject(study, query, userId); - query.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - OpenCGAResult queryResult = interpretationDBAdaptor.get(study.getUid(), query, options, userId); + public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("studyId", studyId) + .append("query", query) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.SEARCH, INTERPRETATION, studyId, token, options, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - List results = new ArrayList<>(queryResult.getResults().size()); - for (Interpretation interpretation : queryResult.getResults()) { - if (StringUtils.isNotEmpty(interpretation.getClinicalAnalysisId())) { - try { - catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), interpretation.getClinicalAnalysisId(), - ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, userId); - results.add(interpretation); - } catch (CatalogException e) { - // Maybe the clinical analysis was deleted - Query clinicalQuery = new Query(ClinicalAnalysisDBAdaptor.QueryParams.DELETED.key(), true); + OpenCGAResult queryResult = interpretationDBAdaptor.get(study.getUid(), finalQuery, qOptions, userId); + List results = new ArrayList<>(queryResult.getResults().size()); + for (Interpretation interpretation : queryResult.getResults()) { + if (StringUtils.isNotEmpty(interpretation.getClinicalAnalysisId())) { try { catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), interpretation.getClinicalAnalysisId(), - clinicalQuery, ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, userId); + ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, userId); results.add(interpretation); - } catch (CatalogException e1) { - logger.debug("Removing interpretation " + interpretation.getUuid() + " from results. User " + userId - + " does not have proper permissions"); + } catch (CatalogException e) { + // Maybe the clinical analysis was deleted + Query clinicalQuery = new Query(ClinicalAnalysisDBAdaptor.QueryParams.DELETED.key(), true); + + try { + catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), interpretation.getClinicalAnalysisId(), + clinicalQuery, ClinicalAnalysisManager.INCLUDE_CLINICAL_IDS, userId); + results.add(interpretation); + } catch (CatalogException e1) { + logger.debug("Removing interpretation " + interpretation.getUuid() + " from results. User " + userId + + " does not have proper permissions"); + } } } } - } - queryResult.setResults(results); - queryResult.setNumMatches(results.size()); - queryResult.setNumResults(results.size()); - return queryResult; + queryResult.setResults(results); + queryResult.setNumMatches(results.size()); + queryResult.setNumResults(results.size()); + return queryResult; + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId); - - query.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = interpretationDBAdaptor.distinct(study.getUid(), field, query, userId); - auditManager.auditDistinct(userId, Enums.Resource.INTERPRETATION, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.INTERPRETATION, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.DISTINCT, INTERPRETATION, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(InterpretationDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return interpretationDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } @Override @@ -1245,15 +1130,6 @@ public OpenCGAResult delete(String studyStr, String clinicalAnalysisId, List interpretationIds, boolean ignoreException, String token) throws CatalogException { - if (interpretationIds == null || ListUtils.isEmpty(interpretationIds)) { - throw new CatalogException("Missing list of interpretation ids"); - } - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("clinicalAnalysisId", clinicalAnalysisId) @@ -1261,19 +1137,16 @@ public OpenCGAResult delete(String studyStr, String clinicalAnalysisId, List { + if (interpretationIds == null || ListUtils.isEmpty(interpretationIds)) { + throw new CatalogException("Missing list of interpretation ids"); + } + // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationId, userId, Enums.Resource.INTERPRETATION, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - ClinicalAnalysis clinicalAnalysis; - try { - clinicalAnalysis = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), clinicalAnalysisId, + ClinicalAnalysis clinicalAnalysis = catalogManager.getClinicalAnalysisManager().internalGet(study.getUid(), clinicalAnalysisId, INCLUDE_CLINICAL_ANALYSIS, userId).first(); if (clinicalAnalysis.isLocked()) { throw new CatalogException("Could not delete the Interpretation. Case is locked so no further modifications can be made to" @@ -1283,62 +1156,52 @@ public OpenCGAResult delete(String studyStr, String clinicalAnalysisId, List internalResult = internalGet(study.getUid(), id, INCLUDE_INTERPRETATION_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Interpretation '" + id + "' not found"); - } - Interpretation interpretation = internalResult.first(); + OpenCGAResult result = OpenCGAResult.empty(Interpretation.class); + for (String id : interpretationIds) { + try { + run(auditParams, Enums.Action.DELETE, INTERPRETATION, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_INTERPRETATION_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Interpretation '" + id + "' not found"); + } + Interpretation interpretation = internalResult.first(); - // We set the proper values for the audit - interpretationId = interpretation.getId(); - interpretationUuid = interpretation.getUuid(); + // We set the proper values for the audit + rp.setId(interpretation.getId()); + rp.setUuid(interpretation.getUuid()); - if (interpretation.isLocked()) { - throw new CatalogException("Could not delete the Interpretation. Interpretation '" + interpretation.getId() - + " is locked. Please, unlock it first."); - } - if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysis.getId())) { - throw new CatalogException("Cannot delete interpretation '" + interpretationId + "': Interpretation does not belong" - + " to ClinicalAnalysis '" + clinicalAnalysis.getId() + "'."); - } - - // Check if the interpretation can be deleted - // checkCanBeDeleted(study.getUid(), interpretation, params.getBoolean(Constants.FORCE, false)); + if (interpretation.isLocked()) { + throw new CatalogException("Could not delete the Interpretation. Interpretation '" + interpretation.getId() + + " is locked. Please, unlock it first."); + } + if (!interpretation.getClinicalAnalysisId().equals(clinicalAnalysis.getId())) { + throw new CatalogException("Cannot delete interpretation '" + id + "': Interpretation does not belong" + + " to ClinicalAnalysis '" + clinicalAnalysis.getId() + "'."); + } - ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.DELETE_INTERPRETATION, - "Delete interpretation '" + interpretation.getId() + "'", TimeUtils.getTime()); - result.append(interpretationDBAdaptor.delete(interpretation, Collections.singletonList(clinicalAudit))); + // Check if the interpretation can be deleted + // checkCanBeDeleted(study.getUid(), interpretation, params.getBoolean(Constants.FORCE, false)); - auditManager.auditDelete(operationId, userId, Enums.Resource.INTERPRETATION, interpretation.getId(), - interpretation.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete interpretation " + interpretationId + ": " + e.getMessage(); + ClinicalAudit clinicalAudit = new ClinicalAudit(userId, ClinicalAudit.Action.DELETE_INTERPRETATION, + "Delete interpretation '" + interpretation.getId() + "'", TimeUtils.getTime()); + result.append(interpretationDBAdaptor.delete(interpretation, Collections.singletonList(clinicalAudit))); + return null; + }); + } catch (CatalogException e) { + String errorMsg = "Cannot delete interpretation " + id + ": " + e.getMessage(); - Event event = new Event(Event.Type.ERROR, interpretationId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error(errorMsg); - auditManager.auditDelete(operationId, userId, Enums.Resource.INTERPRETATION, interpretationId, interpretationUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error(errorMsg); + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/JobManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/JobManager.java index ca02d6315e8..65f41c6eace 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/JobManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/JobManager.java @@ -22,7 +22,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.StopWatch; import org.opencb.commons.datastore.core.*; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -42,7 +41,6 @@ import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; import org.opencb.opencga.core.models.AclParams; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.file.File; import org.opencb.opencga.core.models.file.FileContent; @@ -59,10 +57,12 @@ import java.nio.file.Paths; import java.time.Instant; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; +import static org.opencb.opencga.core.models.common.Enums.Resource.JOB; /** * @author Jacobo Coll <jacobo167@gmail.com> @@ -88,8 +88,8 @@ public class JobManager extends ResourceManager { } @Override - Enums.Resource getEntity() { - return Enums.Resource.JOB; + Enums.Resource getResource() { + return JOB; } // @Override @@ -193,15 +193,15 @@ public Long getStudyId(long jobId) throws CatalogException { return jobDBAdaptor.getStudyId(jobId); } - public Study getStudy(Job job, String sessionId) throws CatalogException { + public Study getStudy(Job job, String token) throws CatalogException { ParamUtils.checkObj(job, "job"); - ParamUtils.checkObj(sessionId, "session id"); + ParamUtils.checkObj(token, "token"); if (job.getStudyUid() <= 0) { throw new CatalogException("Missing study uid field in job"); } - String user = catalogManager.getUserManager().getUserId(sessionId); + String user = catalogManager.getUserManager().getUserId(token); Query query = new Query(StudyDBAdaptor.QueryParams.UID.key(), job.getStudyUid()); OpenCGAResult studyDataResult = studyDBAdaptor.get(query, QueryOptions.empty(), user); @@ -214,42 +214,29 @@ public Study getStudy(Job job, String sessionId) throws CatalogException { } public OpenCGAResult visit(String studyId, String jobId, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("jobId", jobId) .append("token", token); - try { + return run(auditParams, Enums.Action.VISIT, JOB, studyId, token, null, (study, userId, rp, qOptions) -> { + rp.setId(jobId); JobUpdateParams updateParams = new JobUpdateParams().setVisited(true); Job job = internalGet(study.getUid(), jobId, INCLUDE_JOB_IDS, userId).first(); - - OpenCGAResult result = update(study, job, updateParams, QueryOptions.empty(), userId); - auditManager.audit(userId, Enums.Action.VISIT, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.VISIT, Enums.Resource.JOB, jobId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + return update(study, job, updateParams, QueryOptions.empty(), userId); + }); } @Override public OpenCGAResult create(String studyStr, Job job, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("job", job) .append("options", options) .append("token", token); - try { - options = ParamUtils.defaultObject(options, QueryOptions::new); + return run(auditParams, Enums.Action.CREATE, JOB, studyStr, token, options, (study, userId, rp, qOptions) -> { authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_JOBS); ParamUtils.checkObj(job, "Job"); @@ -275,20 +262,23 @@ public OpenCGAResult create(String studyStr, Job job, QueryOptions options, job.setRelease(catalogManager.getStudyManager().getCurrentRelease(study)); job.setOutDir(job.getOutDir() != null && StringUtils.isNotEmpty(job.getOutDir().getPath()) ? job.getOutDir() : null); job.setStudy(new JobStudyParam(study.getFqn())); + job.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.JOB)); + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); if (!Arrays.asList(Enums.ExecutionStatus.ABORTED, Enums.ExecutionStatus.DONE, Enums.ExecutionStatus.UNREGISTERED, Enums.ExecutionStatus.ERROR).contains(job.getInternal().getStatus().getId())) { throw new CatalogException("Cannot create a job in a status different from one of the final ones."); } - if (ListUtils.isNotEmpty(job.getInput())) { + if (CollectionUtils.isNotEmpty(job.getInput())) { List inputFiles = new ArrayList<>(job.getInput().size()); for (File file : job.getInput()) { inputFiles.add(getFile(study.getUid(), file.getPath(), userId)); } job.setInput(inputFiles); } - if (ListUtils.isNotEmpty(job.getOutput())) { + if (CollectionUtils.isNotEmpty(job.getOutput())) { List outputFiles = new ArrayList<>(job.getOutput().size()); for (File file : job.getOutput()) { outputFiles.add(getFile(study.getUid(), file.getPath(), userId)); @@ -308,22 +298,15 @@ public OpenCGAResult create(String studyStr, Job job, QueryOptions options, job.setStderr(getFile(study.getUid(), job.getStderr().getPath(), userId)); } - job.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.JOB)); OpenCGAResult insert = jobDBAdaptor.insert(study.getUid(), job, options); if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch created job OpenCGAResult queryResult = getJob(study.getUid(), job.getUuid(), options); insert.setResults(queryResult.getResults()); } - auditManager.auditCreate(userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); return insert; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.JOB, job.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } private void autoCompleteNewJob(Study study, Job job, String token) throws CatalogException { @@ -419,24 +402,39 @@ public List getJobInputFilesFromParams(String study, Job job, String token public OpenCGAResult retry(String studyStr, JobRetryParams jobRetry, Enums.Priority priority, String jobId, String jobDescription, List jobDependsOn, List jobTags, String token) throws CatalogException { - Job job = get(studyStr, jobRetry.getJob(), new QueryOptions(), token).first(); - if (jobRetry.isForce() - || job.getInternal().getStatus().getId().equals(Enums.ExecutionStatus.ERROR) - || job.getInternal().getStatus().getId().equals(Enums.ExecutionStatus.ABORTED)) { - Map params = new ObjectMap(job.getParams()); - if (jobRetry.getParams() != null) { - params.putAll(jobRetry.getParams()); - } - HashMap attributes = new HashMap<>(); - attributes.put("retry_from", jobRetry.getJob()); - if (StringUtils.isEmpty(jobDescription)) { - jobDescription = "Retry from job '" + jobRetry.getJob() + "'"; + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("jobRetry", jobRetry) + .append("priority", priority) + .append("jobId", jobId) + .append("jobDescription", jobDescription) + .append("jobDependsOn", jobDependsOn) + .append("jobTags", jobTags) + .append("token", token); + + return run(auditParams, Enums.Action.CREATE, JOB, studyStr, token, null, (study, userId, rp, qOptions) -> { + rp.setId(jobId); + Job job = get(studyStr, jobRetry.getJob(), new QueryOptions(), token).first(); + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + if (jobRetry.isForce() + || job.getInternal().getStatus().getId().equals(Enums.ExecutionStatus.ERROR) + || job.getInternal().getStatus().getId().equals(Enums.ExecutionStatus.ABORTED)) { + Map params = new ObjectMap(job.getParams()); + if (jobRetry.getParams() != null) { + params.putAll(jobRetry.getParams()); + } + HashMap attributes = new HashMap<>(); + attributes.put("retry_from", jobRetry.getJob()); + String finalDescription = StringUtils.isNotEmpty(jobDescription) + ? jobDescription + : "Retry from job '" + jobRetry.getJob() + "'"; + return submit(studyStr, job.getTool().getId(), priority, params, jobId, finalDescription, jobDependsOn, jobTags, attributes, + token); + } else { + throw new CatalogException("Unable to retry job with status " + job.getInternal().getStatus().getId()); } - return submit(studyStr, job.getTool().getId(), priority, params, jobId, jobDescription, jobDependsOn, jobTags, - attributes, token); - } else { - throw new CatalogException("Unable to retry job with status " + job.getInternal().getStatus().getId()); - } + }); } public OpenCGAResult submit(String studyStr, String toolId, Enums.Priority priority, Map params, String token) @@ -472,9 +470,6 @@ public OpenCGAResult submit(String studyStr, String toolId, Enums.Priority String jobDescription, List jobDependsOn, List jobTags, Map attributes, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("toolId", toolId) @@ -491,62 +486,68 @@ public OpenCGAResult submit(String studyStr, String toolId, Enums.Priority job.setDescription(jobDescription); job.setTool(new ToolInfo().setId(toolId)); job.setTags(jobTags); - job.setStudy(new JobStudyParam(study.getFqn())); - job.setUserId(userId); job.setParams(params); job.setPriority(priority); job.setDependsOn(jobDependsOn != null ? jobDependsOn.stream().map(j -> new Job().setId(j)).collect(Collectors.toList()) : Collections.emptyList()); job.setAttributes(attributes); - try { - autoCompleteNewJob(study, job, token); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.EXECUTE_JOBS); - - // Check params - ParamUtils.checkObj(params, "params"); - for (Map.Entry entry : params.entrySet()) { - if (entry.getValue() == null) { - throw new CatalogException("Found '" + entry.getKey() + "' param with null value"); + try { + return run(auditParams, Enums.Action.CREATE, JOB, studyStr, token, null, (study, userId, rp, qOptions) -> { + job.setStudyUid(study.getUid()); + job.setStudy(new JobStudyParam(study.getFqn())); + job.setUserId(userId); + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.EXECUTE_JOBS); + + autoCompleteNewJob(study, job, token); + + // Check params + ParamUtils.checkObj(params, "params"); + for (Map.Entry entry : params.entrySet()) { + if (entry.getValue() == null) { + throw new CatalogException("Found '" + entry.getKey() + "' param with null value"); + } } - } - jobDBAdaptor.insert(study.getUid(), job, new QueryOptions()); - OpenCGAResult jobResult = jobDBAdaptor.get(job.getUid(), new QueryOptions()); - - auditManager.auditCreate(userId, Enums.Resource.JOB, job.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return jobResult; + jobDBAdaptor.insert(study.getUid(), job, new QueryOptions()); + return jobDBAdaptor.get(job.getUid(), new QueryOptions()); + }); } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.JOB, job.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - job.getInternal().setStatus(new Enums.ExecutionStatus(Enums.ExecutionStatus.ABORTED)); job.getInternal().getStatus().setDescription(e.toString()); - jobDBAdaptor.insert(study.getUid(), job, new QueryOptions()); - + jobDBAdaptor.insert(job.getStudyUid(), job, new QueryOptions()); throw e; } } public OpenCGAResult count(Query query, String token) throws CatalogException { - String userId = userManager.getUserId(token); - authorizationManager.isInstallationAdministrator(userId); + ObjectMap auditParams = new ObjectMap() + .append("query", query) + .append("token", token); - return jobDBAdaptor.count(query); + return run(auditParams, Enums.Action.COUNT, JOB, null, token, null, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + authorizationManager.isInstallationAdministrator(userId); + return jobDBAdaptor.count(finalQuery); + }); } public DBIterator iterator(Query query, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - authorizationManager.isInstallationAdministrator(userId); + ObjectMap auditParams = new ObjectMap() + .append("query", query) + .append("options", options) + .append("token", token); - return jobDBAdaptor.iterator(query, options); + return run(auditParams, Enums.Action.ITERATE, JOB, null, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + authorizationManager.isInstallationAdministrator(userId); + return jobDBAdaptor.iterator(finalQuery, queryOptions); + }); } - public OpenCGAResult get(long jobId, QueryOptions options, String sessionId) throws CatalogException { - return get(null, String.valueOf(jobId), options, sessionId); + public OpenCGAResult get(long jobId, QueryOptions options, String token) throws CatalogException { + return get(null, String.valueOf(jobId), options, token); } // public OpenCGAResult get(List jobIds, QueryOptions options, boolean ignoreException, String sessionId) @@ -587,103 +588,66 @@ private void fixQueryObject(Study study, Query query, String userId) throws Cata @Override public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("token", token); - try { - fixQueryObject(study, query, userId); - query.put(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = jobDBAdaptor.get(study.getUid(), query, options, userId); - auditManager.auditSearch(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.SEARCH, JOB, studyId, token, options, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.put(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return jobDBAdaptor.get(study.getUid(), finalQuery, qOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId); - - query.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = jobDBAdaptor.distinct(study.getUid(), field, query, userId); - - auditManager.auditDistinct(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.DISTINCT, JOB, studyId, token, null, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return jobDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } @Override public DBIterator iterator(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - - fixQueryObject(study, query, userId); - query.put(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + ObjectMap auditParams = new ObjectMap() + .append("studyId", studyId) + .append("query", query) + .append("options", options) + .append("token", token); - return jobDBAdaptor.iterator(study.getUid(), query, options, userId); + return run(auditParams, Enums.Action.ITERATE, JOB, studyId, token, options, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.put(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return jobDBAdaptor.iterator(study.getUid(), finalQuery, qOptions, userId); + }); } @Override public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId); - query.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResultAux = jobDBAdaptor.count(query, userId); - - auditManager.auditCount(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.COUNT, JOB, studyId, token, null, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return jobDBAdaptor.count(finalQuery, userId); + }); } @Override @@ -693,11 +657,6 @@ public OpenCGAResult delete(String studyStr, List jobIds, QueryOptions o public OpenCGAResult delete(String studyStr, List jobIds, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("jobIds", jobIds) @@ -705,57 +664,45 @@ public OpenCGAResult delete(String studyStr, List jobIds, ObjectMap para .append("ignoreException", ignoreException) .append("token", token); - boolean checkPermissions; - try { - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.JOB, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return runBatch(auditParams, Enums.Action.DELETE, JOB, studyStr, token, null, (study, userId, qOptions, operationUuid) -> { - auditManager.initAuditBatch(operationUuid); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : jobIds) { - String jobId = id; - String jobUuid = ""; + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Job.class); + for (String id : jobIds) { + try { + run(auditParams, Enums.Action.DELETE, JOB, operationUuid, study, userId, null, (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_JOB_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Job '" + id + "' not found"); + } - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_JOB_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Job '" + id + "' not found"); - } + Job job = internalResult.first(); + // We set the proper values for the audit + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); - Job job = internalResult.first(); - // We set the proper values for the audit - jobId = job.getId(); - jobUuid = job.getUuid(); + if (checkPermissions) { + authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.DELETE); + } - if (checkPermissions) { - authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.DELETE); + // Check if the job can be deleted + checkJobCanBeDeleted(job); + result.append(jobDBAdaptor.delete(job)); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot delete job {}: {}", id, e.getMessage(), e); } - - // Check if the job can be deleted - checkJobCanBeDeleted(job); - - result.append(jobDBAdaptor.delete(job)); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, jobId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot delete job {}: {}", jobId, e.getMessage(), e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.FAMILY, jobId, jobUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -765,71 +712,54 @@ public OpenCGAResult delete(String studyId, Query query, QueryOptions options, S public OpenCGAResult delete(String studyId, Query query, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) - .append("query", new Query(query)) + .append("query", query) .append("params", params) .append("ignoreException", ignoreException) .append("token", token); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; + return runBatch(auditParams, Enums.Action.DELETE, JOB, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + OpenCGAResult result = OpenCGAResult.empty(Job.class); - // We try to get an iterator containing all the jobs to be deleted - DBIterator iterator; - try { - fixQueryObject(study, query, userId); + fixQueryObject(study, finalQuery, userId); finalQuery.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = jobDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_JOB_IDS, userId); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.JOB, "", "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - while (iterator.hasNext()) { - Job job = iterator.next(); - - try { - if (checkPermissions) { - authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.DELETE); - } - - // Check if the job can be deleted - checkJobCanBeDeleted(job); - - result.append(jobDBAdaptor.delete(job)); + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + // We try to get an iterator containing all the jobs to be deleted + try (DBIterator iterator = jobDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_JOB_IDS, userId)) { + while (iterator.hasNext()) { + Job job = iterator.next(); + try { + run(auditParams, Enums.Action.DELETE, JOB, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + if (checkPermissions) { + authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.DELETE); + } - auditManager.auditDelete(operationUuid, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete job " + job.getId() + ": " + e.getMessage(); + // Check if the job can be deleted + checkJobCanBeDeleted(job); + result.append(jobDBAdaptor.delete(job)); + return null; + }); + } catch (CatalogException e) { + String errorMsg = "Cannot delete job " + job.getId() + ": " + e.getMessage(); - Event event = new Event(Event.Type.ERROR, job.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + Event event = new Event(Event.Type.ERROR, job.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error(errorMsg, e); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error(errorMsg, e); + } + } } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private void checkJobCanBeDeleted(Job job) throws CatalogException { @@ -848,11 +778,6 @@ private void checkJobCanBeDeleted(Job job) throws CatalogException { public OpenCGAResult log(String studyId, String jobId, long offset, int lines, String type, boolean tail, String token) throws CatalogException { - long startTime = System.currentTimeMillis(); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("jobId", jobId) @@ -861,11 +786,12 @@ public OpenCGAResult log(String studyId, String jobId, long offset, .append("type", type) .append("tail", tail) .append("token", token); - try { - if (StringUtils.isEmpty(type)) { - type = "stderr"; - } - if (!"stderr".equalsIgnoreCase(type) && !"stdout".equalsIgnoreCase(type)) { + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.VIEW_LOG, JOB, studyId, token, null, (study, userId, rp, qOptions) -> { + rp.setId(jobId); + String finalType = StringUtils.isNotEmpty(type) ? type : "stderr"; + if (!"stderr".equalsIgnoreCase(finalType) && !"stdout".equalsIgnoreCase(finalType)) { throw new CatalogException("Incorrect log type. It must be 'stdout' or 'stderr'"); } @@ -874,9 +800,11 @@ public OpenCGAResult log(String studyId, String jobId, long offset, JobDBAdaptor.QueryParams.INTERNAL_STATUS.key(), JobDBAdaptor.QueryParams.STDOUT.key(), JobDBAdaptor.QueryParams.OUT_DIR.key())); Job job = internalGet(study.getUid(), jobId, options, userId).first(); + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); Path logFile; - if ("stderr".equalsIgnoreCase(type)) { + if ("stderr".equalsIgnoreCase(finalType)) { if (job.getStderr() != null && job.getStderr().getUri() != null) { logFile = Paths.get(job.getStderr().getUri()); } else { @@ -915,16 +843,9 @@ public OpenCGAResult log(String studyId, String jobId, long offset, fileContent = ioManager.head(logFile, offset, lines); } - auditManager.audit(userId, Enums.Action.VIEW_LOG, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), 1, + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), 1, Collections.singletonList(fileContent), 1); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.VIEW_LOG, Enums.Resource.JOB, jobId, "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult update(String studyStr, Query query, JobUpdateParams updateParams, QueryOptions options, String token) @@ -934,13 +855,6 @@ public OpenCGAResult update(String studyStr, Query query, JobUpdateParams u public OpenCGAResult update(String studyStr, Query query, JobUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -956,41 +870,35 @@ public OpenCGAResult update(String studyStr, Query query, JobUpdateParams u .append("options", options) .append("token", token); - DBIterator iterator; - try { + return runBatch(auditParams, Enums.Action.UPDATE, JOB, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); fixQueryObject(study, finalQuery, userId); finalQuery.append(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = jobDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_JOB_IDS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Job job = iterator.next(); - try { - OpenCGAResult updateResult = update(study, job, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, job.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + try (DBIterator iterator = jobDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_JOB_IDS, userId)) { + OpenCGAResult result = OpenCGAResult.empty(Job.class); + while (iterator.hasNext()) { + Job job = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, JOB, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + OpenCGAResult updateResult = update(study, job, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, job.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update job {}: {}", job.getId(), e.getMessage(), e); + } + } - logger.error("Could not update job {}: {}", job.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } /** @@ -1012,11 +920,6 @@ public OpenCGAResult update(String studyStr, List jobIds, JobUpdate public OpenCGAResult update(String studyStr, List jobIds, JobUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -1032,50 +935,40 @@ public OpenCGAResult update(String studyStr, List jobIds, JobUpdate .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : jobIds) { - String jobId = id; - String jobUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_JOB_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Job '" + id + "' not found"); + return runBatch(auditParams, Enums.Action.UPDATE, JOB, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Job.class); + for (String id : jobIds) { + try { + run(auditParams, Enums.Action.UPDATE, JOB, operationUuid, study, userId, null, (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_JOB_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Job '" + id + "' not found"); + } + Job job = internalResult.first(); + + // We set the proper values for the audit + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + + OpenCGAResult updateResult = update(study, job, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update job {}: {}", id, e.getMessage(), e); } - Job job = internalResult.first(); - - // We set the proper values for the audit - jobId = job.getId(); - jobUuid = job.getUuid(); - - OpenCGAResult updateResult = update(study, job, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, jobId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update job {}: {}", jobId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, jobId, jobUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyStr, String jobId, JobUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -1090,9 +983,8 @@ public OpenCGAResult update(String studyStr, String jobId, JobUpdateParams .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String jobUuid = ""; - try { + return run(auditParams, Enums.Action.UPDATE, JOB, studyStr, token, options, (study, userId, rp, qOptions) -> { + rp.setId(jobId); OpenCGAResult internalResult = internalGet(study.getUid(), jobId, INCLUDE_JOB_IDS, userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Job '" + jobId + "' not found"); @@ -1100,26 +992,11 @@ public OpenCGAResult update(String studyStr, String jobId, JobUpdateParams Job job = internalResult.first(); // We set the proper values for the audit - jobId = job.getId(); - jobUuid = job.getUuid(); - - OpenCGAResult updateResult = update(study, job, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, jobId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update job {}: {}", jobId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, jobId, jobUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); - return result; + return update(study, job, updateParams, qOptions, userId); + }); } private OpenCGAResult update(Study study, Job job, JobUpdateParams updateParams, QueryOptions options, String userId) @@ -1211,11 +1088,6 @@ public OpenCGAResult update(String studyId, Query query, ObjectMap paramete public OpenCGAResult update(String studyId, Query query, ObjectMap parameters, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("query", query) @@ -1224,56 +1096,44 @@ public OpenCGAResult update(String studyId, Query query, ObjectMap paramete .append("options", options) .append("token", token); - ParamUtils.checkObj(parameters, "parameters"); - - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); + return runBatch(auditParams, Enums.Action.UPDATE, JOB, studyId, token, options, (study, userId, qOptions, operationUuid) -> { + ParamUtils.checkObj(parameters, "parameters"); - DBIterator iterator; - try { + Query finalQuery = query != null ? new Query(query) : new Query(); fixQueryObject(study, finalQuery, token); finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = jobDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_JOB_IDS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Job job = iterator.next(); - try { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.WRITE); - - OpenCGAResult updateResult = jobDBAdaptor.update(job.getUid(), parameters, options); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, job.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + try (DBIterator iterator = jobDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_JOB_IDS, userId)) { + OpenCGAResult result = OpenCGAResult.empty(Job.class); + while (iterator.hasNext()) { + Job job = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, JOB, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + + authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.WRITE); + + OpenCGAResult updateResult = jobDBAdaptor.update(job.getUid(), parameters, qo); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, job.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot update job {}: {}", job.getId(), e.getMessage()); + } + } - logger.error("Cannot update job {}: {}", job.getId(), e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - - return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyId, String jobId, ObjectMap parameters, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("jobId", jobId) @@ -1281,11 +1141,10 @@ public OpenCGAResult update(String studyId, String jobId, ObjectMap paramet .append("options", options) .append("token", token); - ParamUtils.checkObj(parameters, "parameters"); + return run(auditParams, Enums.Action.UPDATE, JOB, studyId, token, options, (study, userId, rp, qOptions) -> { + rp.setId(jobId); + ParamUtils.checkObj(parameters, "parameters"); - OpenCGAResult result = OpenCGAResult.empty(); - String jobUuid = ""; - try { OpenCGAResult internalResult = internalGet(study.getUid(), jobId, QueryOptions.empty(), userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Job '" + jobId + "' not found"); @@ -1293,30 +1152,13 @@ public OpenCGAResult update(String studyId, String jobId, ObjectMap paramet Job job = internalResult.first(); // We set the proper values for the audit - jobId = job.getId(); - jobUuid = job.getUuid(); - - options = ParamUtils.defaultObject(options, QueryOptions::new); + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.WRITE); - OpenCGAResult updateResult = jobDBAdaptor.update(job.getUid(), parameters, options); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, jobId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update job {}: {}", jobId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, jobId, jobUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - return result; + return jobDBAdaptor.update(job.getUid(), parameters, options); + }); } public OpenCGAResult update(String studyId, List jobIds, ObjectMap parameters, QueryOptions options, String token) @@ -1326,11 +1168,6 @@ public OpenCGAResult update(String studyId, List jobIds, ObjectMap public OpenCGAResult update(String studyId, List jobIds, ObjectMap parameters, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("jobIds", jobIds) @@ -1339,67 +1176,69 @@ public OpenCGAResult update(String studyId, List jobIds, ObjectMap .append("options", options) .append("token", token); - ParamUtils.checkObj(parameters, "parameters"); - - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : jobIds) { - String jobId = id; - String jobUuid = ""; + return runBatch(auditParams, Enums.Action.UPDATE, JOB, studyId, token, options, (study, userId, qOptions, operationUuid) -> { + ParamUtils.checkObj(parameters, "parameters"); - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Job '" + id + "' not found"); - } - Job job = internalResult.first(); - - // We set the proper values for the audit - jobId = job.getId(); - jobUuid = job.getUuid(); + OpenCGAResult result = OpenCGAResult.empty(Job.class); + for (String id : jobIds) { + try { + run(auditParams, Enums.Action.UPDATE, JOB, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Job '" + id + "' not found"); + } + Job job = internalResult.first(); - options = ParamUtils.defaultObject(options, QueryOptions::new); + // We set the proper values for the audit + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); - authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.WRITE); + authorizationManager.checkJobPermission(study.getUid(), job.getUid(), userId, JobPermissions.WRITE); - OpenCGAResult updateResult = jobDBAdaptor.update(job.getUid(), parameters, options); - result.append(updateResult); + OpenCGAResult updateResult = jobDBAdaptor.update(job.getUid(), parameters, options); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, job.getId(), job.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, jobId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Cannot update job {}: {}", jobId, e.getMessage()); - auditManager.auditUpdate(operationId, userId, Enums.Resource.JOB, jobId, jobUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error("Cannot update job {}: {}", id, e.getMessage()); + } } - } - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } - public OpenCGAResult top(Query baseQuery, int limit, String token) throws CatalogException { + OpenCGAResult top(Query baseQuery, int limit, String token) throws CatalogException { String userId = userManager.getUserId(token); List studies = studyManager.search(new Query(StudyDBAdaptor.QueryParams.OWNER.key(), userId), new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.UUID.key()), token).getResults() .stream() .map(Study::getUuid) .collect(Collectors.toList()); - return top(studies, baseQuery, limit, token); + return top(studies, baseQuery, limit, userId); } public OpenCGAResult top(String studyStr, Query baseQuery, int limit, String token) throws CatalogException { - if (StringUtils.isEmpty(studyStr)) { - return top(baseQuery, limit, token); - } else { - return top(Collections.singletonList(studyStr), baseQuery, limit, token); - } + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("baseQuery", baseQuery) + .append("limit", limit) + .append("token", token); + + return run(auditParams, Enums.Action.TOP, JOB, null, token, null, (study, userId, rp, qOptions) -> { + if (StringUtils.isEmpty(studyStr)) { + return top(baseQuery, limit, token); + } else { + return top(Collections.singletonList(studyStr), baseQuery, limit, userId); + } + }); } - public OpenCGAResult top(List studiesStr, Query baseQuery, int limit, String token) throws CatalogException { - String userId = userManager.getUserId(token); + private OpenCGAResult top(List studiesStr, Query baseQuery, int limit, String userId) throws CatalogException { fixQueryObject(null, baseQuery, userId); List studies = new ArrayList<>(studiesStr.size()); for (String studyStr : studiesStr) { @@ -1562,45 +1401,54 @@ public OpenCGAResult top(List studiesStr, Query baseQuery, int l @Override public OpenCGAResult rank(String studyId, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(token, "sessionId"); + ObjectMap auditParams = new ObjectMap() + .append("studyId", studyId) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_JOBS); - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - //query.append(CatalogJobDBAdaptor.QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = jobDBAdaptor.rank(query, field, numResults, asc); - } + return run(auditParams, Enums.Action.RANK, JOB, studyId, token, null, (study, userId, rp, qOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_JOBS); + + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(token, "sessionId"); + Query finalQuery = query != null ? new Query(query) : new Query(); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + //query.append(CatalogJobDBAdaptor.QueryParams.STUDY_UID.key(), studyId); + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = jobDBAdaptor.rank(finalQuery, field, numResults, asc); + } - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } @Override public OpenCGAResult groupBy(@Nullable String studyId, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - ParamUtils.checkObj(fields, "fields"); - if (fields == null || fields.size() == 0) { - throw new CatalogException("Empty fields parameter."); - } - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - - // Add study id to the query - query.put(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + ObjectMap auditParams = new ObjectMap() + .append("studyId", studyId) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.GROUP_BY, JOB, studyId, token, options, (study, userId, rp, qOptions) -> { + if (CollectionUtils.isEmpty(fields)) { + throw new CatalogException("Empty fields parameter."); + } - OpenCGAResult queryResult = jobDBAdaptor.groupBy(query, fields, options, userId); + Query finalQuery = query != null ? new Query(query) : new Query(); + // Add study id to the query + finalQuery.put(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return jobDBAdaptor.groupBy(finalQuery, fields, qOptions, userId); + }); } // ************************** ACLs ******************************** // @@ -1611,10 +1459,6 @@ public OpenCGAResult> getAcls(String studyId, List< public OpenCGAResult> getAcls(String studyId, List jobList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("jobList", jobList) @@ -1622,11 +1466,10 @@ public OpenCGAResult> getAcls(String studyId, List< .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> jobAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), jobList, INCLUDE_JOB_IDS, user, ignoreException); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, JOB, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult> jobAcls; + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), jobList, INCLUDE_JOB_IDS, userId, ignoreException); if (queryResult.getMissing() != null) { missingMap = queryResult.getMissing().stream() @@ -1635,9 +1478,9 @@ public OpenCGAResult> getAcls(String studyId, List< List jobUids = queryResult.getResults().stream().map(Job::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - jobAcls = authorizationManager.getAcl(user, study.getUid(), jobUids, members, Enums.Resource.JOB, JobPermissions.class); + jobAcls = authorizationManager.getAcl(userId, study.getUid(), jobUids, members, JOB, JobPermissions.class); } else { - jobAcls = authorizationManager.getAcl(user, study.getUid(), jobUids, Enums.Resource.JOB, JobPermissions.class); + jobAcls = authorizationManager.getAcl(userId, study.getUid(), jobUids, JOB, JobPermissions.class); } // Include non-existing jobs to the result list @@ -1647,49 +1490,31 @@ public OpenCGAResult> getAcls(String studyId, List< for (String jobId : jobList) { if (!missingMap.containsKey(jobId)) { Job job = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, JOB, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + return null; + }); resultList.add(jobAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.JOB, job.getId(), job.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), - new ObjectMap()); counter++; } else { + if (!ignoreException) { + throw new CatalogException(missingMap.get(jobId).getErrorMsg()); + } resultList.add(new AclEntryList<>()); eventList.add(new Event(Event.Type.ERROR, jobId, missingMap.get(jobId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.JOB, jobId, "", study.getId(), - study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", missingMap.get(jobId).getErrorMsg())), - new ObjectMap()); } } jobAcls.setResults(resultList); jobAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String jobId : jobList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.JOB, jobId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String jobId : jobList) { - Event event = new Event(Event.Type.ERROR, jobId, e.getMessage()); - jobAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return jobAcls; + return jobAcls; + }); } public OpenCGAResult> updateAcl(String studyId, List jobStrList, String memberList, AclParams aclParams, ParamUtils.AclAction action, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("jobStrList", jobStrList) @@ -1697,10 +1522,9 @@ public OpenCGAResult> updateAcl(String studyId, Lis .append("aclParams", aclParams) .append("action", action) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, JOB, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); if (jobStrList == null || jobStrList.isEmpty()) { throw new CatalogException("Missing job parameter"); @@ -1718,8 +1542,6 @@ public OpenCGAResult> updateAcl(String studyId, Lis List jobList = internalGet(study.getUid(), jobStrList, INCLUDE_JOB_IDS, userId, false).getResults(); - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); - // Validate that the members are actually valid members List members; if (memberList != null && !memberList.isEmpty()) { @@ -1731,8 +1553,7 @@ public OpenCGAResult> updateAcl(String studyId, Lis checkMembers(study.getUid(), members); List jobUids = jobList.stream().map(Job::getUid).collect(Collectors.toList()); - AuthorizationManager.CatalogAclParams catalogAclParams = new AuthorizationManager.CatalogAclParams(jobUids, permissions, - Enums.Resource.JOB); + AuthorizationManager.CatalogAclParams catalogAclParams = new AuthorizationManager.CatalogAclParams(jobUids, permissions, JOB); switch (action) { case SET: @@ -1751,66 +1572,41 @@ public OpenCGAResult> updateAcl(String studyId, Lis default: throw new CatalogException("Unexpected error occurred. No valid action found."); } - OpenCGAResult> queryResultList = authorizationManager.getAcls(study.getUid(), jobUids, - members, Enums.Resource.JOB, JobPermissions.class); - for (Job job : jobList) { - auditManager.audit(operationId, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.JOB, job.getId(), - job.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); - } - return queryResultList; - } catch (CatalogException e) { - if (jobStrList != null) { - for (String jobId : jobStrList) { - auditManager.audit(operationId, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.JOB, jobId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - e.getError()), new ObjectMap()); - } + run(auditParams, Enums.Action.UPDATE_ACLS, JOB, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(job.getId()); + rp.setUuid(job.getUuid()); + return null; + }); } - throw e; - } finally { - auditManager.finishAuditBatch(operationId); - } + + return authorizationManager.getAcls(study.getUid(), jobUids, members, JOB, JobPermissions.class); + }); } public DataResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) - throws CatalogException, IOException { - String userId = userManager.getUserId(token); - // We need to add variableSets and groups to avoid additional queries as it will be used in the catalogSolrManager - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()))); - - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - + throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("defaultStats", defaultStats) .append("token", token); - try { - if (defaultStats || StringUtils.isEmpty(options.getString(QueryOptions.FACET))) { - String facet = options.getString(QueryOptions.FACET); - options.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); - } - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); - - try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { - DataResult result = catalogSolrManager.facetedQuery(study, CatalogSolrManager.JOB_SOLR_COLLECTION, query, - options, userId); + return run(auditParams, Enums.Action.FACET, JOB, studyId, token, options, + Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()), + (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + if (defaultStats || StringUtils.isEmpty(qOptions.getString(QueryOptions.FACET))) { + String facet = qOptions.getString(QueryOptions.FACET); + qOptions.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); + } + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); - auditManager.auditFacet(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } - } catch (CatalogException e) { - auditManager.auditFacet(userId, Enums.Resource.JOB, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage()))); - throw e; - } + try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { + return catalogSolrManager.facetedQuery(study, CatalogSolrManager.JOB_SOLR_COLLECTION, finalQuery, qOptions, userId); + } + }); } } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/PanelManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/PanelManager.java index 09ba5a9f859..09d468366e4 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/PanelManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/PanelManager.java @@ -24,7 +24,6 @@ import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -45,7 +44,6 @@ import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; import org.opencb.opencga.core.models.AclParams; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.panel.Panel; import org.opencb.opencga.core.models.panel.PanelInternal; @@ -59,7 +57,6 @@ import javax.annotation.Nullable; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; @@ -68,6 +65,7 @@ import java.util.stream.Collectors; import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; +import static org.opencb.opencga.core.models.common.Enums.Resource.DISEASE_PANEL; public class PanelManager extends ResourceManager { @@ -87,8 +85,8 @@ public class PanelManager extends ResourceManager { } @Override - Enums.Resource getEntity() { - return Enums.Resource.DISEASE_PANEL; + Enums.Resource getResource() { + return DISEASE_PANEL; } @Override @@ -161,50 +159,35 @@ private OpenCGAResult getPanel(long studyUid, String panelUuid, QueryOpti @Override public OpenCGAResult create(String studyStr, Panel panel, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("panel", panel) .append("options", options) .append("token", token); - try { + + return run(auditParams, Enums.Action.CREATE, DISEASE_PANEL, studyStr, token, options, (study, userId, rp, qOptions) -> { // 1. We check everything can be done authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_PANELS); autoCompletePanel(study, panel); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - OpenCGAResult insert = panelDBAdaptor.insert(study.getUid(), panel, options); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + OpenCGAResult insert = panelDBAdaptor.insert(study.getUid(), panel, qOptions); + if (qOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch created panel - OpenCGAResult result = getPanel(study.getUid(), panel.getUuid(), options); + OpenCGAResult result = getPanel(study.getUid(), panel.getUuid(), qOptions); insert.setResults(result.getResults()); } - auditManager.auditCreate(userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return insert; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.DISEASE_PANEL, panel.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult importFromSource(String studyId, String source, String panelIds, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("source", source) .append("panelIds", panelIds) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - try { + return runBatch(auditParams, Enums.Action.IMPORT, DISEASE_PANEL, studyId, token, null, (study, userId, qOptions, operationUuid) -> { // 1. We check everything can be done authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_PANELS); ParamUtils.checkParameter(source, "source"); @@ -245,7 +228,7 @@ public OpenCGAResult importFromSource(String studyId, String source, Stri } } - OpenCGAResult result = OpenCGAResult.empty(); + OpenCGAResult result = OpenCGAResult.empty(Panel.class); List importedPanels = new LinkedList<>(); for (String auxSource : sources) { // Obtain available panel ids from panel host @@ -289,28 +272,18 @@ public OpenCGAResult importFromSource(String studyId, String source, Stri result.append(panelDBAdaptor.insert(study.getUid(), panelList)); } result.setResults(importedPanels); - auditManager.initAuditBatch(operationId); + // Audit creation for (Panel importedPanel : importedPanels) { - auditManager.audit(operationId, userId, Enums.Action.IMPORT, Enums.Resource.DISEASE_PANEL, importedPanel.getId(), - importedPanel.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + run(auditParams, Enums.Action.IMPORT, DISEASE_PANEL, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(importedPanel.getId()); + rp.setUuid(importedPanel.getUuid()); + return null; + }); } - auditManager.finishAuditBatch(operationId); return result; - } catch (CatalogException e) { - auditManager.audit(operationId, userId, Enums.Action.IMPORT, Enums.Resource.DISEASE_PANEL, "", "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - throw e; - } catch (IOException e) { - CatalogException exception = new CatalogException("Error parsing panels: " + e.getMessage(), e); - auditManager.audit(operationId, userId, Enums.Action.IMPORT, Enums.Resource.DISEASE_PANEL, "", "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - exception.getError()), new ObjectMap()); - throw exception; - } + }); } private void autoCompletePanel(Study study, Panel panel) throws CatalogException { @@ -342,20 +315,12 @@ public OpenCGAResult update(String studyId, Query query, PanelUpdateParam public OpenCGAResult update(String studyId, Query query, PanelUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse PanelUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("query", query) @@ -363,50 +328,41 @@ public OpenCGAResult update(String studyId, Query query, PanelUpdateParam .append("ignoreException", ignoreException) .append("options", options) .append("token", token); - fixQueryObject(finalQuery); - DBIterator iterator; - try { + return runBatch(auditParams, Enums.Action.UPDATE, DISEASE_PANEL, studyId, token, options, (study, userId, qOptions, + operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); finalQuery.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = panelDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_PANEL_IDS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.DISEASE_PANEL, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Panel panel = iterator.next(); - try { - OpenCGAResult updateResult = update(study, panel, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, panel.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update panel {}: {}", panel.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } - } - auditManager.finishAuditBatch(operationId); + try (DBIterator iterator = panelDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_PANEL_IDS, userId)) { + OpenCGAResult result = OpenCGAResult.empty(Panel.class); + while (iterator.hasNext()) { + Panel panel = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, DISEASE_PANEL, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); + OpenCGAResult updateResult = update(study, panel, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, panel.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update panel {}: {}", panel.getId(), e.getMessage(), e); + } + } - return endResult(result, ignoreException); + return endResult(result, ignoreException); + } + }); } public OpenCGAResult update(String studyStr, String panelId, PanelUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -421,36 +377,20 @@ public OpenCGAResult update(String studyStr, String panelId, PanelUpdateP .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String panelUuid = ""; - try { + return run(auditParams, Enums.Action.UPDATE, DISEASE_PANEL, studyStr, token, options, (study, userId, rp, queryOptions) -> { + rp.setId(panelId); + OpenCGAResult internalResult = internalGet(study.getUid(), panelId, QueryOptions.empty(), userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Panel '" + panelId + "' not found"); } Panel panel = internalResult.first(); - // We set the proper values for the audit - panelId = panel.getId(); - panelUuid = panel.getUuid(); - - OpenCGAResult updateResult = update(study, panel, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, panelId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update panel {}: {}", panelId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.DISEASE_PANEL, panelId, panelUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); - return result; + return update(study, panel, updateParams, options, userId); + }); } /** @@ -472,11 +412,6 @@ public OpenCGAResult update(String studyStr, List panelIds, Panel public OpenCGAResult update(String studyStr, List panelIds, PanelUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -492,41 +427,38 @@ public OpenCGAResult update(String studyStr, List panelIds, Panel .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : panelIds) { - String panelId = id; - String panelUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), panelId, QueryOptions.empty(), userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Panel '" + id + "' not found"); + return runBatch(auditParams, Enums.Action.UPDATE, DISEASE_PANEL, studyStr, token, options, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Panel.class); + for (String id : panelIds) { + try { + run(auditParams, Enums.Action.UPDATE, DISEASE_PANEL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, QueryOptions.empty(), userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Panel '" + id + "' not found"); + } + Panel panel = internalResult.first(); + + // We set the proper values for the audit + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); + + OpenCGAResult updateResult = update(study, panel, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update panel {}: {}", id, e.getMessage(), e); } - Panel panel = internalResult.first(); - - // We set the proper values for the audit - panelId = panel.getId(); - panelUuid = panel.getUuid(); - - OpenCGAResult updateResult = update(study, panel, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, panelId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update panel {}: {}", panelId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.DISEASE_PANEL, panelId, panelUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, Panel panel, PanelUpdateParams updateParams, QueryOptions options, String userId) @@ -563,107 +495,70 @@ private OpenCGAResult update(Study study, Panel panel, PanelUpdateParams updateP } @Override - public DBIterator iterator(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); + public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("options", options) + .append("token", token); - fixQueryObject(query); - query.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return panelDBAdaptor.iterator(study.getUid(), query, options, userId); + return run(auditParams, Enums.Action.ITERATE, DISEASE_PANEL, studyStr, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return panelDBAdaptor.iterator(study.getUid(), finalQuery, queryOptions, userId); + }); } @Override public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("token", token); - try { - fixQueryObject(query); - query.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = panelDBAdaptor.get(study.getUid(), query, options, userId); - - auditManager.auditSearch(userId, Enums.Resource.DISEASE_PANEL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.DISEASE_PANEL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.SEARCH, DISEASE_PANEL, studyId, token, options, + Collections.singletonList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key()), (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return panelDBAdaptor.get(study.getUid(), finalQuery, qOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) - .append("query", new Query(query)) + .append("field", field) + .append("query", query) .append("token", token); - fixQueryObject(query); - try { - fixQueryObject(query); - - query.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = panelDBAdaptor.distinct(study.getUid(), field, query, userId); - auditManager.auditDistinct(userId, Enums.Resource.DISEASE_PANEL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.DISEASE_PANEL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.DISTINCT, DISEASE_PANEL, studyId, token, null, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return panelDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } @Override public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("token", token); - fixQueryObject(query); - try { - query.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - // Here view permissions will be checked - OpenCGAResult queryResultAux = panelDBAdaptor.count(query, userId); + return run(auditParams, Enums.Action.COUNT, DISEASE_PANEL, studyId, token, null, + Collections.singletonList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key()), (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - auditManager.auditCount(userId, Enums.Resource.DISEASE_PANEL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.DISEASE_PANEL, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + // Here view permissions will be checked + return panelDBAdaptor.count(finalQuery, userId); + }); } @Override @@ -673,15 +568,6 @@ public OpenCGAResult delete(String studyStr, List panelIds, QueryOptions public OpenCGAResult delete(String studyStr, List panelIds, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - if (panelIds == null || ListUtils.isEmpty(panelIds)) { - throw new CatalogException("Missing list of panel ids"); - } - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("panelIds", panelIds) @@ -689,59 +575,53 @@ public OpenCGAResult delete(String studyStr, List panelIds, ObjectMap pa .append("ignoreException", ignoreException) .append("token", token); - boolean checkPermissions; - try { - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationId, userId, Enums.Resource.DISEASE_PANEL, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : panelIds) { - - String panelId = id; - String panelUuid = ""; - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_PANEL_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Panel '" + id + "' not found"); - } + return runBatch(auditParams, Enums.Action.DELETE, DISEASE_PANEL, studyStr, token, null, (study, userId, qOptions, + operationUuid) -> { + if (CollectionUtils.isEmpty(panelIds)) { + throw new CatalogException("Missing list of panel ids"); + } - Panel panel = internalResult.first(); - // We set the proper values for the audit - panelId = panel.getId(); - panelUuid = panel.getUuid(); + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Panel.class); + for (String id : panelIds) { + try { + run(auditParams, Enums.Action.DELETE, DISEASE_PANEL, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_PANEL_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Panel '" + id + "' not found"); + } - if (checkPermissions) { - authorizationManager.checkPanelPermission(study.getUid(), panel.getUid(), userId, - PanelPermissions.DELETE); - } + Panel panel = internalResult.first(); + // We set the proper values for the audit + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); - // Check if the panel can be deleted - // TODO: Check if the panel is used in an interpretation. At this point, it can be deleted no matter what. + if (checkPermissions) { + authorizationManager.checkPanelPermission(study.getUid(), panel.getUid(), userId, + PanelPermissions.DELETE); + } - // Delete the panel - result.append(panelDBAdaptor.delete(panel)); + // Check if the panel can be deleted + // TODO: Check if the panel is used in an interpretation. At this point, it can be deleted no matter what. - auditManager.auditDelete(operationId, userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, id, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + // Delete the panel + result.append(panelDBAdaptor.delete(panel)); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error("Cannot delete panel {}: {}", panelId, e.getMessage()); - auditManager.auditDelete(operationId, userId, Enums.Resource.DISEASE_PANEL, panelId, panelUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error("Cannot delete panel {}: {}", id, e.getMessage()); + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -751,119 +631,111 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) - .append("query", new Query(query)) + .append("query", query) .append("params", params) .append("ignoreException", ignoreException) .append("token", token); - fixQueryObject(finalQuery); - - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; - - // We try to get an iterator containing all the families to be deleted - DBIterator iterator; - try { - finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - - iterator = panelDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_PANEL_IDS, userId); + return runBatch(auditParams, Enums.Action.DELETE, DISEASE_PANEL, studyStr, token, null, (study, userId, qOptions, + operationUuid) -> { // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationId, userId, Enums.Resource.DISEASE_PANEL, "", "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - auditManager.initAuditBatch(operationId); - while (iterator.hasNext()) { - Panel panel = iterator.next(); + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.append(FamilyDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - try { - if (checkPermissions) { - authorizationManager.checkPanelPermission(study.getUid(), panel.getUid(), userId, - PanelPermissions.DELETE); + OpenCGAResult result = OpenCGAResult.empty(Panel.class); + try (DBIterator iterator = panelDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_PANEL_IDS, userId)) { + while (iterator.hasNext()) { + Panel panel = iterator.next(); + try { + run(auditParams, Enums.Action.DELETE, DISEASE_PANEL, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); + + if (checkPermissions) { + authorizationManager.checkPanelPermission(study.getUid(), panel.getUid(), userId, PanelPermissions.DELETE); + } + + // Check if the panel can be deleted + // TODO: Check if the panel is used in an interpretation. At this point, it can be deleted no matter what. + + // Delete the panel + result.append(panelDBAdaptor.delete(panel)); + return null; + }); + } catch (CatalogException e) { + String errorMsg = "Cannot delete panel " + panel.getId() + ": " + e.getMessage(); + + Event event = new Event(Event.Type.ERROR, panel.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error(errorMsg); + } } - // Check if the panel can be deleted - // TODO: Check if the panel is used in an interpretation. At this point, it can be deleted no matter what. - - // Delete the panel - result.append(panelDBAdaptor.delete(panel)); - - auditManager.auditDelete(operationId, userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete panel " + panel.getId() + ": " + e.getMessage(); - - Event event = new Event(Event.Type.ERROR, panel.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error(errorMsg); - auditManager.auditDelete(operationId, userId, Enums.Resource.DISEASE_PANEL, panel.getId(), panel.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } @Override - public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String sessionId) + public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(sessionId, "sessionId"); - - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_PANELS); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); - fixQueryObject(query); + return run(auditParams, Enums.Action.RANK, DISEASE_PANEL, studyStr, token, null, (study, userId, rp, queryOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_PANELS); + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(token, "sessionId"); + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - query.append(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = panelDBAdaptor.rank(query, field, numResults, asc); - } + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = panelDBAdaptor.rank(finalQuery, field, numResults, asc); + } - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - fixQueryObject(query); - options = ParamUtils.defaultObject(options, QueryOptions::new); - if (fields == null || fields.size() == 0) { - throw new CatalogException("Empty fields parameter."); - } + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + return run(auditParams, Enums.Action.GROUP_BY, DISEASE_PANEL, studyStr, token, options, (study, userId, rp, qOptions) -> { + if (CollectionUtils.isEmpty(fields)) { + throw new CatalogException("Empty fields parameter."); + } - // Add study id to the query - query.put(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); + finalQuery.put(PanelDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = sampleDBAdaptor.groupBy(query, fields, options, userId); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + OpenCGAResult queryResult = sampleDBAdaptor.groupBy(finalQuery, fields, options, userId); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } // ************************** ACLs ******************************** // @@ -874,10 +746,6 @@ public OpenCGAResult> getAcls(String studyId, Lis public OpenCGAResult> getAcls(String studyId, List panelList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("panelList", panelList) @@ -885,11 +753,11 @@ public OpenCGAResult> getAcls(String studyId, Lis .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> panelAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), panelList, INCLUDE_PANEL_IDS, user, ignoreException); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, DISEASE_PANEL, studyId, token, null, (study, userId, qOptions, + operationUuid) -> { + OpenCGAResult> panelAcls; + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), panelList, INCLUDE_PANEL_IDS, userId, ignoreException); if (queryResult.getMissing() != null) { missingMap = queryResult.getMissing().stream() @@ -898,11 +766,9 @@ public OpenCGAResult> getAcls(String studyId, Lis List panelUids = queryResult.getResults().stream().map(Panel::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - panelAcls = authorizationManager.getAcl(user, study.getUid(), panelUids, members, Enums.Resource.DISEASE_PANEL, - PanelPermissions.class); + panelAcls = authorizationManager.getAcl(userId, study.getUid(), panelUids, members, DISEASE_PANEL, PanelPermissions.class); } else { - panelAcls = authorizationManager.getAcl(user, study.getUid(), panelUids, Enums.Resource.DISEASE_PANEL, - PanelPermissions.class); + panelAcls = authorizationManager.getAcl(userId, study.getUid(), panelUids, DISEASE_PANEL, PanelPermissions.class); } // Include non-existing panels to the result list @@ -912,49 +778,32 @@ public OpenCGAResult> getAcls(String studyId, Lis for (String panelId : panelList) { if (!missingMap.containsKey(panelId)) { Panel panel = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, DISEASE_PANEL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); + return null; + }); resultList.add(panelAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.DISEASE_PANEL, panel.getId(), - panel.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); counter++; } else { + if (!ignoreException) { + throw new CatalogException(missingMap.get(panelId).getErrorMsg()); + } resultList.add(new AclEntryList<>()); eventList.add(new Event(Event.Type.ERROR, panelId, missingMap.get(panelId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.DISEASE_PANEL, panelId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(panelId).getErrorMsg())), new ObjectMap()); } } panelAcls.setResults(resultList); panelAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String panelId : panelList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.DISEASE_PANEL, panelId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String panelId : panelList) { - Event event = new Event(Event.Type.ERROR, panelId, e.getMessage()); - panelAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return panelAcls; + return panelAcls; + }); } public OpenCGAResult> updateAcl(String studyId, List panelStrList, String memberList, AclParams aclParams, ParamUtils.AclAction action, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("panelStrList", panelStrList) @@ -962,11 +811,10 @@ public OpenCGAResult> updateAcl(String studyId, L .append("aclParams", aclParams) .append("action", action) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, DISEASE_PANEL, studyId, token, null, (study, userId, qOptions, + operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); if (panelStrList == null || panelStrList.isEmpty()) { throw new CatalogException("Update ACL: Missing panel parameter"); } @@ -981,8 +829,7 @@ public OpenCGAResult> updateAcl(String studyId, L checkPermissions(permissions, PanelPermissions::valueOf); } - OpenCGAResult panelDataResult = internalGet(study.getUid(), panelStrList, INCLUDE_PANEL_IDS, user, false); - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), user); + OpenCGAResult panelDataResult = internalGet(study.getUid(), panelStrList, INCLUDE_PANEL_IDS, userId, false); // Validate that the members are actually valid members List members; @@ -996,7 +843,7 @@ public OpenCGAResult> updateAcl(String studyId, L List panelUids = panelDataResult.getResults().stream().map(Panel::getUid).collect(Collectors.toList()); AuthorizationManager.CatalogAclParams catalogAclParams = new AuthorizationManager.CatalogAclParams(panelUids, permissions, - Enums.Resource.DISEASE_PANEL); + DISEASE_PANEL); switch (action) { case SET: @@ -1015,27 +862,18 @@ public OpenCGAResult> updateAcl(String studyId, L default: throw new CatalogException("Unexpected error occurred. No valid action found."); } - OpenCGAResult> queryResultList = authorizationManager.getAcls(study.getUid(), - panelUids, members, Enums.Resource.DISEASE_PANEL, PanelPermissions.class); for (Panel panel : panelDataResult.getResults()) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.DISEASE_PANEL, panel.getId(), - panel.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + // To audit + run(auditParams, Enums.Action.UPDATE_ACLS, DISEASE_PANEL, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(panel.getId()); + rp.setUuid(panel.getUuid()); + return null; + }); } - return queryResultList; - } catch (CatalogException e) { - if (panelStrList != null) { - for (String panelId : panelStrList) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.DISEASE_PANEL, panelId, "", - study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - } - throw e; - } finally { - auditManager.finishAuditBatch(operationId); - } + + return authorizationManager.getAcls(study.getUid(), panelUids, members, DISEASE_PANEL, PanelPermissions.class); + }); } protected void fixQueryObject(Query query) { diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ProjectManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ProjectManager.java index 1806148ed97..35f6686115b 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ProjectManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ProjectManager.java @@ -22,7 +22,6 @@ import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -36,7 +35,6 @@ import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.config.storage.CellBaseConfiguration; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.cohort.Cohort; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.individual.Individual; @@ -55,6 +53,7 @@ import java.util.stream.Collectors; import static org.opencb.opencga.core.common.JacksonUtils.getDefaultObjectMapper; +import static org.opencb.opencga.core.models.common.Enums.Resource.PROJECT; /** * @author Jacobo Coll <jacobo167@gmail.com> @@ -197,13 +196,13 @@ private OpenCGAResult getProject(String userId, String projectUuid, Que * * @param userId user whose projects and studies are being shared with. * @param queryOptions QueryOptions object. - * @param sessionId Session id which should correspond to userId. + * @param token Session id which should correspond to userId. * @return A OpenCGAResult object containing the list of projects and studies that are shared with the user. * @throws CatalogException CatalogException */ - public OpenCGAResult getSharedProjects(String userId, QueryOptions queryOptions, String sessionId) throws CatalogException { + public OpenCGAResult getSharedProjects(String userId, QueryOptions queryOptions, String token) throws CatalogException { OpenCGAResult result = search(new Query(ProjectDBAdaptor.QueryParams.USER_ID.key(), "!=" + userId), queryOptions, - sessionId); + token); for (Event event : result.getEvents()) { if (event.getType() == Event.Type.ERROR) { throw new CatalogAuthorizationException(event.getMessage()); @@ -222,23 +221,23 @@ public OpenCGAResult create(String id, String name, String description, public OpenCGAResult create(ProjectCreateParams projectCreateParams, QueryOptions options, String token) throws CatalogException { - //Only the user can create a project - String userId = this.catalogManager.getUserManager().getUserId(token); - if (userId.isEmpty()) { - throw new CatalogException("The token introduced does not correspond to any registered user."); - } - ObjectMap auditParams = new ObjectMap() .append("project", projectCreateParams) .append("options", options) .append("token", token); - options = ParamUtils.defaultObject(options, QueryOptions::new); - OpenCGAResult queryResult; - Project project; - try { + return run(auditParams, Enums.Action.CREATE, PROJECT, null, token, options, (study, userId, rp, queryOptions) -> { + //Only the user can create a project + if (userId.isEmpty()) { + throw new CatalogException("The token introduced does not correspond to any registered user."); + } ParamUtils.checkObj(projectCreateParams, "ProjectCreateParams"); + rp.setId(projectCreateParams.getId()); + + OpenCGAResult queryResult; + Project project; + // Check that the account type is not guest OpenCGAResult user = userDBAdaptor.get(userId, QueryOptions.empty()); if (user.getNumResults() == 0) { @@ -250,57 +249,46 @@ public OpenCGAResult create(ProjectCreateParams projectCreateParams, Qu // Check it is the first project if (user.first().getProjects() != null && !user.first().getProjects().isEmpty()) { String errorMsg = "Cannot create more projects for ADMINISTRATOR user '" + user.first().getId() + "'."; - auditManager.auditCreate(userId, Enums.Resource.PROJECT, projectCreateParams.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", errorMsg))); throw new CatalogException(errorMsg); } } else { - String errorMsg = "User " + userId + " is not authorized to create new projects. Only users with " + Account.AccountType.FULL + " accounts are allowed to do so."; - auditManager.auditCreate(userId, Enums.Resource.PROJECT, projectCreateParams.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", errorMsg))); throw new CatalogException(errorMsg); } } project = projectCreateParams.toProject(); validateProjectForCreation(project, user.first()); + rp.setId(project.getId()); + rp.setUuid(project.getUuid()); - queryResult = projectDBAdaptor.insert(project, userId, options); - OpenCGAResult result = getProject(userId, project.getUuid(), options); + queryResult = projectDBAdaptor.insert(project, userId, queryOptions); + OpenCGAResult result = getProject(userId, project.getUuid(), queryOptions); project = result.first(); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + if (queryOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch created project queryResult.setResults(result.getResults()); } - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.PROJECT, projectCreateParams.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - try { - catalogIOManager.createProject(userId, Long.toString(project.getUid())); - } catch (CatalogIOException e) { - auditManager.auditCreate(userId, Enums.Resource.PROJECT, project.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); try { - projectDBAdaptor.delete(project); - } catch (Exception e1) { - logger.error("Error deleting project from catalog after failing creating the folder in the filesystem", e1); + catalogIOManager.createProject(userId, Long.toString(project.getUid())); + } catch (CatalogIOException e) { + try { + projectDBAdaptor.delete(project); + } catch (Exception e1) { + logger.error("Error deleting project from catalog after failing creating the folder in the filesystem", e1); + throw e; + } throw e; } - throw e; - } - auditManager.auditCreate(userId, Enums.Resource.PROJECT, project.getId(), project.getUuid(), "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return queryResult; + return queryResult; + }); } private void validateProjectForCreation(Project project, User user) throws CatalogParameterException { - ParamUtils.checkParameter(project.getId(), ProjectDBAdaptor.QueryParams.ID.key()); + ParamUtils.checkIdentifier(project.getId(), ProjectDBAdaptor.QueryParams.ID.key()); project.setName(ParamUtils.defaultString(project.getName(), project.getId())); project.setDescription(ParamUtils.defaultString(project.getDescription(), "")); project.setCreationDate(ParamUtils.checkDateOrGetCurrentDate(project.getCreationDate(), @@ -337,23 +325,17 @@ private void validateProjectForCreation(Project project, User user) throws Catal * @throws CatalogException CatalogException */ public OpenCGAResult get(String projectId, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - ObjectMap auditParams = new ObjectMap() .append("projectId", projectId) .append("options", options) .append("token", token); - try { + + return run(auditParams, Enums.Action.INFO, PROJECT, null, token, options, (study, userId, rp, queryOptions) -> { Project project = resolveId(projectId, userId); - OpenCGAResult queryResult = projectDBAdaptor.get(project.getUid(), options); - auditManager.auditInfo(userId, Enums.Resource.PROJECT, project.getId(), project.getUuid(), "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return queryResult; - } catch (CatalogException e) { - auditManager.auditInfo(userId, Enums.Resource.PROJECT, projectId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(project.getId()); + rp.setUuid(project.getUuid()); + return projectDBAdaptor.get(project.getUid(), queryOptions); + }); } public OpenCGAResult get(List projectList, QueryOptions options, boolean ignoreException, String sessionId) @@ -390,36 +372,25 @@ public OpenCGAResult get(List projectList, QueryOptions options * @throws CatalogException CatalogException */ public OpenCGAResult search(Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - String userId = catalogManager.getUserManager().getUserId(token); - query = new Query(query); - ObjectMap auditParams = new ObjectMap() .append("query", query) .append("options", options) .append("token", token); - try { - fixQueryObject(query); + return run(auditParams, Enums.Action.SEARCH, PROJECT, null, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(finalQuery); // If study is provided, we need to check if it will be study alias or id - if (StringUtils.isNotEmpty(query.getString(ProjectDBAdaptor.QueryParams.STUDY.key()))) { + if (StringUtils.isNotEmpty(finalQuery.getString(ProjectDBAdaptor.QueryParams.STUDY.key()))) { List studies = catalogManager.getStudyManager() - .resolveIds(query.getAsStringList(ProjectDBAdaptor.QueryParams.STUDY.key()), userId); - query.remove(ProjectDBAdaptor.QueryParams.STUDY.key()); - query.put(ProjectDBAdaptor.QueryParams.STUDY_UID.key(), studies.stream().map(Study::getUid).collect(Collectors.toList())); + .resolveIds(finalQuery.getAsStringList(ProjectDBAdaptor.QueryParams.STUDY.key()), userId); + finalQuery.remove(ProjectDBAdaptor.QueryParams.STUDY.key()); + finalQuery.put(ProjectDBAdaptor.QueryParams.STUDY_UID.key(), + studies.stream().map(Study::getUid).collect(Collectors.toList())); } - OpenCGAResult queryResult = projectDBAdaptor.get(query, options, userId); - auditManager.auditSearch(userId, Enums.Resource.PROJECT, "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.PROJECT, "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - - throw e; - } + return projectDBAdaptor.get(finalQuery, queryOptions, userId); + }); } /** @@ -451,27 +422,20 @@ public OpenCGAResult update(String projectId, ObjectMap parameters, Que private OpenCGAResult update(String projectId, ObjectMap parameters, QueryOptions options, boolean allowProtectedUpdates, String token) throws CatalogException { - String userId = this.catalogManager.getUserManager().getUserId(token); - ObjectMap auditParams = new ObjectMap() .append("project", projectId) .append("updateParams", parameters) .append("options", options) + .append("allowProtectedUpdates", allowProtectedUpdates) .append("token", token); - Project project; - try { - project = resolveId(projectId, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.PROJECT, projectId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.UPDATE, PROJECT, null, token, options, (study, userId, rp, queryOptions) -> { + Project project = resolveId(projectId, userId); + rp.setId(project.getId()); + rp.setUuid(project.getUuid()); - try { ParamUtils.checkObj(parameters, "Parameters"); ParamUtils.checkParameter(token, "token"); - options = ParamUtils.defaultObject(options, QueryOptions::new); long projectUid = project.getUid(); authorizationManager.checkCanEditProject(projectUid, userId); @@ -529,9 +493,6 @@ private OpenCGAResult update(String projectId, ObjectMap parameters, Qu } OpenCGAResult update = projectDBAdaptor.update(projectUid, parameters, QueryOptions.empty()); - auditManager.auditUpdate(userId, Enums.Resource.PROJECT, project.getId(), project.getUuid(), "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch updated project OpenCGAResult result = projectDBAdaptor.get(new Query(ProjectDBAdaptor.QueryParams.UID.key(), projectUid), options, @@ -540,11 +501,7 @@ private OpenCGAResult update(String projectId, ObjectMap parameters, Qu } return update; - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.PROJECT, project.getId(), project.getUuid(), "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult setDatastoreVariant(String projectStr, DataStore dataStore, String token) throws CatalogException { @@ -559,28 +516,48 @@ public OpenCGAResult setCellbaseConfiguration(String projectStr, CellBa } public Map facet(String projectStr, String fileFields, String sampleFields, String individualFields, - String cohortFields, String familyFields, String jobFields, boolean defaultStats, String sessionId) + String cohortFields, String familyFields, String jobFields, boolean defaultStats, String token) throws CatalogException, IOException { - String userId = catalogManager.getUserManager().getUserId(sessionId); - Project project = resolveId(projectStr, userId); - Query query = new Query(StudyDBAdaptor.QueryParams.PROJECT_UID.key(), project.getUid()); - OpenCGAResult studyDataResult = catalogManager.getStudyManager().search(query, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.FQN.key(), StudyDBAdaptor.QueryParams.ID.key())), sessionId); - - Map result = new HashMap<>(); - for (Study study : studyDataResult.getResults()) { - result.put(study.getId(), catalogManager.getStudyManager().facet(study.getFqn(), fileFields, sampleFields, individualFields, - cohortFields, familyFields, jobFields, defaultStats, sessionId)); - } + ObjectMap auditParams = new ObjectMap() + .append("projectStr", projectStr) + .append("fileFields", fileFields) + .append("sampleFields", sampleFields) + .append("individualFields", individualFields) + .append("cohortFields", cohortFields) + .append("familyFields", familyFields) + .append("jobFields", jobFields) + .append("defaultStats", defaultStats) + .append("token", token); - return result; + return run(auditParams, Enums.Action.FACET, PROJECT, null, token, null, (s, userId, rp, queryOptions) -> { + rp.setId(projectStr); + Project project = resolveId(projectStr, userId); + rp.setId(project.getId()); + rp.setUuid(project.getUuid()); + Query query = new Query(StudyDBAdaptor.QueryParams.PROJECT_UID.key(), project.getUid()); + OpenCGAResult studyDataResult = catalogManager.getStudyManager().search(query, new QueryOptions(QueryOptions.INCLUDE, + Arrays.asList(StudyDBAdaptor.QueryParams.FQN.key(), StudyDBAdaptor.QueryParams.ID.key())), token); + + Map result = new HashMap<>(); + for (Study study : studyDataResult.getResults()) { + result.put(study.getId(), catalogManager.getStudyManager().facet(study.getFqn(), fileFields, sampleFields, individualFields, + cohortFields, familyFields, jobFields, defaultStats, token)); + } + + return result; + }); } - public OpenCGAResult incrementRelease(String projectStr, String sessionId) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(sessionId); + public OpenCGAResult incrementRelease(String projectStr, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("projectStr", projectStr) + .append("token", token); - try { + return run(auditParams, Enums.Action.UPDATE, PROJECT, null, token, null, (s, userId, rp, queryOptions) -> { + rp.setId(projectStr); Project project = resolveId(projectStr, userId); + rp.setId(project.getId()); + rp.setUuid(project.getUuid()); long projectUid = project.getUid(); authorizationManager.checkCanEditProject(projectUid, userId); @@ -596,7 +573,7 @@ public OpenCGAResult incrementRelease(String projectStr, String session if (checkCurrentReleaseInUse(allStudiesInProject, currentRelease)) { // Increment current project release - OpenCGAResult writeResult = projectDBAdaptor.incrementCurrentRelease(projectUid); + OpenCGAResult writeResult = projectDBAdaptor.incrementCurrentRelease(projectUid); OpenCGAResult projectDataResult = projectDBAdaptor.get(projectUid, new QueryOptions(QueryOptions.INCLUDE, ProjectDBAdaptor.QueryParams.CURRENT_RELEASE.key())); OpenCGAResult queryResult = new OpenCGAResult<>(projectDataResult.getTime() + writeResult.getTime(), @@ -611,21 +588,151 @@ public OpenCGAResult incrementRelease(String projectStr, String session interpretationDBAdaptor.updateProjectRelease(study.getUid(), queryResult.first()); } - auditManager.audit(userId, Enums.Action.INCREMENT_PROJECT_RELEASE, Enums.Resource.PROJECT, project.getId(), - project.getUuid(), "", "", new ObjectMap(), new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return queryResult; } else { throw new CatalogException("Cannot increment current release number. The current release " + currentRelease + " has not yet been used in any entry"); } - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.INCREMENT_PROJECT_RELEASE, Enums.Resource.PROJECT, projectStr, "", "", "", - new ObjectMap(), new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; + }); + } + + public OpenCGAResult rank(String userId, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("userId", userId) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); + + return run(auditParams, Enums.Action.RANK, PROJECT, null, token, null, (study, userOfQuery, rp, queryOptions) -> { + if (!userOfQuery.equals(userId)) { + // The user cannot read projects of other users. + throw CatalogAuthorizationException.cantRead(userOfQuery, "Project", null, userId); + } + + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(userId, "userId"); + ParamUtils.checkObj(token, "sessionId"); + Query finalQuery = query != null ? new Query(query) : new Query(); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; +// query.append(CatalogFileDBAdaptor.QueryParams.STUDY_UID.key(), studyId); + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = projectDBAdaptor.rank(finalQuery, field, numResults, asc); + } + + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); + } + + public OpenCGAResult groupBy(String userId, Query query, String field, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("userId", userId) + .append("query", query) + .append("field", field) + .append("options", options) + .append("sessionId", token); + return run(auditParams, Enums.Action.GROUP_BY, PROJECT, null, token, options, (study, userOfQuery, rp, queryOptions) -> { + if (!userOfQuery.equals(userId)) { + // The user cannot read projects of other users. + throw CatalogAuthorizationException.cantRead(userOfQuery, "Project", null, userId); + } + + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(userId, "userId"); + ParamUtils.checkObj(token, "sessionId"); + Query finalQuery = query != null ? new Query(query) : new Query(); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = projectDBAdaptor.groupBy(finalQuery, field, queryOptions); + } + + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); + } + + public OpenCGAResult groupBy(String userId, Query query, List fields, QueryOptions options, String token) + throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("userId", userId) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); + + return run(auditParams, Enums.Action.GROUP_BY, PROJECT, null, token, options, (study, userOfQuery, rp, queryOptions) -> { + if (!userOfQuery.equals(userId)) { + // The user cannot read projects of other users. + throw CatalogAuthorizationException.cantRead(userOfQuery, "Project", null, userId); + } + + ParamUtils.checkObj(fields, "fields"); + ParamUtils.checkObj(userId, "userId"); + ParamUtils.checkObj(token, "sessionId"); + Query finalQuery = query != null ? new Query(query) : new Query(); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = projectDBAdaptor.groupBy(finalQuery, fields, queryOptions); + } + + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); + } + + // Return true if currentRelease is found in any entry + private boolean checkCurrentReleaseInUse(List allStudiesInProject, int currentRelease) throws CatalogException { + for (Study study : allStudiesInProject) { + if (study.getRelease() == currentRelease) { + return true; + } + } + List studyIds = allStudiesInProject.stream().map(Study::getUid).collect(Collectors.toList()); + Query query = new Query() + .append(FileDBAdaptor.QueryParams.STUDY_UID.key(), studyIds) + .append(FileDBAdaptor.QueryParams.RELEASE.key(), currentRelease); + if (fileDBAdaptor.count(query).getNumMatches() > 0) { + return true; + } + if (sampleDBAdaptor.count(query).getNumMatches() > 0) { + return true; + } + if (individualDBAdaptor.count(query).getNumMatches() > 0) { + return true; + } + if (cohortDBAdaptor.count(query).getNumMatches() > 0) { + return true; + } + if (familyDBAdaptor.count(query).getNumMatches() > 0) { + return true; + } + if (jobDBAdaptor.count(query).getNumMatches() > 0) { + return true; + } +// if (diseasePanelDBAdaptor.count(query).getNumMatches() > 0) { +// return true; +// } + if (clinicalDBAdaptor.count(query).getNumMatches() > 0) { + return true; } + + return false; } + // --------------- IMPORT - EXPORT ------------------- + + @Deprecated public void importReleases(String owner, String inputDirStr, String sessionId) throws CatalogException, IOException { String userId = catalogManager.getUserManager().getUserId(sessionId); if (!authorizationManager.isInstallationAdministrator(userId)) { @@ -742,6 +849,7 @@ public void importReleases(String owner, String inputDirStr, String sessionId) t } } + @Deprecated public void exportByFileNames(String studyStr, File outputDir, File filePath, String token) throws CatalogException { String userId = catalogManager.getUserManager().getUserId(token); if (!authorizationManager.isInstallationAdministrator(userId)) { @@ -897,6 +1005,7 @@ public void exportByFileNames(String studyStr, File outputDir, File filePath, St } } + @Deprecated private void exportToFile(List dataList, File file, ObjectMapper objectMapper) throws CatalogException { if (ListUtils.isEmpty(dataList)) { return; @@ -926,6 +1035,7 @@ private void exportToFile(List dataList, File file, ObjectMapper objectM } } + @Deprecated public void exportReleases(String projectStr, int release, String outputDirStr, String sessionId) throws CatalogException { String userId = catalogManager.getUserManager().getUserId(sessionId); if (!authorizationManager.isInstallationAdministrator(userId)) { @@ -1030,6 +1140,7 @@ public void exportReleases(String projectStr, int release, String outputDirStr, } + @Deprecated private void exportToFile(DBIterator dbIterator, File file, ObjectMapper objectMapper, String entity) throws CatalogException { FileWriter fileWriter; try { @@ -1062,117 +1173,4 @@ private void exportToFile(DBIterator dbIterator, File file, ObjectMapper objectM } } - public OpenCGAResult rank(String userId, Query query, String field, int numResults, boolean asc, String sessionId) - throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(userId, "userId"); - ParamUtils.checkObj(sessionId, "sessionId"); - - String userOfQuery = this.catalogManager.getUserManager().getUserId(sessionId); - if (!userOfQuery.equals(userId)) { - // The user cannot read projects of other users. - throw CatalogAuthorizationException.cantRead(userOfQuery, "Project", null, userId); - } - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; -// query.append(CatalogFileDBAdaptor.QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = projectDBAdaptor.rank(query, field, numResults, asc); - } - - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); - } - - public OpenCGAResult groupBy(String userId, Query query, String field, QueryOptions options, String sessionId) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(userId, "userId"); - ParamUtils.checkObj(sessionId, "sessionId"); - - String userOfQuery = this.catalogManager.getUserManager().getUserId(sessionId); - if (!userOfQuery.equals(userId)) { - // The user cannot read projects of other users. - throw CatalogAuthorizationException.cantRead(userOfQuery, "Project", null, userId); - } - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = projectDBAdaptor.groupBy(query, field, options); - } - - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); - } - - public OpenCGAResult groupBy(String userId, Query query, List fields, QueryOptions options, String sessionId) - throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - ParamUtils.checkObj(fields, "fields"); - ParamUtils.checkObj(userId, "userId"); - ParamUtils.checkObj(sessionId, "sessionId"); - - String userOfQuery = this.catalogManager.getUserManager().getUserId(sessionId); - if (!userOfQuery.equals(userId)) { - // The user cannot read projects of other users. - throw CatalogAuthorizationException.cantRead(userOfQuery, "Project", null, userId); - } - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = projectDBAdaptor.groupBy(query, fields, options); - } - - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); - } - - // Return true if currentRelease is found in any entry - private boolean checkCurrentReleaseInUse(List allStudiesInProject, int currentRelease) throws CatalogException { - for (Study study : allStudiesInProject) { - if (study.getRelease() == currentRelease) { - return true; - } - } - List studyIds = allStudiesInProject.stream().map(Study::getUid).collect(Collectors.toList()); - Query query = new Query() - .append(FileDBAdaptor.QueryParams.STUDY_UID.key(), studyIds) - .append(FileDBAdaptor.QueryParams.RELEASE.key(), currentRelease); - if (fileDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } - if (sampleDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } - if (individualDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } - if (cohortDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } - if (familyDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } - if (jobDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } -// if (diseasePanelDBAdaptor.count(query).getNumMatches() > 0) { -// return true; -// } - if (clinicalDBAdaptor.count(query).getNumMatches() > 0) { - return true; - } - - return false; - } - } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ResourceManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ResourceManager.java index eb2bae8c009..c9eff3db287 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ResourceManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/ResourceManager.java @@ -22,18 +22,15 @@ import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; import org.opencb.commons.utils.ListUtils; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; import org.opencb.opencga.catalog.db.api.DBIterator; import org.opencb.opencga.catalog.exceptions.CatalogException; import org.opencb.opencga.catalog.models.InternalGetDataResult; import org.opencb.opencga.catalog.utils.ParamUtils; -import org.opencb.opencga.catalog.utils.UuidUtils; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.IPrivateStudyUid; import org.opencb.opencga.core.models.common.Enums; -import org.opencb.opencga.core.models.study.Study; import org.opencb.opencga.core.response.OpenCGAResult; import javax.annotation.Nullable; @@ -51,7 +48,7 @@ public abstract class ResourceManager extends Abstra super(authorizationManager, auditManager, catalogManager, catalogDBAdaptorFactory, configuration); } - abstract Enums.Resource getEntity(); + abstract Enums.Resource getResource(); OpenCGAResult internalGet(long studyUid, String entry, QueryOptions options, String user) throws CatalogException { return internalGet(studyUid, entry, null, options, user); @@ -94,9 +91,17 @@ abstract InternalGetDataResult internalGet(long studyUid, List entryL * @throws CatalogException CatalogException. */ public OpenCGAResult get(String studyStr, String entryStr, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - return internalGet(study.getUid(), entryStr, options, userId); + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("id", entryStr) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.INFO, getResource(), studyStr, token, options, (study, userId, rp, queryOptions) -> { + OpenCGAResult result = internalGet(study.getUid(), entryStr, queryOptions, userId); + rp.setId(result.first().getId()); + rp.setUuid(result.first().getUuid()); + return result; + }); } /** @@ -120,27 +125,20 @@ public OpenCGAResult get(String studyStr, List entryList, QueryOption public OpenCGAResult get(String studyId, List entryList, Query query, QueryOptions options, boolean ignoreException, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId); - - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - ObjectMap auditParams = new ObjectMap() - .append("studyId", studyId) - .append("entryList", entryList) - .append("query", new Query(query)) - .append("options", new QueryOptions(options)) + .append("study", studyId) + .append("id", entryList) + .append("query", query) + .append("options", options) .append("ignoreException", ignoreException) .append("token", token); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - auditManager.initAuditBatch(operationUuid); + return runBatch(auditParams, Enums.Action.INFO, getResource(), studyId, token, options, (study, user, qOptions, operationUuid) -> { + Query myQuery = query != null ? new Query(query) : new Query(); - try { OpenCGAResult result = OpenCGAResult.empty(); options.remove(QueryOptions.LIMIT); - InternalGetDataResult responseResult = internalGet(study.getUid(), entryList, query, options, userId, ignoreException); + InternalGetDataResult responseResult = internalGet(study.getUid(), entryList, myQuery, options, user, ignoreException); Map missingMap = new HashMap<>(); if (responseResult.getMissing() != null) { @@ -160,25 +158,17 @@ public OpenCGAResult get(String studyId, List entryList, Query query, } else { int size = versionedResults.get(i).size(); result.append(new OpenCGAResult<>(0, Collections.emptyList(), size, versionedResults.get(i), size)); -// resultList.add(new OpenCGAResult<>(responseResult.getTime(), Collections.emptyList(), size, versionedResults.get(i), -// size)); - R entry = versionedResults.get(i).get(0); - auditManager.auditInfo(operationUuid, userId, getEntity(), entry.getId(), entry.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + run(auditParams, Enums.Action.INFO, getResource(), operationUuid, study, user, qOptions, (s, u, rp, qo) -> { + rp.setId(entry.getId()); + rp.setUuid(entry.getUuid()); + return null; + }); } } return result; - } catch (CatalogException e) { - for (String entryId : entryList) { - auditManager.auditInfo(operationUuid, userId, getEntity(), entryId, "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } - throw e; - } finally { - auditManager.finishAuditBatch(operationUuid); - } + }); } /** diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/SampleManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/SampleManager.java index 8bfd31d01e8..440ad555fec 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/SampleManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/SampleManager.java @@ -24,7 +24,6 @@ import org.opencb.biodata.models.variant.StudyEntry; import org.opencb.biodata.models.variant.metadata.SampleVariantStats; import org.opencb.commons.datastore.core.*; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; @@ -42,7 +41,6 @@ import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.models.AclEntryList; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.cohort.Cohort; import org.opencb.opencga.core.models.cohort.CohortStatus; import org.opencb.opencga.core.models.common.AnnotationSet; @@ -70,6 +68,7 @@ import static org.opencb.opencga.catalog.auth.authorization.CatalogAuthorizationManager.checkPermissions; import static org.opencb.opencga.core.common.JacksonUtils.getUpdateObjectMapper; +import static org.opencb.opencga.core.models.common.Enums.Resource.SAMPLE; /** * @author Jacobo Coll <jacobo167@gmail.com> @@ -93,8 +92,8 @@ public class SampleManager extends AnnotationSetManager { } @Override - Enums.Resource getEntity() { - return Enums.Resource.SAMPLE; + Enums.Resource getResource() { + return SAMPLE; } @Override @@ -224,116 +223,82 @@ void validateNewSample(Study study, Sample sample, String userId) throws Catalog @Override public OpenCGAResult create(String studyStr, Sample sample, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("sample", sample) .append("options", options) .append("token", token); - try { + return run(auditParams, Enums.Action.CREATE, SAMPLE, studyStr, token, options, (study, userId, rp, qOptions) -> { // 1. We check everything can be done authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.WRITE_SAMPLES); - validateNewSample(study, sample, userId); + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); // We create the sample - OpenCGAResult insert = sampleDBAdaptor.insert(study.getUid(), sample, study.getVariableSets(), options); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + OpenCGAResult insert = sampleDBAdaptor.insert(study.getUid(), sample, study.getVariableSets(), qOptions); + if (qOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch created sample - OpenCGAResult result = getSample(study.getUid(), sample.getUuid(), options); + OpenCGAResult result = getSample(study.getUid(), sample.getUuid(), qOptions); insert.setResults(result.getResults()); } - auditManager.auditCreate(userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); return insert; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.SAMPLE, sample.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } @Override - public DBIterator iterator(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); + public DBIterator iterator(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("options", options) + .append("token", token); - Query finalQuery = new Query(query); - fixQueryObject(study, finalQuery, userId); - AnnotationUtils.fixQueryOptionAnnotation(options); - finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return run(auditParams, Enums.Action.ITERATE, SAMPLE, studyStr, token, options, (study, userId, rp, queryOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + AnnotationUtils.fixQueryOptionAnnotation(options); + finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return sampleDBAdaptor.iterator(study.getUid(), finalQuery, options, userId); + return sampleDBAdaptor.iterator(study.getUid(), finalQuery, options, userId); + }); } @Override public OpenCGAResult search(String studyId, Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("token", token); - try { - fixQueryObject(study, query, userId); - AnnotationUtils.fixQueryOptionAnnotation(options); - - query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = sampleDBAdaptor.get(study.getUid(), query, options, userId); + return run(auditParams, Enums.Action.SEARCH, SAMPLE, studyId, token, options, + Collections.singletonList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key()), (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + AnnotationUtils.fixQueryOptionAnnotation(qOptions); + finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - auditManager.auditSearch(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return queryResult; - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return sampleDBAdaptor.get(study.getUid(), finalQuery, qOptions, userId); + }); } @Override public OpenCGAResult distinct(String studyId, String field, Query query, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("field", new Query(query)) + .append("field", field) .append("query", new Query(query)) .append("token", token); - try { - fixQueryObject(study, query, userId); - - query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult result = sampleDBAdaptor.distinct(study.getUid(), field, query, userId); - auditManager.auditDistinct(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.DISTINCT, SAMPLE, studyId, token, null, + Collections.singletonList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key()), (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return result; - } catch (CatalogException e) { - auditManager.auditDistinct(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return sampleDBAdaptor.distinct(study.getUid(), field, finalQuery, userId); + }); } void fixQueryObject(Study study, Query query, String userId) throws CatalogException { @@ -417,32 +382,17 @@ void fixQueryObject(Study study, Query query, String userId) throws CatalogExcep @Override public OpenCGAResult count(String studyId, Query query, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - query = new Query(ParamUtils.defaultObject(query, Query::new)); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("query", query) .append("token", token); - try { - fixQueryObject(study, query, userId); - - query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResultAux = sampleDBAdaptor.count(query, userId); - auditManager.auditCount(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return new OpenCGAResult<>(queryResultAux.getTime(), queryResultAux.getEvents(), 0, Collections.emptyList(), - queryResultAux.getNumMatches()); - } catch (CatalogException e) { - auditManager.auditCount(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.COUNT, SAMPLE, studyId, token, null, (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + fixQueryObject(study, finalQuery, userId); + finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + return sampleDBAdaptor.count(finalQuery, userId); + }); } @Override @@ -452,16 +402,6 @@ public OpenCGAResult delete(String studyStr, List sampleIds, QueryOption public OpenCGAResult delete(String studyStr, List sampleIds, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - if (sampleIds == null || ListUtils.isEmpty(sampleIds)) { - throw new CatalogException("Missing list of sample ids"); - } - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, new QueryOptions(QueryOptions.INCLUDE, - StudyDBAdaptor.QueryParams.VARIABLE_SET.key())); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("sampleIds", sampleIds) @@ -469,59 +409,52 @@ public OpenCGAResult delete(String studyStr, List sampleIds, ObjectMap p .append("ignoreException", ignoreException) .append("token", token); - boolean checkPermissions; - try { - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationId, userId, Enums.Resource.SAMPLE, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : sampleIds) { - String sampleId = id; - String sampleUuid = ""; - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_SAMPLE_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Sample '" + id + "' not found"); - } - Sample sample = internalResult.first(); + return runBatch(auditParams, Enums.Action.DELETE, SAMPLE, studyStr, token, null, (study, userId, qOptions, operationUuid) -> { + if (sampleIds == null || CollectionUtils.isEmpty(sampleIds)) { + throw new CatalogException("Missing list of sample ids"); + } - // We set the proper values for the audit - sampleId = sample.getId(); - sampleUuid = sample.getUuid(); + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); + + OpenCGAResult result = OpenCGAResult.empty(Sample.class); + for (String id : sampleIds) { + try { + run(auditParams, Enums.Action.DELETE, SAMPLE, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(id); + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_SAMPLE_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Sample '" + id + "' not found"); + } + Sample sample = internalResult.first(); - if (checkPermissions) { - authorizationManager.checkSamplePermission(study.getUid(), sample.getUid(), userId, - SamplePermissions.DELETE); - } + // We set the proper values for the audit + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); - // Check if the sample can be deleted - checkSampleCanBeDeleted(study.getUid(), sample, params.getBoolean(Constants.FORCE, false)); + if (checkPermissions) { + authorizationManager.checkSamplePermission(study.getUid(), sample.getUid(), userId, SamplePermissions.DELETE); + } - result.append(sampleDBAdaptor.delete(sample)); + // Check if the sample can be deleted + checkSampleCanBeDeleted(study.getUid(), sample, params.getBoolean(Constants.FORCE, false)); - auditManager.auditDelete(operationId, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete sample " + sampleId + ": " + e.getMessage(); + result.append(sampleDBAdaptor.delete(sample)); + return null; + }); + } catch (CatalogException e) { + String errorMsg = "Cannot delete sample " + id + ": " + e.getMessage(); - Event event = new Event(Event.Type.ERROR, sampleId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); - logger.error(errorMsg); - auditManager.auditDelete(operationId, userId, Enums.Resource.SAMPLE, sampleId, sampleUuid, - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + logger.error(errorMsg); + } } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } @Override @@ -531,133 +464,79 @@ public OpenCGAResult delete(String studyStr, Query query, QueryOptions options, public OpenCGAResult delete(String studyStr, Query query, ObjectMap params, boolean ignoreException, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - params = ParamUtils.defaultObject(params, ObjectMap::new); - - OpenCGAResult result = OpenCGAResult.empty(); - - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) - .append("query", new Query(query)) + .append("query", query) .append("params", params) .append("ignoreException", ignoreException) .append("token", token); - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - boolean checkPermissions; + return runBatch(auditParams, Enums.Action.DELETE, SAMPLE, studyStr, token, null, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); + ObjectMap finalParams = params != null ? new ObjectMap(params) : new ObjectMap(); - // We try to get an iterator containing all the samples to be deleted - DBIterator iterator; - try { - // TODO: Propagation of delete to orphan files and cohorts need to be implemented in the dbAdaptor layer -// if (StringUtils.isNotEmpty(params.getString(Constants.EMPTY_FILES_ACTION))) { -// // Validate the action -// String filesAction = params.getString(Constants.EMPTY_FILES_ACTION); -// params.put(Constants.EMPTY_FILES_ACTION, filesAction.toUpperCase()); -// if (!"NONE".equals(filesAction) && !"TRASH".equals(filesAction) && !"DELETE".equals(filesAction)) { -// throw new CatalogException("Unrecognised " + Constants.EMPTY_FILES_ACTION + " value. Accepted actions are NONE,TRASH," -// + " DELETE"); -// } -// } else { -// params.put(Constants.EMPTY_FILES_ACTION, "NONE"); -// } + OpenCGAResult result = OpenCGAResult.empty(); + + // If the user is the owner or the admin, we won't check if he has permissions for every single entry + boolean checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); fixQueryObject(study, finalQuery, userId); finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = sampleDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_SAMPLE_IDS, userId); - - // If the user is the owner or the admin, we won't check if he has permissions for every single entry - checkPermissions = !authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, userId, Enums.Resource.SAMPLE, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - auditManager.initAuditBatch(operationUuid); - while (iterator.hasNext()) { - Sample sample = iterator.next(); + // We try to get an iterator containing all the samples to be deleted + try (DBIterator iterator = sampleDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_SAMPLE_IDS, userId)) { + while (iterator.hasNext()) { + Sample sample = iterator.next(); + try { + run(auditParams, Enums.Action.DELETE, SAMPLE, operationUuid, study, userId, null, (s, u, rp, qo) -> { + if (checkPermissions) { + authorizationManager.checkSamplePermission(study.getUid(), sample.getUid(), userId, + SamplePermissions.DELETE); + } - try { - if (checkPermissions) { - authorizationManager.checkSamplePermission(study.getUid(), sample.getUid(), userId, - SamplePermissions.DELETE); + // Check if the sample can be deleted + checkSampleCanBeDeleted(study.getUid(), sample, finalParams.getBoolean(Constants.FORCE, false)); + OpenCGAResult delete = sampleDBAdaptor.delete(sample); + result.append(delete); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, sample.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Cannot delete sample {}: {}", sample.getId(), e.getMessage()); + } } - - // Check if the sample can be deleted - checkSampleCanBeDeleted(study.getUid(), sample, params.getBoolean(Constants.FORCE, false)); - - result.append(sampleDBAdaptor.delete(sample)); - - auditManager.auditDelete(operationUuid, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - String errorMsg = "Cannot delete sample " + sample.getId() + ": " + e.getMessage(); - - Event event = new Event(Event.Type.ERROR, sample.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error(errorMsg); - auditManager.auditDelete(operationUuid, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationUuid); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } // TODO: This method should be private. This should only be accessible internally. public OpenCGAResult resetRgaIndexes(String studyStr, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("token", token); - OpenCGAResult result; - try { + return run(auditParams, Enums.Action.RESET_RGA_INDEXES, SAMPLE, studyStr, token, null, (study, userId, rp, queryOptions) -> { authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - result = sampleDBAdaptor.setRgaIndexes(study.getUid(), new RgaIndex(RgaIndex.Status.NOT_INDEXED, TimeUtils.getTime())); - - auditManager.audit(userId, Enums.Action.RESET_RGA_INDEXES, Enums.Resource.SAMPLE, "ALL", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.RESET_RGA_INDEXES, Enums.Resource.SAMPLE, "ALL", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw new CatalogException("Could not reset all sample RGA indexes", e); - } - - return result; + return sampleDBAdaptor.setRgaIndexes(study.getUid(), new RgaIndex(RgaIndex.Status.NOT_INDEXED, TimeUtils.getTime())); + }); } // TODO: This method should be somehow private. This should only be accessible internally. public OpenCGAResult updateRgaIndexes(String studyStr, List samples, RgaIndex rgaIndex, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("samples", samples) .append("rgaIndex", rgaIndex) .append("token", token); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - InternalGetDataResult sampleResult = null; - - OpenCGAResult result; - try { - auditManager.initAuditBatch(operationUuid); - + return runBatch(auditParams, Enums.Action.UPDATE_RGA_INDEX, SAMPLE, studyStr, token, null, (study, userId, qOptions, + operationUuid) -> { authorizationManager.isOwnerOrAdmin(study.getUid(), userId); ParamUtils.checkNotEmptyArray(samples, "samples"); @@ -666,32 +545,19 @@ public OpenCGAResult updateRgaIndexes(String studyStr, List samp rgaIndex.setDate(TimeUtils.getTime()); - sampleResult = internalGet(study.getUid(), samples, INCLUDE_SAMPLE_IDS, userId, false); - result = sampleDBAdaptor.setRgaIndexes(study.getUid(), + InternalGetDataResult sampleResult = internalGet(study.getUid(), samples, INCLUDE_SAMPLE_IDS, userId, false); + OpenCGAResult result = sampleDBAdaptor.setRgaIndexes(study.getUid(), sampleResult.getResults().stream().map(Sample::getUid).collect(Collectors.toList()), rgaIndex); for (Sample sample : sampleResult.getResults()) { - auditManager.audit(operationUuid, userId, Enums.Action.UPDATE_RGA_INDEX, Enums.Resource.SAMPLE, sample.getId(), - sample.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + run(auditParams, Enums.Action.UPDATE_RGA_INDEX, SAMPLE, operationUuid, study, userId, null, (s, u, rp, qo) -> { + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); + return null; + }); } - } catch (CatalogException e) { - if (sampleResult == null) { - auditManager.audit(operationUuid, userId, Enums.Action.UPDATE_RGA_INDEX, Enums.Resource.SAMPLE, "", "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } else { - for (Sample sample : sampleResult.getResults()) { - auditManager.audit(operationUuid, userId, Enums.Action.UPDATE_RGA_INDEX, Enums.Resource.SAMPLE, sample.getId(), - sample.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } - } - throw new CatalogException("Could not reset all sample RGA indexes", e); - } finally { - auditManager.finishAuditBatch(operationUuid); - } - - return result; + return result; + }); } public OpenCGAResult updateSampleInternalVariantIndex(Sample sample, SampleInternalVariantIndex index, String token) @@ -716,27 +582,26 @@ public OpenCGAResult updateSampleInternalVariantSecondaryIndex(Sample sample, private OpenCGAResult updateSampleInternalVariant(Sample sample, Object value, String fieldKey, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyDBAdaptor.get(sample.getStudyUid(), StudyManager.INCLUDE_STUDY_IDS).first(); - ObjectMap auditParams = new ObjectMap() - .append("sample", sample) - .append(fieldKey, value) + .append("field", fieldKey) + .append("value", value) .append("token", token); + String studyFqn = studyDBAdaptor.get(sample.getStudyUid(), StudyManager.INCLUDE_STUDY_IDS).first().getFqn(); - authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - - ObjectMap params; - try { - params = new ObjectMap(fieldKey, new ObjectMap(getUpdateObjectMapper().writeValueAsString(value))); - } catch (JsonProcessingException e) { - throw new CatalogException("Cannot parse SampleInternalVariant object: " + e.getMessage(), e); - } - OpenCGAResult update = sampleDBAdaptor.update(sample.getUid(), params, QueryOptions.empty()); - auditManager.audit(userId, Enums.Action.UPDATE_INTERNAL, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.UPDATE_INTERNAL, SAMPLE, studyFqn, token, null, (study, userId, rp, qOptions) -> { + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); + authorizationManager.isOwnerOrAdmin(study.getUid(), userId); - return new OpenCGAResult<>(update.getTime(), update.getEvents(), 1, Collections.emptyList(), 1); + ObjectMap params; + try { + params = new ObjectMap(fieldKey, new ObjectMap(getUpdateObjectMapper().writeValueAsString(value))); + } catch (JsonProcessingException e) { + throw new CatalogException("Cannot parse SampleInternalVariant object: " + e.getMessage(), e); + } + OpenCGAResult update = sampleDBAdaptor.update(sample.getUid(), params, QueryOptions.empty()); + return new OpenCGAResult<>(update.getTime(), update.getEvents(), 1, Collections.emptyList(), 1); + }); } public OpenCGAResult updateAnnotationSet(String studyStr, String sampleStr, List annotationSetList, @@ -882,20 +747,12 @@ public OpenCGAResult update(String studyStr, Query query, SampleUpdatePa public OpenCGAResult update(String studyStr, Query query, SampleUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); - - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse SampleUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("query", query) @@ -904,58 +761,45 @@ public OpenCGAResult update(String studyStr, Query query, SampleUpdatePa .append("options", options) .append("token", token); - DBIterator iterator; - try { + return runBatch(auditParams, Enums.Action.UPDATE, SAMPLE, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + Query finalQuery = new Query(ParamUtils.defaultObject(query, Query::new)); fixQueryObject(study, finalQuery, userId); - finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - iterator = sampleDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_SAMPLE_IDS, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, "", "", study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + OpenCGAResult result = OpenCGAResult.empty(Sample.class); + try (DBIterator iterator = sampleDBAdaptor.iterator(study.getUid(), finalQuery, INCLUDE_SAMPLE_IDS, userId)) { + while (iterator.hasNext()) { + Sample sample = iterator.next(); + try { + run(auditParams, Enums.Action.UPDATE, SAMPLE, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); + OpenCGAResult updateResult = update(study, sample, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, sample.getId(), e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update sample {}: {}", sample.getId(), e.getMessage(), e); + } + } - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - while (iterator.hasNext()) { - Sample sample = iterator.next(); - try { - OpenCGAResult updateResult = update(study, sample, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, sample.getId(), e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update sample {}: {}", sample.getId(), e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return endResult(result, ignoreException); } - } - auditManager.finishAuditBatch(operationId); - - return endResult(result, ignoreException); + }); } public OpenCGAResult update(String studyStr, String sampleId, SampleUpdateParams updateParams, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; } catch (JsonProcessingException e) { throw new CatalogException("Could not parse SampleUpdateParams object: " + e.getMessage(), e); } - ObjectMap auditParams = new ObjectMap() .append("study", studyStr) .append("sampleId", sampleId) @@ -963,36 +807,19 @@ public OpenCGAResult update(String studyStr, String sampleId, SampleUpda .append("options", options) .append("token", token); - OpenCGAResult result = OpenCGAResult.empty(); - String sampleUuid = ""; - try { + return run(auditParams, Enums.Action.UPDATE, SAMPLE, studyStr, token, options, (study, userId, rp, qOptions) -> { + rp.setId(sampleId); OpenCGAResult internalResult = internalGet(study.getUid(), sampleId, INCLUDE_SAMPLE_IDS, userId); if (internalResult.getNumResults() == 0) { throw new CatalogException("Sample '" + sampleId + "' not found"); } Sample sample = internalResult.first(); - // We set the proper values for the audit - sampleId = sample.getId(); - sampleUuid = sample.getUuid(); - - OpenCGAResult updateResult = update(study, sample, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, sampleId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update sample {}: {}", sampleId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, sampleId, sampleUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); - return result; + return update(study, sample, updateParams, qOptions, userId); + }); } /** @@ -1014,11 +841,6 @@ public OpenCGAResult update(String studyStr, List sampleIds, Sam public OpenCGAResult update(String studyStr, List sampleIds, SampleUpdateParams updateParams, boolean ignoreException, QueryOptions options, String token) throws CatalogException { - String userId = userManager.getUserId(token); - Study study = studyManager.resolveId(studyStr, userId, StudyManager.INCLUDE_VARIABLE_SET); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - ObjectMap updateMap; try { updateMap = updateParams != null ? updateParams.getUpdateMap() : null; @@ -1034,41 +856,36 @@ public OpenCGAResult update(String studyStr, List sampleIds, Sam .append("options", options) .append("token", token); - auditManager.initAuditBatch(operationId); - OpenCGAResult result = OpenCGAResult.empty(); - for (String id : sampleIds) { - String sampleId = id; - String sampleUuid = ""; - - try { - OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_SAMPLE_IDS, userId); - if (internalResult.getNumResults() == 0) { - throw new CatalogException("Sample '" + id + "' not found"); + return runBatch(auditParams, Enums.Action.UPDATE, SAMPLE, studyStr, token, options, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult result = OpenCGAResult.empty(Sample.class); + for (String id : sampleIds) { + try { + run(auditParams, Enums.Action.UPDATE, SAMPLE, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + OpenCGAResult internalResult = internalGet(study.getUid(), id, INCLUDE_SAMPLE_IDS, userId); + if (internalResult.getNumResults() == 0) { + throw new CatalogException("Sample '" + id + "' not found"); + } + Sample sample = internalResult.first(); + + // We set the proper values for the audit + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); + + OpenCGAResult updateResult = update(study, sample, updateParams, options, userId); + result.append(updateResult); + return null; + }); + } catch (CatalogException e) { + Event event = new Event(Event.Type.ERROR, id, e.getMessage()); + result.getEvents().add(event); + result.setNumErrors(result.getNumErrors() + 1); + + logger.error("Could not update sample {}: {}", id, e.getMessage(), e); } - Sample sample = internalResult.first(); - - // We set the proper values for the audit - sampleId = sample.getId(); - sampleUuid = sample.getUuid(); - - OpenCGAResult updateResult = update(study, sample, updateParams, options, userId); - result.append(updateResult); - - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, sample.getId(), sample.getUuid(), study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - Event event = new Event(Event.Type.ERROR, sampleId, e.getMessage()); - result.getEvents().add(event); - result.setNumErrors(result.getNumErrors() + 1); - - logger.error("Could not update sample {}: {}", sampleId, e.getMessage(), e); - auditManager.auditUpdate(operationId, userId, Enums.Resource.SAMPLE, sampleId, sampleUuid, study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } - } - auditManager.finishAuditBatch(operationId); - return endResult(result, ignoreException); + return endResult(result, ignoreException); + }); } private OpenCGAResult update(Study study, Sample sample, SampleUpdateParams updateParams, QueryOptions options, String userId) @@ -1156,54 +973,64 @@ private OpenCGAResult update(Study study, Sample sample, SampleUpdateParams upda } @Override - public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String sessionId) + public OpenCGAResult rank(String studyStr, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(sessionId, "sessionId"); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + return run(auditParams, Enums.Action.RANK, SAMPLE, studyStr, token, null, (study, userId, rp, qOptions) -> { + authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_SAMPLES); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); + ParamUtils.checkObj(field, "field"); + Query finalQuery = query != null ? new Query(query) : new Query(); - authorizationManager.checkStudyPermission(study.getUid(), userId, StudyPermissions.Permissions.VIEW_SAMPLES); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - query.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = sampleDBAdaptor.rank(query, field, numResults, asc); - } + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + finalQuery.append(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = sampleDBAdaptor.rank(finalQuery, field, numResults, asc); + } - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } @Override - public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(@Nullable String studyStr, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - if (fields == null || fields.size() == 0) { - throw new CatalogException("Empty fields parameter."); - } - - String userId = userManager.getUserId(sessionId); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); - // Fix query if it contains any annotation - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); - AnnotationUtils.fixQueryOptionAnnotation(options); + return run(auditParams, Enums.Action.GROUP_BY, SAMPLE, studyStr, token, options, (study, userId, rp, qOptions) -> { + if (fields == null || fields.size() == 0) { + throw new CatalogException("Empty fields parameter."); + } - // Add study id to the query - query.put(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); + Query finalQuery = query != null ? new Query(query) : new Query(); + // Fix query if it contains any annotation + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); + AnnotationUtils.fixQueryOptionAnnotation(qOptions); - OpenCGAResult queryResult = sampleDBAdaptor.groupBy(query, fields, options, userId); + // Add study id to the query + finalQuery.put(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()); - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + OpenCGAResult queryResult = sampleDBAdaptor.groupBy(finalQuery, fields, qOptions, userId); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } // ************************** ACLs ******************************** // @@ -1216,10 +1043,6 @@ public OpenCGAResult> getAcls(String studyId, Li public OpenCGAResult> getAcls(String studyId, List sampleList, List members, boolean ignoreException, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("sampleList", sampleList) @@ -1227,11 +1050,11 @@ public OpenCGAResult> getAcls(String studyId, Li .append("ignoreException", ignoreException) .append("token", token); - OpenCGAResult> sampleAcls = OpenCGAResult.empty(); - Map missingMap = new HashMap<>(); - try { - auditManager.initAuditBatch(operationId); - InternalGetDataResult queryResult = internalGet(study.getUid(), sampleList, INCLUDE_SAMPLE_IDS, user, ignoreException); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, SAMPLE, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + OpenCGAResult> sampleAcls; + Map missingMap = new HashMap<>(); + InternalGetDataResult queryResult = internalGet(study.getUid(), sampleList, INCLUDE_SAMPLE_IDS, userId, + ignoreException); if (queryResult.getMissing() != null) { missingMap = queryResult.getMissing().stream() @@ -1240,11 +1063,9 @@ public OpenCGAResult> getAcls(String studyId, Li List sampleUids = queryResult.getResults().stream().map(Sample::getUid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(members)) { - sampleAcls = authorizationManager.getAcl(user, study.getUid(), sampleUids, members, Enums.Resource.SAMPLE, - SamplePermissions.class); + sampleAcls = authorizationManager.getAcl(userId, study.getUid(), sampleUids, members, SAMPLE, SamplePermissions.class); } else { - sampleAcls = authorizationManager.getAcl(user, study.getUid(), sampleUids, Enums.Resource.SAMPLE, - SamplePermissions.class); + sampleAcls = authorizationManager.getAcl(userId, study.getUid(), sampleUids, SAMPLE, SamplePermissions.class); } // Include non-existing samples to the result list @@ -1254,49 +1075,32 @@ public OpenCGAResult> getAcls(String studyId, Li for (String sampleId : sampleList) { if (!missingMap.containsKey(sampleId)) { Sample sample = queryResult.getResults().get(counter); + run(auditParams, Enums.Action.FETCH_ACLS, SAMPLE, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); + return null; + }); resultList.add(sampleAcls.getResults().get(counter)); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.SAMPLE, sample.getId(), - sample.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); counter++; } else { + if (!ignoreException) { + throw new CatalogException(missingMap.get(sampleId).getErrorMsg()); + } resultList.add(new AclEntryList<>()); eventList.add(new Event(Event.Type.ERROR, sampleId, missingMap.get(sampleId).getErrorMsg())); - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.SAMPLE, sampleId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(0, "", missingMap.get(sampleId).getErrorMsg())), new ObjectMap()); } } sampleAcls.setResults(resultList); sampleAcls.setEvents(eventList); - } catch (CatalogException e) { - for (String sampleId : sampleList) { - auditManager.audit(operationId, user, Enums.Action.FETCH_ACLS, Enums.Resource.SAMPLE, sampleId, "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), - new ObjectMap()); - } - if (!ignoreException) { - throw e; - } else { - for (String sampleId : sampleList) { - Event event = new Event(Event.Type.ERROR, sampleId, e.getMessage()); - sampleAcls.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, new AclEntryList<>(), 0)); - } - } - } finally { - auditManager.finishAuditBatch(operationId); - } - return sampleAcls; + return sampleAcls; + }); } public OpenCGAResult> updateAcl(String studyId, List sampleStringList, String memberList, SampleAclParams sampleAclParams, ParamUtils.AclAction action, String token) throws CatalogException { - String user = userManager.getUserId(token); - Study study = studyManager.resolveId(studyId, user); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("sampleStringList", sampleStringList) @@ -1304,13 +1108,13 @@ public OpenCGAResult> updateAcl(String studyId, .append("sampleAclParams", sampleAclParams) .append("action", action) .append("token", token); - String operationId = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - List members; - List sampleList; - List permissions = Collections.emptyList(); - try { - auditManager.initAuditBatch(operationId); + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, SAMPLE, studyId, token, null, (study, userId, qOptions, operationUuid) -> { + authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), userId); + + List members; + List sampleList; + List permissions = Collections.emptyList(); int count = 0; count += sampleStringList != null && !sampleStringList.isEmpty() ? 1 : 0; @@ -1331,6 +1135,8 @@ public OpenCGAResult> updateAcl(String studyId, throw new CatalogException("Invalid action found. Please choose a valid action to be performed."); } + List finalSampleStringList = sampleStringList; + if (StringUtils.isNotEmpty(sampleAclParams.getPermissions())) { permissions = Arrays.asList(sampleAclParams.getPermissions().trim().replaceAll("\\s", "").split(",")); checkPermissions(permissions, SamplePermissions::valueOf); @@ -1345,8 +1151,7 @@ public OpenCGAResult> updateAcl(String studyId, for (Individual individual : indDataResult.getResults()) { sampleSet.addAll(individual.getSamples().stream().map(Sample::getId).collect(Collectors.toSet())); } - sampleStringList = new ArrayList<>(); - sampleStringList.addAll(sampleSet); + finalSampleStringList = new ArrayList<>(sampleSet); } if (StringUtils.isNotEmpty(sampleAclParams.getFamily())) { @@ -1363,22 +1168,20 @@ public OpenCGAResult> updateAcl(String studyId, } } } - sampleStringList = new ArrayList<>(); - sampleStringList.addAll(sampleSet); + finalSampleStringList = new ArrayList<>(sampleSet); } if (StringUtils.isNotEmpty(sampleAclParams.getFile())) { // // Obtain the samples of the files QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, FileDBAdaptor.QueryParams.SAMPLE_IDS.key()); OpenCGAResult fileDataResult = catalogManager.getFileManager().internalGet(study.getUid(), - Arrays.asList(StringUtils.split(sampleAclParams.getFile(), ",")), options, user, false); + Arrays.asList(StringUtils.split(sampleAclParams.getFile(), ",")), options, userId, false); Set sampleSet = new HashSet<>(); for (File file : fileDataResult.getResults()) { sampleSet.addAll(file.getSampleIds()); } - sampleStringList = new ArrayList<>(); - sampleStringList.addAll(sampleSet); + finalSampleStringList = new ArrayList<>(sampleSet); } if (StringUtils.isNotEmpty(sampleAclParams.getCohort())) { @@ -1390,12 +1193,10 @@ public OpenCGAResult> updateAcl(String studyId, for (Cohort cohort : cohortDataResult.getResults()) { sampleSet.addAll(cohort.getSamples().stream().map(Sample::getId).collect(Collectors.toList())); } - sampleStringList = new ArrayList<>(); - sampleStringList.addAll(sampleSet); + finalSampleStringList = new ArrayList<>(sampleSet); } - sampleList = internalGet(study.getUid(), sampleStringList, INCLUDE_SAMPLE_IDS, user, false).getResults(); - authorizationManager.checkCanAssignOrSeePermissions(study.getUid(), user); + sampleList = internalGet(study.getUid(), finalSampleStringList, INCLUDE_SAMPLE_IDS, userId, false).getResults(); // Validate that the members are actually valid members if (memberList != null && !memberList.isEmpty()) { @@ -1405,32 +1206,20 @@ public OpenCGAResult> updateAcl(String studyId, } checkMembers(study.getUid(), members); authorizationManager.checkNotAssigningPermissionsToAdminsGroup(members); - } catch (CatalogException e) { - if (sampleStringList != null) { - for (String sampleId : sampleStringList) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.SAMPLE, sampleId, "", - study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - } - auditManager.finishAuditBatch(operationId); - throw e; - } - OpenCGAResult> aclResultList = OpenCGAResult.empty(); - int numProcessed = 0; - do { - List batchSampleList = new ArrayList<>(); - while (numProcessed < Math.min(numProcessed + BATCH_OPERATION_SIZE, sampleList.size())) { - batchSampleList.add(sampleList.get(numProcessed)); - numProcessed += 1; - } + OpenCGAResult> aclResultList = OpenCGAResult.empty(); + int numProcessed = 0; + do { + List batchSampleList = new ArrayList<>(); + while (numProcessed < Math.min(numProcessed + BATCH_OPERATION_SIZE, sampleList.size())) { + batchSampleList.add(sampleList.get(numProcessed)); + numProcessed += 1; + } - List sampleUids = batchSampleList.stream().map(Sample::getUid).collect(Collectors.toList()); - List aclParamsList = new ArrayList<>(); - AuthorizationManager.CatalogAclParams.addToList(sampleUids, permissions, Enums.Resource.SAMPLE, aclParamsList); + List sampleUids = batchSampleList.stream().map(Sample::getUid).collect(Collectors.toList()); + List aclParamsList = new ArrayList<>(); + AuthorizationManager.CatalogAclParams.addToList(sampleUids, permissions, SAMPLE, aclParamsList); - try { switch (action) { case SET: authorizationManager.setAcls(study.getUid(), members, aclParamsList); @@ -1452,76 +1241,48 @@ public OpenCGAResult> updateAcl(String studyId, } OpenCGAResult> queryResults = authorizationManager.getAcls(study.getUid(), - sampleUids, members, Enums.Resource.SAMPLE, SamplePermissions.class); + sampleUids, members, SAMPLE, SamplePermissions.class); aclResultList.append(queryResults); for (Sample sample : batchSampleList) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.SAMPLE, sample.getId(), - sample.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + // To audit + run(auditParams, Enums.Action.UPDATE_ACLS, SAMPLE, operationUuid, study, userId, qOptions, (s, u, rp, qo) -> { + rp.setId(sample.getId()); + rp.setUuid(sample.getUuid()); + return null; + }); } - } catch (CatalogException e) { - // Process current batch - for (Sample sample : batchSampleList) { - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.SAMPLE, sample.getId(), - sample.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - - // Process remaining unprocessed batches - while (numProcessed < sampleList.size()) { - Sample sample = sampleList.get(numProcessed); - auditManager.audit(operationId, user, Enums.Action.UPDATE_ACLS, Enums.Resource.SAMPLE, sample.getId(), - sample.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - - auditManager.finishAuditBatch(operationId); - throw e; - } - } while (numProcessed < sampleList.size()); + } while (numProcessed < sampleList.size()); - auditManager.finishAuditBatch(operationId); - return aclResultList; + return aclResultList; + }); } public DataResult facet(String studyId, Query query, QueryOptions options, boolean defaultStats, String token) throws CatalogException { - String userId = userManager.getUserId(token); - // We need to add variableSets and groups to avoid additional queries as it will be used in the catalogSolrManager - Study study = catalogManager.getStudyManager().resolveId(studyId, userId, new QueryOptions(QueryOptions.INCLUDE, - Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()))); - - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) - .append("query", new Query(query)) + .append("query", query) .append("options", options) .append("defaultStats", defaultStats) .append("token", token); - try { - if (defaultStats || StringUtils.isEmpty(options.getString(QueryOptions.FACET))) { - String facet = options.getString(QueryOptions.FACET); - options.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); - } - AnnotationUtils.fixQueryAnnotationSearch(study, userId, query, authorizationManager); + return run(auditParams, Enums.Action.FACET, SAMPLE, studyId, token, options, + Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key()), + (study, userId, rp, qOptions) -> { + Query finalQuery = query != null ? new Query(query) : new Query(); - try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { - DataResult result = catalogSolrManager.facetedQuery(study, CatalogSolrManager.SAMPLE_SOLR_COLLECTION, query, - options, userId); + if (defaultStats || StringUtils.isEmpty(qOptions.getString(QueryOptions.FACET))) { + String facet = qOptions.getString(QueryOptions.FACET); + qOptions.put(QueryOptions.FACET, StringUtils.isNotEmpty(facet) ? defaultFacet + ";" + facet : defaultFacet); + } + AnnotationUtils.fixQueryAnnotationSearch(study, userId, finalQuery, authorizationManager); - auditManager.auditFacet(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } - } catch (CatalogException e) { - auditManager.auditFacet(userId, Enums.Resource.SAMPLE, study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", e.getMessage()))); - throw e; - } + try (CatalogSolrManager catalogSolrManager = new CatalogSolrManager(catalogManager)) { + return catalogSolrManager.facetedQuery(study, CatalogSolrManager.SAMPLE_SOLR_COLLECTION, finalQuery, qOptions, + userId); + } + }); } private List getIndividualsUidsFromSampleUids(long studyUid, List sampleUids) throws CatalogException { diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/StudyManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/StudyManager.java index e9e48ab01f4..5f71f1d7ab6 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/StudyManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/StudyManager.java @@ -26,14 +26,11 @@ import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager; import org.opencb.opencga.catalog.db.DBAdaptorFactory; -import org.opencb.opencga.catalog.db.api.*; -import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException; -import org.opencb.opencga.catalog.exceptions.CatalogDBException; -import org.opencb.opencga.catalog.exceptions.CatalogException; -import org.opencb.opencga.catalog.exceptions.CatalogIOException; +import org.opencb.opencga.catalog.db.api.StudyDBAdaptor; +import org.opencb.opencga.catalog.db.api.UserDBAdaptor; +import org.opencb.opencga.catalog.exceptions.*; import org.opencb.opencga.catalog.io.CatalogIOManager; import org.opencb.opencga.catalog.io.IOManager; import org.opencb.opencga.catalog.io.IOManagerFactory; @@ -46,7 +43,6 @@ import org.opencb.opencga.core.config.Configuration; import org.opencb.opencga.core.config.storage.SampleIndexConfiguration; import org.opencb.opencga.core.models.AclEntryList; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.clinical.ClinicalAnalysisPermissions; import org.opencb.opencga.core.models.cohort.CohortPermissions; import org.opencb.opencga.core.models.common.Enums; @@ -54,15 +50,11 @@ import org.opencb.opencga.core.models.file.File; import org.opencb.opencga.core.models.file.FileInternal; import org.opencb.opencga.core.models.file.FilePermissions; -import org.opencb.opencga.core.models.file.FileStatus; import org.opencb.opencga.core.models.individual.IndividualPermissions; import org.opencb.opencga.core.models.job.JobPermissions; import org.opencb.opencga.core.models.project.Project; import org.opencb.opencga.core.models.sample.SamplePermissions; import org.opencb.opencga.core.models.study.*; -import org.opencb.opencga.core.models.summaries.StudySummary; -import org.opencb.opencga.core.models.summaries.VariableSetSummary; -import org.opencb.opencga.core.models.summaries.VariableSummary; import org.opencb.opencga.core.models.user.User; import org.opencb.opencga.core.response.OpenCGAResult; import org.reflections.Reflections; @@ -87,6 +79,7 @@ import static org.opencb.opencga.core.api.ParamConstants.ADMIN_PROJECT; import static org.opencb.opencga.core.api.ParamConstants.ADMIN_STUDY; import static org.opencb.opencga.core.common.JacksonUtils.getUpdateObjectMapper; +import static org.opencb.opencga.core.models.common.Enums.Resource.STUDY; /** * @author Jacobo Coll <jacobo167@gmail.com> @@ -110,6 +103,9 @@ public class StudyManager extends AbstractManager { public static final QueryOptions INCLUDE_STUDY_IDS = new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( StudyDBAdaptor.QueryParams.UID.key(), StudyDBAdaptor.QueryParams.ID.key(), StudyDBAdaptor.QueryParams.UUID.key(), StudyDBAdaptor.QueryParams.FQN.key())); + static final QueryOptions INCLUDE_BASE = keepFieldsInQueryOptions(INCLUDE_STUDY_IDS, + Arrays.asList(StudyDBAdaptor.QueryParams.VARIABLE_SET.key(), StudyDBAdaptor.QueryParams.GROUPS.key(), + StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION.key())); static final QueryOptions INCLUDE_VARIABLE_SET = new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.VARIABLE_SET.key()); static final QueryOptions INCLUDE_CONFIGURATION = new QueryOptions(QueryOptions.INCLUDE, StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION.key()); @@ -217,7 +213,7 @@ private OpenCGAResult smartResolutor(String studyStr, String userId, Quer if (!queryOptions.isEmpty()) { // Ensure at least the ids are included - fixQueryOptions(queryOptions, INCLUDE_STUDY_IDS.getAsStringList(QueryOptions.INCLUDE)); + keepFieldsInQueryOptions(queryOptions, INCLUDE_STUDY_IDS.getAsStringList(QueryOptions.INCLUDE)); } OpenCGAResult studyDataResult = studyDBAdaptor.get(query, queryOptions, userId); @@ -242,30 +238,6 @@ private OpenCGAResult smartResolutor(String studyStr, String userId, Quer return studyDataResult; } - private void fixQueryOptions(QueryOptions queryOptions, List includeFieldsList) { - if (queryOptions.containsKey(QueryOptions.INCLUDE)) { - Set includeList = new HashSet<>(queryOptions.getAsStringList(QueryOptions.INCLUDE)); - includeList.addAll(Arrays.asList( - StudyDBAdaptor.QueryParams.UUID.key(), StudyDBAdaptor.QueryParams.ID.key(), StudyDBAdaptor.QueryParams.UID.key(), - StudyDBAdaptor.QueryParams.ALIAS.key(), StudyDBAdaptor.QueryParams.CREATION_DATE.key(), - StudyDBAdaptor.QueryParams.NOTIFICATION.key(), StudyDBAdaptor.QueryParams.FQN.key(), - StudyDBAdaptor.QueryParams.URI.key())); - // We create a new object in case there was an exclude or any other field. We only want to include fields in this case - queryOptions.put(QueryOptions.INCLUDE, new ArrayList<>(includeList)); - } else if (queryOptions.containsKey(QueryOptions.EXCLUDE)) { - // We will make sure that the user does not exclude the minimum required fields - Set excludeList = new HashSet<>(queryOptions.getAsStringList(QueryOptions.EXCLUDE)); - for (String field : includeFieldsList) { - excludeList.remove(field); - } - if (!excludeList.isEmpty()) { - queryOptions.put(QueryOptions.EXCLUDE, new ArrayList<>(excludeList)); - } else { - queryOptions.remove(QueryOptions.EXCLUDE); - } - } - } - private OpenCGAResult getStudy(long projectUid, String studyUuid, QueryOptions options) throws CatalogDBException { Query query = new Query() .append(StudyDBAdaptor.QueryParams.PROJECT_UID.key(), projectUid) @@ -290,74 +262,29 @@ public OpenCGAResult create(String projectStr, String id, String alias, S } public OpenCGAResult create(String projectStr, Study study, QueryOptions options, String token) throws CatalogException { - ParamUtils.checkObj(study, "study"); - ParamUtils.checkIdentifier(study.getId(), "id"); - - String userId = catalogManager.getUserManager().getUserId(token); - Project project = catalogManager.getProjectManager().resolveId(projectStr, userId); - ObjectMap auditParams = new ObjectMap() .append("projectId", projectStr) .append("study", study) .append("options", options) .append("token", token); - try { - options = ParamUtils.defaultObject(options, QueryOptions::new); - + return run(auditParams, Enums.Action.CREATE, STUDY, "", token, options, (study1, userId, rp, queryOptions) -> { + Project project = catalogManager.getProjectManager().resolveId(projectStr, userId); /* Check project permissions */ if (!project.getFqn().startsWith(userId + "@")) { throw new CatalogException("Permission denied: Only the owner of the project can create studies."); } - long projectUid = project.getUid(); + validateNewStudy(study, project, userId); + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); - // Initialise fields - study.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.STUDY)); - study.setName(ParamUtils.defaultString(study.getName(), study.getId())); - study.setAlias(ParamUtils.defaultString(study.getAlias(), study.getId())); - study.setType(ParamUtils.defaultObject(study.getType(), StudyType::init)); - study.setSources(ParamUtils.defaultObject(study.getSources(), Collections::emptyList)); - study.setDescription(ParamUtils.defaultString(study.getDescription(), "")); - study.setInternal(StudyInternal.init()); - study.setStatus(ParamUtils.defaultObject(study.getStatus(), Status::new)); - study.setCreationDate(ParamUtils.checkDateOrGetCurrentDate(study.getCreationDate(), - StudyDBAdaptor.QueryParams.CREATION_DATE.key())); - study.setModificationDate(ParamUtils.checkDateOrGetCurrentDate(study.getModificationDate(), - StudyDBAdaptor.QueryParams.MODIFICATION_DATE.key())); - study.setRelease(project.getCurrentRelease()); - study.setNotification(ParamUtils.defaultObject(study.getNotification(), new StudyNotification())); - study.setPermissionRules(ParamUtils.defaultObject(study.getPermissionRules(), HashMap::new)); - study.setAdditionalInfo(ParamUtils.defaultObject(study.getAdditionalInfo(), Collections::emptyList)); - study.setAttributes(ParamUtils.defaultObject(study.getAttributes(), HashMap::new)); - - study.setClinicalAnalyses(ParamUtils.defaultObject(study.getClinicalAnalyses(), ArrayList::new)); - study.setCohorts(ParamUtils.defaultObject(study.getCohorts(), ArrayList::new)); - study.setFamilies(ParamUtils.defaultObject(study.getFamilies(), ArrayList::new)); - study.setPanels(ParamUtils.defaultObject(study.getPanels(), ArrayList::new)); - study.setSamples(ParamUtils.defaultObject(study.getSamples(), ArrayList::new)); - study.setIndividuals(ParamUtils.defaultObject(study.getIndividuals(), ArrayList::new)); - study.setVariableSets(ParamUtils.defaultObject(study.getVariableSets(), ArrayList::new)); - - LinkedList files = new LinkedList<>(); - File rootFile = new File(".", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "", null, "study root folder", - FileInternal.init(), 0, project.getCurrentRelease()); - File jobsFile = new File("JOBS", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "JOBS/", - catalogIOManager.getJobsUri(), "Default jobs folder", FileInternal.init(), 0, project.getCurrentRelease()); - files.add(rootFile); - files.add(jobsFile); - - List groups = Arrays.asList( - new Group(MEMBERS, Collections.singletonList(userId)), - new Group(ADMINS, Collections.emptyList()) - ); - study.setFiles(files); - study.setGroups(groups); + long projectUid = project.getUid(); /* CreateStudy */ studyDBAdaptor.insert(project, study, options); OpenCGAResult result = getStudy(projectUid, study.getUuid(), options); - study = result.getResults().get(0); + study.setUid(result.first().getUid()); URI uri; try { @@ -381,18 +308,58 @@ public OpenCGAResult create(String projectStr, Study study, QueryOptions // Read and process installation variable sets createDefaultVariableSets(study, token); - auditManager.auditCreate(userId, Enums.Resource.STUDY, study.getId(), study.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { result.setResults(Arrays.asList(study)); } return result; - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Resource.STUDY, study.getId(), "", study.getId(), "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); + } + + private void validateNewStudy(Study study, Project project, String userId) throws CatalogParameterException { + ParamUtils.checkObj(study, "study"); + ParamUtils.checkIdentifier(study.getId(), "id"); + + // Initialise fields + study.setUuid(UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.STUDY)); + study.setName(ParamUtils.defaultString(study.getName(), study.getId())); + study.setAlias(ParamUtils.defaultString(study.getAlias(), study.getId())); + study.setType(ParamUtils.defaultObject(study.getType(), StudyType::init)); + study.setSources(ParamUtils.defaultObject(study.getSources(), Collections::emptyList)); + study.setDescription(ParamUtils.defaultString(study.getDescription(), "")); + study.setInternal(StudyInternal.init()); + study.setStatus(ParamUtils.defaultObject(study.getStatus(), Status::new)); + study.setCreationDate(ParamUtils.checkDateOrGetCurrentDate(study.getCreationDate(), + StudyDBAdaptor.QueryParams.CREATION_DATE.key())); + study.setModificationDate(ParamUtils.checkDateOrGetCurrentDate(study.getModificationDate(), + StudyDBAdaptor.QueryParams.MODIFICATION_DATE.key())); + study.setRelease(project.getCurrentRelease()); + study.setNotification(ParamUtils.defaultObject(study.getNotification(), new StudyNotification())); + study.setPermissionRules(ParamUtils.defaultObject(study.getPermissionRules(), HashMap::new)); + study.setAdditionalInfo(ParamUtils.defaultObject(study.getAdditionalInfo(), Collections::emptyList)); + study.setAttributes(ParamUtils.defaultObject(study.getAttributes(), HashMap::new)); + + study.setClinicalAnalyses(ParamUtils.defaultObject(study.getClinicalAnalyses(), ArrayList::new)); + study.setCohorts(ParamUtils.defaultObject(study.getCohorts(), ArrayList::new)); + study.setFamilies(ParamUtils.defaultObject(study.getFamilies(), ArrayList::new)); + study.setPanels(ParamUtils.defaultObject(study.getPanels(), ArrayList::new)); + study.setSamples(ParamUtils.defaultObject(study.getSamples(), ArrayList::new)); + study.setIndividuals(ParamUtils.defaultObject(study.getIndividuals(), ArrayList::new)); + study.setVariableSets(ParamUtils.defaultObject(study.getVariableSets(), ArrayList::new)); + + LinkedList files = new LinkedList<>(); + File rootFile = new File(".", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "", null, "study root folder", + FileInternal.init(), 0, project.getCurrentRelease()); + File jobsFile = new File("JOBS", File.Type.DIRECTORY, File.Format.UNKNOWN, File.Bioformat.UNKNOWN, "JOBS/", + catalogIOManager.getJobsUri(), "Default jobs folder", FileInternal.init(), 0, project.getCurrentRelease()); + files.add(rootFile); + files.add(jobsFile); + + List groups = Arrays.asList( + new Group(MEMBERS, Collections.singletonList(userId)), + new Group(ADMINS, Collections.emptyList()) + ); + study.setFiles(files); + study.setGroups(groups); } public void createDefaultVariableSets(String studyStr, String token) throws CatalogException { @@ -426,12 +393,6 @@ private void createDefaultVariableSets(Study study, String token) throws Catalog } } - public int getCurrentRelease(Study study, String sessionId) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(sessionId); - authorizationManager.checkCanViewStudy(study.getUid(), userId); - return getCurrentRelease(study); - } - int getCurrentRelease(Study study) throws CatalogException { String[] split = StringUtils.split(study.getFqn(), ":"); String userId = StringUtils.split(split[0], "@")[0]; @@ -470,44 +431,12 @@ MyResourceId getVariableSetId(String variableStr, @Nullable String studyStr, Str return new MyResourceId(userId, studyId, variableSetId); } - /** - * Fetch a study from Catalog given a study id or alias. - * - * @param studyStr Study id or alias. - * @param options Read options - * @param token sessionId - * @return The specified object - * @throws CatalogException CatalogException - */ - public OpenCGAResult get(String studyStr, QueryOptions options, String token) throws CatalogException { - options = ParamUtils.defaultObject(options, QueryOptions::new); - - String userId = catalogManager.getUserManager().getUserId(token); - ObjectMap auditParams = new ObjectMap() - .append("studyStr", studyStr) - .append("options", options) - .append("token", token); - try { - StopWatch stopWatch = StopWatch.createStarted(); - Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, options); - auditManager.auditInfo(userId, Enums.Resource.STUDY, study.getId(), study.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - OpenCGAResult studyDataResult = new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), null, 1, - Collections.singletonList(study), 1); - return filterResults(studyDataResult); - } catch (CatalogException e) { - auditManager.auditInfo(userId, Enums.Resource.STUDY, studyStr, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - } - - public OpenCGAResult get(List studyList, QueryOptions queryOptions, boolean ignoreException, String sessionId) + public OpenCGAResult get(List studyList, QueryOptions queryOptions, boolean ignoreException, String token) throws CatalogException { OpenCGAResult result = OpenCGAResult.empty(Study.class); for (String study : studyList) { try { - OpenCGAResult studyObj = get(study, queryOptions, sessionId); + OpenCGAResult studyObj = get(study, queryOptions, token); result.append(studyObj); } catch (CatalogException e) { String warning = "Missing " + study + ": " + e.getMessage(); @@ -524,39 +453,70 @@ public OpenCGAResult get(List studyList, QueryOptions queryOption return filterResults(result); } + /** + * Fetch a study from Catalog given a study id or alias. + * + * @param studyStr Study id or alias. + * @param options Read options + * @param token sessionId + * @return The specified object + * @throws CatalogException CatalogException + */ + public OpenCGAResult get(String studyStr, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("options", options) + .append("token", token); + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.INFO, STUDY, "", token, options, (s, userId, rp, queryOptions) -> { + Study study = catalogManager.getStudyManager().resolveId(studyStr, userId, queryOptions); + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + OpenCGAResult studyDataResult = new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), null, 1, + Collections.singletonList(study), 1); + return filterResults(studyDataResult); + }); + } + /** * Fetch all the study objects matching the query. * * @param projectStr Project id or alias. * @param query Query to catalog. * @param options Query options, like "include", "exclude", "limit" and "skip" - * @param sessionId sessionId + * @param token sessionId * @return All matching elements. * @throws CatalogException CatalogException */ - public OpenCGAResult search(String projectStr, Query query, QueryOptions options, String sessionId) throws CatalogException { - ParamUtils.checkParameter(projectStr, "project"); - ParamUtils.defaultObject(query, Query::new); - ParamUtils.defaultObject(options, QueryOptions::new); - - String auxProject = null; - String auxOwner = null; - if (StringUtils.isNotEmpty(projectStr)) { - String[] split = projectStr.split("@"); - if (split.length == 1) { - auxProject = projectStr; - } else if (split.length == 2) { - auxOwner = split[0]; - auxProject = split[1]; - } else { - throw new CatalogException(projectStr + " does not follow the expected pattern [ownerId@projectId]"); + public OpenCGAResult search(String projectStr, Query query, QueryOptions options, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("project", projectStr) + .append("query", query) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.SEARCH, STUDY, "", token, options, (study, userId, rp, queryOptions) -> { + ParamUtils.checkParameter(projectStr, "project"); + Query myQuery = query != null ? new Query(query) : new Query(); + + String auxProject = null; + String auxOwner = null; + if (StringUtils.isNotEmpty(projectStr)) { + String[] split = projectStr.split("@"); + if (split.length == 1) { + auxProject = projectStr; + } else if (split.length == 2) { + auxOwner = split[0]; + auxProject = split[1]; + } else { + throw new CatalogException(projectStr + " does not follow the expected pattern [ownerId@projectId]"); + } } - } - query.putIfNotNull(StudyDBAdaptor.QueryParams.PROJECT_ID.key(), auxProject); - query.putIfNotNull(StudyDBAdaptor.QueryParams.OWNER.key(), auxOwner); + myQuery.putIfNotNull(StudyDBAdaptor.QueryParams.PROJECT_ID.key(), auxProject); + myQuery.putIfNotNull(StudyDBAdaptor.QueryParams.OWNER.key(), auxOwner); - return search(query, options, sessionId); + return search(myQuery, queryOptions, token); + }); } /** @@ -569,31 +529,22 @@ public OpenCGAResult search(String projectStr, Query query, QueryOptions * @throws CatalogException CatalogException */ public OpenCGAResult search(Query query, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - QueryOptions qOptions = options != null ? new QueryOptions(options) : new QueryOptions(); - - String userId = catalogManager.getUserManager().getUserId(token); - ObjectMap auditParams = new ObjectMap() .append("query", query) .append("options", options) .append("token", token); - if (!qOptions.containsKey("include") || qOptions.get("include") == null || qOptions.getAsStringList("include").isEmpty()) { - qOptions.addToListOption("exclude", "projects.studies.attributes.studyConfiguration"); - } - try { - fixQueryObject(query); + return run(auditParams, Enums.Action.SEARCH, STUDY, "", token, options, (study, userId, rp, qOptions) -> { + Query myQuery = query != null ? new Query(query) : new Query(); + + if (!qOptions.containsKey("include") || qOptions.get("include") == null || qOptions.getAsStringList("include").isEmpty()) { + qOptions.addToListOption("exclude", "projects.studies.attributes.studyConfiguration"); + } + fixQueryObject(myQuery); - OpenCGAResult studyDataResult = studyDBAdaptor.get(query, qOptions, userId); - auditManager.auditSearch(userId, Enums.Resource.STUDY, "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + OpenCGAResult studyDataResult = studyDBAdaptor.get(myQuery, qOptions, userId); return filterResults(studyDataResult); - } catch (CatalogException e) { - auditManager.auditSearch(userId, Enums.Resource.STUDY, "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } private OpenCGAResult filterResults(OpenCGAResult result) { @@ -621,28 +572,17 @@ private OpenCGAResult filterResults(OpenCGAResult result) { */ public OpenCGAResult update(String studyId, StudyUpdateParams parameters, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("updateParams", parameters) .append("options", options) .append("token", token); - Study study; - try { - study = resolveId(studyId, userId); - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.STUDY, studyId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - - try { - options = ParamUtils.defaultObject(options, QueryOptions::new); - ParamUtils.checkObj(parameters, "Parameters"); - + return run(auditParams, Enums.Action.UPDATE, STUDY, studyId, token, options, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkCanEditStudy(study.getUid(), userId); + ParamUtils.checkObj(parameters, "Parameters"); if (StringUtils.isNotEmpty(parameters.getAlias())) { ParamUtils.checkIdentifier(parameters.getAlias(), "alias"); } @@ -661,130 +601,101 @@ public OpenCGAResult update(String studyId, StudyUpdateParams parameters, throw new CatalogException("Jackson casting error: " + e.getMessage(), e); } - OpenCGAResult updateResult = studyDBAdaptor.update(study.getUid(), update, options); - auditManager.auditUpdate(userId, Enums.Resource.STUDY, study.getId(), study.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + OpenCGAResult updateResult = studyDBAdaptor.update(study.getUid(), update, queryOptions); + if (queryOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch updated study - OpenCGAResult result = studyDBAdaptor.get(study.getUid(), options); + OpenCGAResult result = studyDBAdaptor.get(study.getUid(), queryOptions); updateResult.setResults(result.getResults()); } return updateResult; - } catch (CatalogException e) { - auditManager.auditUpdate(userId, Enums.Resource.STUDY, study.getId(), study.getUuid(), study.getId(), study.getUuid(), - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult createPermissionRule(String studyId, Enums.Entity entry, PermissionRule permissionRule, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId, INCLUDE_STUDY_IDS); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("entry", entry) .append("permissionRule", permissionRule) .append("token", token); - try { - ParamUtils.checkObj(entry, "entry"); - ParamUtils.checkObj(permissionRule, "permission rule"); + return run(auditParams, Enums.Action.ADD_STUDY_PERMISSION_RULE, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkCanUpdatePermissionRules(study.getUid(), userId); + + ParamUtils.checkObj(entry, "entry"); + ParamUtils.checkObj(permissionRule, "permission rule"); validatePermissionRules(study.getUid(), entry, permissionRule); OpenCGAResult result = studyDBAdaptor.createPermissionRule(study.getUid(), entry, permissionRule); - auditManager.audit(userId, Enums.Action.ADD_STUDY_PERMISSION_RULE, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>(result.getTime(), result.getEvents(), 1, Collections.singletonList(permissionRule), 1, result.getNumInserted(), result.getNumUpdated(), result.getNumDeleted(), result.getNumErrors(), new ObjectMap()); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.ADD_STUDY_PERMISSION_RULE, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public void markDeletedPermissionRule(String studyId, Enums.Entity entry, String permissionRuleId, PermissionRule.DeleteAction deleteAction, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("entry", entry) .append("permissionRuleId", permissionRuleId) .append("deleteAction", deleteAction) .append("token", token); - try { + run(auditParams, Enums.Action.REMOVE_STUDY_PERMISSION_RULE, STUDY, studyId, token, null, (study, userId, rp, qo) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkCanUpdatePermissionRules(study.getUid(), userId); + ParamUtils.checkObj(entry, "entry"); ParamUtils.checkObj(deleteAction, "Delete action"); ParamUtils.checkObj(permissionRuleId, "permission rule id"); - authorizationManager.checkCanUpdatePermissionRules(study.getUid(), userId); studyDBAdaptor.markDeletedPermissionRule(study.getUid(), entry, permissionRuleId, deleteAction); - - auditManager.audit(userId, Enums.Action.REMOVE_STUDY_PERMISSION_RULE, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.REMOVE_STUDY_PERMISSION_RULE, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return null; + }); } public OpenCGAResult getPermissionRules(String studyId, Enums.Entity entry, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("entry", entry) .append("token", token); - try { - authorizationManager.checkCanViewStudy(study.getUid(), userId); - OpenCGAResult result = studyDBAdaptor.getPermissionRules(study.getUid(), entry); - - auditManager.audit(userId, Enums.Action.FETCH_STUDY_PERMISSION_RULES, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.FETCH_STUDY_PERMISSION_RULES, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.FETCH_STUDY_PERMISSION_RULES, STUDY, studyId, token, null, (study, userId, rp, qo) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + return studyDBAdaptor.getPermissionRules(study.getUid(), entry); + }); } - public OpenCGAResult rank(long projectId, Query query, String field, int numResults, boolean asc, String sessionId) + public OpenCGAResult rank(long projectId, Query query, String field, int numResults, boolean asc, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - ParamUtils.checkObj(field, "field"); - ParamUtils.checkObj(projectId, "projectId"); - - String userId = catalogManager.getUserManager().getUserId(sessionId); - authorizationManager.checkCanViewProject(projectId, userId); - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; -// query.append(CatalogFileDBAdaptor.QueryParams.STUDY_UID.key(), studyId); - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = studyDBAdaptor.rank(query, field, numResults, asc); - } + ObjectMap auditParams = new ObjectMap() + .append("projectId", projectId) + .append("query", query) + .append("field", field) + .append("numResults", numResults) + .append("asc", asc) + .append("token", token); + return run(auditParams, Enums.Action.RANK, STUDY, "", token, null, (study, userId, rp, queryOptions) -> { + authorizationManager.checkCanViewProject(projectId, userId); + + Query myQuery = query != null ? new Query(query) : new Query(); + ParamUtils.checkObj(field, "field"); + ParamUtils.checkObj(projectId, "projectId"); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = studyDBAdaptor.rank(myQuery, field, numResults, asc); + } - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } public OpenCGAResult groupBy(long projectId, Query query, String field, QueryOptions options, String sessionId) @@ -792,85 +703,30 @@ public OpenCGAResult groupBy(long projectId, Query query, String field, QueryOpt return groupBy(projectId, query, Collections.singletonList(field), options, sessionId); } - public OpenCGAResult groupBy(long projectId, Query query, List fields, QueryOptions options, String sessionId) + public OpenCGAResult groupBy(long projectId, Query query, List fields, QueryOptions options, String token) throws CatalogException { - query = ParamUtils.defaultObject(query, Query::new); - options = ParamUtils.defaultObject(options, QueryOptions::new); - ParamUtils.checkObj(fields, "fields"); - ParamUtils.checkObj(projectId, "projectId"); - - String userId = catalogManager.getUserManager().getUserId(sessionId); - authorizationManager.checkCanViewProject(projectId, userId); - - // TODO: In next release, we will have to check the count parameter from the queryOptions object. - boolean count = true; - OpenCGAResult queryResult = null; - if (count) { - // We do not need to check for permissions when we show the count of files - queryResult = studyDBAdaptor.groupBy(query, fields, options); - } - - return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); - } - - public OpenCGAResult getSummary(String studyStr, QueryOptions queryOptions, String sessionId) throws CatalogException { - long startTime = System.currentTimeMillis(); - - Study study = get(studyStr, queryOptions, sessionId).first(); - - StudySummary studySummary = new StudySummary() - .setAlias(study.getId()) - .setAttributes(study.getAttributes()) - .setCreationDate(study.getCreationDate()) - .setDescription(study.getDescription()) - .setSize(study.getSize()) - .setGroups(study.getGroups()) - .setName(study.getName()) - .setInternal(study.getInternal()) - .setVariableSets(study.getVariableSets()); - - Long nFiles = fileDBAdaptor.count( - new Query(FileDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid()) - .append(FileDBAdaptor.QueryParams.TYPE.key(), File.Type.FILE) - .append(FileDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key(), "!=" + FileStatus.TRASHED + ";!=" - + FileStatus.DELETED)) - .getNumMatches(); - studySummary.setFiles(nFiles); - - Long nSamples = sampleDBAdaptor.count(new Query(SampleDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid())).getNumMatches(); - studySummary.setSamples(nSamples); - - Long nJobs = jobDBAdaptor.count(new Query(JobDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid())).getNumMatches(); - studySummary.setJobs(nJobs); - - Long nCohorts = cohortDBAdaptor.count(new Query(CohortDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid())).getNumMatches(); - studySummary.setCohorts(nCohorts); - - Long nIndividuals = individualDBAdaptor.count(new Query(IndividualDBAdaptor.QueryParams.STUDY_UID.key(), study.getUid())) - .getNumMatches(); - studySummary.setIndividuals(nIndividuals); - - return new OpenCGAResult<>((int) (System.currentTimeMillis() - startTime), Collections.emptyList(), 1, - Collections.singletonList(studySummary), 1); - } - - public List> getSummary(List studyList, QueryOptions queryOptions, boolean ignoreException, - String token) throws CatalogException { - List> results = new ArrayList<>(studyList.size()); - for (String study : studyList) { - try { - OpenCGAResult summaryObj = getSummary(study, queryOptions, token); - results.add(summaryObj); - } catch (CatalogException e) { - if (ignoreException) { - Event event = new Event(Event.Type.ERROR, study, e.getMessage()); - results.add(new OpenCGAResult<>(0, Collections.singletonList(event), 0, Collections.emptyList(), 0)); - } else { - throw e; - } + ObjectMap auditParams = new ObjectMap() + .append("projectId", projectId) + .append("query", query) + .append("fields", fields) + .append("options", options) + .append("token", token); + return run(auditParams, Enums.Action.GROUP_BY, STUDY, "", token, options, (study, userId, rp, queryOptions) -> { + authorizationManager.checkCanViewProject(projectId, userId); + ParamUtils.checkObj(fields, "fields"); + ParamUtils.checkObj(projectId, "projectId"); + Query myQuery = query != null ? new Query(query) : new Query(); + + // TODO: In next release, we will have to check the count parameter from the queryOptions object. + boolean count = true; + OpenCGAResult queryResult = null; + if (count) { + // We do not need to check for permissions when we show the count of files + queryResult = studyDBAdaptor.groupBy(myQuery, fields, queryOptions); } - } - return results; + + return ParamUtils.defaultObject(queryResult, OpenCGAResult::new); + }); } public OpenCGAResult createGroup(String studyStr, String groupId, List users, String sessionId) @@ -880,14 +736,17 @@ public OpenCGAResult createGroup(String studyStr, String groupId, List createGroup(String studyId, Group group, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("group", group) .append("token", token); - try { + + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.ADD_STUDY_GROUP, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkCreateDeleteGroupPermissions(study.getUid(), userId, group.getId()); + ParamUtils.checkObj(group, "group"); ParamUtils.checkGroupId(group.getId()); group.setUserIds(ParamUtils.defaultObject(group.getUserIds(), Collections::emptyList)); @@ -902,8 +761,6 @@ public OpenCGAResult createGroup(String studyId, Group group, String toke group.setId("@" + group.getId()); } - authorizationManager.checkCreateDeleteGroupPermissions(study.getUid(), userId, group.getId()); - // Check group exists if (existsGroup(study.getUid(), group.getId())) { throw new CatalogException("The group " + group.getId() + " already exists."); @@ -912,7 +769,7 @@ public OpenCGAResult createGroup(String studyId, Group group, String toke List users = group.getUserIds(); if (CollectionUtils.isNotEmpty(users)) { // We remove possible duplicates - users = users.stream().collect(Collectors.toSet()).stream().collect(Collectors.toList()); + users = new ArrayList<>(new HashSet<>(users)); userDBAdaptor.checkIds(users); } else { users = Collections.emptyList(); @@ -925,69 +782,53 @@ public OpenCGAResult createGroup(String studyId, Group group, String toke } // Create the group - OpenCGAResult result = studyDBAdaptor.createGroup(study.getUid(), group); + studyDBAdaptor.createGroup(study.getUid(), group); OpenCGAResult queryResult = studyDBAdaptor.getGroup(study.getUid(), group.getId(), null); - queryResult.setTime(queryResult.getTime() + result.getTime()); - - auditManager.audit(userId, Enums.Action.ADD_STUDY_GROUP, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - + queryResult.setTime((int) stopWatch.getTime(TimeUnit.MILLISECONDS)); return queryResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.ADD_STUDY_GROUP, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult getGroup(String studyId, String groupId, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("groupId", groupId) .append("token", token); - try { + + return run(auditParams, Enums.Action.FETCH_STUDY_GROUPS, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkCanViewStudy(study.getUid(), userId); // Fix the groupId + String finalGroupId = groupId; if (groupId != null && !groupId.startsWith("@")) { - groupId = "@" + groupId; + finalGroupId = "@" + groupId; } - OpenCGAResult result = studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); - auditManager.audit(userId, Enums.Action.FETCH_STUDY_GROUPS, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.FETCH_STUDY_GROUPS, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return studyDBAdaptor.getGroup(study.getUid(), finalGroupId, Collections.emptyList()); + }); } public OpenCGAResult getCustomGroups(String studyId, String groupId, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("groupId", groupId) .append("token", token); - try { - StopWatch stopWatch = new StopWatch(); - stopWatch.start(); - + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.FETCH_STUDY_GROUPS, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); // Fix the groupId + String finalGroupId = groupId; if (groupId != null && !groupId.startsWith("@")) { - groupId = "@" + groupId; + finalGroupId = "@" + groupId; } - OpenCGAResult result = studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); + OpenCGAResult result = studyDBAdaptor.getGroup(study.getUid(), finalGroupId, Collections.emptyList()); // Extract all users from all groups Set userIds = new HashSet<>(); @@ -1020,47 +861,38 @@ public OpenCGAResult getCustomGroups(String studyId, String groupId OpenCGAResult finalResult = new OpenCGAResult<>(result.getTime(), result.getEvents(), result.getNumResults(), customGroupList, result.getNumMatches(), result.getNumInserted(), result.getNumUpdated(), result.getNumDeleted(), result.getNumErrors(), result.getAttributes(), result.getFederationNode()); - - auditManager.audit(userId, Enums.Action.FETCH_STUDY_GROUPS, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - stopWatch.stop(); finalResult.setTime((int) stopWatch.getTime(TimeUnit.MILLISECONDS)); - return finalResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.FETCH_STUDY_GROUPS, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult updateGroup(String studyId, String groupId, ParamUtils.BasicUpdateAction action, GroupUpdateParams updateParams, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("groupId", groupId) .append("action", action) .append("updateParams", updateParams) .append("token", token); - try { + return run(auditParams, Enums.Action.UPDATE_USERS_FROM_STUDY_GROUP, STUDY, studyId, token, null, (study, userId, rp, qOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + ParamUtils.checkObj(updateParams, "Group parameters"); ParamUtils.checkParameter(groupId, "Group name"); ParamUtils.checkObj(action, "Action"); // Fix the group name + String finalGroupId = groupId; if (!groupId.startsWith("@")) { - groupId = "@" + groupId; + finalGroupId = "@" + groupId; } - authorizationManager.checkUpdateGroupPermissions(study.getUid(), userId, groupId, action); + authorizationManager.checkUpdateGroupPermissions(study.getUid(), userId, finalGroupId, action); if (CollectionUtils.isNotEmpty(updateParams.getUsers())) { List tmpUsers = updateParams.getUsers(); - if (groupId.equals(MEMBERS) || groupId.equals(ADMINS)) { + if (finalGroupId.equals(MEMBERS) || finalGroupId.equals(ADMINS)) { // Remove anonymous user if present for the checks. // Anonymous user is only allowed in MEMBERS group, otherwise we keep it as if it is present it should fail. tmpUsers = updateParams.getUsers().stream() @@ -1077,20 +909,20 @@ public OpenCGAResult updateGroup(String studyId, String groupId, ParamUti switch (action) { case SET: - if (MEMBERS.equals(groupId)) { + if (MEMBERS.equals(finalGroupId)) { throw new CatalogException("Operation not valid. Valid actions over the '@members' group are ADD or REMOVE."); } - studyDBAdaptor.setUsersToGroup(study.getUid(), groupId, updateParams.getUsers()); + studyDBAdaptor.setUsersToGroup(study.getUid(), finalGroupId, updateParams.getUsers()); studyDBAdaptor.addUsersToGroup(study.getUid(), MEMBERS, updateParams.getUsers()); break; case ADD: - studyDBAdaptor.addUsersToGroup(study.getUid(), groupId, updateParams.getUsers()); - if (!MEMBERS.equals(groupId)) { + studyDBAdaptor.addUsersToGroup(study.getUid(), finalGroupId, updateParams.getUsers()); + if (!MEMBERS.equals(finalGroupId)) { studyDBAdaptor.addUsersToGroup(study.getUid(), MEMBERS, updateParams.getUsers()); } break; case REMOVE: - if (MEMBERS.equals(groupId)) { + if (MEMBERS.equals(finalGroupId)) { // Check we are not trying to remove the owner of the study from the group String owner = getOwner(study); if (updateParams.getUsers().contains(owner)) { @@ -1101,36 +933,27 @@ public OpenCGAResult updateGroup(String studyId, String groupId, ParamUti authorizationManager.resetPermissionsFromAllEntities(study.getUid(), updateParams.getUsers()); studyDBAdaptor.removeUsersFromAllGroups(study.getUid(), updateParams.getUsers()); } else { - studyDBAdaptor.removeUsersFromGroup(study.getUid(), groupId, updateParams.getUsers()); + studyDBAdaptor.removeUsersFromGroup(study.getUid(), finalGroupId, updateParams.getUsers()); } break; default: throw new CatalogException("Unknown action " + action + " found."); } - auditManager.audit(userId, Enums.Action.UPDATE_USERS_FROM_STUDY_GROUP, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.UPDATE_USERS_FROM_STUDY_GROUP, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return studyDBAdaptor.getGroup(study.getUid(), finalGroupId, Collections.emptyList()); + }); } - public OpenCGAResult updateSummaryIndex(String studyStr, RecessiveGeneSummaryIndex summaryIndex, - String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyStr, userId); - + public OpenCGAResult updateSummaryIndex(String studyStr, RecessiveGeneSummaryIndex summaryIndex, String token) + throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("studyId", studyStr) .append("recessiveGeneSummaryIndex", summaryIndex) .append("token", token); - try { + + return run(auditParams, Enums.Action.UPDATE_INTERNAL, STUDY, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); ParamUtils.checkObj(summaryIndex, "RecessiveGeneSummaryIndex"); @@ -1144,136 +967,42 @@ public OpenCGAResult updateSummaryIndex(String studyStr, RecessiveGeneSummary throw new CatalogException("Jackson casting error: " + e.getMessage(), e); } - OpenCGAResult result = studyDBAdaptor.update(study.getUid(), update, QueryOptions.empty()); - - auditManager.audit(userId, Enums.Action.UPDATE_INTERNAL, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.UPDATE_INTERNAL, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - + return studyDBAdaptor.update(study.getUid(), update, QueryOptions.empty()); + }); } -// public OpenCGAResult 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); -// -// OpenCGAResult 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_ID.key(), groupId); -// if (studyDBAdaptor.count(query).getNumMatches() == 0) { -// throw new CatalogException("The group " + groupId + " does not exist."); -// } -// -// studyDBAdaptor.syncGroup(study.getUid(), groupId, syncedFrom); -// -// return studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); -// } - public OpenCGAResult deleteGroup(String studyId, String groupId, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("groupId", groupId) .append("token", token); - try { + + return run(auditParams, Enums.Action.REMOVE_STUDY_GROUP, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); if (StringUtils.isEmpty(groupId)) { throw new CatalogException("Missing group id"); } // Fix the groupId + String finalGroupId = groupId; if (!groupId.startsWith("@")) { - groupId = "@" + groupId; + finalGroupId = "@" + groupId; } - authorizationManager.checkCreateDeleteGroupPermissions(study.getUid(), userId, groupId); + authorizationManager.checkCreateDeleteGroupPermissions(study.getUid(), userId, finalGroupId); - OpenCGAResult group = studyDBAdaptor.getGroup(study.getUid(), groupId, Collections.emptyList()); + OpenCGAResult group = studyDBAdaptor.getGroup(study.getUid(), finalGroupId, Collections.emptyList()); // Remove the permissions the group might have had StudyAclParams aclParams = new StudyAclParams(null, null); - updateAcl(Collections.singletonList(studyId), groupId, aclParams, ParamUtils.AclAction.RESET, token); - - studyDBAdaptor.deleteGroup(study.getUid(), groupId); - - auditManager.audit(userId, Enums.Action.REMOVE_STUDY_GROUP, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + updateAcl(Collections.singletonList(studyId), finalGroupId, aclParams, ParamUtils.AclAction.RESET, token); + studyDBAdaptor.deleteGroup(study.getUid(), finalGroupId); return group; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.REMOVE_STUDY_GROUP, Enums.Resource.STUDY, study.getId(), study.getUuid(), - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } - public OpenCGAResult getVariableSetSummary(String studyStr, String variableSetStr, String sessionId) - throws CatalogException { - MyResourceId resource = getVariableSetId(variableSetStr, studyStr, sessionId); - - String userId = resource.getUser(); - - OpenCGAResult 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()); - - OpenCGAResult annotationSummary = sampleDBAdaptor.getAnnotationSummary(resource.getStudyId(), - resource.getResourceId()); - dbTime += annotationSummary.getTime(); - variableSetSummary.setSamples(annotationSummary.getResults()); - - annotationSummary = cohortDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); - dbTime += annotationSummary.getTime(); - variableSetSummary.setCohorts(annotationSummary.getResults()); - - annotationSummary = individualDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); - dbTime += annotationSummary.getTime(); - variableSetSummary.setIndividuals(annotationSummary.getResults()); - - annotationSummary = familyDBAdaptor.getAnnotationSummary(resource.getStudyId(), resource.getResourceId()); - dbTime += annotationSummary.getTime(); - variableSetSummary.setFamilies(annotationSummary.getResults()); - - return new OpenCGAResult<>(dbTime, Collections.emptyList(), 1, Collections.singletonList(variableSetSummary), 1); - } - - /* * Variables Methods */ @@ -1320,61 +1049,43 @@ public OpenCGAResult createVariableSet(String studyId, String id, S } public OpenCGAResult createVariableSet(String studyId, VariableSet variableSet, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("variableSet", variableSet) .append("token", token); - try { - OpenCGAResult queryResult = createVariableSet(study, variableSet, token); - auditManager.audit(userId, Enums.Action.ADD_VARIABLE_SET, Enums.Resource.STUDY, queryResult.first().getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return queryResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.ADD_VARIABLE_SET, Enums.Resource.STUDY, variableSet.getId(), "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return run(auditParams, Enums.Action.ADD_VARIABLE_SET, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + return createVariableSet(study, variableSet, token); + }); } public OpenCGAResult getVariableSet(String studyId, String variableSetId, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId, StudyManager.INCLUDE_VARIABLE_SET); - ObjectMap auditParams = new ObjectMap() .append("studyId", studyId) .append("variableSetId", variableSetId) .append("options", options) .append("token", token); - try { - options = ParamUtils.defaultObject(options, QueryOptions::new); + return run(auditParams, Enums.Action.FETCH_VARIABLE_SET, STUDY, studyId, token, options, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); VariableSet variableSet = extractVariableSet(study, variableSetId, userId); - OpenCGAResult result = studyDBAdaptor.getVariableSet(variableSet.getUid(), options, userId); - - auditManager.audit(userId, Enums.Action.FETCH_VARIABLE_SET, Enums.Resource.STUDY, variableSet.getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return result; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.FETCH_VARIABLE_SET, Enums.Resource.STUDY, variableSetId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return studyDBAdaptor.getVariableSet(variableSet.getUid(), queryOptions, userId); + }); } - public OpenCGAResult searchVariableSets(String studyStr, Query query, QueryOptions options, String sessionId) + @Deprecated + public OpenCGAResult searchVariableSets(String studyStr, Query query, QueryOptions options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(sessionId); + String userId = catalogManager.getUserManager().getUserId(token); Study study = resolveId(studyStr, userId); // authorizationManager.checkStudyPermission(studyId, userId, StudyAclEntry.StudyPermissions.VIEW_VARIABLE_SET); options = ParamUtils.defaultObject(options, QueryOptions::new); query = ParamUtils.defaultObject(query, Query::new); if (query.containsKey(StudyDBAdaptor.VariableSetParams.UID.key())) { // Id could be either the id or the name - MyResourceId resource = getVariableSetId(query.getString(StudyDBAdaptor.VariableSetParams.UID.key()), studyStr, sessionId); + MyResourceId resource = getVariableSetId(query.getString(StudyDBAdaptor.VariableSetParams.UID.key()), studyStr, token); query.put(StudyDBAdaptor.VariableSetParams.UID.key(), resource.getResourceId()); } query.put(StudyDBAdaptor.VariableSetParams.STUDY_UID.key(), study.getUid()); @@ -1383,41 +1094,35 @@ public OpenCGAResult searchVariableSets(String studyStr, Query quer public OpenCGAResult deleteVariableSet(String studyId, String variableSetId, boolean force, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId, StudyManager.INCLUDE_VARIABLE_SET); - VariableSet variableSet = extractVariableSet(study, variableSetId, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("variableSetId", variableSetId) .append("force", force) .append("token", token); - try { + return run(auditParams, Enums.Action.DELETE_VARIABLE_SET, STUDY, studyId, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkCanCreateUpdateDeleteVariableSets(study.getUid(), userId); - OpenCGAResult writeResult = studyDBAdaptor.deleteVariableSet(study.getUid(), variableSet, force); - auditManager.audit(userId, Enums.Action.DELETE_VARIABLE_SET, Enums.Resource.STUDY, variableSet.getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - return writeResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.DELETE_VARIABLE_SET, Enums.Resource.STUDY, variableSet.getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + VariableSet variableSet = extractVariableSet(study, variableSetId, userId); + + return studyDBAdaptor.deleteVariableSet(study.getUid(), variableSet, force); + }); } public OpenCGAResult addFieldToVariableSet(String studyId, String variableSetId, Variable variable, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId, StudyManager.INCLUDE_VARIABLE_SET); - VariableSet variableSet = extractVariableSet(study, variableSetId, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("variableSetId", variableSetId) .append("variable", variable) .append("token", token); - try { + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.ADD_VARIABLE_TO_VARIABLE_SET, STUDY, studyId, token, null, (study, userId, rp, qOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkCanCreateUpdateDeleteVariableSets(study.getUid(), userId); + + VariableSet variableSet = extractVariableSet(study, variableSetId, userId); if (StringUtils.isEmpty(variable.getId())) { if (StringUtils.isEmpty(variable.getName())) { throw new CatalogException("Missing variable id"); @@ -1425,49 +1130,33 @@ public OpenCGAResult addFieldToVariableSet(String studyId, String v variable.setId(variable.getName()); } - authorizationManager.checkCanCreateUpdateDeleteVariableSets(study.getUid(), userId); - OpenCGAResult result = studyDBAdaptor.addFieldToVariableSet(variableSet.getUid(), variable, userId); - auditManager.audit(userId, Enums.Action.ADD_VARIABLE_TO_VARIABLE_SET, Enums.Resource.STUDY, variableSet.getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - + studyDBAdaptor.addFieldToVariableSet(variableSet.getUid(), variable, userId); OpenCGAResult queryResult = studyDBAdaptor.getVariableSet(variableSet.getUid(), QueryOptions.empty()); - queryResult.setTime(queryResult.getTime() + result.getTime()); + queryResult.setTime((int) stopWatch.getTime(TimeUnit.MILLISECONDS)); return queryResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.ADD_VARIABLE_TO_VARIABLE_SET, Enums.Resource.STUDY, variableSet.getId(), "", - study.getId(), study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } public OpenCGAResult removeFieldFromVariableSet(String studyId, String variableSetId, String variableId, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyId, userId, StudyManager.INCLUDE_VARIABLE_SET); - VariableSet variableSet = extractVariableSet(study, variableSetId, userId); - ObjectMap auditParams = new ObjectMap() .append("study", studyId) .append("variableSetId", variableSetId) .append("variableId", variableId) .append("token", token); - - try { + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.REMOVE_VARIABLE_FROM_VARIABLE_SET, STUDY, studyId, token, null, (study, userId, rp, qo) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkCanCreateUpdateDeleteVariableSets(study.getUid(), userId); - OpenCGAResult result = studyDBAdaptor.removeFieldFromVariableSet(variableSet.getUid(), variableId, userId); - auditManager.audit(userId, Enums.Action.REMOVE_VARIABLE_FROM_VARIABLE_SET, Enums.Resource.STUDY, - variableSet.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + + VariableSet variableSet = extractVariableSet(study, variableSetId, userId); + studyDBAdaptor.removeFieldFromVariableSet(variableSet.getUid(), variableId, userId); OpenCGAResult queryResult = studyDBAdaptor.getVariableSet(variableSet.getUid(), QueryOptions.empty()); - queryResult.setTime(queryResult.getTime() + result.getTime()); + queryResult.setTime((int) stopWatch.getTime(TimeUnit.MILLISECONDS)); return queryResult; - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.REMOVE_VARIABLE_FROM_VARIABLE_SET, Enums.Resource.STUDY, - variableSet.getId(), "", study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } private VariableSet extractVariableSet(Study study, String variableSetId, String userId) throws CatalogException { @@ -1489,64 +1178,49 @@ private VariableSet extractVariableSet(Study study, String variableSetId, String return queryResult.first(); } - public OpenCGAResult renameFieldFromVariableSet(String studyStr, String variableSetStr, String oldName, String newName, - String sessionId) throws CatalogException { - throw new UnsupportedOperationException("Operation not yet supported"); - -// MyResourceId resource = getVariableSetId(variableSetStr, studyStr, sessionId); -// String userId = resource.getUser(); -// -// authorizationManager.checkCanCreateUpdateDeleteVariableSets(resource.getStudyId(), userId); -// OpenCGAResult queryResult = studyDBAdaptor.renameFieldVariableSet(resource.getResourceId(), oldName, newName,userId); -// auditManager.recordDeletion(AuditRecord.Resource.variableSet, resource.getResourceId(), userId, queryResult.first(), null, null); -// return queryResult; - } - - // ************************** ACLs ******************************** // public OpenCGAResult> getAcls(List studyIdList, String member, boolean ignoreException, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - List studyList = resolveIds(studyIdList, userId); - ObjectMap auditParams = new ObjectMap() .append("studyIdList", studyIdList) .append("member", member) .append("ignoreException", ignoreException) .append("token", token); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - OpenCGAResult> studyAclList = OpenCGAResult.empty(); + return runBatch(auditParams, Enums.Action.FETCH_ACLS, STUDY, "", token, null, (s, userId, qo, operationUuid) -> { + List studyList = resolveIds(studyIdList, userId); - for (int i = 0; i < studyList.size(); i++) { - Study study = studyList.get(i); - long studyId = study.getUid(); - try { - OpenCGAResult> allStudyAcls; - if (StringUtils.isNotEmpty(member)) { - allStudyAcls = authorizationManager.getStudyAcl(userId, studyId, member); - } else { - allStudyAcls = authorizationManager.getAllStudyAcls(userId, studyId); + OpenCGAResult> studyAclList = OpenCGAResult.empty(); + for (int i = 0; i < studyList.size(); i++) { + Study study = studyList.get(i); + long studyId = study.getUid(); + OpenCGAResult> tmpStudyAcls; + try { + if (StringUtils.isNotEmpty(member)) { + tmpStudyAcls = authorizationManager.getStudyAcl(userId, studyId, member); + } else { + tmpStudyAcls = authorizationManager.getAllStudyAcls(userId, studyId); + } + studyAclList.append(tmpStudyAcls); + } catch (CatalogException e) { + if (ignoreException) { + Event event = new Event(Event.Type.ERROR, study.getFqn(), e.getMessage()); + studyAclList.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, Collections.singletonList(null), + 0)); + } else { + throw e; + } } - studyAclList.append(allStudyAcls); - auditManager.audit(operationUuid, userId, Enums.Action.FETCH_ACLS, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); - } catch (CatalogException e) { - auditManager.audit(operationUuid, userId, Enums.Action.FETCH_ACLS, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - if (ignoreException) { - Event event = new Event(Event.Type.ERROR, study.getFqn(), e.getMessage()); - studyAclList.append(new OpenCGAResult<>(0, Collections.singletonList(event), 0, Collections.singletonList(null), 0)); - } else { - throw e; - } + run(auditParams, Enums.Action.FETCH_ACLS, STUDY, operationUuid, study, userId, qo, (s2, u2, rp, qo2) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + return null; + }); } - } - return studyAclList; + return studyAclList; + }); } public OpenCGAResult> updateAcl(String studyId, String memberIds, StudyAclParams aclParams, @@ -1558,21 +1232,19 @@ public OpenCGAResult> updateAcl(Strin public OpenCGAResult> updateAcl(List studyIdList, String memberIds, StudyAclParams aclParams, ParamUtils.AclAction action, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - List studies = resolveIds(studyIdList, userId); - ObjectMap auditParams = new ObjectMap() .append("studyIdList", studyIdList) .append("memberIds", memberIds) .append("aclParams", aclParams) .append("action", action) .append("token", token); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - try { + + return runBatch(auditParams, Enums.Action.UPDATE_ACLS, STUDY, "", token, null, (s, userId, qo, operationUuid) -> { + List studies = resolveIds(studyIdList, userId); + if (studyIdList == null || studyIdList.isEmpty()) { throw new CatalogException("Missing study parameter"); } - if (action == null) { throw new CatalogException("Invalid action found. Please choose a valid action to be performed."); } @@ -1659,39 +1331,31 @@ public OpenCGAResult> updateAcl(List< members); for (Study study : studies) { - auditManager.audit(operationUuid, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS), new ObjectMap()); + run(auditParams, Enums.Action.UPDATE_ACLS, STUDY, operationUuid, study, userId, qo, (s1, u1, rp, qo2) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + return null; + }); } return remainingAcls; - } catch (CatalogException e) { - for (Study study : studies) { - auditManager.audit(operationUuid, userId, Enums.Action.UPDATE_ACLS, Enums.Resource.STUDY, study.getId(), - study.getUuid(), study.getId(), study.getUuid(), auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError()), new ObjectMap()); - } - - throw e; - } + }); } public Map facet(String studyStr, String fileFields, String sampleFields, String individualFields, String cohortFields, - String familyFields, String jobFields, boolean defaultStats, String sessionId) - throws CatalogException, IOException { + String familyFields, String jobFields, boolean defaultStats, String token) + throws CatalogException { Map result = new HashMap<>(); result.put("sample", catalogManager.getSampleManager().facet(studyStr, new Query(), setFacetFields(sampleFields), defaultStats, - sessionId)); - result.put("file", catalogManager.getFileManager().facet(studyStr, new Query(), setFacetFields(fileFields), defaultStats, - sessionId)); + token)); + result.put("file", catalogManager.getFileManager().facet(studyStr, new Query(), setFacetFields(fileFields), defaultStats, token)); result.put("individual", catalogManager.getIndividualManager().facet(studyStr, new Query(), setFacetFields(individualFields), - defaultStats, sessionId)); + defaultStats, token)); result.put("family", catalogManager.getFamilyManager().facet(studyStr, new Query(), setFacetFields(familyFields), defaultStats, - sessionId)); + token)); result.put("cohort", catalogManager.getCohortManager().facet(studyStr, new Query(), setFacetFields(cohortFields), defaultStats, - sessionId)); - result.put("job", catalogManager.getJobManager().facet(studyStr, new Query(), setFacetFields(jobFields), defaultStats, - sessionId)); + token)); + result.put("job", catalogManager.getJobManager().facet(studyStr, new Query(), setFacetFields(jobFields), defaultStats, token)); return result; } @@ -1705,38 +1369,50 @@ private QueryOptions setFacetFields(String fields) { // ************************** Protected internal methods ******************************** // public void setVariantEngineConfigurationOptions(String studyStr, ObjectMap options, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = get(studyStr, new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( - StudyDBAdaptor.QueryParams.UID.key(), - StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key())), token).first(); - - authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); - StudyVariantEngineConfiguration configuration = study.getInternal().getConfiguration().getVariantEngine(); - if (configuration == null) { - configuration = new StudyVariantEngineConfiguration(); - } - configuration.setOptions(options); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("options", options) + .append("token", token); + run(auditParams, Enums.Action.UPDATE_INTERNAL, STUDY, studyStr, token, options, + Collections.singletonList(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key()), + (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); + + StudyVariantEngineConfiguration configuration = study.getInternal().getConfiguration().getVariantEngine(); + if (configuration == null) { + configuration = new StudyVariantEngineConfiguration(); + } + configuration.setOptions(options); - ObjectMap parameters = new ObjectMap(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key(), configuration); - studyDBAdaptor.update(study.getUid(), parameters, QueryOptions.empty()); + ObjectMap parameters = new ObjectMap(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key(), + configuration); + return studyDBAdaptor.update(study.getUid(), parameters, QueryOptions.empty()); + }); } public void setVariantEngineConfigurationSampleIndex(String studyStr, SampleIndexConfiguration sampleIndexConfiguration, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = get(studyStr, new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( - StudyDBAdaptor.QueryParams.UID.key(), - StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key())), token).first(); - - authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); - StudyVariantEngineConfiguration configuration = study.getInternal().getConfiguration().getVariantEngine(); - if (configuration == null) { - configuration = new StudyVariantEngineConfiguration(); - } - configuration.setSampleIndex(sampleIndexConfiguration); - - ObjectMap parameters = new ObjectMap(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key(), configuration); - studyDBAdaptor.update(study.getUid(), parameters, QueryOptions.empty()); + ObjectMap auditParams = new ObjectMap() + .append("studyStr", studyStr) + .append("sampleIndexConfiguration", sampleIndexConfiguration) + .append("token", token); + run(auditParams, Enums.Action.UPDATE_INTERNAL, STUDY, studyStr, token, null, + Collections.singletonList(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key()), + (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); + StudyVariantEngineConfiguration configuration = study.getInternal().getConfiguration().getVariantEngine(); + if (configuration == null) { + configuration = new StudyVariantEngineConfiguration(); + } + configuration.setSampleIndex(sampleIndexConfiguration); + ObjectMap parameters = new ObjectMap(StudyDBAdaptor.QueryParams.INTERNAL_CONFIGURATION_VARIANT_ENGINE.key(), + configuration); + return studyDBAdaptor.update(study.getUid(), parameters, QueryOptions.empty()); + }); } // ************************** Private methods ******************************** // @@ -1830,78 +1506,69 @@ public String getProjectFqn(String studyFqn) throws CatalogException { */ public OpenCGAResult uploadTemplate(String studyStr, String filename, InputStream inputStream, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyStr, userId, QueryOptions.empty()); - - String templateId = "template." + TimeUtils.getTime() + "." + RandomStringUtils.random(6, true, false); - ObjectMap auditParams = new ObjectMap() .append("studyStr", studyStr) + .append("filename", filename) .append("token", token); - try { - StopWatch stopWatch = StopWatch.createStarted(); - - authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); - ParamUtils.checkParameter(filename, "File name"); - if (!filename.endsWith(".zip") && !filename.endsWith(".tar.gz")) { - throw new CatalogException("Expected zip or tar.gz file"); - } + StopWatch stopWatch = StopWatch.createStarted(); + try { + return run(auditParams, Enums.Action.UPLOAD_TEMPLATE, STUDY, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); + authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); + ParamUtils.checkParameter(filename, "File name"); + if (!filename.endsWith(".zip") && !filename.endsWith(".tar.gz")) { + throw new CatalogException("Expected zip or tar.gz file"); + } + String templateId = "template." + TimeUtils.getTime() + "." + RandomStringUtils.random(6, true, false); - // We obtain the basic studyPath where we will upload the file temporarily - java.nio.file.Path studyPath = Paths.get(study.getUri()); - Path path = studyPath.resolve("OPENCGA").resolve("TEMPLATE").resolve(templateId); + // We obtain the basic studyPath where we will upload the file temporarily + java.nio.file.Path studyPath = Paths.get(study.getUri()); + Path path = studyPath.resolve("OPENCGA").resolve("TEMPLATE").resolve(templateId); - IOManager ioManager; - try { - ioManager = ioManagerFactory.get(study.getUri()); - } catch (IOException e) { - throw CatalogIOException.ioManagerException(study.getUri(), e); - } - if (ioManager.exists(path.toUri())) { - throw new CatalogException("Template '" + templateId + "' already exists"); - } + IOManager ioManager; + try { + ioManager = ioManagerFactory.get(study.getUri()); + } catch (IOException e) { + throw CatalogIOException.ioManagerException(study.getUri(), e); + } + if (ioManager.exists(path.toUri())) { + throw new CatalogException("Template '" + templateId + "' already exists"); + } - Path filePath = path.resolve(filename); - URI fileUri = path.resolve(filename).toUri(); - try { - logger.debug("Creating folder '{}' to write the template file", path); - ioManager.createDirectory(path.toUri(), true); + Path filePath = path.resolve(filename); + URI fileUri = path.resolve(filename).toUri(); + try { + logger.debug("Creating folder '{}' to write the template file", path); + ioManager.createDirectory(path.toUri(), true); - // Start uploading the file to the directory - ioManager.copy(inputStream, fileUri); - } catch (Exception e) { - logger.error("Error uploading file '{}'. Trying to clean directory '{}'", filename, path, e); + // Start uploading the file to the directory + ioManager.copy(inputStream, fileUri); + } catch (Exception e) { + logger.error("Error uploading file '{}'. Trying to clean directory '{}'", filename, path, e); - // Clean temporal directory - ioManager.deleteDirectory(path.toUri()); + // Clean temporal directory + ioManager.deleteDirectory(path.toUri()); - throw new CatalogException("Error uploading file " + filename, e); - } + throw new CatalogException("Error uploading file " + filename, e); + } - // Decompress file - try { - ioManager.decompress(filePath, path); - } catch (CatalogIOException e) { - logger.error("Error decompressing file '{}'. Trying to clean directory '{}'", filePath, path, e); + // Decompress file + try { + ioManager.decompress(filePath, path); + } catch (CatalogIOException e) { + logger.error("Error decompressing file '{}'. Trying to clean directory '{}'", filePath, path, e); - // Clean temporal directory - ioManager.deleteDirectory(path.toUri()); + // Clean temporal directory + ioManager.deleteDirectory(path.toUri()); - throw new CatalogException("Error decompressing file: '" + filePath + "'", e); - } + throw new CatalogException("Error decompressing file: '" + filePath + "'", e); + } - return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), - 1, Collections.singletonList(templateId), 1); - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Action.UPLOAD_TEMPLATE, Enums.Resource.STUDY, templateId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } catch (Exception e) { - auditManager.auditCreate(userId, Enums.Action.UPLOAD_TEMPLATE, Enums.Resource.STUDY, templateId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(-1, "template upload", e.getMessage()))); - throw e; + return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), Collections.emptyList(), + 1, Collections.singletonList(templateId), 1); + }); } finally { try { inputStream.close(); @@ -1921,16 +1588,15 @@ public OpenCGAResult uploadTemplate(String studyStr, String filename, In * @throws CatalogException if there is any issue with the upload. */ public OpenCGAResult deleteTemplate(String studyStr, String templateId, String token) throws CatalogException { - String userId = catalogManager.getUserManager().getUserId(token); - Study study = resolveId(studyStr, userId, QueryOptions.empty()); - ObjectMap auditParams = new ObjectMap() .append("studyStr", studyStr) .append("templateId", templateId) .append("token", token); - try { - StopWatch stopWatch = StopWatch.createStarted(); + StopWatch stopWatch = StopWatch.createStarted(); + return run(auditParams, Enums.Action.DELETE_TEMPLATE, STUDY, studyStr, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); authorizationManager.checkIsOwnerOrAdmin(study.getUid(), userId); // We obtain the basic studyPath where we will upload the file temporarily @@ -1950,16 +1616,7 @@ public OpenCGAResult deleteTemplate(String studyStr, String templateId, ioManager.deleteDirectory(path.toUri()); return new OpenCGAResult<>((int) stopWatch.getTime(TimeUnit.MILLISECONDS), null, 1, Collections.singletonList(true), 1); - } catch (CatalogException e) { - auditManager.auditCreate(userId, Enums.Action.DELETE_TEMPLATE, Enums.Resource.STUDY, templateId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } catch (Exception e) { - auditManager.auditCreate(userId, Enums.Action.DELETE_TEMPLATE, Enums.Resource.STUDY, templateId, "", study.getId(), - study.getUuid(), auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, - new Error(-1, "template delete", e.getMessage()))); - throw e; - } + }); } } diff --git a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java index b67ebed9baf..93bdf007ac6 100644 --- a/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java +++ b/opencga-catalog/src/main/java/org/opencb/opencga/catalog/managers/UserManager.java @@ -21,7 +21,6 @@ import org.opencb.commons.datastore.core.ObjectMap; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; -import org.opencb.commons.datastore.core.result.Error; import org.opencb.commons.utils.ListUtils; import org.opencb.opencga.catalog.auth.authentication.AuthenticationManager; import org.opencb.opencga.catalog.auth.authentication.AzureADAuthenticationManager; @@ -35,13 +34,11 @@ import org.opencb.opencga.catalog.exceptions.*; import org.opencb.opencga.catalog.io.CatalogIOManager; import org.opencb.opencga.catalog.utils.ParamUtils; -import org.opencb.opencga.catalog.utils.UuidUtils; import org.opencb.opencga.core.api.ParamConstants; import org.opencb.opencga.core.common.PasswordUtils; import org.opencb.opencga.core.common.TimeUtils; import org.opencb.opencga.core.config.AuthenticationOrigin; import org.opencb.opencga.core.config.Configuration; -import org.opencb.opencga.core.models.audit.AuditRecord; import org.opencb.opencga.core.models.common.Enums; import org.opencb.opencga.core.models.project.Project; import org.opencb.opencga.core.models.study.Group; @@ -53,11 +50,12 @@ import org.slf4j.LoggerFactory; import javax.annotation.Nullable; -import java.io.IOException; import java.util.*; import java.util.stream.Collectors; import static org.opencb.opencga.catalog.utils.ParamUtils.checkEmail; +import static org.opencb.opencga.core.models.common.Enums.Resource.STUDY; +import static org.opencb.opencga.core.models.common.Enums.Resource.USER; /** * @author Jacobo Coll <jacobo167@gmail.com> @@ -105,7 +103,7 @@ public class UserManager extends AbstractManager { new CatalogAuthenticationManager(catalogDBAdaptorFactory, configuration.getEmail(), secretKey, expiration)); AuthenticationOrigin authenticationOrigin = new AuthenticationOrigin(); if (configuration.getAuthentication().getAuthenticationOrigins() == null) { - configuration.getAuthentication().setAuthenticationOrigins(Arrays.asList(authenticationOrigin)); + configuration.getAuthentication().setAuthenticationOrigins(Collections.singletonList(authenticationOrigin)); } else { // Check if OPENCGA authentication is already present in catalog configuration boolean catalogPresent = false; @@ -125,10 +123,17 @@ public class UserManager extends AbstractManager { } public void changePassword(String userId, String oldPassword, String newPassword) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(oldPassword, "oldPassword"); - ParamUtils.checkParameter(newPassword, "newPassword"); - try { + ObjectMap auditParams = new ObjectMap() + .append("userId", userId) + .append("oldPassword", "**********") + .append("newPassword", "**********"); + run(auditParams, Enums.Action.CHANGE_USER_PASSWORD, USER, "", "", null, (study, userId1, rp, queryOptions) -> { + rp.setId(userId); + + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(oldPassword, "oldPassword"); + ParamUtils.checkParameter(newPassword, "newPassword"); + if (oldPassword.equals(newPassword)) { throw new CatalogException("New password is the same as the old password."); } @@ -136,93 +141,84 @@ public void changePassword(String userId, String oldPassword, String newPassword userDBAdaptor.checkId(userId); String authOrigin = getAuthenticationOriginId(userId); authenticationManagerMap.get(authOrigin).changePassword(userId, oldPassword, newPassword); - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_PASSWORD, userId, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_PASSWORD, userId, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return null; + }); } public OpenCGAResult create(User user, String password, @Nullable String token) throws CatalogException { // Check if the users can be registered publicly or just the admin. - ObjectMap auditParams = new ObjectMap("user", user); - - // Initialise fields - ParamUtils.checkObj(user, "User"); - ParamUtils.checkValidUserId(user.getId()); - ParamUtils.checkParameter(user.getName(), "name"); - user.setEmail(ParamUtils.defaultString(user.getEmail(), "")); - if (StringUtils.isNotEmpty(user.getEmail())) { - checkEmail(user.getEmail()); - } - user.setOrganization(ParamUtils.defaultObject(user.getOrganization(), "")); - ParamUtils.checkObj(user.getAccount(), "account"); - user.getAccount().setType(ParamUtils.defaultObject(user.getAccount().getType(), Account.AccountType.GUEST)); - user.getAccount().setCreationDate(TimeUtils.getTime()); - user.getAccount().setExpirationDate(ParamUtils.defaultString(user.getAccount().getExpirationDate(), "")); - user.setInternal(new UserInternal(new UserStatus(UserStatus.READY))); - user.setQuota(ParamUtils.defaultObject(user.getQuota(), UserQuota::new)); - user.setProjects(ParamUtils.defaultObject(user.getProjects(), Collections::emptyList)); - user.setSharedProjects(ParamUtils.defaultObject(user.getSharedProjects(), Collections::emptyList)); - user.setConfigs(ParamUtils.defaultObject(user.getConfigs(), HashMap::new)); - user.setFilters(ParamUtils.defaultObject(user.getFilters(), LinkedList::new)); - user.setAttributes(ParamUtils.defaultObject(user.getAttributes(), Collections::emptyMap)); - - if (StringUtils.isEmpty(password)) { - // The authentication origin must be different than internal - Set authOrigins = configuration.getAuthentication().getAuthenticationOrigins() - .stream() - .map(AuthenticationOrigin::getId) - .collect(Collectors.toSet()); - if (!authOrigins.contains(user.getAccount().getAuthentication().getId())) { - throw new CatalogException("Unknown authentication origin id '" + user.getAccount().getAuthentication() + "'"); + ObjectMap auditParams = new ObjectMap() + .append("user", user) + .append("password", "********") + .append("token", token); + return run(auditParams, Enums.Action.CREATE, USER, null, token, null, (study, userId, rp, queryOptions) -> { + // Initialise fields + ParamUtils.checkObj(user, "User"); + ParamUtils.checkValidUserId(user.getId()); + ParamUtils.checkParameter(user.getName(), "name"); + user.setEmail(ParamUtils.defaultString(user.getEmail(), "")); + if (StringUtils.isNotEmpty(user.getEmail())) { + checkEmail(user.getEmail()); } - } else { - user.getAccount().setAuthentication(new Account.AuthenticationOrigin(INTERNAL_AUTHORIZATION, false)); - } - - String userId = user.getId(); - // We add a condition to check if the registration is private + user (or system) is not trying to create the ADMINISTRATOR user - if (!authorizationManager.isPublicRegistration() && !OPENCGA.equals(user.getId())) { - userId = authenticationManagerMap.get(INTERNAL_AUTHORIZATION).getUserId(token); - if (!OPENCGA.equals(userId)) { - String errorMsg = "The registration is closed to the public: Please talk to your administrator."; - auditManager.auditCreate(userId, Enums.Resource.USER, user.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", errorMsg))); - throw new CatalogException(errorMsg); + user.setOrganization(ParamUtils.defaultObject(user.getOrganization(), "")); + ParamUtils.checkObj(user.getAccount(), "account"); + user.getAccount().setType(ParamUtils.defaultObject(user.getAccount().getType(), Account.AccountType.GUEST)); + user.getAccount().setCreationDate(TimeUtils.getTime()); + user.getAccount().setExpirationDate(ParamUtils.defaultString(user.getAccount().getExpirationDate(), "")); + user.setInternal(new UserInternal(new UserStatus(UserStatus.READY))); + user.setQuota(ParamUtils.defaultObject(user.getQuota(), UserQuota::new)); + user.setProjects(ParamUtils.defaultObject(user.getProjects(), Collections::emptyList)); + user.setSharedProjects(ParamUtils.defaultObject(user.getSharedProjects(), Collections::emptyList)); + user.setConfigs(ParamUtils.defaultObject(user.getConfigs(), HashMap::new)); + user.setFilters(ParamUtils.defaultObject(user.getFilters(), LinkedList::new)); + user.setAttributes(ParamUtils.defaultObject(user.getAttributes(), Collections::emptyMap)); + + if (StringUtils.isEmpty(password)) { + // The authentication origin must be different than internal + Set authOrigins = configuration.getAuthentication().getAuthenticationOrigins() + .stream() + .map(AuthenticationOrigin::getId) + .collect(Collectors.toSet()); + if (!authOrigins.contains(user.getAccount().getAuthentication().getId())) { + throw new CatalogException("Unknown authentication origin id '" + user.getAccount().getAuthentication() + "'"); + } + } else { + user.getAccount().setAuthentication(new Account.AuthenticationOrigin(INTERNAL_AUTHORIZATION, false)); } - } - checkUserExists(user.getId()); - - try { - if (StringUtils.isNotEmpty(password) && !PasswordUtils.isStrongPassword(password)) { - throw new CatalogException("Invalid password. Check password strength for user " + user.getId()); - } - if (user.getProjects() != null && !user.getProjects().isEmpty()) { - throw new CatalogException("Creating user and projects in a single transaction is forbidden"); + userId = user.getId(); + // We add a condition to check if the registration is private + user (or system) is not trying to create the ADMINISTRATOR user + if (!authorizationManager.isPublicRegistration() && !OPENCGA.equals(user.getId())) { + userId = authenticationManagerMap.get(INTERNAL_AUTHORIZATION).getUserId(token); + if (!OPENCGA.equals(userId)) { + String errorMsg = "The registration is closed to the public: Please talk to your administrator."; + throw new CatalogException(errorMsg); + } } - catalogIOManager.createUser(user.getId()); - userDBAdaptor.insert(user, password, QueryOptions.empty()); + checkUserExists(user.getId()); - auditManager.auditCreate(userId, Enums.Resource.USER, user.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + try { + if (StringUtils.isNotEmpty(password) && !PasswordUtils.isStrongPassword(password)) { + throw new CatalogException("Invalid password. Check password strength for user " + user.getId()); + } + if (user.getProjects() != null && !user.getProjects().isEmpty()) { + throw new CatalogException("Creating user and projects in a single transaction is forbidden"); + } - return userDBAdaptor.get(user.getId(), QueryOptions.empty()); - } catch (CatalogIOException | CatalogDBException e) { - if (userDBAdaptor.exists(user.getId())) { - logger.error("ERROR! DELETING USER! " + user.getId()); - catalogIOManager.deleteUser(user.getId()); - } + catalogIOManager.createUser(user.getId()); + userDBAdaptor.insert(user, password, QueryOptions.empty()); - auditManager.auditCreate(userId, Enums.Resource.USER, user.getId(), "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); + return userDBAdaptor.get(user.getId(), QueryOptions.empty()); + } catch (CatalogIOException | CatalogDBException e) { + if (userDBAdaptor.exists(user.getId())) { + logger.error("ERROR! DELETING USER! " + user.getId()); + catalogIOManager.deleteUser(user.getId()); + } - throw e; - } + throw e; + } + }); } /** @@ -247,65 +243,78 @@ public OpenCGAResult create(String id, String name, String email, String p return create(user, password, token); } - public void syncAllUsersOfExternalGroup(String study, String authOrigin, String token) throws CatalogException { - if (!OPENCGA.equals(authenticationManagerMap.get(INTERNAL_AUTHORIZATION).getUserId(token))) { - throw new CatalogAuthorizationException("Only the root user can perform this action"); - } + public void syncAllUsersOfExternalGroup(String studyStr, String authOrigin, String token) throws CatalogException { + ObjectMap auditParams = new ObjectMap() + .append("study", studyStr) + .append("authOrigin", authOrigin) + .append("token", token); + run(auditParams, Enums.Action.SYNC_EXTERNAL_GROUP_OF_USERS, STUDY, studyStr, token, null, + Collections.singletonList(StudyDBAdaptor.QueryParams.GROUPS.key()), (study, userId, rp, queryOptions) -> { + rp.setId(study.getId()); + rp.setUuid(study.getUuid()); - OpenCGAResult allGroups = catalogManager.getStudyManager().getGroup(study, null, token); + if (!OPENCGA.equals(authenticationManagerMap.get(INTERNAL_AUTHORIZATION).getUserId(token))) { + throw new CatalogAuthorizationException("Only the root user can perform this action"); + } - boolean foundAny = false; - for (Group group : allGroups.getResults()) { - if (group.getSyncedFrom() != null && group.getSyncedFrom().getAuthOrigin().equals(authOrigin)) { - logger.info("Fetching users of group '{}' from authentication origin '{}'", group.getSyncedFrom().getRemoteGroup(), - group.getSyncedFrom().getAuthOrigin()); - foundAny = true; + List allGroups = study.getGroups(); + + boolean foundAny = false; + for (Group group : allGroups) { + if (group.getSyncedFrom() != null && group.getSyncedFrom().getAuthOrigin().equals(authOrigin)) { + logger.info("Fetching users of group '{}' from authentication origin '{}'", + group.getSyncedFrom().getRemoteGroup(), group.getSyncedFrom().getAuthOrigin()); + foundAny = true; + + List userList; + try { + userList = authenticationManagerMap.get(group.getSyncedFrom().getAuthOrigin()) + .getUsersFromRemoteGroup(group.getSyncedFrom().getRemoteGroup()); + } catch (CatalogException e) { + // There was some kind of issue for which we could not retrieve the group information. + logger.info("Removing all users from group '{}' belonging to group '{}' in the external authentication " + + "origin", group.getId(), group.getSyncedFrom().getAuthOrigin()); + logger.info("Please, manually remove group '{}' if external group '{}' was removed from the authentication" + + " origin", group.getId(), group.getSyncedFrom().getAuthOrigin()); + catalogManager.getStudyManager().updateGroup(studyStr, group.getId(), ParamUtils.BasicUpdateAction.SET, + new GroupUpdateParams(Collections.emptyList()), token); + continue; + } + Iterator iterator = userList.iterator(); + while (iterator.hasNext()) { + User user = iterator.next(); + try { + create(user, null, token); + logger.info("User '{}' ({}) successfully created", user.getId(), user.getName()); + } catch (CatalogParameterException e) { + logger.warn("Could not create user '{}' ({}). {}", user.getId(), user.getName(), e.getMessage()); + iterator.remove(); + } catch (CatalogException e) { + if (!e.getMessage().contains("already exists")) { + logger.warn("Could not create user '{}' ({}). {}", user.getId(), user.getName(), e.getMessage()); + iterator.remove(); + } + } + } - List userList; - try { - userList = authenticationManagerMap.get(group.getSyncedFrom().getAuthOrigin()) - .getUsersFromRemoteGroup(group.getSyncedFrom().getRemoteGroup()); - } catch (CatalogException e) { - // There was some kind of issue for which we could not retrieve the group information. - logger.info("Removing all users from group '{}' belonging to group '{}' in the external authentication origin", - group.getId(), group.getSyncedFrom().getAuthOrigin()); - logger.info("Please, manually remove group '{}' if external group '{}' was removed from the authentication origin", - group.getId(), group.getSyncedFrom().getAuthOrigin()); - catalogManager.getStudyManager().updateGroup(study, group.getId(), ParamUtils.BasicUpdateAction.SET, - new GroupUpdateParams(Collections.emptyList()), token); - continue; - } - Iterator iterator = userList.iterator(); - while (iterator.hasNext()) { - User user = iterator.next(); - try { - create(user, null, token); - logger.info("User '{}' ({}) successfully created", user.getId(), user.getName()); - } catch (CatalogParameterException e) { - logger.warn("Could not create user '{}' ({}). {}", user.getId(), user.getName(), e.getMessage()); - iterator.remove(); - } catch (CatalogException e) { - if (!e.getMessage().contains("already exists")) { - logger.warn("Could not create user '{}' ({}). {}", user.getId(), user.getName(), e.getMessage()); - iterator.remove(); + GroupUpdateParams updateParams; + if (ListUtils.isEmpty(userList)) { + logger.info("No members associated to the external group"); + updateParams = new GroupUpdateParams(Collections.emptyList()); + } else { + logger.info("Associating members to the internal OpenCGA group"); + updateParams = new GroupUpdateParams(new ArrayList<>(userList.stream().map(User::getId) + .collect(Collectors.toSet()))); + } + catalogManager.getStudyManager().updateGroup(studyStr, group.getId(), ParamUtils.BasicUpdateAction.SET, + updateParams, token); } } - } - - GroupUpdateParams updateParams; - if (ListUtils.isEmpty(userList)) { - logger.info("No members associated to the external group"); - updateParams = new GroupUpdateParams(Collections.emptyList()); - } else { - logger.info("Associating members to the internal OpenCGA group"); - updateParams = new GroupUpdateParams(new ArrayList<>(userList.stream().map(User::getId).collect(Collectors.toSet()))); - } - catalogManager.getStudyManager().updateGroup(study, group.getId(), ParamUtils.BasicUpdateAction.SET, updateParams, token); - } - } - if (!foundAny) { - logger.info("No synced groups found in study '{}' from authentication origin '{}'", study, authOrigin); - } + if (!foundAny) { + logger.info("No synced groups found in study '{}' from authentication origin '{}'", study, authOrigin); + } + return null; + }); } /** @@ -322,8 +331,6 @@ public void syncAllUsersOfExternalGroup(String study, String authOrigin, String */ public void importRemoteGroupOfUsers(String authOrigin, String remoteGroup, @Nullable String internalGroup, @Nullable String study, boolean sync, String token) throws CatalogException { - String userId = getUserId(token); - ObjectMap auditParams = new ObjectMap() .append("authOrigin", authOrigin) .append("remoteGroup", remoteGroup) @@ -331,7 +338,8 @@ public void importRemoteGroupOfUsers(String authOrigin, String remoteGroup, @Nul .append("study", study) .append("sync", sync) .append("token", token); - try { + + runBatch(auditParams, Enums.Action.IMPORT_EXTERNAL_GROUP_OF_USERS, USER, study, token, null, (s, userId, qo, operationUuid) -> { if (!OPENCGA.equals(authenticationManagerMap.get(INTERNAL_AUTHORIZATION).getUserId(token))) { throw new CatalogAuthorizationException("Only the root user can perform this action"); } @@ -354,8 +362,12 @@ public void importRemoteGroupOfUsers(String authOrigin, String remoteGroup, @Nul userList = authenticationManagerMap.get(authOrigin).getUsersFromRemoteGroup(remoteGroup); for (User user : userList) { try { - create(user, null, token); - logger.info("User '{}' successfully created", user.getId()); + run(auditParams, Enums.Action.IMPORT_EXTERNAL_GROUP_OF_USERS, USER, operationUuid, s, userId, null, (s2, u, rp, + qo2) -> { + create(user, null, token); + logger.info("User '{}' successfully created", user.getId()); + return null; + }); } catch (CatalogException e) { logger.warn("{}", e.getMessage()); } @@ -382,19 +394,14 @@ public void importRemoteGroupOfUsers(String authOrigin, String remoteGroup, @Nul .setSyncedFrom(groupSync); catalogManager.getStudyManager().createGroup(study, group, token); logger.info("Group '{}' created and synchronised with external group", internalGroup); - auditManager.audit(userId, Enums.Action.IMPORT_EXTERNAL_GROUP_OF_USERS, Enums.Resource.USER, group.getId(), - "", study, "", auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); } catch (CatalogException e) { logger.error("Could not register group '{}' in study '{}'\n{}", internalGroup, study, e.getMessage(), e); throw new CatalogException("Could not register group '" + internalGroup + "' in study '" + study + "': " + e.getMessage(), e); } } - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.IMPORT_EXTERNAL_GROUP_OF_USERS, Enums.Resource.USER, "", "", "", "", - auditParams, new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return null; + }); } /** @@ -404,23 +411,21 @@ public void importRemoteGroupOfUsers(String authOrigin, String remoteGroup, @Nul * @param idList List of entity ids existing in the authentication origin. * @param isApplication boolean indicating whether the id list belong to external applications or users. * @param internalGroup Group name in Catalog that will be associated to the remote group. - * @param study Study where the internal group will be associated. + * @param studyStr Study where the internal group will be associated. * @param token JWT token. The token should belong to the root user. * @throws CatalogException If any of the parameters is wrong or there is any internal error. */ public void importRemoteEntities(String authOrigin, List idList, boolean isApplication, @Nullable String internalGroup, - @Nullable String study, String token) throws CatalogException { + @Nullable String studyStr, String token) throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("authOrigin", authOrigin) .append("idList", idList) .append("isApplication", isApplication) .append("internalGroup", internalGroup) - .append("study", study) + .append("study", studyStr) .append("token", token); - String userId = getUserId(token); - - try { + runBatch(auditParams, Enums.Action.IMPORT_EXTERNAL_USERS, USER, studyStr, token, null, (study, userId, qo, operationUuid) -> { if (!OPENCGA.equals(userId)) { throw new CatalogAuthorizationException("Only the root user can perform this action"); } @@ -437,8 +442,6 @@ public void importRemoteEntities(String authOrigin, List idList, boolean List parsedUserList = authenticationManagerMap.get(authOrigin).getRemoteUserInformation(idList); for (User user : parsedUserList) { create(user, null, token); - auditManager.audit(userId, Enums.Action.IMPORT_EXTERNAL_USERS, Enums.Resource.USER, user.getId(), "", "", - "", auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); logger.info("User '{}' successfully created", user.getId()); } } else { @@ -448,21 +451,19 @@ public void importRemoteEntities(String authOrigin, List idList, boolean .setAuthentication(new Account.AuthenticationOrigin(authOrigin, true))) .setEmail("mail@mail.co.uk"); create(application, null, token); - auditManager.audit(userId, Enums.Action.IMPORT_EXTERNAL_USERS, Enums.Resource.USER, application.getId(), "", - "", "", auditParams, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); logger.info("User (application) '{}' successfully created", application.getId()); } } - if (StringUtils.isNotEmpty(internalGroup) && StringUtils.isNotEmpty(study)) { + if (StringUtils.isNotEmpty(internalGroup) && StringUtils.isNotEmpty(studyStr)) { // Check if the group already exists try { - OpenCGAResult group = catalogManager.getStudyManager().getGroup(study, internalGroup, token); + OpenCGAResult group = catalogManager.getStudyManager().getGroup(studyStr, internalGroup, token); if (group.getNumResults() == 1) { // We will add those users to the existing group - catalogManager.getStudyManager().updateGroup(study, internalGroup, ParamUtils.BasicUpdateAction.ADD, + catalogManager.getStudyManager().updateGroup(studyStr, internalGroup, ParamUtils.BasicUpdateAction.ADD, new GroupUpdateParams(idList), token); - return; + return null; } } catch (CatalogException e) { logger.warn("The group '{}' did not exist.", internalGroup); @@ -470,18 +471,16 @@ public void importRemoteEntities(String authOrigin, List idList, boolean // Create new group associating it to the remote group try { - logger.info("Attempting to register group '{}' in study '{}'", internalGroup, study); + logger.info("Attempting to register group '{}' in study '{}'", internalGroup, studyStr); Group group = new Group(internalGroup, idList); - catalogManager.getStudyManager().createGroup(study, group, token); + catalogManager.getStudyManager().createGroup(studyStr, group, token); } catch (CatalogException e) { - logger.error("Could not register group '{}' in study '{}'\n{}", internalGroup, study, e.getMessage()); + logger.error("Could not register group '{}' in study '{}'\n{}", internalGroup, studyStr, e.getMessage()); } } - } catch (CatalogException e) { - auditManager.audit(userId, Enums.Action.IMPORT_EXTERNAL_USERS, Enums.Resource.USER, "", "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + + return null; + }); } /** @@ -539,29 +538,24 @@ public OpenCGAResult get(String userId, QueryOptions options, String token * @return The requested users * @throws CatalogException CatalogException */ - public OpenCGAResult get(List userIdList, QueryOptions options, String token) - throws CatalogException { - ParamUtils.checkNotEmptyArray(userIdList, "userId"); - ParamUtils.checkParameter(token, "token"); - options = ParamUtils.defaultObject(options, QueryOptions::new); - + public OpenCGAResult get(List userIdList, QueryOptions options, String token) throws CatalogException { ObjectMap auditParams = new ObjectMap() .append("userIdList", userIdList) .append("options", options) .append("token", token); - String userId = getUserId(token); + return runBatch(auditParams, Enums.Action.INFO, USER, null, token, options, (s, userId, queryOptions, operationUuid) -> { + ParamUtils.checkNotEmptyArray(userIdList, "userId"); + ParamUtils.checkParameter(token, "token"); - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); - auditManager.initAuditBatch(operationUuid); - try { OpenCGAResult userDataResult; if (userIdList.size() == 1 && userId.equals(userIdList.get(0))) { - userDataResult = userDBAdaptor.get(userId, options); - auditManager.auditInfo(operationUuid, userId, Enums.Resource.USER, userId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return userDataResult; + return run(auditParams, Enums.Action.INFO, USER, operationUuid, null, userId, null, (study, userId1, rp, queryOptions1) -> { + rp.setId(userId); + return userDBAdaptor.get(userId, queryOptions); + }); } + // We will obtain the users this user is administrating QueryOptions adminOptions = new QueryOptions(QueryOptions.INCLUDE, Arrays.asList( UserDBAdaptor.QueryParams.PROJECTS.key() + "." + ProjectDBAdaptor.QueryParams.STUDIES.key() + "." @@ -610,7 +604,6 @@ public OpenCGAResult get(List userIdList, QueryOptions options, St } } } - if (!isAdmin) { throw new CatalogAuthorizationException("Only owners or administrators can see other user information"); } @@ -619,7 +612,7 @@ public OpenCGAResult get(List userIdList, QueryOptions options, St List auxUserList = userIdList.stream().filter(users::contains).collect(Collectors.toList()); Query query = new Query(UserDBAdaptor.QueryParams.ID.key(), auxUserList); - OpenCGAResult result = userDBAdaptor.get(query, options); + OpenCGAResult result = userDBAdaptor.get(query, queryOptions); Map userMap = new HashMap<>(); for (User user : result.getResults()) { userMap.put(user.getId(), user); @@ -629,18 +622,20 @@ public OpenCGAResult get(List userIdList, QueryOptions options, St List finalUserList = new ArrayList<>(userIdList.size()); List eventList = new ArrayList<>(userIdList.size()); for (String tmpUserId : userIdList) { - if (userMap.containsKey(tmpUserId)) { - finalUserList.add(userMap.get(tmpUserId)); - auditManager.auditInfo(operationUuid, userId, Enums.Resource.USER, tmpUserId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - } else { + try { + run(auditParams, Enums.Action.INFO, USER, operationUuid, null, userId, null, (study, userId1, rp, queryOptions1) -> { + rp.setId(tmpUserId); + if (userMap.containsKey(tmpUserId)) { + finalUserList.add(userMap.get(tmpUserId)); + } else { + throw new CatalogException("'" + userId + "' is not administrating a study of user '" + tmpUserId + + "' or user does not exist."); + } + return null; + }); + } catch (CatalogException e) { finalUserList.add(new User().setId(tmpUserId)); - - String msg = "'" + userId + "' is not administrating a study of user '" + tmpUserId + "' or user does not exist."; - eventList.add(new Event(Event.Type.ERROR, msg)); - - auditManager.auditInfo(operationUuid, userId, Enums.Resource.USER, tmpUserId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(-1, tmpUserId, msg))); + eventList.add(new Event(Event.Type.ERROR, e.getMessage())); } } @@ -648,32 +643,24 @@ public OpenCGAResult get(List userIdList, QueryOptions options, St result.setEvents(eventList); return result; - } catch (CatalogException e) { - for (String tmpUserId : userIdList) { - auditManager.auditInfo(operationUuid, userId, Enums.Resource.USER, tmpUserId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - } - throw e; - } finally { - auditManager.finishAuditBatch(operationUuid); - } + }); } public OpenCGAResult update(String userId, ObjectMap parameters, QueryOptions options, String token) throws CatalogException { - String loggedUser = getUserId(token); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("updateParams", parameters) .append("options", options) .append("token", token); - try { - options = ParamUtils.defaultObject(options, QueryOptions::new); + + return run(auditParams, Enums.Action.UPDATE, USER, null, token, options, (study, loggedUser, rp, queryOptions) -> { + rp.setId(userId); ParamUtils.checkParameter(userId, "userId"); ParamUtils.checkObj(parameters, "parameters"); ParamUtils.checkParameter(token, "token"); - userId = getCatalogUserId(userId, token); + String realUserId = getCatalogUserId(userId, token); + rp.setId(realUserId); for (String s : parameters.keySet()) { if (!s.matches("name|email|organization|attributes")) { throw new CatalogDBException("Parameter '" + s + "' can't be changed"); @@ -683,22 +670,16 @@ public OpenCGAResult update(String userId, ObjectMap parameters, QueryOpti if (parameters.containsKey("email")) { checkEmail(parameters.getString("email")); } - OpenCGAResult updateResult = userDBAdaptor.update(userId, parameters); - auditManager.auditUpdate(loggedUser, Enums.Resource.USER, userId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + OpenCGAResult updateResult = userDBAdaptor.update(realUserId, parameters); - if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { + if (queryOptions.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) { // Fetch updated user - OpenCGAResult result = userDBAdaptor.get(userId, options); + OpenCGAResult result = userDBAdaptor.get(realUserId, queryOptions); updateResult.setResults(result.getResults()); } return updateResult; - } catch (CatalogException e) { - auditManager.auditUpdate(loggedUser, Enums.Resource.USER, userId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -711,83 +692,47 @@ public OpenCGAResult update(String userId, ObjectMap parameters, QueryOpti * @throws CatalogException CatalogException. */ public OpenCGAResult delete(String userIdList, QueryOptions options, String token) throws CatalogException { - ParamUtils.checkParameter(userIdList, "userIdList"); - ParamUtils.checkParameter(token, "token"); - - String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT); ObjectMap auditParams = new ObjectMap() .append("userIdList", userIdList) .append("options", options) .append("token", token); + return runBatch(auditParams, Enums.Action.DELETE, USER, null, token, options, (s, tokenUser, qOptions, operationUuid) -> { + ParamUtils.checkParameter(userIdList, "userIdList"); + ParamUtils.checkParameter(token, "token"); - String tokenUser = getUserId(token); - - List userIds = Arrays.asList(userIdList.split(",")); - OpenCGAResult deletedUsers = OpenCGAResult.empty(); - for (String userId : userIds) { - // Only if the user asking the deletion is the ADMINISTRATOR or the user to be deleted itself... - if (OPENCGA.equals(tokenUser) || userId.equals(tokenUser)) { - try { - OpenCGAResult result = userDBAdaptor.delete(userId, options); - - auditManager.auditDelete(operationUuid, tokenUser, Enums.Resource.USER, userId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - - Query query = new Query() - .append(UserDBAdaptor.QueryParams.ID.key(), userId) - .append(UserDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key(), UserStatus.DELETED); - OpenCGAResult deletedUser = userDBAdaptor.get(query, QueryOptions.empty()); - deletedUser.setTime(deletedUser.getTime() + result.getTime()); - + String[] userIds = userIdList.split(","); + OpenCGAResult deletedUsers = OpenCGAResult.empty(); + for (String userId : userIds) { + // Only if the user asking the deletion is the ADMINISTRATOR or the user to be deleted itself... + if (OPENCGA.equals(tokenUser) || userId.equals(tokenUser)) { + OpenCGAResult deletedUser = run(auditParams, Enums.Action.DELETE, USER, operationUuid, null, userId, null, + (study, userId1, rp, qo) -> { + rp.setId(userId); + userDBAdaptor.delete(userId, options); + Query query = new Query() + .append(UserDBAdaptor.QueryParams.ID.key(), userId) + .append(UserDBAdaptor.QueryParams.INTERNAL_STATUS_ID.key(), UserStatus.DELETED); + return userDBAdaptor.get(query, QueryOptions.empty()); + }); deletedUsers.append(deletedUser); - } catch (CatalogException e) { - auditManager.auditDelete(operationUuid, tokenUser, Enums.Resource.USER, userId, "", "", "", auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); } } - } - return deletedUsers; - } - - /** - * 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 OpenCGAResult delete(Query query, QueryOptions options, String sessionId) throws CatalogException, IOException { - QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.ID.key()); - OpenCGAResult userDataResult = userDBAdaptor.get(query, queryOptions); - List userIds = userDataResult.getResults().stream().map(User::getId).collect(Collectors.toList()); - String userIdStr = StringUtils.join(userIds, ","); - return delete(userIdStr, options, sessionId); - } - - public OpenCGAResult restore(String ids, QueryOptions options, String sessionId) throws CatalogException { - throw new UnsupportedOperationException(); + return deletedUsers; + }); } public OpenCGAResult resetPassword(String userId, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "token"); - try { - String authenticatedUserId = getUserId(token); - authorizationManager.checkIsInstallationAdministrator(authenticatedUserId); + ObjectMap auditParams = new ObjectMap() + .append("userId", userId) + .append("token", token); + return run(auditParams, Enums.Action.RESET_USER_PASSWORD, USER, null, token, null, (s, loggedUser, rp, queryOptions) -> { + rp.setId(userId); + authorizationManager.checkIsInstallationAdministrator(loggedUser); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "token"); String authOrigin = getAuthenticationOriginId(userId); - OpenCGAResult writeResult = authenticationManagerMap.get(authOrigin).resetPassword(userId); - - auditManager.auditUser(userId, Enums.Action.RESET_USER_PASSWORD, userId, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return writeResult; - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.RESET_USER_PASSWORD, userId, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + return authenticationManagerMap.get(authOrigin).resetPassword(userId); + }); } public AuthenticationResponse loginAsAdmin(String password) throws CatalogException { @@ -795,72 +740,70 @@ public AuthenticationResponse loginAsAdmin(String password) throws CatalogExcept } public AuthenticationResponse login(String username, String password) throws CatalogException { - ParamUtils.checkParameter(username, "userId"); - ParamUtils.checkParameter(password, "password"); - - String authId = null; - AuthenticationResponse response = null; - - OpenCGAResult userOpenCGAResult = userDBAdaptor.get(username, INCLUDE_ACCOUNT); - if (userOpenCGAResult.getNumResults() == 1) { - authId = userOpenCGAResult.first().getAccount().getAuthentication().getId(); - if (!authenticationManagerMap.containsKey(authId)) { - throw new CatalogException("Could not authenticate user '" + username + "'. The authentication origin '" + authId - + "' could not be found."); - } - try { - response = authenticationManagerMap.get(authId).authenticate(username, password); - } catch (CatalogAuthenticationException e) { - auditManager.auditUser(username, Enums.Action.LOGIN, username, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } - } else { - // We attempt to login the user with the different authentication managers - for (Map.Entry entry : authenticationManagerMap.entrySet()) { - AuthenticationManager authenticationManager = entry.getValue(); - try { - response = authenticationManager.authenticate(username, password); - authId = entry.getKey(); - break; - } catch (CatalogAuthenticationException e) { - logger.debug("Attempted authentication failed with {} for user '{}'\n{}", entry.getKey(), username, e.getMessage(), e); + ObjectMap auditParams = new ObjectMap() + .append("username", username) + .append("password", "********"); + return run(auditParams, Enums.Action.LOGIN, USER, null, null, null, (s, u, rp, queryOptions) -> { + rp.setId(username); + ParamUtils.checkParameter(username, "userId"); + ParamUtils.checkParameter(password, "password"); + + String authId = null; + AuthenticationResponse response = null; + + OpenCGAResult userOpenCGAResult = userDBAdaptor.get(username, INCLUDE_ACCOUNT); + if (userOpenCGAResult.getNumResults() == 1) { + authId = userOpenCGAResult.first().getAccount().getAuthentication().getId(); + if (!authenticationManagerMap.containsKey(authId)) { + throw new CatalogException("Could not authenticate user '" + username + "'. The authentication origin '" + authId + + "' could not be found."); + } + return authenticationManagerMap.get(authId).authenticate(username, password); + } else { + // We attempt to login the user with the different authentication managers + for (Map.Entry entry : authenticationManagerMap.entrySet()) { + AuthenticationManager authenticationManager = entry.getValue(); + try { + response = authenticationManager.authenticate(username, password); + authId = entry.getKey(); + break; + } catch (CatalogAuthenticationException e) { + logger.debug("Attempted authentication failed with {} for user '{}'\n{}", entry.getKey(), username, e.getMessage(), + e); + } } } - } - - if (response == null) { - auditManager.auditUser(username, Enums.Action.LOGIN, username, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, new Error(0, "", "Incorrect user or password."))); - throw CatalogAuthenticationException.incorrectUserOrPassword(); - } - auditManager.auditUser(username, Enums.Action.LOGIN, username, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - String userId = authenticationManagerMap.get(authId).getUserId(response.getToken()); - if (!INTERNAL_AUTHORIZATION.equals(authId)) { - // External authorization - try { - // If the user is not registered, an exception will be raised - userDBAdaptor.checkId(userId); - } catch (CatalogDBException e) { - // The user does not exist so we register it - User user = authenticationManagerMap.get(authId).getRemoteUserInformation(Collections.singletonList(userId)).get(0); - // Generate a root token to be able to create the user even if the installation is private - String rootToken = authenticationManagerMap.get(INTERNAL_AUTHORIZATION).createToken(OPENCGA); - create(user, null, rootToken); + if (response == null) { + throw CatalogAuthenticationException.incorrectUserOrPassword(); } - try { - List remoteGroups = authenticationManagerMap.get(authId).getRemoteGroups(response.getToken()); + String userId = authenticationManagerMap.get(authId).getUserId(response.getToken()); + if (!INTERNAL_AUTHORIZATION.equals(authId)) { + // External authorization + try { + // If the user is not registered, an exception will be raised + userDBAdaptor.checkId(userId); + } catch (CatalogDBException e) { + // The user does not exist so we register it + User user = authenticationManagerMap.get(authId).getRemoteUserInformation(Collections.singletonList(userId)).get(0); + // Generate a root token to be able to create the user even if the installation is private + String rootToken = authenticationManagerMap.get(INTERNAL_AUTHORIZATION).createToken(OPENCGA); + create(user, null, rootToken); + } - // Resync synced groups of user in OpenCGA - studyDBAdaptor.resyncUserWithSyncedGroups(userId, remoteGroups, authId); - } catch (CatalogException e) { - logger.error("Could not update synced groups for user '" + userId + "'\n" + e.getMessage(), e); + try { + List remoteGroups = authenticationManagerMap.get(authId).getRemoteGroups(response.getToken()); + + // Resync synced groups of user in OpenCGA + studyDBAdaptor.resyncUserWithSyncedGroups(userId, remoteGroups, authId); + } catch (CatalogException e) { + logger.error("Could not update synced groups for user '" + userId + "'\n" + e.getMessage(), e); + } } - } - return response; + return response; + }); } /** @@ -871,32 +814,30 @@ public AuthenticationResponse login(String username, String password) throws Cat * @throws CatalogException if the token does not correspond to the user or the token is expired. */ public AuthenticationResponse refreshToken(String token) throws CatalogException { - AuthenticationResponse response = null; - CatalogAuthenticationException exception = null; - String userId = ""; - // We attempt to renew the token with the different authentication managers - for (Map.Entry entry : authenticationManagerMap.entrySet()) { - AuthenticationManager authenticationManager = entry.getValue(); - try { - response = authenticationManager.refreshToken(token); - userId = authenticationManager.getUserId(token); - break; - } catch (CatalogAuthenticationException e) { - logger.debug("Could not refresh token with '{}' provider: {}", entry.getKey(), e.getMessage(), e); - if (INTERNAL_AUTHORIZATION.equals(entry.getKey())) { - exception = e; + ObjectMap auditParams = new ObjectMap() + .append("token", token); + return run(auditParams, Enums.Action.REFRESH_TOKEN, USER, null, token, null, (study, userId, rp, queryOptions) -> { + rp.setId(userId); + AuthenticationResponse response = null; + CatalogAuthenticationException exception = null; + // We attempt to renew the token with the different authentication managers + for (Map.Entry entry : authenticationManagerMap.entrySet()) { + AuthenticationManager authenticationManager = entry.getValue(); + try { + response = authenticationManager.refreshToken(token); + break; + } catch (CatalogAuthenticationException e) { + logger.debug("Could not refresh token with '{}' provider: {}", entry.getKey(), e.getMessage(), e); + if (INTERNAL_AUTHORIZATION.equals(entry.getKey())) { + exception = e; + } } } - } - - if (response == null && exception != null) { - auditManager.auditUser(userId, Enums.Action.REFRESH_TOKEN, userId, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, exception.getError())); - throw exception; - } - - auditManager.auditUser(userId, Enums.Action.REFRESH_TOKEN, userId, new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return response; + if (response == null && exception != null) { + throw exception; + } + return response; + }); } /** @@ -908,11 +849,17 @@ public AuthenticationResponse refreshToken(String token) throws CatalogException * @throws CatalogException if the password is not correct or the userId does not exist. */ public String getNonExpiringToken(String userId, String token) throws CatalogException { - if (OPENCGA.equals(getUserId(token))) { - return authenticationManagerMap.get(INTERNAL_AUTHORIZATION).createNonExpiringToken(userId); - } else { - throw new CatalogException("Only user '" + OPENCGA + "' is allowed to create non expiring tokens"); - } + ObjectMap auditParams = new ObjectMap() + .append("userId", userId) + .append("token", token); + return run(auditParams, Enums.Action.FETCH_NON_EXPIRING_TOKEN, USER, null, token, null, (study, userId1, rp, queryOptions) -> { + rp.setId(userId); + if (OPENCGA.equals(getUserId(token))) { + return authenticationManagerMap.get(INTERNAL_AUTHORIZATION).createNonExpiringToken(userId); + } else { + throw new CatalogException("Only user '" + OPENCGA + "' is allowed to create non expiring tokens"); + } + }); } public String getAdminNonExpiringToken(String token) throws CatalogException { @@ -936,16 +883,6 @@ public String getAdminNonExpiringToken(String token) throws CatalogException { */ public OpenCGAResult addFilter(String userId, String id, String description, Enums.Resource resource, Query query, QueryOptions queryOptions, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "sessionId"); - ParamUtils.checkParameter(id, "id"); - ParamUtils.checkObj(resource, "resource"); - ParamUtils.checkObj(query, "Query"); - ParamUtils.checkObj(queryOptions, "QueryOptions"); - if (description == null) { - description = ""; - } - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("id", id) @@ -954,27 +891,32 @@ public OpenCGAResult addFilter(String userId, String id, String desc .append("query", query) .append("queryOptions", queryOptions) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); + + return run(auditParams, Enums.Action.CHANGE_USER_CONFIG, USER, null, token, queryOptions, (study, u, rp, qOptions) -> { + rp.setId(userId); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "sessionId"); + ParamUtils.checkParameter(id, "id"); + ParamUtils.checkObj(resource, "resource"); + ParamUtils.checkObj(query, "Query"); + ParamUtils.checkObj(queryOptions, "QueryOptions"); + String finalDescription = ParamUtils.defaultString(description, ""); + + String finalUserId = getCatalogUserId(userId, token); + rp.setId(finalUserId); + userDBAdaptor.checkId(finalUserId); Query queryExists = new Query() - .append(UserDBAdaptor.QueryParams.ID.key(), userId) + .append(UserDBAdaptor.QueryParams.ID.key(), finalUserId) .append(UserDBAdaptor.QueryParams.FILTERS_ID.key(), id); if (userDBAdaptor.count(queryExists).getNumMatches() > 0) { - throw new CatalogException("There already exists a filter called " + id + " for user " + userId); + throw new CatalogException("There already exists a filter called " + id + " for user " + finalUserId); } - UserFilter filter = new UserFilter(id, description, resource, query, queryOptions); - OpenCGAResult result = userDBAdaptor.addFilter(userId, filter); - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + UserFilter filter = new UserFilter(id, finalDescription, resource, query, queryOptions); + OpenCGAResult result = userDBAdaptor.addFilter(finalUserId, filter); return new OpenCGAResult<>(result.getTime(), Collections.emptyList(), 1, Collections.singletonList(filter), 1); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -990,39 +932,35 @@ public OpenCGAResult addFilter(String userId, String id, String desc * the session id is not the same as the provided user id. */ public OpenCGAResult updateFilter(String userId, String name, ObjectMap params, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "token"); - ParamUtils.checkParameter(name, "name"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("name", name) .append("params", params) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); + return run(auditParams, Enums.Action.CHANGE_USER_CONFIG, USER, null, token, null, (study, u, rp, queryOptions) -> { + rp.setId(userId); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "token"); + ParamUtils.checkParameter(name, "name"); + + String finalUserId = getCatalogUserId(userId, token); + rp.setId(finalUserId); + userDBAdaptor.checkId(finalUserId); Query queryExists = new Query() - .append(UserDBAdaptor.QueryParams.ID.key(), userId) + .append(UserDBAdaptor.QueryParams.ID.key(), finalUserId) .append(UserDBAdaptor.QueryParams.FILTERS_ID.key(), name); if (userDBAdaptor.count(queryExists).getNumMatches() == 0) { - throw new CatalogException("There is no filter called " + name + " for user " + userId); + throw new CatalogException("There is no filter called " + name + " for user " + finalUserId); } - OpenCGAResult result = userDBAdaptor.updateFilter(userId, name, params); - UserFilter filter = getFilter(userId, name); + OpenCGAResult result = userDBAdaptor.updateFilter(finalUserId, name, params); + UserFilter filter = getFilter(finalUserId, name); if (filter == null) { throw new CatalogException("Internal error: The filter " + name + " could not be found."); } - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); return new OpenCGAResult<>(result.getTime(), Collections.emptyList(), 1, Collections.singletonList(filter), 1); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -1037,32 +975,27 @@ public OpenCGAResult updateFilter(String userId, String name, Object * is not the same as the provided user id. */ public OpenCGAResult deleteFilter(String userId, String name, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "token"); - ParamUtils.checkParameter(name, "name"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("name", name) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); - UserFilter filter = getFilter(userId, name); + return run(auditParams, Enums.Action.CHANGE_USER_CONFIG, USER, null, token, null, (study, u, rp, qOptions) -> { + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "token"); + ParamUtils.checkParameter(name, "name"); + + String finalUserId = getCatalogUserId(userId, token); + userDBAdaptor.checkId(finalUserId); + + UserFilter filter = getFilter(finalUserId, name); if (filter == null) { - throw new CatalogException("There is no filter called " + name + " for user " + userId); + throw new CatalogException("There is no filter called " + name + " for user " + finalUserId); } - OpenCGAResult result = userDBAdaptor.deleteFilter(userId, name); - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + OpenCGAResult result = userDBAdaptor.deleteFilter(finalUserId, name); return new OpenCGAResult<>(result.getTime(), Collections.emptyList(), 1, Collections.singletonList(filter), 1); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -1076,32 +1009,28 @@ public OpenCGAResult deleteFilter(String userId, String name, String * @throws CatalogException if the user corresponding to the session id is not the same as the provided user id. */ public OpenCGAResult getFilter(String userId, String name, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "sessionId"); - ParamUtils.checkParameter(name, "name"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("name", name) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); - UserFilter filter = getFilter(userId, name); - auditManager.auditUser(userId, Enums.Action.FETCH_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + return run(auditParams, Enums.Action.FETCH_USER_CONFIG, USER, null, token, null, (study, u, rp, queryOptions) -> { + rp.setId(userId); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "sessionId"); + ParamUtils.checkParameter(name, "name"); + + String finalUserId = getCatalogUserId(userId, token); + rp.setId(finalUserId); + userDBAdaptor.checkId(finalUserId); + UserFilter filter = getFilter(finalUserId, name); if (filter == null) { throw new CatalogException("Filter " + name + " not found."); } else { return new OpenCGAResult<>(0, Collections.emptyList(), 1, Collections.singletonList(filter), 1); } - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.FETCH_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -1113,35 +1042,29 @@ public OpenCGAResult getFilter(String userId, String name, String to * @throws CatalogException if the user corresponding to the session id is not the same as the provided user id. */ public OpenCGAResult getAllFilters(String userId, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "sessionId"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); + + return run(auditParams, Enums.Action.FETCH_USER_CONFIG, USER, null, token, null, (study, userId1, rp, qOptions) -> { + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "sessionId"); + + String finalUserId = getCatalogUserId(userId, token); + userDBAdaptor.checkId(finalUserId); Query query = new Query() - .append(UserDBAdaptor.QueryParams.ID.key(), userId); + .append(UserDBAdaptor.QueryParams.ID.key(), finalUserId); QueryOptions queryOptions = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.FILTERS.key()); OpenCGAResult userDataResult = userDBAdaptor.get(query, queryOptions); if (userDataResult.getNumResults() != 1) { - throw new CatalogException("Internal error: User " + userId + " not found."); + throw new CatalogException("Internal error: User " + finalUserId + " not found."); } List filters = userDataResult.first().getFilters(); - auditManager.auditUser(userId, Enums.Action.FETCH_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult<>(0, Collections.emptyList(), filters.size(), filters, filters.size()); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.FETCH_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -1156,31 +1079,26 @@ public OpenCGAResult getAllFilters(String userId, String token) thro * @throws CatalogException if the user corresponding to the session id is not the same as the provided user id. */ public OpenCGAResult setConfig(String userId, String name, Map config, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "sessionId"); - ParamUtils.checkParameter(name, "name"); - ParamUtils.checkObj(config, "ObjectMap"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("name", name) .append("config", config) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); + return run(auditParams, Enums.Action.CHANGE_USER_CONFIG, USER, null, token, null, (study, userId1, rp, queryOptions) -> { + rp.setId(userId); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "sessionId"); + ParamUtils.checkParameter(name, "name"); + ParamUtils.checkObj(config, "ObjectMap"); - OpenCGAResult result = userDBAdaptor.setConfig(userId, name, config); - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + String finalUserId = getCatalogUserId(userId, token); + rp.setId(finalUserId); + userDBAdaptor.checkId(finalUserId); + OpenCGAResult result = userDBAdaptor.setConfig(finalUserId, name, config); return new OpenCGAResult(result.getTime(), Collections.emptyList(), 1, Collections.singletonList(config), 1); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -1195,22 +1113,25 @@ public OpenCGAResult setConfig(String userId, String name, Map c * not exist. */ public OpenCGAResult deleteConfig(String userId, String name, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "token"); - ParamUtils.checkParameter(name, "name"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("name", name) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); + + return run(auditParams, Enums.Action.CHANGE_USER_CONFIG, USER, null, token, null, (study, userId1, rp, queryOptions) -> { + rp.setId(userId); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "token"); + ParamUtils.checkParameter(name, "name"); + + String finalUserId = getCatalogUserId(userId, token); + rp.setId(finalUserId); + userDBAdaptor.checkId(finalUserId); QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.CONFIGS.key()); - OpenCGAResult userDataResult = userDBAdaptor.get(userId, options); + OpenCGAResult userDataResult = userDBAdaptor.get(finalUserId, options); if (userDataResult.getNumResults() == 0) { - throw new CatalogException("Internal error: Could not get user " + userId); + throw new CatalogException("Internal error: Could not get user " + finalUserId); } Map configs = userDataResult.first().getConfigs(); @@ -1222,15 +1143,9 @@ public OpenCGAResult deleteConfig(String userId, String name, String token) thro throw new CatalogException("Error: Cannot delete configuration with name " + name + ". Configuration name not found."); } - OpenCGAResult result = userDBAdaptor.deleteConfig(userId, name); - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); + OpenCGAResult result = userDBAdaptor.deleteConfig(finalUserId, name); return new OpenCGAResult(result.getTime(), Collections.emptyList(), 1, Collections.singletonList(configs.get(name)), 1); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.CHANGE_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } /** @@ -1245,21 +1160,24 @@ public OpenCGAResult deleteConfig(String userId, String name, String token) thro * does not exist. */ public OpenCGAResult getConfig(String userId, String name, String token) throws CatalogException { - ParamUtils.checkParameter(userId, "userId"); - ParamUtils.checkParameter(token, "sessionId"); - ObjectMap auditParams = new ObjectMap() .append("userId", userId) .append("name", name) .append("token", token); - try { - userId = getCatalogUserId(userId, token); - userDBAdaptor.checkId(userId); + + return run(auditParams, Enums.Action.FETCH_USER_CONFIG, USER, null, token, null, (study, u, rp, queryOptions) -> { + rp.setId(userId); + ParamUtils.checkParameter(userId, "userId"); + ParamUtils.checkParameter(token, "sessionId"); + + String finalUserId = getCatalogUserId(userId, token); + rp.setId(finalUserId); + userDBAdaptor.checkId(finalUserId); QueryOptions options = new QueryOptions(QueryOptions.INCLUDE, UserDBAdaptor.QueryParams.CONFIGS.key()); - OpenCGAResult userDataResult = userDBAdaptor.get(userId, options); + OpenCGAResult userDataResult = userDBAdaptor.get(finalUserId, options); if (userDataResult.getNumResults() == 0) { - throw new CatalogException("Internal error: Could not get user " + userId); + throw new CatalogException("Internal error: Could not get user " + finalUserId); } Map configs = userDataResult.first().getConfigs(); @@ -1271,16 +1189,9 @@ public OpenCGAResult getConfig(String userId, String name, String token) throws throw new CatalogException("Error: Cannot fetch configuration with name " + name + ". Configuration name not found."); } - auditManager.auditUser(userId, Enums.Action.FETCH_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS)); - return new OpenCGAResult(userDataResult.getTime(), userDataResult.getEvents(), 1, Collections.singletonList(configs.get(name)), 1); - } catch (CatalogException e) { - auditManager.auditUser(userId, Enums.Action.FETCH_USER_CONFIG, userId, auditParams, - new AuditRecord.Status(AuditRecord.Status.Result.ERROR, e.getError())); - throw e; - } + }); } private UserFilter getFilter(String userId, String name) throws CatalogException { @@ -1303,7 +1214,7 @@ private UserFilter getFilter(String userId, String name) throws CatalogException } private void checkUserExists(String userId) throws CatalogException { - if (userId.toLowerCase().equals(ANONYMOUS)) { + if (userId.equalsIgnoreCase(ANONYMOUS)) { throw new CatalogException("Permission denied: Cannot create users with special treatments in catalog."); } diff --git a/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java b/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java index bb99d52c927..5116e7c7cd9 100644 --- a/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java +++ b/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java @@ -1677,14 +1677,14 @@ public void getSamplesFromCohort() throws CatalogException, IOException { Cohort myCohort = catalogManager.getCohortManager().create(studyId, new Cohort().setId("MyCohort").setType(Enums.CohortType.FAMILY) .setSamples(Arrays.asList(sampleId1, sampleId2, sampleId3)), INCLUDE_RESULT, token).first(); - DataResult myCohort1 = catalogManager.getCohortManager().getSamples(studyId, "MyCohort", token); - assertEquals(3, myCohort1.getNumResults()); + List samples = myCohort.getSamples(); + assertEquals(3, samples.size()); thrown.expect(CatalogParameterException.class); - catalogManager.getCohortManager().getSamples(studyId, "MyCohort,AnotherCohort", token); + catalogManager.getCohortManager().get(studyId, "MyCohort,AnotherCohort", QueryOptions.empty(), token); thrown.expect(CatalogParameterException.class); - catalogManager.getCohortManager().getSamples(studyId, "MyCohort,MyCohort", token); + catalogManager.getCohortManager().get(studyId, "MyCohort,MyCohort", QueryOptions.empty(), token); } @Test diff --git a/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/SampleManagerTest.java b/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/SampleManagerTest.java index cc51c75d9bf..83c7281e321 100644 --- a/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/SampleManagerTest.java +++ b/opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/SampleManagerTest.java @@ -53,12 +53,13 @@ import org.opencb.opencga.core.models.common.InternalStatus; import org.opencb.opencga.core.models.common.StatusParams; import org.opencb.opencga.core.models.family.Family; -import org.opencb.opencga.core.models.individual.*; +import org.opencb.opencga.core.models.individual.Individual; +import org.opencb.opencga.core.models.individual.IndividualAclParams; +import org.opencb.opencga.core.models.individual.IndividualPermissions; +import org.opencb.opencga.core.models.individual.IndividualUpdateParams; import org.opencb.opencga.core.models.project.Project; import org.opencb.opencga.core.models.sample.*; import org.opencb.opencga.core.models.study.*; -import org.opencb.opencga.core.models.summaries.FeatureCount; -import org.opencb.opencga.core.models.summaries.VariableSetSummary; import org.opencb.opencga.core.models.user.Account; import org.opencb.opencga.core.response.OpenCGAResult; @@ -2050,47 +2051,6 @@ public void testDeleteAnnotationSet() throws CatalogException { assertEquals(0, sampleDataResult.first().getAnnotationSets().size()); } - @Test - public void getVariableSetSummary() throws CatalogException { - VariableSet variableSet = catalogManager.getStudyManager().getVariableSet(studyFqn, "vs", null, token).first(); - - DataResult variableSetSummary = catalogManager.getStudyManager() - .getVariableSetSummary(studyFqn, variableSet.getId(), token); - - assertEquals(1, variableSetSummary.getNumResults()); - VariableSetSummary summary = variableSetSummary.first(); - - assertEquals(5, summary.getSamples().size()); - - // PHEN - int i; - for (i = 0; i < summary.getSamples().size(); i++) { - if ("PHEN".equals(summary.getSamples().get(i).getName())) { - break; - } - } - List annotations = summary.getSamples().get(i).getAnnotations(); - assertEquals("PHEN", summary.getSamples().get(i).getName()); - assertEquals(2, annotations.size()); - - for (i = 0; i < annotations.size(); i++) { - if ("CONTROL".equals(annotations.get(i).getName())) { - break; - } - } - assertEquals("CONTROL", annotations.get(i).getName()); - assertEquals(5, annotations.get(i).getCount()); - - for (i = 0; i < annotations.size(); i++) { - if ("CASE".equals(annotations.get(i).getName())) { - break; - } - } - assertEquals("CASE", annotations.get(i).getName()); - assertEquals(3, annotations.get(i).getCount()); - - } - @Test public void testModifySample() throws CatalogException { String sampleId1 = catalogManager.getSampleManager() diff --git a/opencga-core/src/main/java/org/opencb/opencga/core/models/common/EntryParam.java b/opencga-core/src/main/java/org/opencb/opencga/core/models/common/EntryParam.java deleted file mode 100644 index 12cb35d8642..00000000000 --- a/opencga-core/src/main/java/org/opencb/opencga/core/models/common/EntryParam.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.opencb.opencga.core.models.common; - -public class EntryParam { - - private String id; - - public EntryParam() { - } - - public EntryParam(String id) { - this.id = id; - } - - public String getId() { - return id; - } - - public EntryParam setId(String id) { - this.id = id; - return this; - } -} diff --git a/opencga-core/src/main/java/org/opencb/opencga/core/models/common/Enums.java b/opencga-core/src/main/java/org/opencb/opencga/core/models/common/Enums.java index 701b3f2ab67..7c79a2df208 100644 --- a/opencga-core/src/main/java/org/opencb/opencga/core/models/common/Enums.java +++ b/opencga-core/src/main/java/org/opencb/opencga/core/models/common/Enums.java @@ -115,9 +115,12 @@ public enum Action { UPDATE_INTERNAL, MERGE, INFO, + ITERATE, SEARCH, COUNT, DISTINCT, + RANK, + GROUP_BY, DELETE, DOWNLOAD, VIEW_LOG, @@ -128,9 +131,11 @@ public enum Action { INDEX, CHANGE_PERMISSION, REVERT, + TOP, LOGIN, REFRESH_TOKEN, + FETCH_NON_EXPIRING_TOKEN, CHANGE_USER_PASSWORD, RESET_USER_PASSWORD, CHANGE_USER_CONFIG, @@ -162,6 +167,7 @@ public enum Action { RELATIVES, UPLOAD, + SYNC, LINK, UNLINK, GREP, diff --git a/opencga-core/src/main/java/org/opencb/opencga/core/models/common/ReferenceParam.java b/opencga-core/src/main/java/org/opencb/opencga/core/models/common/ReferenceParam.java new file mode 100644 index 00000000000..94a193226c8 --- /dev/null +++ b/opencga-core/src/main/java/org/opencb/opencga/core/models/common/ReferenceParam.java @@ -0,0 +1,43 @@ +package org.opencb.opencga.core.models.common; + +public class ReferenceParam { + + private String id; + private String uuid; + + public ReferenceParam() { + this("", ""); + } + + public ReferenceParam(String id, String uuid) { + this.id = id; + this.uuid = uuid; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("ReferenceParam{"); + sb.append("id='").append(id).append('\''); + sb.append(", uuid='").append(uuid).append('\''); + sb.append('}'); + return sb.toString(); + } + + public String getId() { + return id; + } + + public ReferenceParam setId(String id) { + this.id = id; + return this; + } + + public String getUuid() { + return uuid; + } + + public ReferenceParam setUuid(String uuid) { + this.uuid = uuid; + return this; + } +} diff --git a/opencga-core/src/main/java/org/opencb/opencga/core/models/summaries/VariableSetSummary.java b/opencga-core/src/main/java/org/opencb/opencga/core/models/summaries/VariableSetSummary.java deleted file mode 100644 index d2150ccb7a8..00000000000 --- a/opencga-core/src/main/java/org/opencb/opencga/core/models/summaries/VariableSetSummary.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2015-2020 OpenCB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.opencb.opencga.core.models.summaries; - -import java.util.Collections; -import java.util.List; - -/** - * Created by pfurio on 12/08/16. - */ -public class VariableSetSummary { - private long id; - private String name; - private List samples; - private List individuals; - private List cohorts; - private List families; - - public VariableSetSummary() { - this(0L, "", Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - } - - public VariableSetSummary(long id, String name) { - this(id, name, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - } - - public VariableSetSummary(long id, String name, List samples, List individuals, - List cohorts) { - this.id = id; - this.name = name; - this.samples = samples; - this.individuals = individuals; - this.cohorts = cohorts; - } - - public long getId() { - return id; - } - - public VariableSetSummary setId(long id) { - this.id = id; - return this; - } - - public String getName() { - return name; - } - - public VariableSetSummary setName(String name) { - this.name = name; - return this; - } - - public List getSamples() { - return samples; - } - - public VariableSetSummary setSamples(List samples) { - this.samples = samples; - return this; - } - - public List getIndividuals() { - return individuals; - } - - public VariableSetSummary setIndividuals(List individuals) { - this.individuals = individuals; - return this; - } - - public List getCohorts() { - return cohorts; - } - - public VariableSetSummary setCohorts(List cohorts) { - this.cohorts = cohorts; - return this; - } - - public List getFamilies() { - return families; - } - - public VariableSetSummary setFamilies(List families) { - this.families = families; - return this; - } -} diff --git a/opencga-storage/opencga-storage-mongodb/src/test/java/org/opencb/opencga/storage/mongodb/variant/MongoVariantStorageEngineTest.java b/opencga-storage/opencga-storage-mongodb/src/test/java/org/opencb/opencga/storage/mongodb/variant/MongoVariantStorageEngineTest.java index af8ee660581..e7364dd25c4 100644 --- a/opencga-storage/opencga-storage-mongodb/src/test/java/org/opencb/opencga/storage/mongodb/variant/MongoVariantStorageEngineTest.java +++ b/opencga-storage/opencga-storage-mongodb/src/test/java/org/opencb/opencga/storage/mongodb/variant/MongoVariantStorageEngineTest.java @@ -207,7 +207,7 @@ private long simulateStageError(StudyMetadata studyMetadata, VariantMongoDBAdapt // 3) Clean some variants from the Stage collection. MongoDBCollection stage = dbAdaptor.getStageCollection(studyMetadata.getId()); - long stageCount = stage.count().first(); + long stageCount = stage.count().getNumMatches(); System.out.println("stage count : " + stageCount); int i = 0; for (Document document : stage.find(new Document(), Projections.include("_id"), null).getResults()) { @@ -613,7 +613,7 @@ public long compareCollections(MongoDBCollection expectedCollection, MongoDBColl System.out.println("Comparing " + expectedCollection + " vs " + actualCollection); assertNotEquals(expectedCollection.toString(), actualCollection.toString()); assertEquals(expectedCollection.count().first(), actualCollection.count().first()); - assertNotEquals(0L, expectedCollection.count().first().longValue()); + assertNotEquals(0L, expectedCollection.count().getNumMatches()); Iterator actualIterator = actualCollection.nativeQuery().find(new Document(), options); Iterator expectedIterator = expectedCollection.nativeQuery().find(new Document(), options); diff --git a/pom.xml b/pom.xml index b2fe1f4c8c3..fb7dff39af0 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,6 @@ 2.4.5-SNAPSHOT 4.4.3-SNAPSHOT 2.4.10 - 0.2.0 2.11.4