@Test @RunWithCustomExecutor public void givenDuplicateIdentifiersThenKO() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(ADMIN_TENANT); final File fileOntology = PropertiesUtils.getResourceFile("ontology_ko_duplicate_identifier.json"); final List<OntologyModel> ontologyModelList = JsonHandler.getFromFileAsTypeReference(fileOntology, listOfOntologyType); final RequestResponse response = ontologyService.importOntologies(true, ontologyModelList); assertThat(response.isOk()).isFalse(); }
@Override public RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList) throws VitamException, IOException { ParametersChecker.checkParameter(ONTOLOGIES_IS_MANDATORY_PARAMETER, ontologyModelList); if (ontologyModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); Map<String, List<ErrorReportOntologies>> errors = new HashMap<>(); OntologyManager manager = new OntologyManager(logbookClient, eip, errors); manager.logStarted(ONTOLOGY_IMPORT_EVENT, null); try { checkAdminTenant(manager, errors); VitamError tenantValidationErrors = abortOnErrors(eip, errors, manager); if (tenantValidationErrors != null) { return tenantValidationErrors; } validateRequest(ontologyModelList, errors, manager); VitamError requestValidationErrors = abortOnErrors(eip, errors, manager); if (requestValidationErrors != null) { return requestValidationErrors; } List<OntologyModel> actualOntologies = this.selectOntologies(); Map<String, OntologyModel> currentOntologiesMap = actualOntologies.stream().collect(Collectors.toMap(OntologyModel::getIdentifier, item -> item)); List<OntologyModel> toDelete = new ArrayList<>(); List<OntologyModel> toCreate = new ArrayList<>(); List<OntologyModel> toUpdate = new ArrayList<>(); Set<String> importedOntologyIdentifiers = new HashSet<>(); for (final OntologyModel ontm : ontologyModelList) { if (currentOntologiesMap.containsKey(ontm.getIdentifier())) { toUpdate.add(ontm); } else { toCreate.add(ontm); } importedOntologyIdentifiers.add(ontm.getIdentifier()); } for (final OntologyModel actualTenantOntology : actualOntologies) { if (!importedOntologyIdentifiers.contains(actualTenantOntology.getIdentifier())) { toDelete.add(actualTenantOntology); } } if (!forceUpdate) { checkTypeChangeCompatibility(toUpdate, currentOntologiesMap, manager, errors); checkUsedByArchiveUnitProfileValidator(toDelete, manager, errors); checkInternalFieldDelete(toDelete, manager, errors); } VitamError coherenceErrors = abortOnErrors(eip, errors, manager); if (coherenceErrors != null) { return coherenceErrors; } commitToDisk(currentOntologiesMap, toDelete, toCreate, toUpdate); backupDatabaseToOffers(eip); InputStream errorStream = generateReportOK(toCreate, toDelete, toUpdate, eip); backupReport(errorStream, eip); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import ontologies schema error > " + e.getMessage(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); manager.logValidationError(CTR_SCHEMA, null, err); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, null); return getVitamError(VitamCode.ONTOLOGY_VALIDATION_ERROR.getItem(), err, StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (Exception e) { LOGGER.error(e); final String err = new StringBuilder("Import ontologies error : ").append(e.getMessage()).toString(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ONTOLOGY_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ONTOLOGY_IMPORT_EVENT, null, null); return new RequestResponseOK<OntologyModel>().addAllResults(ontologyModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); }
OntologyServiceImpl implements OntologyService { @Override public RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList) throws VitamException, IOException { ParametersChecker.checkParameter(ONTOLOGIES_IS_MANDATORY_PARAMETER, ontologyModelList); if (ontologyModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); Map<String, List<ErrorReportOntologies>> errors = new HashMap<>(); OntologyManager manager = new OntologyManager(logbookClient, eip, errors); manager.logStarted(ONTOLOGY_IMPORT_EVENT, null); try { checkAdminTenant(manager, errors); VitamError tenantValidationErrors = abortOnErrors(eip, errors, manager); if (tenantValidationErrors != null) { return tenantValidationErrors; } validateRequest(ontologyModelList, errors, manager); VitamError requestValidationErrors = abortOnErrors(eip, errors, manager); if (requestValidationErrors != null) { return requestValidationErrors; } List<OntologyModel> actualOntologies = this.selectOntologies(); Map<String, OntologyModel> currentOntologiesMap = actualOntologies.stream().collect(Collectors.toMap(OntologyModel::getIdentifier, item -> item)); List<OntologyModel> toDelete = new ArrayList<>(); List<OntologyModel> toCreate = new ArrayList<>(); List<OntologyModel> toUpdate = new ArrayList<>(); Set<String> importedOntologyIdentifiers = new HashSet<>(); for (final OntologyModel ontm : ontologyModelList) { if (currentOntologiesMap.containsKey(ontm.getIdentifier())) { toUpdate.add(ontm); } else { toCreate.add(ontm); } importedOntologyIdentifiers.add(ontm.getIdentifier()); } for (final OntologyModel actualTenantOntology : actualOntologies) { if (!importedOntologyIdentifiers.contains(actualTenantOntology.getIdentifier())) { toDelete.add(actualTenantOntology); } } if (!forceUpdate) { checkTypeChangeCompatibility(toUpdate, currentOntologiesMap, manager, errors); checkUsedByArchiveUnitProfileValidator(toDelete, manager, errors); checkInternalFieldDelete(toDelete, manager, errors); } VitamError coherenceErrors = abortOnErrors(eip, errors, manager); if (coherenceErrors != null) { return coherenceErrors; } commitToDisk(currentOntologiesMap, toDelete, toCreate, toUpdate); backupDatabaseToOffers(eip); InputStream errorStream = generateReportOK(toCreate, toDelete, toUpdate, eip); backupReport(errorStream, eip); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import ontologies schema error > " + e.getMessage(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); manager.logValidationError(CTR_SCHEMA, null, err); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, null); return getVitamError(VitamCode.ONTOLOGY_VALIDATION_ERROR.getItem(), err, StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (Exception e) { LOGGER.error(e); final String err = new StringBuilder("Import ontologies error : ").append(e.getMessage()).toString(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ONTOLOGY_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ONTOLOGY_IMPORT_EVENT, null, null); return new RequestResponseOK<OntologyModel>().addAllResults(ontologyModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } }
OntologyServiceImpl implements OntologyService { @Override public RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList) throws VitamException, IOException { ParametersChecker.checkParameter(ONTOLOGIES_IS_MANDATORY_PARAMETER, ontologyModelList); if (ontologyModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); Map<String, List<ErrorReportOntologies>> errors = new HashMap<>(); OntologyManager manager = new OntologyManager(logbookClient, eip, errors); manager.logStarted(ONTOLOGY_IMPORT_EVENT, null); try { checkAdminTenant(manager, errors); VitamError tenantValidationErrors = abortOnErrors(eip, errors, manager); if (tenantValidationErrors != null) { return tenantValidationErrors; } validateRequest(ontologyModelList, errors, manager); VitamError requestValidationErrors = abortOnErrors(eip, errors, manager); if (requestValidationErrors != null) { return requestValidationErrors; } List<OntologyModel> actualOntologies = this.selectOntologies(); Map<String, OntologyModel> currentOntologiesMap = actualOntologies.stream().collect(Collectors.toMap(OntologyModel::getIdentifier, item -> item)); List<OntologyModel> toDelete = new ArrayList<>(); List<OntologyModel> toCreate = new ArrayList<>(); List<OntologyModel> toUpdate = new ArrayList<>(); Set<String> importedOntologyIdentifiers = new HashSet<>(); for (final OntologyModel ontm : ontologyModelList) { if (currentOntologiesMap.containsKey(ontm.getIdentifier())) { toUpdate.add(ontm); } else { toCreate.add(ontm); } importedOntologyIdentifiers.add(ontm.getIdentifier()); } for (final OntologyModel actualTenantOntology : actualOntologies) { if (!importedOntologyIdentifiers.contains(actualTenantOntology.getIdentifier())) { toDelete.add(actualTenantOntology); } } if (!forceUpdate) { checkTypeChangeCompatibility(toUpdate, currentOntologiesMap, manager, errors); checkUsedByArchiveUnitProfileValidator(toDelete, manager, errors); checkInternalFieldDelete(toDelete, manager, errors); } VitamError coherenceErrors = abortOnErrors(eip, errors, manager); if (coherenceErrors != null) { return coherenceErrors; } commitToDisk(currentOntologiesMap, toDelete, toCreate, toUpdate); backupDatabaseToOffers(eip); InputStream errorStream = generateReportOK(toCreate, toDelete, toUpdate, eip); backupReport(errorStream, eip); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import ontologies schema error > " + e.getMessage(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); manager.logValidationError(CTR_SCHEMA, null, err); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, null); return getVitamError(VitamCode.ONTOLOGY_VALIDATION_ERROR.getItem(), err, StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (Exception e) { LOGGER.error(e); final String err = new StringBuilder("Import ontologies error : ").append(e.getMessage()).toString(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ONTOLOGY_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ONTOLOGY_IMPORT_EVENT, null, null); return new RequestResponseOK<OntologyModel>().addAllResults(ontologyModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } OntologyServiceImpl(MongoDbAccessAdminImpl mongoAccess, FunctionalBackupService functionalBackupService); }
OntologyServiceImpl implements OntologyService { @Override public RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList) throws VitamException, IOException { ParametersChecker.checkParameter(ONTOLOGIES_IS_MANDATORY_PARAMETER, ontologyModelList); if (ontologyModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); Map<String, List<ErrorReportOntologies>> errors = new HashMap<>(); OntologyManager manager = new OntologyManager(logbookClient, eip, errors); manager.logStarted(ONTOLOGY_IMPORT_EVENT, null); try { checkAdminTenant(manager, errors); VitamError tenantValidationErrors = abortOnErrors(eip, errors, manager); if (tenantValidationErrors != null) { return tenantValidationErrors; } validateRequest(ontologyModelList, errors, manager); VitamError requestValidationErrors = abortOnErrors(eip, errors, manager); if (requestValidationErrors != null) { return requestValidationErrors; } List<OntologyModel> actualOntologies = this.selectOntologies(); Map<String, OntologyModel> currentOntologiesMap = actualOntologies.stream().collect(Collectors.toMap(OntologyModel::getIdentifier, item -> item)); List<OntologyModel> toDelete = new ArrayList<>(); List<OntologyModel> toCreate = new ArrayList<>(); List<OntologyModel> toUpdate = new ArrayList<>(); Set<String> importedOntologyIdentifiers = new HashSet<>(); for (final OntologyModel ontm : ontologyModelList) { if (currentOntologiesMap.containsKey(ontm.getIdentifier())) { toUpdate.add(ontm); } else { toCreate.add(ontm); } importedOntologyIdentifiers.add(ontm.getIdentifier()); } for (final OntologyModel actualTenantOntology : actualOntologies) { if (!importedOntologyIdentifiers.contains(actualTenantOntology.getIdentifier())) { toDelete.add(actualTenantOntology); } } if (!forceUpdate) { checkTypeChangeCompatibility(toUpdate, currentOntologiesMap, manager, errors); checkUsedByArchiveUnitProfileValidator(toDelete, manager, errors); checkInternalFieldDelete(toDelete, manager, errors); } VitamError coherenceErrors = abortOnErrors(eip, errors, manager); if (coherenceErrors != null) { return coherenceErrors; } commitToDisk(currentOntologiesMap, toDelete, toCreate, toUpdate); backupDatabaseToOffers(eip); InputStream errorStream = generateReportOK(toCreate, toDelete, toUpdate, eip); backupReport(errorStream, eip); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import ontologies schema error > " + e.getMessage(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); manager.logValidationError(CTR_SCHEMA, null, err); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, null); return getVitamError(VitamCode.ONTOLOGY_VALIDATION_ERROR.getItem(), err, StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (Exception e) { LOGGER.error(e); final String err = new StringBuilder("Import ontologies error : ").append(e.getMessage()).toString(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ONTOLOGY_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ONTOLOGY_IMPORT_EVENT, null, null); return new RequestResponseOK<OntologyModel>().addAllResults(ontologyModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } OntologyServiceImpl(MongoDbAccessAdminImpl mongoAccess, FunctionalBackupService functionalBackupService); @Override RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList); @Override RequestResponseOK<OntologyModel> findOntologiesForCache(JsonNode queryDsl); @Override RequestResponseOK<OntologyModel> findOntologies(JsonNode queryDsl); @Override void close(); }
OntologyServiceImpl implements OntologyService { @Override public RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList) throws VitamException, IOException { ParametersChecker.checkParameter(ONTOLOGIES_IS_MANDATORY_PARAMETER, ontologyModelList); if (ontologyModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); Map<String, List<ErrorReportOntologies>> errors = new HashMap<>(); OntologyManager manager = new OntologyManager(logbookClient, eip, errors); manager.logStarted(ONTOLOGY_IMPORT_EVENT, null); try { checkAdminTenant(manager, errors); VitamError tenantValidationErrors = abortOnErrors(eip, errors, manager); if (tenantValidationErrors != null) { return tenantValidationErrors; } validateRequest(ontologyModelList, errors, manager); VitamError requestValidationErrors = abortOnErrors(eip, errors, manager); if (requestValidationErrors != null) { return requestValidationErrors; } List<OntologyModel> actualOntologies = this.selectOntologies(); Map<String, OntologyModel> currentOntologiesMap = actualOntologies.stream().collect(Collectors.toMap(OntologyModel::getIdentifier, item -> item)); List<OntologyModel> toDelete = new ArrayList<>(); List<OntologyModel> toCreate = new ArrayList<>(); List<OntologyModel> toUpdate = new ArrayList<>(); Set<String> importedOntologyIdentifiers = new HashSet<>(); for (final OntologyModel ontm : ontologyModelList) { if (currentOntologiesMap.containsKey(ontm.getIdentifier())) { toUpdate.add(ontm); } else { toCreate.add(ontm); } importedOntologyIdentifiers.add(ontm.getIdentifier()); } for (final OntologyModel actualTenantOntology : actualOntologies) { if (!importedOntologyIdentifiers.contains(actualTenantOntology.getIdentifier())) { toDelete.add(actualTenantOntology); } } if (!forceUpdate) { checkTypeChangeCompatibility(toUpdate, currentOntologiesMap, manager, errors); checkUsedByArchiveUnitProfileValidator(toDelete, manager, errors); checkInternalFieldDelete(toDelete, manager, errors); } VitamError coherenceErrors = abortOnErrors(eip, errors, manager); if (coherenceErrors != null) { return coherenceErrors; } commitToDisk(currentOntologiesMap, toDelete, toCreate, toUpdate); backupDatabaseToOffers(eip); InputStream errorStream = generateReportOK(toCreate, toDelete, toUpdate, eip); backupReport(errorStream, eip); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import ontologies schema error > " + e.getMessage(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); manager.logValidationError(CTR_SCHEMA, null, err); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, null); return getVitamError(VitamCode.ONTOLOGY_VALIDATION_ERROR.getItem(), err, StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (Exception e) { LOGGER.error(e); final String err = new StringBuilder("Import ontologies error : ").append(e.getMessage()).toString(); Map<String, List<ErrorReportOntologies>> exception = new HashMap<>(); ErrorReportOntologies errorReport = new ErrorReportOntologies(OntologyErrorCode.STP_IMPORT_ONTOLOGIES_EXCEPTION, "", err, null); exception.put(err, Collections.singletonList(errorReport)); InputStream errorStream = generateErrorReport(exception, StatusCode.KO, eip); backupReport(errorStream, eip); manager.logFatalError(ONTOLOGY_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ONTOLOGY_IMPORT_ERROR.getItem(), err, StatusCode.KO).setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ONTOLOGY_IMPORT_EVENT, null, null); return new RequestResponseOK<OntologyModel>().addAllResults(ontologyModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } OntologyServiceImpl(MongoDbAccessAdminImpl mongoAccess, FunctionalBackupService functionalBackupService); @Override RequestResponse<OntologyModel> importOntologies(boolean forceUpdate, List<OntologyModel> ontologyModelList); @Override RequestResponseOK<OntologyModel> findOntologiesForCache(JsonNode queryDsl); @Override RequestResponseOK<OntologyModel> findOntologies(JsonNode queryDsl); @Override void close();  }