Skip to content

Commit

Permalink
Merge pull request #1106 from eclipse-tractusx/feature/985-notificati…
Browse files Browse the repository at this point in the history
…on-contractagreements

feature(chore):985 Added notification contracts.
  • Loading branch information
ds-mwesener authored Jun 25, 2024
2 parents 6fdf3ef + 8e8fb89 commit 9d056f0
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _**For better traceability add the corresponding GitHub issue number in each cha
- #753 Refactored message history in notification detail view
- XXX updated local deployment documentation
- #1037 extended autocomplete api by contractAgreementId
- #985 Added function to save Contracts based on notification contractAgreementIds into the database

### Added
- #832 added policymanagement list view, creator and editor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.eclipse.tractusx.traceability.contracts.application.mapper;

import contract.response.ContractResponse;
import contract.response.ContractTypeResponse;
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.contracts.domain.model.Contract;

Expand All @@ -37,6 +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.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 @@ -18,10 +18,16 @@
********************************************************************************/
package org.eclipse.tractusx.traceability.contracts.application.service;

import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException;
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.domain.model.ContractType;

import java.util.List;

public interface ContractService {
PageResult<Contract> getContracts(PageableFilterRequest pageableFilterRequest);

void saveContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.List;

@Slf4j
@AllArgsConstructor
Expand All @@ -36,5 +39,18 @@ 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()
.id(contract.getContractId())
.contractAgreementId(contract.getContractId())
.type(contractType)
.created(Instant.now())
.build();
}

public static List<ContractAgreementView> toEntityList(List<Contract> contracts, ContractType contractType) {
return contracts.stream().map(contract -> Contract.toEntity(contract, contractType)).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.eclipse.tractusx.traceability.contracts.domain.model;

public enum ContractType {
ASSET_AS_PLANNED, ASSET_AS_BUILT, NOTIFICATION;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@
********************************************************************************/
package org.eclipse.tractusx.traceability.contracts.domain.repository;

import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException;
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.domain.model.ContractType;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface ContractRepository {

PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCriteria searchCriteria);

void saveAllContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.eclipse.tractusx.traceability.contracts.domain.service;

import lombok.RequiredArgsConstructor;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException;
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.common.model.SearchCriteria;
import org.eclipse.tractusx.traceability.common.request.OwnPageable;
Expand All @@ -27,9 +28,12 @@
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.domain.model.ContractType;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class ContractServiceImpl implements ContractService {
Expand All @@ -45,4 +49,9 @@ public PageResult<Contract> getContracts(PageableFilterRequest pageableFilterReq

return contractRepository.getContractsByPageable(pageable, searchCriteria);
}

@Override
public void saveContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException {
contractRepository.saveAllContractAgreements(contractAgreementIds, contractType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
package org.eclipse.tractusx.traceability.contracts.infrastructure.model;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import org.springframework.data.annotation.Immutable;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;

import java.time.Instant;

Expand All @@ -35,13 +37,12 @@
@Entity
@SuperBuilder
@Table(name = "contract_agreement_view")
@Immutable
public class ContractAgreementView {

@Id
private String id;
private String contractAgreementId;
private String assetType;
@Enumerated(EnumType.STRING)
private ContractType type;
private Instant created;

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +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.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,7 +75,7 @@ public PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCrit
return new PageResult<>(List.of(), 0, 0, 0, 0L);
}

return new PageResult<>(fetchEdcContractAgreements(contractAgreementInfoViews),
return new PageResult<>(fetchEdcContractAgreements(contractAgreementInfoViews.getContent()),
contractAgreementInfoViews.getPageable().getPageNumber(),
contractAgreementInfoViews.getTotalPages(),
contractAgreementInfoViews.getPageable().getPageSize(),
Expand All @@ -86,14 +87,32 @@ public PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCrit

}

private List<Contract> fetchEdcContractAgreements(Page<ContractAgreementView> contractAgreementInfoViews) throws ContractAgreementException {
List<String> contractAgreementIds = contractAgreementInfoViews.getContent().stream().map(ContractAgreementView::getContractAgreementId).toList();
@Override
public void saveAllContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException {

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<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 @@ -109,6 +128,7 @@ private List<Contract> fetchEdcContractAgreements(Page<ContractAgreementView> co
.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 @@ -36,6 +36,8 @@
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.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 Expand Up @@ -83,6 +85,8 @@ public class NotificationsEDCFacade {
private final ContractNegotiationService contractNegotiationService;
private final EndpointDataReferenceStorage endpointDataReferenceStorage;
private final PolicyCheckerService policyCheckerService;
private final ContractService contractService;


private static final String CX_TAXO_QUALITY_INVESTIGATION_RECEIVE = "https://w3id.org/catenax/taxonomy#ReceiveQualityInvestigationNotification";
private static final String CX_TAXO_QUALITY_INVESTIGATION_UPDATE = "https://w3id.org/catenax/taxonomy#UpdateQualityInvestigationNotification";
Expand All @@ -103,6 +107,11 @@ public void startEdcTransfer(
.orElseThrow(() -> new NoEndpointDataReferenceException("No EndpointDataReference was found"));

notificationMessage.setContractAgreementId(contractAgreementId);
try {
contractService.saveContractAgreements(List.of(contractAgreementId), ContractType.NOTIFICATION);
} catch (Exception e) {
log.warn("Could not save contractAgreementId for notification {}", e.getMessage());
}

try {
EdcNotificationRequest notificationRequest = toEdcNotificationRequest(notificationMessage, senderEdcUrl, dataReference, notification);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
create or replace view contract_agreement_view as
SELECT *
FROM ((SELECT assets_as_built.id, contract_agreement_id, 'assets_as_built' as asset_type, created
FROM ((SELECT assets_as_built.id, contract_agreement_id, 'ASSET_AS_BUILT' as type, created
FROM assets_as_built
where contract_agreement_id is not null)
UNION ALL
(SELECT assets_as_planned.id, contract_agreement_id, 'assets_as_planned' as asset_type, created
(SELECT assets_as_planned.id, contract_agreement_id, 'ASSET_AS_PLANNED' as type, created
FROM assets_as_planned
where contract_agreement_id is not null)) results
ORDER BY created DESC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ public class ContractResponse {
private String state;
@Schema(example = "{\\\"@id\\\":\\\"eb0c8486-914a-4d36-84c0-b4971cbc52e4\\\",\\\"@type\\\":\\\"odrl:Set\\\",\\\"odrl:permission\\\":{\\\"odrl:target\\\":\\\"registry-asset\\\",\\\"odrl:action\\\":{\\\"odrl:type\\\":\\\"USE\\\"},\\\"odrl:constraint\\\":{\\\"odrl:or\\\":{\\\"odrl:leftOperand\\\":\\\"PURPOSE\\\",\\\"odrl:operator\\\":{\\\"@id\\\":\\\"odrl:eq\\\"},\\\"odrl:rightOperand\\\":\\\"ID 3.0 Trace\\\"}}},\\\"odrl:prohibition\\\":[],\\\"odrl:obligation\\\":[],\\\"odrl:target\\\":\\\"registry-asset\\\"}", maxLength = 255)
private String policy;
private ContractTypeResponse contractType;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package contract.response;

public enum ContractTypeResponse {
ASSET_AS_PLANNED, ASSET_AS_BUILT, NOTIFICATION;
}

0 comments on commit 9d056f0

Please sign in to comment.