Skip to content

Commit

Permalink
[VAS] Story 11586: add missing steps
Browse files Browse the repository at this point in the history
  • Loading branch information
laedanrex committed Sep 13, 2023
1 parent dd16dff commit ae09951
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import fr.gouv.vitam.common.model.administration.ActivationStatus;
import fr.gouv.vitam.common.model.administration.SignaturePolicy;
import fr.gouv.vitamui.commons.api.domain.IdDto;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -98,5 +97,5 @@ public class IngestContractDto extends IdDto implements Serializable {

private boolean computeInheritedRulesAtIngest;

private SignaturePolicy signaturePolicy;
private SignaturePolicyDto signaturePolicy;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2015-2022)
*
* contact.vitam@culture.gouv.fr
*
* This software is a computer program whose purpose is to implement a digital archiving back-office system managing
* high volumetry securely and efficiently.
*
* This software is governed by the CeCILL 2.1 license under French law and abiding by the rules of distribution of free
* software. You can use, modify and/ or redistribute the software under the terms of the CeCILL 2.1 license as
* circulated by CEA, CNRS and INRIA at the following URL "https://cecill.info".
*
* As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
* users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the
* successive licensors have only limited liability.
*
* In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or
* developing or reproducing the software by the user in light of its specific status of free software, that may mean
* that it is complicated to manipulate, and that also therefore means that it is reserved for developers and
* experienced professionals having in-depth computer knowledge. Users are therefore encouraged to load and test the
* software's suitability as regards their requirements in conditions enabling the security of their systems and/or data
* to be ensured and, more generally, to use and operate it in the same conditions as regards security.
*
* The fact that you are presently reading this means that you have had knowledge of the CeCILL 2.1 license and that you
* accept its terms.
*
*/

package fr.gouv.vitamui.referential.common.dto;

import fr.gouv.vitam.common.model.administration.SignaturePolicy;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class SignaturePolicyDto {
private SignaturePolicy.SignedDocumentPolicyEnum signedDocument;
private Boolean needSignature;
private Boolean needTimestamp;
private Boolean needAdditionalProof;
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ public IngestContractService(final AdminExternalClient adminExternalClient) {
this.adminExternalClient = adminExternalClient;
}

public RequestResponse patchIngestContract(VitamContext vitamContext, String id, ObjectNode jsonNode) throws InvalidParseOperationException, AccessExternalClientException {
public RequestResponse patchIngestContract(VitamContext vitamContext, String id, ObjectNode jsonNode)
throws InvalidParseOperationException, AccessExternalClientException {
LOGGER.debug("patch: {}, {}", id, jsonNode);
LOGGER.info("Patch Ingest Contract EvIdAppSession : {} " , vitamContext.getApplicationSessionId());
LOGGER.info("Patch Ingest Contract EvIdAppSession : {} ", vitamContext.getApplicationSessionId());
return adminExternalClient.updateIngestContract(vitamContext, id, jsonNode);
}

public RequestResponse<IngestContractModel> findIngestContractById(final VitamContext vitamContext,
final String contractId) throws VitamClientException {
final String contractId) throws VitamClientException {

RequestResponse<IngestContractModel> jsonResponse = adminExternalClient.findIngestContractById(vitamContext,
contractId);
Expand All @@ -101,7 +102,7 @@ public RequestResponse<IngestContractModel> findIngestContractById(final VitamCo
}

public RequestResponse<IngestContractModel> findIngestContracts(final VitamContext vitamContext,
final JsonNode query) throws VitamClientException {
final JsonNode query) throws VitamClientException {

RequestResponse<IngestContractModel> jsonResponse = adminExternalClient.findIngestContracts(vitamContext,
query);
Expand All @@ -110,7 +111,8 @@ public RequestResponse<IngestContractModel> findIngestContracts(final VitamConte
}

//TODO: Add attribute to fr.gouv.vitamui.commons.api.domain.IngestContractVitamDto
private List<IngestContractModel> convertIngestContractsToModelOfCreation(final List<IngestContractModel> ingestContractModels) {
private List<IngestContractModel> convertIngestContractsToModelOfCreation(
final List<IngestContractModel> ingestContractModels) {
final List<IngestContractModel> listOfAC = new ArrayList<>();
for (final IngestContractModel acModel : ingestContractModels) {
final IngestContractModel ac = new IngestContractModel();
Expand All @@ -129,11 +131,11 @@ private List<IngestContractModel> convertIngestContractsToModelOfCreation(final
ac.setEveryDataObjectVersion(acModel.isEveryDataObjectVersion());
ac.setComputeInheritedRulesAtIngest(acModel.isComputeInheritedRulesAtIngest());

if(acModel.getActivationdate() != null ) {
if (acModel.getActivationdate() != null) {
ac.setActivationdate(LocalDateUtil.getFormattedDateForMongo(acModel.getActivationdate()));
}

if(acModel.getDeactivationdate() != null ) {
if (acModel.getDeactivationdate() != null) {
ac.setDeactivationdate(LocalDateUtil.getFormattedDateForMongo(acModel.getDeactivationdate()));
}

Expand All @@ -145,7 +147,8 @@ private List<IngestContractModel> convertIngestContractsToModelOfCreation(final
return listOfAC;
}

private ByteArrayInputStream serializeIngestContracts(final List<IngestContractModel> ingestContractModels) throws IOException {
private ByteArrayInputStream serializeIngestContracts(final List<IngestContractModel> ingestContractModels)
throws IOException {
final List<IngestContractModel> listOfAC = convertIngestContractsToModelOfCreation(ingestContractModels);
final ObjectMapper mapper = new ObjectMapper();
final JsonNode node = mapper.convertValue(listOfAC, JsonNode.class);
Expand All @@ -158,20 +161,27 @@ private ByteArrayInputStream serializeIngestContracts(final List<IngestContractM
}
}

public RequestResponse<?> createIngestContracts(final VitamContext vitamContext, final List<IngestContractModel> ingestContracts) throws InvalidParseOperationException, AccessExternalClientException, IOException {
public RequestResponse<?> createIngestContracts(final VitamContext vitamContext,
final List<IngestContractModel> ingestContracts)
throws InvalidParseOperationException, AccessExternalClientException, IOException {
try (ByteArrayInputStream byteArrayInputStream = serializeIngestContracts(ingestContracts)) {
LOGGER.info("Create Ingest Contract EvIdAppSession : {} " , vitamContext.getApplicationSessionId());
return adminExternalClient.createIngestContracts(vitamContext, byteArrayInputStream);
LOGGER.info("Create Ingest Contract EvIdAppSession : {} ", vitamContext.getApplicationSessionId());
RequestResponse<IngestContractModel> jsonResponse =
adminExternalClient.createIngestContracts(vitamContext, byteArrayInputStream);
VitamRestUtils.checkResponse(jsonResponse);
return jsonResponse;
}
}

public Integer checkAbilityToCreateIngestContractInVitam(List<IngestContractModel> ingestContracts, String applicationSessionId) {
public Integer checkAbilityToCreateIngestContractInVitam(List<IngestContractModel> ingestContracts,
String applicationSessionId) {
if (ingestContracts != null && !ingestContracts.isEmpty()) {
// check if tenant is ok in the request body
final Optional<IngestContractModel> ingestContract = ingestContracts.stream().findFirst();
final Integer tenantIdentifier = ingestContract.isPresent() ? ingestContract.get().getTenant() : null;
if (tenantIdentifier != null) {
final boolean sameTenant = ingestContracts.stream().allMatch(ac -> tenantIdentifier.equals(ac.getTenant()));
final boolean sameTenant =
ingestContracts.stream().allMatch(ac -> tenantIdentifier.equals(ac.getTenant()));
if (!sameTenant) {
final String msg = "All the ingest contracts must have the same tenant identifier";
LOGGER.error(msg);
Expand All @@ -185,22 +195,26 @@ public Integer checkAbilityToCreateIngestContractInVitam(List<IngestContractMode

try {
// check if tenant exist in Vitam
final VitamContext vitamContext = new VitamContext(tenantIdentifier).setApplicationSessionId(applicationSessionId);
final VitamContext vitamContext =
new VitamContext(tenantIdentifier).setApplicationSessionId(applicationSessionId);
final JsonNode select = new Select().getFinalSelect();
final RequestResponse<IngestContractModel> response = findIngestContracts(vitamContext, select);
if (response.getStatus() == HttpStatus.UNAUTHORIZED.value()) {
final String msg = "Can't create ingest contracts for the tenant : " + tenantIdentifier + " not found in Vitam";
final String msg =
"Can't create ingest contracts for the tenant : " + tenantIdentifier + " not found in Vitam";
LOGGER.error(msg);
throw new PreconditionFailedException(msg);
} else if (response.getStatus() != HttpStatus.OK.value()) {
final String msg = "Can't create ingest contracts for this tenant, Vitam response code : " + response.getStatus();
final String msg =
"Can't create ingest contracts for this tenant, Vitam response code : " + response.getStatus();
LOGGER.error(msg);
throw new UnavailableServiceException(msg);
}

verifyIngestContractExistence(ingestContracts, response);
} catch (final VitamClientException e) {
final String msg = "Can't create ingest contracts for this tenant, error while calling Vitam : " + e.getMessage();
final String msg =
"Can't create ingest contracts for this tenant, error while calling Vitam : " + e.getMessage();
LOGGER.error(msg);
throw new UnavailableServiceException(msg);
}
Expand All @@ -217,27 +231,33 @@ public Integer checkAbilityToCreateIngestContractInVitam(List<IngestContractMode
* @param ingestContracts : ingest contract to verify existence
* @param response : list of ingest contracts in vitam
*/
private void verifyIngestContractExistence(final List<IngestContractModel> ingestContracts, final RequestResponse<IngestContractModel> response) {
private void verifyIngestContractExistence(final List<IngestContractModel> ingestContracts,
final RequestResponse<IngestContractModel> response) {
try {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final IngestContractResponseDto ingestContractResponseDto = objectMapper.treeToValue(response.toJsonNode(), IngestContractResponseDto.class);
final IngestContractResponseDto ingestContractResponseDto =
objectMapper.treeToValue(response.toJsonNode(), IngestContractResponseDto.class);
final List<String> ingestContractsNames = ingestContracts.stream().map(AbstractContractModel::getName)
.filter(Objects::nonNull).map(String::strip)
.collect(Collectors.toList());
boolean alreadyCreated = ingestContractResponseDto.getResults().stream().anyMatch(ac -> ingestContractsNames.contains(ac.getName()));
boolean alreadyCreated = ingestContractResponseDto.getResults().stream()
.anyMatch(ac -> ingestContractsNames.contains(ac.getName()));
if (alreadyCreated) {
final String msg = "Can't create ingest contract, a contract with the same name already exist in Vitam";
LOGGER.error(msg);
throw new ConflictException(msg);
}

final List<String> ingestContractsIdentifiers = ingestContracts.stream().map(AbstractContractModel::getIdentifier)
.filter(Objects::nonNull).map(String::strip)
.collect(Collectors.toList());
alreadyCreated = ingestContractResponseDto.getResults().stream().anyMatch(ac -> ingestContractsIdentifiers.contains(ac.getIdentifier()));
final List<String> ingestContractsIdentifiers =
ingestContracts.stream().map(AbstractContractModel::getIdentifier)
.filter(Objects::nonNull).map(String::strip)
.collect(Collectors.toList());
alreadyCreated = ingestContractResponseDto.getResults().stream()
.anyMatch(ac -> ingestContractsIdentifiers.contains(ac.getIdentifier()));
if (alreadyCreated) {
final String msg = "Can't create ingest contract, a contract with the same identifier already exist in Vitam";
final String msg =
"Can't create ingest contract, a contract with the same identifier already exist in Vitam";
LOGGER.error(msg);
throw new ConflictException(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@

import fr.gouv.vitam.common.model.administration.IngestContractCheckState;
import fr.gouv.vitam.common.model.administration.IngestContractModel;
import fr.gouv.vitam.common.model.administration.SignaturePolicy;
import fr.gouv.vitamui.commons.api.logger.VitamUILogger;
import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory;
import fr.gouv.vitamui.commons.utils.VitamUIUtils;
import fr.gouv.vitamui.referential.common.dto.IngestContractDto;
import fr.gouv.vitamui.referential.common.dto.SignaturePolicyDto;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class IngestContractConverter {
Expand All @@ -65,7 +68,7 @@ public IngestContractModel convertDtoToVitam(final IngestContractDto dto) {
ingestContract.setActivationdate(dto.getActivationdate());
ingestContract.setDeactivationdate(dto.getDeactivationdate());
ingestContract.setComputeInheritedRulesAtIngest(dto.isComputeInheritedRulesAtIngest());
ingestContract.setSignaturePolicy(dto.getSignaturePolicy());
ingestContract.setSignaturePolicy(convertDtoToVitam(dto.getSignaturePolicy()));

return ingestContract;
}
Expand All @@ -86,11 +89,34 @@ public IngestContractDto convertVitamToDto(final IngestContractModel ingestContr
dto.setLastupdate(ingestContract.getLastupdate());
dto.setActivationdate(ingestContract.getActivationdate());
dto.setDeactivationdate(ingestContract.getDeactivationdate());
dto.setSignaturePolicy(ingestContract.getSignaturePolicy());
dto.setSignaturePolicy(convertVitamToDto(ingestContract.getSignaturePolicy()));

return dto;
}

public SignaturePolicyDto convertVitamToDto(final SignaturePolicy vitam) {
if (Objects.isNull(vitam)) {
return null;
}
return new SignaturePolicyDto()
.setSignedDocument(vitam.getSignedDocument())
.setNeedSignature(vitam.isNeedSignature())
.setNeedTimestamp(vitam.isNeedTimestamp())
.setNeedAdditionalProof(vitam.isNeedAdditionalProof());
}

public SignaturePolicy convertDtoToVitam(final SignaturePolicyDto dto) {
if (Objects.isNull(dto)) {
return null;
}
SignaturePolicy signaturePolicy = new SignaturePolicy();
signaturePolicy.setSignedDocument(dto.getSignedDocument());
signaturePolicy.setNeedSignature(dto.getNeedSignature());
signaturePolicy.setNeedTimestamp(dto.getNeedTimestamp());
signaturePolicy.setNeedAdditionalProof(dto.getNeedAdditionalProof());
return signaturePolicy;
}

public List<IngestContractModel> convertDtosToVitams(final List<IngestContractDto> dtos) {
return dtos.stream().map(this::convertDtoToVitam).collect(Collectors.toList());
}
Expand Down
Loading

0 comments on commit ae09951

Please sign in to comment.