Skip to content

Commit

Permalink
feature(chore):985 Added notification contracts.
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-mwesener committed Jun 25, 2024
1 parent 314c8a1 commit 6f253f7
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static PageResult<ContractResponse> from(PageResult<Contract> contractPag
public static ContractResponse from(Contract contract) {
return ContractResponse.builder()
.contractId(contract.getContractId())
.contractType(ContractTypeResponse.valueOf(contract.get))
.contractType(ContractTypeResponse.valueOf(contract.getType().name()))
.state(contract.getState())
.counterpartyAddress(contract.getCounterpartyAddress())
.endDate(contract.getEndDate())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.common.request.PageableFilterRequest;
import org.eclipse.tractusx.traceability.contracts.domain.model.Contract;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractType;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractType;

import java.time.Instant;
import java.time.OffsetDateTime;
Expand All @@ -40,6 +39,7 @@ public class Contract {
private OffsetDateTime endDate;
private String state;
private String policy;
private ContractType type;

public static ContractAgreementView toEntity(Contract contract, ContractType contractType) {
return ContractAgreementView.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.eclipse.tractusx.traceability.contracts.infrastructure.model;
package org.eclipse.tractusx.traceability.contracts.domain.model;

public enum ContractType {
ASSET_AS_PLANNED, ASSET_AS_BUILT, NOTIFICATION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.common.model.SearchCriteria;
import org.eclipse.tractusx.traceability.contracts.domain.model.Contract;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractType;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.tractusx.traceability.contracts.application.service.ContractService;
import org.eclipse.tractusx.traceability.contracts.domain.model.Contract;
import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepository;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractType;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;

import java.time.Instant;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.eclipse.tractusx.traceability.contracts.domain.model.Contract;
import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepository;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractType;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -74,9 +74,8 @@ public PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCrit
log.warn("Cannot find contract agreement Ids for asset ids in searchCriteria: " + searchCriteria.getSearchCriteriaFilterList());
return new PageResult<>(List.of(), 0, 0, 0, 0L);
}
// TODO we need the contractAgreementInfoViews into the featchEdcContractAgreements because we need to pass the type of it into the contract for the notifications
List<String> contractAgreementIds = contractAgreementInfoViews.getContent().stream().map(ContractAgreementView::getContractAgreementId).toList();
return new PageResult<>(fetchEdcContractAgreements(contractAgreementIds),

return new PageResult<>(fetchEdcContractAgreements(contractAgreementInfoViews.getContent()),
contractAgreementInfoViews.getPageable().getPageNumber(),
contractAgreementInfoViews.getTotalPages(),
contractAgreementInfoViews.getPageable().getPageSize(),
Expand All @@ -90,18 +89,30 @@ public PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCrit

@Override
public void saveAllContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException {
List<Contract> contracts = fetchEdcContractAgreements(contractAgreementIds);
List<ContractAgreementView> contractAgreementViews = Contract.toEntityList(contracts, contractType);
contractAgreementInfoViewRepository.saveAll(contractAgreementViews);

List<ContractAgreementView> contractAgreementViews = contractAgreementIds.stream()
.map(contractAgreementId -> ContractAgreementView.builder()
.contractAgreementId(contractAgreementId)
.type(contractType)
.build())
.collect(Collectors.toList());

List<Contract> contracts = fetchEdcContractAgreements(contractAgreementViews);
List<ContractAgreementView> contractAgreementViewsUpdated = Contract.toEntityList(contracts, contractType);
contractAgreementInfoViewRepository.saveAll(contractAgreementViewsUpdated);
}

private List<Contract> fetchEdcContractAgreements(List<String> contractAgreementIds) throws ContractAgreementException {
log.info("Trying to fetch contractAgreementIds from EDC: {}", contractAgreementIds);
private List<Contract> fetchEdcContractAgreements(List<ContractAgreementView> contractAgreementInfoViews) throws ContractAgreementException {
List<String> contractAgreementIds = contractAgreementInfoViews.stream().map(ContractAgreementView::getContractAgreementId).toList();
log.info("Trying to fetch contractAgreementIds from EDC: " + contractAgreementIds);

List<EdcContractAgreementsResponse> contractAgreements = edcContractAgreementService.getContractAgreements(contractAgreementIds);

validateContractAgreements(contractAgreementIds, contractAgreements);

Map<String, ContractType> contractTypes = contractAgreementInfoViews.stream()
.collect(Collectors.toMap(ContractAgreementView::getContractAgreementId, ContractAgreementView::getType));

Map<String, EdcContractAgreementNegotiationResponse> contractNegotiations = contractAgreements.stream()
.map(agreement -> new ImmutablePair<>(agreement.contractAgreementId(),
edcContractAgreementService.getContractAgreementNegotiation(agreement.contractAgreementId()))
Expand All @@ -117,6 +128,7 @@ private List<Contract> fetchEdcContractAgreements(List<String> contractAgreement
.creationDate(OffsetDateTime.ofInstant(Instant.ofEpochSecond(contractAgreement.contractSigningDate()), ZoneId.systemDefault()))
.state(contractNegotiations.get(contractAgreement.contractAgreementId()).state())
.policy(objectMapper.writeValueAsString(contractAgreement.policy()))
.type(contractTypes.get(contractAgreement.contractAgreementId()))
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@
import org.eclipse.tractusx.irs.edc.client.ContractNegotiationService;
import org.eclipse.tractusx.irs.edc.client.EDCCatalogFacade;
import org.eclipse.tractusx.irs.edc.client.EndpointDataReferenceStorage;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException;
import org.eclipse.tractusx.irs.edc.client.model.CatalogItem;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyCheckerService;
import org.eclipse.tractusx.traceability.common.properties.EdcProperties;
import org.eclipse.tractusx.traceability.contracts.application.service.ContractService;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractType;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;
import org.eclipse.tractusx.traceability.notification.domain.base.exception.BadRequestException;
import org.eclipse.tractusx.traceability.notification.domain.base.exception.ContractNegotiationException;
import org.eclipse.tractusx.traceability.notification.domain.base.exception.NoCatalogItemException;
Expand Down

0 comments on commit 6f253f7

Please sign in to comment.