Skip to content

Commit

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

feature(chore):985 removed logging message.
  • Loading branch information
ds-mwesener authored Jun 26, 2024
2 parents 05ff65d + de6d8a8 commit 3039796
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public BusinessPartnerResponse getBusinessPartner(final String bpn) {
try {
return bpdmRestTemplate.getForObject(uriBuilder.build(values), BusinessPartnerResponse.class);
} catch (HttpClientErrorException httpClientErrorException) {
log.warn("Could not request BPDM service.", httpClientErrorException);
log.debug("Could not request BPDM service. {}", httpClientErrorException.getMessage());
return BusinessPartnerResponse.builder().bpn(bpn).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,42 @@ public void insertIntoContractAgreements() {
executor.execute(() -> {
log.info("on ApplicationReadyEvent insert into contracts.");
try {
log.info("Method yourMethod() started.");

List<AssetBase> asBuilt = asBuiltService.findAll();
List<AssetBase> asPlanned = asPlannedService.findAll();

List<ContractAgreement> contractAgreementIdsAsBuilt = asBuilt.stream().map(asBuiltAsset -> ContractAgreement
.builder()
.type(ContractType.ASSET_AS_BUILT)
.contractAgreementId(asBuiltAsset.getContractAgreementId())
.id(asBuiltAsset.getId())
.created(Instant.now())
.build()).toList();

List<ContractAgreement> contractAgreementIdsAsPlanned = asPlanned.stream().map(asPlannedAsset -> ContractAgreement
.builder()
.type(ContractType.ASSET_AS_BUILT)
.contractAgreementId(asPlannedAsset.getContractAgreementId())
.id(asPlannedAsset.getId())
.created(Instant.now())
.build()).toList();
log.info("Retrieved assets: asBuilt={}, asPlanned={}", asBuilt, asPlanned);

List<ContractAgreement> contractAgreementIdsAsBuilt = asBuilt.stream()
.filter(asBuiltAsset -> asBuiltAsset.getContractAgreementId() != null) // Filtering out null contractAgreementIds
.map(asBuiltAsset -> ContractAgreement.builder()
.type(ContractType.ASSET_AS_BUILT)
.contractAgreementId(asBuiltAsset.getContractAgreementId())
.id(asBuiltAsset.getId())
.created(Instant.now())
.build())
.toList();

List<ContractAgreement> contractAgreementIdsAsPlanned = asPlanned.stream()
.filter(asPlannedAsset -> asPlannedAsset.getContractAgreementId() != null) // Filtering out null contractAgreementIds
.map(asPlannedAsset -> ContractAgreement.builder()
.type(ContractType.ASSET_AS_PLANNED) // Assuming the type should be ASSET_AS_PLANNED for asPlanned list
.contractAgreementId(asPlannedAsset.getContractAgreementId())
.id(asPlannedAsset.getId())
.created(Instant.now())
.build())
.toList();


log.info("Created ContractAgreements: asBuilt={}, asPlanned={}", contractAgreementIdsAsBuilt, contractAgreementIdsAsPlanned);

List<ContractAgreement> mergedAgreements = Stream.concat(contractAgreementIdsAsBuilt.stream(), contractAgreementIdsAsPlanned.stream())
.toList();
log.info("Merged agreements: {}", mergedAgreements);

contractService.saveAll(mergedAgreements);

log.info("Saved merged agreements successfully.");

} catch (Exception exception) {
log.error("Failed to insert contracts: ", exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import lombok.Builder;
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.ContractAgreementEntity;

import java.time.Instant;
import java.time.OffsetDateTime;
Expand All @@ -41,16 +41,16 @@ public class Contract {
private String policy;
private ContractType type;

public static ContractAgreementView toEntity(Contract contract, ContractType contractType) {
return ContractAgreementView.builder()
public static ContractAgreementEntity toEntity(Contract contract, ContractType contractType) {
return ContractAgreementEntity.builder()
.id(contract.getContractId())
.contractAgreementId(contract.getContractId())
.type(contractType)
.created(Instant.now())
.build();
}

public static List<ContractAgreementView> toEntityList(List<Contract> contracts, ContractType contractType) {
public static List<ContractAgreementEntity> 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
Expand Up @@ -24,7 +24,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity;

import java.time.Instant;
import java.util.List;
Expand All @@ -41,16 +41,16 @@ public class ContractAgreement {
private ContractType type;
private Instant created;

public static ContractAgreementView toEntity(ContractAgreement contractAgreement) {
return ContractAgreementView.builder()
public static ContractAgreementEntity toEntity(ContractAgreement contractAgreement) {
return ContractAgreementEntity.builder()
.created(contractAgreement.getCreated())
.id(contractAgreement.getId())
.contractAgreementId(contractAgreement.getContractAgreementId())
.type(contractAgreement.getType())
.build();
}

public static List<ContractAgreementView> toEntityList(List<ContractAgreement> contractAgreementList) {
public static List<ContractAgreementEntity> toEntityList(List<ContractAgreement> contractAgreementList) {
return contractAgreementList.stream().map(ContractAgreement::toEntity).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand All @@ -34,5 +34,5 @@ public interface ContractRepository {

void saveAllContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException;

void saveAll(List<ContractAgreementView> contractAgreements);
void saveAll(List<ContractAgreementEntity> contractAgreements);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement;
import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepository;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
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 @@ -39,7 +39,7 @@
@Entity
@SuperBuilder
@Table(name = "contract_agreement")
public class ContractAgreementView {
public class ContractAgreementEntity {

@Id
private String id;
Expand All @@ -49,7 +49,7 @@ public class ContractAgreementView {
private Instant created;


public static ContractAgreement toDomain(ContractAgreementView contractAgreement) {
public static ContractAgreement toDomain(ContractAgreementEntity contractAgreement) {
return ContractAgreement.builder()
.created(contractAgreement.getCreated())
.id(contractAgreement.getId())
Expand All @@ -58,7 +58,7 @@ public static ContractAgreement toDomain(ContractAgreementView contractAgreement
.build();
}

public static List<ContractAgreement> toDomainList(List<ContractAgreementView> contractAgreementList) {
return contractAgreementList.stream().map(ContractAgreementView::toDomain).toList();
public static List<ContractAgreement> toDomainList(List<ContractAgreementEntity> contractAgreementList) {
return contractAgreementList.stream().map(ContractAgreementEntity::toDomain).toList();
}
}
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.model.ContractType;
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.ContractAgreementEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand All @@ -45,7 +45,6 @@
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -58,7 +57,7 @@
public class ContractRepositoryImpl implements ContractRepository {

private final EdcContractAgreementService edcContractAgreementService;
private final JpaContractAgreementInfoViewRepository contractAgreementInfoViewRepository;
private final JpaContractAgreementRepository contractAgreementRepository;
private final ObjectMapper objectMapper;

@Override
Expand All @@ -67,19 +66,19 @@ public PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCrit
List<ContractSpecification> contractAgreementSpecifications = emptyIfNull(searchCriteria.getSearchCriteriaFilterList()).stream()
.map(ContractSpecification::new)
.toList();
Specification<ContractAgreementView> specification = BaseSpecification.toSpecification(contractAgreementSpecifications);
Page<ContractAgreementView> contractAgreementInfoViews = contractAgreementInfoViewRepository.findAll(specification, pageable);
Specification<ContractAgreementEntity> specification = BaseSpecification.toSpecification(contractAgreementSpecifications);
Page<ContractAgreementEntity> contractAgreementEntities = contractAgreementRepository.findAll(specification, pageable);

if (contractAgreementInfoViews.getContent().isEmpty()) {
if (contractAgreementEntities.getContent().isEmpty()) {
log.warn("Cannot find contract agreement Ids for asset ids in searchCriteria: " + searchCriteria.getSearchCriteriaFilterList());
return new PageResult<>(List.of(), 0, 0, 0, 0L);
}

return new PageResult<>(fetchEdcContractAgreements(contractAgreementInfoViews.getContent()),
contractAgreementInfoViews.getPageable().getPageNumber(),
contractAgreementInfoViews.getTotalPages(),
contractAgreementInfoViews.getPageable().getPageSize(),
contractAgreementInfoViews.getTotalElements());
return new PageResult<>(fetchEdcContractAgreements(contractAgreementEntities.getContent()),
contractAgreementEntities.getPageable().getPageNumber(),
contractAgreementEntities.getTotalPages(),
contractAgreementEntities.getPageable().getPageSize(),
contractAgreementEntities.getTotalElements());

} catch (ContractAgreementException e) {
throw new ContractException(e);
Expand All @@ -90,33 +89,33 @@ public PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCrit
@Override
public void saveAllContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException {

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

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

@Override
public void saveAll(List<ContractAgreementView> contractAgreements) {
contractAgreementInfoViewRepository.saveAll(contractAgreements);
public void saveAll(List<ContractAgreementEntity> contractAgreements) {
contractAgreementRepository.saveAll(contractAgreements);
}

private List<Contract> fetchEdcContractAgreements(List<ContractAgreementView> contractAgreementInfoViews) throws ContractAgreementException {
List<String> contractAgreementIds = contractAgreementInfoViews.stream().map(ContractAgreementView::getContractAgreementId).toList();
private List<Contract> fetchEdcContractAgreements(List<ContractAgreementEntity> contractAgreementEntities) throws ContractAgreementException {
List<String> contractAgreementIds = contractAgreementEntities.stream().map(ContractAgreementEntity::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, ContractType> contractTypes = contractAgreementEntities.stream()
.collect(Collectors.toMap(ContractAgreementEntity::getContractAgreementId, ContractAgreementEntity::getType));

Map<String, EdcContractAgreementNegotiationResponse> contractNegotiations = contractAgreements.stream()
.map(agreement -> new ImmutablePair<>(agreement.contractAgreementId(),
Expand Down Expand Up @@ -147,14 +146,18 @@ private void validateContractAgreements(List<String> contractAgreementIds, List<
Collections.sort(givenList);

List<String> expectedList = contractAgreements.stream()
.sorted(Comparator.comparing(EdcContractAgreementsResponse::contractAgreementId))
.map(EdcContractAgreementsResponse::contractAgreementId)
.sorted()
.toList();
log.info("EDC responded with the following contractAgreementIds: " + expectedList);

if (!givenList.equals(expectedList)) {
givenList.removeAll(expectedList);
throw new ContractException("Can not find the following contract agreement Ids in EDC: " + givenList);
// Filter the givenList to find out which IDs are missing in the expectedList
List<String> missingIds = givenList.stream()
.filter(id -> !expectedList.contains(id))
.toList();

if (!missingIds.isEmpty()) {
log.warn("Cannot find the following contract agreement IDs in EDC: " + missingIds);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
import jakarta.persistence.criteria.Root;
import org.eclipse.tractusx.traceability.common.model.SearchCriteriaFilter;
import org.eclipse.tractusx.traceability.common.repository.BaseSpecification;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.domain.Specification;

public class ContractSpecification extends BaseSpecification<ContractAgreementView> implements Specification<ContractAgreementView> {
public class ContractSpecification extends BaseSpecification<ContractAgreementEntity> implements Specification<ContractAgreementEntity> {

public ContractSpecification(SearchCriteriaFilter criteria) {
super(criteria);
}

@Override
public Predicate toPredicate(@NotNull Root<ContractAgreementView> root, @NotNull CriteriaQuery<?> query, @NotNull CriteriaBuilder builder) {
public Predicate toPredicate(@NotNull Root<ContractAgreementEntity> root, @NotNull CriteriaQuery<?> query, @NotNull CriteriaBuilder builder) {
return createPredicate(getSearchCriteriaFilter(), root, builder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
********************************************************************************/
package org.eclipse.tractusx.traceability.contracts.infrastructure.repository;

import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface JpaContractAgreementInfoViewRepository extends JpaRepository<ContractAgreementView, String>, JpaSpecificationExecutor<ContractAgreementView> {
public interface JpaContractAgreementRepository extends JpaRepository<ContractAgreementEntity, String>, JpaSpecificationExecutor<ContractAgreementEntity> {

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
-- Drop the view if it exists
DROP VIEW IF EXISTS contract_agreement_view;

-- Create the table
CREATE TABLE contract_agreement (
id varchar(255) PRIMARY KEY,
contract_agreement_id varchar(255),
type VARCHAR(255),
created TIMESTAMP
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS contract_agreement (
id VARCHAR(255) PRIMARY KEY,
contract_agreement_id VARCHAR(255),
type VARCHAR(255),
created TIMESTAMP
);
Loading

0 comments on commit 3039796

Please sign in to comment.