@Override
public QueryResult<Cohort> insert(long studyId, Cohort cohort, List<VariableSet> variableSetList, QueryOptions options)
throws CatalogDBException {
long startTime = startQuery();
dbAdaptorFactory.getCatalogStudyDBAdaptor().checkId(studyId);
checkCohortNameExists(studyId, cohort.getId());
long newId = dbAdaptorFactory.getCatalogMetaDBAdaptor().getNewAutoIncrementId();
cohort.setUid(newId);
cohort.setStudyUid(studyId);
if (StringUtils.isEmpty(cohort.getUuid())) {
cohort.setUuid(UUIDUtils.generateOpenCGAUUID(UUIDUtils.Entity.COHORT));
}
Document cohortObject = cohortConverter.convertToStorageType(cohort, variableSetList);
if (StringUtils.isNotEmpty(cohort.getCreationDate())) {
cohortObject.put(PRIVATE_CREATION_DATE, TimeUtils.toDate(cohort.getCreationDate()));
} else {
cohortObject.put(PRIVATE_CREATION_DATE, TimeUtils.getDate());
}
cohortObject.put(PERMISSION_RULES_APPLIED, Collections.emptyList());
try {
cohortCollection.insert(cohortObject, null);
} catch (MongoWriteException e) {
throw ifDuplicateKeyException(() -> CatalogDBException.alreadyExists("Cohort", studyId, "name", cohort.getId(), e), e);
}
Query query = new Query()
.append(QueryParams.UID.key(), newId)
.append(QueryParams.STUDY_UID.key(), studyId);
return endQuery("createCohort", startTime, get(query, options));
}