@Test @RunWithCustomExecutor public void givenTestImportExternalIdentifierMasterMode() throws Exception { VitamThreadUtils.getVitamSession().setTenantId(TENANT_ID); final File fileMetadataProfile = PropertiesUtils.getResourceFile("aup_without_identifier_master_mode.json"); final List<ArchiveUnitProfileModel> profileModelList = JsonHandler .getFromFileAsTypeReference(fileMetadataProfile, new TypeReference<List<ArchiveUnitProfileModel>>() { }); final RequestResponse response = archiveUnitProfileService.createArchiveUnitProfiles(profileModelList); final RequestResponseOK<ArchiveUnitProfileModel> responseCast = (RequestResponseOK<ArchiveUnitProfileModel>) response; assertThat(responseCast.getResults()).hasSize(1); final ArchiveUnitProfileModel acm = responseCast.getResults().iterator().next(); assertThat(acm).isNotNull(); assertThat(acm.getIdentifier()).startsWith("AUP-"); }
@Override public RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles( List<ArchiveUnitProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(ARCHIVE_UNIT_PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.ARCHIVE_UNIT_PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ArchiveUnitProfileManager manager = new ArchiveUnitProfileManager(logbookClient, metaDataClient, eip); manager.logStarted(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); ArrayNode archiveProfilesToPersist; final VitamError error = getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create archive unit profile error", StatusCode.KO).setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ArchiveUnitProfileModel aupm : profileModelList) { if (null != aupm.getId()) { error.addToErrors(getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(aupm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(aupm.getIdentifier())) { if (profileIdentifiers.contains(aupm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), "Duplicate archive unit profiles (ID)", StatusCode.KO) .setMessage( "Archive unit profile identifier " + aupm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(aupm.getIdentifier()); } } if (manager.validateArchiveUnitProfile(aupm, error)) { aupm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (aupm.getTenant() == null) { aupm.setTenant(ParameterHelper.getTenantParameter()); } final Optional<ArchiveUnitProfileValidator.RejectionCause> checkSchema = manager.createJsonSchemaValidator().validate(aupm); checkSchema.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setDescription(checkSchema.get().getReason()) .setMessage(ArchiveUnitProfileManager.INVALID_JSON_SCHEMA) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error.addToErrors(vitamError); } ); if (slaveMode) { final Optional<ArchiveUnitProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(aupm); result.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setMessage(ArchiveUnitProfileManager.EMPTY_REQUIRED_FIELD) .setDescription(result.get().getReason()) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error .addToErrors(vitamError); }); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } archiveProfilesToPersist = JsonHandler.createArrayNode(); final Map<String, OntologyModel> ontologyModelMap = getOntologyModelMap(); for (final ArchiveUnitProfileModel aupm : profileModelList) { setIdentifier(slaveMode, aupm); if (aupm.getControlSchema() != null) { List<String> schemaFields = SchemaValidationUtils.extractFieldsFromSchema(aupm.getControlSchema()); if (checkOntology) { schemaFields.forEach((k) -> { validateFieldInSchemaAgainstOntology(ontologyModelMap, k, error); }); } aupm.setFields(schemaFields); } final ObjectNode archiveProfileNode = (ObjectNode) JsonHandler.toJsonNode(aupm); JsonNode jsonNode = archiveProfileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { archiveProfileNode.set(UND_ID, jsonNode); } JsonNode hashTenant = archiveProfileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { archiveProfileNode.set(UND_TENANT, hashTenant); } archiveProfilesToPersist.add(archiveProfileNode); } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(archiveProfilesToPersist, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE) .close(); functionalBackupService.saveCollectionAndSequence( eip, ARCHIVE_UNIT_PROFILE_BACKUP_EVENT, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE, eip.toString()); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import archive unit profile error > " + e.getMessage(); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err, ArchiveUnitProfileManager.IMPORT_KO); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), e.getMessage(), StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { final String err = new StringBuilder("Import archive unit profiles error : ").append(e.getMessage()).toString(); manager.logFatalError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO) .setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, null); return new RequestResponseOK<ArchiveUnitProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); }
ArchiveUnitProfileServiceImpl implements ArchiveUnitProfileService { @Override public RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles( List<ArchiveUnitProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(ARCHIVE_UNIT_PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.ARCHIVE_UNIT_PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ArchiveUnitProfileManager manager = new ArchiveUnitProfileManager(logbookClient, metaDataClient, eip); manager.logStarted(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); ArrayNode archiveProfilesToPersist; final VitamError error = getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create archive unit profile error", StatusCode.KO).setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ArchiveUnitProfileModel aupm : profileModelList) { if (null != aupm.getId()) { error.addToErrors(getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(aupm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(aupm.getIdentifier())) { if (profileIdentifiers.contains(aupm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), "Duplicate archive unit profiles (ID)", StatusCode.KO) .setMessage( "Archive unit profile identifier " + aupm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(aupm.getIdentifier()); } } if (manager.validateArchiveUnitProfile(aupm, error)) { aupm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (aupm.getTenant() == null) { aupm.setTenant(ParameterHelper.getTenantParameter()); } final Optional<ArchiveUnitProfileValidator.RejectionCause> checkSchema = manager.createJsonSchemaValidator().validate(aupm); checkSchema.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setDescription(checkSchema.get().getReason()) .setMessage(ArchiveUnitProfileManager.INVALID_JSON_SCHEMA) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error.addToErrors(vitamError); } ); if (slaveMode) { final Optional<ArchiveUnitProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(aupm); result.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setMessage(ArchiveUnitProfileManager.EMPTY_REQUIRED_FIELD) .setDescription(result.get().getReason()) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error .addToErrors(vitamError); }); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } archiveProfilesToPersist = JsonHandler.createArrayNode(); final Map<String, OntologyModel> ontologyModelMap = getOntologyModelMap(); for (final ArchiveUnitProfileModel aupm : profileModelList) { setIdentifier(slaveMode, aupm); if (aupm.getControlSchema() != null) { List<String> schemaFields = SchemaValidationUtils.extractFieldsFromSchema(aupm.getControlSchema()); if (checkOntology) { schemaFields.forEach((k) -> { validateFieldInSchemaAgainstOntology(ontologyModelMap, k, error); }); } aupm.setFields(schemaFields); } final ObjectNode archiveProfileNode = (ObjectNode) JsonHandler.toJsonNode(aupm); JsonNode jsonNode = archiveProfileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { archiveProfileNode.set(UND_ID, jsonNode); } JsonNode hashTenant = archiveProfileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { archiveProfileNode.set(UND_TENANT, hashTenant); } archiveProfilesToPersist.add(archiveProfileNode); } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(archiveProfilesToPersist, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE) .close(); functionalBackupService.saveCollectionAndSequence( eip, ARCHIVE_UNIT_PROFILE_BACKUP_EVENT, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE, eip.toString()); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import archive unit profile error > " + e.getMessage(); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err, ArchiveUnitProfileManager.IMPORT_KO); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), e.getMessage(), StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { final String err = new StringBuilder("Import archive unit profiles error : ").append(e.getMessage()).toString(); manager.logFatalError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO) .setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, null); return new RequestResponseOK<ArchiveUnitProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } }
ArchiveUnitProfileServiceImpl implements ArchiveUnitProfileService { @Override public RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles( List<ArchiveUnitProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(ARCHIVE_UNIT_PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.ARCHIVE_UNIT_PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ArchiveUnitProfileManager manager = new ArchiveUnitProfileManager(logbookClient, metaDataClient, eip); manager.logStarted(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); ArrayNode archiveProfilesToPersist; final VitamError error = getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create archive unit profile error", StatusCode.KO).setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ArchiveUnitProfileModel aupm : profileModelList) { if (null != aupm.getId()) { error.addToErrors(getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(aupm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(aupm.getIdentifier())) { if (profileIdentifiers.contains(aupm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), "Duplicate archive unit profiles (ID)", StatusCode.KO) .setMessage( "Archive unit profile identifier " + aupm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(aupm.getIdentifier()); } } if (manager.validateArchiveUnitProfile(aupm, error)) { aupm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (aupm.getTenant() == null) { aupm.setTenant(ParameterHelper.getTenantParameter()); } final Optional<ArchiveUnitProfileValidator.RejectionCause> checkSchema = manager.createJsonSchemaValidator().validate(aupm); checkSchema.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setDescription(checkSchema.get().getReason()) .setMessage(ArchiveUnitProfileManager.INVALID_JSON_SCHEMA) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error.addToErrors(vitamError); } ); if (slaveMode) { final Optional<ArchiveUnitProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(aupm); result.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setMessage(ArchiveUnitProfileManager.EMPTY_REQUIRED_FIELD) .setDescription(result.get().getReason()) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error .addToErrors(vitamError); }); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } archiveProfilesToPersist = JsonHandler.createArrayNode(); final Map<String, OntologyModel> ontologyModelMap = getOntologyModelMap(); for (final ArchiveUnitProfileModel aupm : profileModelList) { setIdentifier(slaveMode, aupm); if (aupm.getControlSchema() != null) { List<String> schemaFields = SchemaValidationUtils.extractFieldsFromSchema(aupm.getControlSchema()); if (checkOntology) { schemaFields.forEach((k) -> { validateFieldInSchemaAgainstOntology(ontologyModelMap, k, error); }); } aupm.setFields(schemaFields); } final ObjectNode archiveProfileNode = (ObjectNode) JsonHandler.toJsonNode(aupm); JsonNode jsonNode = archiveProfileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { archiveProfileNode.set(UND_ID, jsonNode); } JsonNode hashTenant = archiveProfileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { archiveProfileNode.set(UND_TENANT, hashTenant); } archiveProfilesToPersist.add(archiveProfileNode); } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(archiveProfilesToPersist, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE) .close(); functionalBackupService.saveCollectionAndSequence( eip, ARCHIVE_UNIT_PROFILE_BACKUP_EVENT, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE, eip.toString()); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import archive unit profile error > " + e.getMessage(); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err, ArchiveUnitProfileManager.IMPORT_KO); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), e.getMessage(), StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { final String err = new StringBuilder("Import archive unit profiles error : ").append(e.getMessage()).toString(); manager.logFatalError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO) .setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, null); return new RequestResponseOK<ArchiveUnitProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ArchiveUnitProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService); @VisibleForTesting  ArchiveUnitProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService,
        boolean checkOntology); }
ArchiveUnitProfileServiceImpl implements ArchiveUnitProfileService { @Override public RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles( List<ArchiveUnitProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(ARCHIVE_UNIT_PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.ARCHIVE_UNIT_PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ArchiveUnitProfileManager manager = new ArchiveUnitProfileManager(logbookClient, metaDataClient, eip); manager.logStarted(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); ArrayNode archiveProfilesToPersist; final VitamError error = getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create archive unit profile error", StatusCode.KO).setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ArchiveUnitProfileModel aupm : profileModelList) { if (null != aupm.getId()) { error.addToErrors(getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(aupm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(aupm.getIdentifier())) { if (profileIdentifiers.contains(aupm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), "Duplicate archive unit profiles (ID)", StatusCode.KO) .setMessage( "Archive unit profile identifier " + aupm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(aupm.getIdentifier()); } } if (manager.validateArchiveUnitProfile(aupm, error)) { aupm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (aupm.getTenant() == null) { aupm.setTenant(ParameterHelper.getTenantParameter()); } final Optional<ArchiveUnitProfileValidator.RejectionCause> checkSchema = manager.createJsonSchemaValidator().validate(aupm); checkSchema.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setDescription(checkSchema.get().getReason()) .setMessage(ArchiveUnitProfileManager.INVALID_JSON_SCHEMA) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error.addToErrors(vitamError); } ); if (slaveMode) { final Optional<ArchiveUnitProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(aupm); result.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setMessage(ArchiveUnitProfileManager.EMPTY_REQUIRED_FIELD) .setDescription(result.get().getReason()) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error .addToErrors(vitamError); }); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } archiveProfilesToPersist = JsonHandler.createArrayNode(); final Map<String, OntologyModel> ontologyModelMap = getOntologyModelMap(); for (final ArchiveUnitProfileModel aupm : profileModelList) { setIdentifier(slaveMode, aupm); if (aupm.getControlSchema() != null) { List<String> schemaFields = SchemaValidationUtils.extractFieldsFromSchema(aupm.getControlSchema()); if (checkOntology) { schemaFields.forEach((k) -> { validateFieldInSchemaAgainstOntology(ontologyModelMap, k, error); }); } aupm.setFields(schemaFields); } final ObjectNode archiveProfileNode = (ObjectNode) JsonHandler.toJsonNode(aupm); JsonNode jsonNode = archiveProfileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { archiveProfileNode.set(UND_ID, jsonNode); } JsonNode hashTenant = archiveProfileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { archiveProfileNode.set(UND_TENANT, hashTenant); } archiveProfilesToPersist.add(archiveProfileNode); } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(archiveProfilesToPersist, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE) .close(); functionalBackupService.saveCollectionAndSequence( eip, ARCHIVE_UNIT_PROFILE_BACKUP_EVENT, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE, eip.toString()); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import archive unit profile error > " + e.getMessage(); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err, ArchiveUnitProfileManager.IMPORT_KO); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), e.getMessage(), StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { final String err = new StringBuilder("Import archive unit profiles error : ").append(e.getMessage()).toString(); manager.logFatalError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO) .setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, null); return new RequestResponseOK<ArchiveUnitProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ArchiveUnitProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService); @VisibleForTesting  ArchiveUnitProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService,
        boolean checkOntology); @Override RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles(
        List<ArchiveUnitProfileModel> profileModelList); @Override RequestResponseOK<ArchiveUnitProfileModel> findArchiveUnitProfiles(JsonNode queryDsl); @Override RequestResponse updateArchiveUnitProfile(String id, JsonNode queryDsl); @Override void close(); @VisibleForTesting ArchiveUnitProfileModel findByIdentifier(String id); }
ArchiveUnitProfileServiceImpl implements ArchiveUnitProfileService { @Override public RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles( List<ArchiveUnitProfileModel> profileModelList) throws VitamException { ParametersChecker.checkParameter(ARCHIVE_UNIT_PROFILE_IS_MANDATORY_PARAMETER, profileModelList); if (profileModelList.isEmpty()) { return new RequestResponseOK<>(); } String operationId = VitamThreadUtils.getVitamSession().getRequestId(); GUID eip = GUIDReader.getGUID(operationId); boolean slaveMode = vitamCounterService .isSlaveFunctionnalCollectionOnTenant(SequenceType.ARCHIVE_UNIT_PROFILE_SEQUENCE.getCollection(), ParameterHelper.getTenantParameter()); ArchiveUnitProfileManager manager = new ArchiveUnitProfileManager(logbookClient, metaDataClient, eip); manager.logStarted(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null); final Set<String> profileIdentifiers = new HashSet<>(); ArrayNode archiveProfilesToPersist; final VitamError error = getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), "Global create archive unit profile error", StatusCode.KO).setHttpCode( Response.Status.BAD_REQUEST.getStatusCode()); try { for (final ArchiveUnitProfileModel aupm : profileModelList) { if (null != aupm.getId()) { error.addToErrors(getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), RejectionCause.rejectIdNotAllowedInCreate(aupm.getName()).getReason(), StatusCode.KO)); continue; } if (ParametersChecker.isNotEmpty(aupm.getIdentifier())) { if (profileIdentifiers.contains(aupm.getIdentifier())) { error.addToErrors( getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), "Duplicate archive unit profiles (ID)", StatusCode.KO) .setMessage( "Archive unit profile identifier " + aupm.getIdentifier() + " already exists in the json")); continue; } else { profileIdentifiers.add(aupm.getIdentifier()); } } if (manager.validateArchiveUnitProfile(aupm, error)) { aupm.setId(GUIDFactory.newProfileGUID(ParameterHelper.getTenantParameter()).getId()); } if (aupm.getTenant() == null) { aupm.setTenant(ParameterHelper.getTenantParameter()); } final Optional<ArchiveUnitProfileValidator.RejectionCause> checkSchema = manager.createJsonSchemaValidator().validate(aupm); checkSchema.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setDescription(checkSchema.get().getReason()) .setMessage(ArchiveUnitProfileManager.INVALID_JSON_SCHEMA) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error.addToErrors(vitamError); } ); if (slaveMode) { final Optional<ArchiveUnitProfileValidator.RejectionCause> result = manager.checkEmptyIdentifierSlaveModeValidator().validate(aupm); result.ifPresent(t -> { VitamError vitamError = new VitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem()) .setMessage(ArchiveUnitProfileManager.EMPTY_REQUIRED_FIELD) .setDescription(result.get().getReason()) .setState(StatusCode.KO.name()); vitamError.setContext("FunctionalModule-ArchiveUnitProfile"); error .addToErrors(vitamError); }); } } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } archiveProfilesToPersist = JsonHandler.createArrayNode(); final Map<String, OntologyModel> ontologyModelMap = getOntologyModelMap(); for (final ArchiveUnitProfileModel aupm : profileModelList) { setIdentifier(slaveMode, aupm); if (aupm.getControlSchema() != null) { List<String> schemaFields = SchemaValidationUtils.extractFieldsFromSchema(aupm.getControlSchema()); if (checkOntology) { schemaFields.forEach((k) -> { validateFieldInSchemaAgainstOntology(ontologyModelMap, k, error); }); } aupm.setFields(schemaFields); } final ObjectNode archiveProfileNode = (ObjectNode) JsonHandler.toJsonNode(aupm); JsonNode jsonNode = archiveProfileNode.remove(VitamFieldsHelper.id()); if (jsonNode != null) { archiveProfileNode.set(UND_ID, jsonNode); } JsonNode hashTenant = archiveProfileNode.remove(VitamFieldsHelper.tenant()); if (hashTenant != null) { archiveProfileNode.set(UND_TENANT, hashTenant); } archiveProfilesToPersist.add(archiveProfileNode); } if (null != error.getErrors() && !error.getErrors().isEmpty()) { final String errorsDetails = error.getErrors().stream().map(c -> c.getDescription()) .collect(Collectors.joining(",")); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, errorsDetails, error.getErrors().get(0).getMessage()); return error; } mongoAccess.insertDocuments(archiveProfilesToPersist, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE) .close(); functionalBackupService.saveCollectionAndSequence( eip, ARCHIVE_UNIT_PROFILE_BACKUP_EVENT, FunctionalAdminCollections.ARCHIVE_UNIT_PROFILE, eip.toString()); } catch (SchemaValidationException e) { LOGGER.error(e); final String err = "Import archive unit profile error > " + e.getMessage(); manager.logValidationError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err, ArchiveUnitProfileManager.IMPORT_KO); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_VALIDATION_ERROR.getItem(), e.getMessage(), StatusCode.KO).setHttpCode(Response.Status.BAD_REQUEST.getStatusCode()); } catch (final Exception e) { final String err = new StringBuilder("Import archive unit profiles error : ").append(e.getMessage()).toString(); manager.logFatalError(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, err); return getVitamError(VitamCode.ARCHIVE_UNIT_PROFILE_FILE_IMPORT_ERROR.getItem(), err, StatusCode.KO) .setHttpCode( Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } manager.logSuccess(ARCHIVE_UNIT_PROFILE_IMPORT_EVENT, null, null); return new RequestResponseOK<ArchiveUnitProfileModel>().addAllResults(profileModelList) .setHttpCode(Response.Status.CREATED.getStatusCode()); } ArchiveUnitProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService); @VisibleForTesting  ArchiveUnitProfileServiceImpl(MongoDbAccessAdminImpl mongoAccess,
        VitamCounterService vitamCounterService, FunctionalBackupService functionalBackupService,
        boolean checkOntology); @Override RequestResponse<ArchiveUnitProfileModel> createArchiveUnitProfiles(
        List<ArchiveUnitProfileModel> profileModelList); @Override RequestResponseOK<ArchiveUnitProfileModel> findArchiveUnitProfiles(JsonNode queryDsl); @Override RequestResponse updateArchiveUnitProfile(String id, JsonNode queryDsl); @Override void close(); @VisibleForTesting ArchiveUnitProfileModel findByIdentifier(String id); static final String ARCHIVE_UNIT_PROFILE_IDENTIFIER_ALREADY_EXISTS_IN_DATABASE; static final String ARCHIVE_UNIT_PROFILE_IDENTIFIER_MUST_BE_STRING; }