diff --git a/.github/workflows/.trivyignore b/.github/workflows/.trivyignore index c06bb263ca..fa76656331 100644 --- a/.github/workflows/.trivyignore +++ b/.github/workflows/.trivyignore @@ -1,5 +1,5 @@ -# ref https://github.com/catenax-ng/product-traceability-foss-backend/security/code-scanning/1419 +# ref https://github.com/eclipse-tractusx/traceability-foss-backend/security/code-scanning/1419 CVE-2022-25857 -# ref https://github.com/catenax-ng/tx-traceability-foss/security/code-scanning/6879 +# ref https://github.com/eclipse-tractusx/traceability-foss/security/code-scanning/6879 # this is acceptable due to custom environment variable injection script which is executed upon FE container startup AVD-KSV-0014 diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c42129a23..e712a0fc22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,8 +17,11 @@ _**For better traceability add the corresponding GitHub issue number in each cha - #1173 Update IRS-Helm from 7.1.4 to 7.2.0 - updated Compatibility Matrix - #1082 fix duplicate key errors when synchronizing assets with IRS - #970 fixed bug where the right operand of policies was not showing up in table and detailed view +- #1191 Refactored contractAgreementId to be a list of elements. - #1188 prevent retry requests for notification actions to prevent duplicate error messages in history - #1025 fix overlapping table actions +- #1221 Updgrade EDC version to 0.7.3 +- #1196 Update IRS to 7.3.1 - #941 made the naming for quality notifications consistent throughout the FE app - #1190 store accepted policies in catalog offers on startup diff --git a/COMPATIBILITY_MATRIX.md b/COMPATIBILITY_MATRIX.md index 25d7e64333..0a8bcdf867 100644 --- a/COMPATIBILITY_MATRIX.md +++ b/COMPATIBILITY_MATRIX.md @@ -11,8 +11,8 @@ | Dependency | Name of Service | Version | Helm | Comments | |------------------|------------------------------|----------------------|----------|-----------------------------------------------------| | EDC | edc-postgresql | 15.4.0-debian-11-r45 | 12.12.10 | Enterprise Data Connector for PostgreSQL | -| IRS | item-relationship-service | 5.2.0 | 7.2.1 | Helm charts for Item Relationship Service | -| EDC | tractusx-connector | 0.7.0 | 0.7.0 | Connector for Data Transfer and Registration | +| IRS | item-relationship-service | 5.3.0 | 7.3.1 | Helm charts for Item Relationship Service | +| EDC | tractusx-connector | 0.7.3 | 0.7.3 | Connector for Data Transfer and Registration | | Discovery Finder | discovery service | 0.2.5 | - | Service for discovering and registering artifacts | | Portal | portal | 1.8.0 | - | Web portal for interacting with Trace-X | | SD-Factory | SD-Factory | 2.1.7 | - | Service Discovery Factory for managing dependencies | diff --git a/charts/traceability-foss/Chart.yaml b/charts/traceability-foss/Chart.yaml index 8bc8dff417..1e030722f4 100644 --- a/charts/traceability-foss/Chart.yaml +++ b/charts/traceability-foss/Chart.yaml @@ -42,11 +42,11 @@ dependencies: condition: pgadmin4.enabled - name: item-relationship-service repository: https://eclipse-tractusx.github.io/item-relationship-service - version: 7.2.1 + version: 7.3.1 condition: item-relationship-service.enabled - name: tractusx-connector repository: https://eclipse-tractusx.github.io/tractusx-edc - version: 0.7.1 + version: 0.7.3 condition: tractusx-connector.enabled - name: postgresql alias: edc-postgresql diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltFieldMapper.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltFieldMapper.java index 5b46862d2f..d7fac956ca 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltFieldMapper.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltFieldMapper.java @@ -53,9 +53,7 @@ public class AssetAsBuiltFieldMapper extends BaseRequestFieldMapper { Map.entry("receivedQualityAlertIdsInStatusActive", "receivedActiveAlerts"), Map.entry("receivedQualityInvestigationIdsInStatusActive", "receivedActiveInvestigations"), Map.entry("importState", "importState"), - Map.entry("importNote", "importNote"), - Map.entry("contractAgreementId", "contractAgreementId") - ); + Map.entry("importNote", "importNote")); @Override protected Map getSupportedFields() { diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltResponseMapper.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltResponseMapper.java index c24a3c4129..900518cefe 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltResponseMapper.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asbuilt/mapper/AssetAsBuiltResponseMapper.java @@ -22,6 +22,7 @@ import org.eclipse.tractusx.traceability.assets.application.base.mapper.AssetBaseResponseMapper; import org.eclipse.tractusx.traceability.assets.domain.base.model.AssetBase; import org.eclipse.tractusx.traceability.common.model.PageResult; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; import java.util.List; @@ -59,7 +60,6 @@ public static AssetAsBuiltResponse from(final AssetBase asset) { .importState(toImportStateResponse(asset.getImportState())) .importNote(asset.getImportNote()) .tombstone(asset.getTombstone()) - .contractAgreementId(asset.getContractAgreementId()) .build(); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedFieldMapper.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedFieldMapper.java index 06caf49443..3f32631d79 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedFieldMapper.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedFieldMapper.java @@ -46,8 +46,7 @@ public class AssetAsPlannedFieldMapper extends BaseRequestFieldMapper { Map.entry("businessPartner", "manufacturerId"), Map.entry("van", "van"), Map.entry("importState", "importState"), - Map.entry("importNote", "importNote"), - Map.entry("contractAgreementId", "contractAgreementId") + Map.entry("importNote", "importNote") ); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedResponseMapper.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedResponseMapper.java index 895e3ce987..d7511fbaf3 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedResponseMapper.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/application/asplanned/mapper/AssetAsPlannedResponseMapper.java @@ -22,6 +22,7 @@ import org.eclipse.tractusx.traceability.assets.application.base.mapper.AssetBaseResponseMapper; import org.eclipse.tractusx.traceability.assets.domain.base.model.AssetBase; import org.eclipse.tractusx.traceability.common.model.PageResult; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; import java.util.List; @@ -60,7 +61,6 @@ public static AssetAsPlannedResponse from(final AssetBase asset) { .importState(toImportStateResponse(asset.getImportState())) .importNote(asset.getImportNote()) .tombstone(asset.getTombstone()) - .contractAgreementId(asset.getContractAgreementId()) .build(); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/base/model/AssetBase.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/base/model/AssetBase.java index db908070f6..4532b4cb1c 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/base/model/AssetBase.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/base/model/AssetBase.java @@ -28,6 +28,7 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.irs.component.enums.BomLifecycle; import org.eclipse.tractusx.traceability.assets.domain.base.model.aspect.DetailAspectModel; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; import org.eclipse.tractusx.traceability.notification.domain.base.model.Notification; import java.util.List; @@ -62,7 +63,8 @@ public class AssetBase { private String importNote; private String policyId; private String tombstone; - private String contractAgreementId; + private List contractAgreements; + private String latestContractAgreementId; public BomLifecycle getBomLifecycle() { diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/importpoc/service/ImportServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/importpoc/service/ImportServiceImpl.java index 935b59d005..9397cc47bb 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/importpoc/service/ImportServiceImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/domain/importpoc/service/ImportServiceImpl.java @@ -72,8 +72,11 @@ public Map importAssets(MultipartFile file, ImportJob import } }); - List persistedAsBuilt = assetAsBuiltRepository.saveAllIfNotInIRSSyncAndUpdateImportStateAndNote(assetToUploadByBomLifecycle.get(BomLifecycle.AS_BUILT)); - List persistedAsPlanned = assetAsPlannedRepository.saveAllIfNotInIRSSyncAndUpdateImportStateAndNote(assetToUploadByBomLifecycle.get(BomLifecycle.AS_PLANNED)); + List assetAsBuilt = assetToUploadByBomLifecycle.get(BomLifecycle.AS_BUILT); + List assetAsPlanned = assetToUploadByBomLifecycle.get(BomLifecycle.AS_PLANNED); + + List persistedAsBuilt = assetAsBuiltRepository.saveAllIfNotInIRSSyncAndUpdateImportStateAndNote(assetAsBuilt); + List persistedAsPlanned = assetAsPlannedRepository.saveAllIfNotInIRSSyncAndUpdateImportStateAndNote(assetAsPlanned); importJob.setAssetAsBuilt(persistedAsBuilt); importJob.setAssetAsPlanned(persistedAsPlanned); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltEntity.java index d81ea2bf3a..97abf0baca 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltEntity.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltEntity.java @@ -21,6 +21,7 @@ package org.eclipse.tractusx.traceability.assets.infrastructure.asbuilt.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; @@ -43,6 +44,8 @@ import org.eclipse.tractusx.traceability.assets.domain.base.model.aspect.DetailAspectModel; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.AssetBaseEntity; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.SemanticDataModelEntity; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsBuiltEntity; import org.eclipse.tractusx.traceability.notification.infrastructure.notification.model.NotificationEntity; import org.eclipse.tractusx.traceability.notification.infrastructure.notification.model.NotificationSideBaseEntity; import org.eclipse.tractusx.traceability.notification.infrastructure.notification.model.NotificationTypeEntity; @@ -89,6 +92,8 @@ public class AssetAsBuiltEntity extends AssetBaseEntity { @OneToMany(mappedBy = "assetAsBuilt", fetch = FetchType.EAGER) private List submodels; + @OneToMany(mappedBy = "assetAsBuilt", fetch = FetchType.EAGER, cascade = CascadeType.ALL) + private List contractAgreements; public static AssetAsBuiltEntity from(AssetBase asset) { ManufacturingInfo manufacturingInfo = ManufacturingInfo.from(asset.getDetailAspectModels()); @@ -123,7 +128,7 @@ public static AssetAsBuiltEntity from(AssetBase asset) { .importNote(asset.getImportNote()) .policyId(asset.getPolicyId()) .tombstone(asset.getTombstone()) - .contractAgreementId(asset.getContractAgreementId()) + .contractAgreements(ContractAgreementAsBuiltEntity.fromDomainToEntityList(asset.getContractAgreements())) .build(); } @@ -180,7 +185,7 @@ public AssetBase toDomain() { .importNote(this.getImportNote()) .policyId(this.getPolicyId()) .tombstone(this.getTombstone()) - .contractAgreementId(this.getContractAgreementId()) + .contractAgreements(ContractAgreement.fromAsBuiltEntityToContractAgreements(emptyIfNull(this.getContractAgreements()))) .build(); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltViewEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltViewEntity.java index d1549b2155..5ad0f71b62 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltViewEntity.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/model/AssetAsBuiltViewEntity.java @@ -19,14 +19,17 @@ package org.eclipse.tractusx.traceability.assets.infrastructure.asbuilt.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.CollectionTable; import jakarta.persistence.Column; import jakarta.persistence.ElementCollection; import jakarta.persistence.Embeddable; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinTable; import jakarta.persistence.ManyToMany; +import jakarta.persistence.OneToMany; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; @@ -40,6 +43,8 @@ import org.eclipse.tractusx.traceability.assets.domain.base.model.aspect.DetailAspectModel; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.AssetBaseEntity; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.SemanticDataModelEntity; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsBuiltEntity; import org.eclipse.tractusx.traceability.notification.infrastructure.notification.model.NotificationEntity; import org.eclipse.tractusx.traceability.notification.infrastructure.notification.model.NotificationSideBaseEntity; import org.eclipse.tractusx.traceability.notification.infrastructure.notification.model.NotificationTypeEntity; @@ -90,6 +95,9 @@ public class AssetAsBuiltViewEntity extends AssetBaseEntity { inverseJoinColumns = @JoinColumn(name = "notification_id")) private List notifications = new ArrayList<>(); + @OneToMany(mappedBy = "assetAsBuiltView", fetch = FetchType.EAGER, cascade = CascadeType.ALL) + private List contractAgreements; + public AssetBase toDomain() { return AssetBase.builder() .id(this.getId()) @@ -143,7 +151,7 @@ public AssetBase toDomain() { .toList() ) .tombstone(this.getTombstone()) - .contractAgreementId(this.getContractAgreementId()) + .contractAgreements(ContractAgreement.fromAsBuiltEntityToContractAgreements(this.getContractAgreements())) .build(); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/repository/AssetAsBuiltRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/repository/AssetAsBuiltRepositoryImpl.java index 195a248774..cda7ac8079 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/repository/AssetAsBuiltRepositoryImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asbuilt/repository/AssetAsBuiltRepositoryImpl.java @@ -31,13 +31,17 @@ import org.eclipse.tractusx.traceability.assets.domain.base.model.ImportState; import org.eclipse.tractusx.traceability.assets.domain.base.model.Owner; import org.eclipse.tractusx.traceability.assets.infrastructure.asbuilt.model.AssetAsBuiltEntity; +import org.eclipse.tractusx.traceability.assets.infrastructure.asplanned.model.AssetAsPlannedEntity; import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.AssetCallbackRepository; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.AssetBaseEntity; import org.eclipse.tractusx.traceability.common.repository.CriteriaUtility; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.AbstractMap; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -92,11 +96,25 @@ public List getAssets() { @Override public AssetBase save(AssetBase asset) { + enrichContractAgreementsAsBuilt(List.of(asset)); return jpaAssetAsBuiltRepository.save(AssetAsBuiltEntity.from(asset)).toDomain(); } + private void enrichContractAgreementsAsBuilt(List assets) { + + + assets.forEach(assetBase -> { + List contractAgreements = new ArrayList<>(); + contractAgreements.add(ContractAgreement.toDomain(assetBase.getLatestContractAgreementId(), assetBase.getId(), ContractType.ASSET_AS_BUILT)); + Optional byId = jpaAssetAsBuiltRepository.findById(assetBase.getId()); + byId.ifPresent(assetAsBuiltEntity -> contractAgreements.addAll(ContractAgreement.fromAsBuiltEntityToContractAgreements(assetAsBuiltEntity.getContractAgreements()))); + assetBase.setContractAgreements(contractAgreements); + }); + } + @Override public List saveAll(List assets) { + enrichContractAgreementsAsBuilt(assets); return jpaAssetAsBuiltRepository.saveAll(AssetAsBuiltEntity.fromList(assets)).stream() .map(AssetAsBuiltEntity::toDomain) .toList(); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/model/AssetAsPlannedEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/model/AssetAsPlannedEntity.java index 2cfc2fd7dd..50fae795dd 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/model/AssetAsPlannedEntity.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/model/AssetAsPlannedEntity.java @@ -18,6 +18,7 @@ ********************************************************************************/ package org.eclipse.tractusx.traceability.assets.infrastructure.asplanned.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.CollectionTable; import jakarta.persistence.ElementCollection; import jakarta.persistence.Embeddable; @@ -37,6 +38,9 @@ import org.eclipse.tractusx.traceability.assets.domain.base.model.aspect.DetailAspectModel; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.AssetBaseEntity; import org.eclipse.tractusx.traceability.assets.infrastructure.base.model.SemanticDataModelEntity; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsPlannedEntity; import org.eclipse.tractusx.traceability.submodel.infrastructure.model.SubmodelPayloadEntity; import java.time.Instant; @@ -63,6 +67,9 @@ public class AssetAsPlannedEntity extends AssetBaseEntity { @CollectionTable(name = "assets_as_planned_childs", joinColumns = {@JoinColumn(name = "asset_as_planned_id")}) private List childDescriptors; + @OneToMany(mappedBy = "assetAsPlanned", fetch = FetchType.LAZY, cascade = CascadeType.ALL) + private List contractAgreements; + @Builder @NoArgsConstructor @AllArgsConstructor @@ -107,7 +114,7 @@ public static AssetAsPlannedEntity from(AssetBase asset) { .importNote(asset.getImportNote()) .policyId(asset.getPolicyId()) .tombstone(asset.getTombstone()) - .contractAgreementId(asset.getContractAgreementId()) + .contractAgreements(ContractAgreementAsPlannedEntity.fromDomainToEntityList(asset.getContractAgreements())) .build(); } @@ -133,7 +140,7 @@ public static AssetBase toDomain(AssetAsPlannedEntity entity) { .importNote(entity.getImportNote()) .policyId(entity.getPolicyId()) .tombstone(entity.getTombstone()) - .contractAgreementId(entity.getContractAgreementId()) + .contractAgreements(ContractAgreement.fromAsPlannedEntityToContractAgreements(entity.getContractAgreements())) .build(); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/repository/AssetAsPlannedRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/repository/AssetAsPlannedRepositoryImpl.java index 13e4b26659..3e3d435666 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/repository/AssetAsPlannedRepositoryImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/asplanned/repository/AssetAsPlannedRepositoryImpl.java @@ -34,12 +34,15 @@ import org.eclipse.tractusx.traceability.common.model.PageResult; import org.eclipse.tractusx.traceability.common.model.SearchCriteria; import org.eclipse.tractusx.traceability.common.repository.CriteriaUtility; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.AbstractMap; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -100,11 +103,13 @@ public List getAssets() { @Override public AssetBase save(AssetBase asset) { + enrichContractAgreementsAsPlanned(List.of(asset)); return AssetAsPlannedEntity.toDomain(jpaAssetAsPlannedRepository.save(AssetAsPlannedEntity.from(asset))); } @Override public List saveAll(List assets) { + enrichContractAgreementsAsPlanned(assets); return AssetAsPlannedEntity.toDomainList(jpaAssetAsPlannedRepository.saveAll(AssetAsPlannedEntity.fromList(assets))); } @@ -124,7 +129,10 @@ public List saveAllIfNotInIRSSyncAndUpdateImportStateAndNote(List { + enrichContractAgreementsAsPlanned(List.of(assetBase)); + return AssetAsPlannedEntity.from(assetBase); + }).toList(); return jpaAssetAsPlannedRepository.saveAll(toPersist).stream().map(AssetAsPlannedEntity::toDomain).toList(); @@ -173,4 +181,17 @@ public void updateImportStateAndNoteForAssets(ImportState importState, String im }); jpaAssetAsPlannedRepository.saveAll(foundAssets); } + + + private void enrichContractAgreementsAsPlanned(List assets) { + + assets.forEach(assetBase -> { + Optional byId = jpaAssetAsPlannedRepository.findById(assetBase.getId()); + if (byId.isPresent()) { + List contractAgreements = new ArrayList<>(ContractAgreement.fromAsPlannedEntityToContractAgreements(byId.get().getContractAgreements())); + contractAgreements.add(ContractAgreement.toDomain(assetBase.getLatestContractAgreementId(), assetBase.getId(), ContractType.ASSET_AS_PLANNED)); + assetBase.setContractAgreements(contractAgreements); + } + }); + } } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/JobRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/JobRepositoryImpl.java index c45c1d7b75..fbbaf2248d 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/JobRepositoryImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/JobRepositoryImpl.java @@ -21,12 +21,11 @@ package org.eclipse.tractusx.traceability.assets.infrastructure.base.irs; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.traceability.assets.domain.base.JobRepository; import org.eclipse.tractusx.traceability.assets.domain.base.model.AssetBase; -import org.eclipse.tractusx.traceability.assets.domain.base.model.ImportNote; -import org.eclipse.tractusx.traceability.assets.domain.base.model.ImportState; -import org.eclipse.tractusx.traceability.assets.domain.base.model.Owner; import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.request.BomLifecycle; import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.request.RegisterJobRequest; import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.response.Direction; @@ -34,10 +33,15 @@ import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.response.JobStatus; import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.response.factory.IrsResponseAssetMapper; import org.eclipse.tractusx.traceability.common.properties.TraceabilityProperties; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractAsBuiltRepositoryImpl; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractAsPlannedRepositoryImpl; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -52,12 +56,12 @@ public class JobRepositoryImpl implements JobRepository { private final TraceabilityProperties traceabilityProperties; private final AssetCallbackRepository assetAsBuiltCallbackRepository; private final AssetCallbackRepository assetAsPlannedCallbackRepository; - private static final String JOB_STATUS_COMPLETED = "COMPLETED"; private final IrsResponseAssetMapper assetMapperFactory; private final JobClient jobClient; + private final ObjectMapper objectMapper; public JobRepositoryImpl( JobClient jobClient, @@ -65,12 +69,14 @@ public JobRepositoryImpl( @Qualifier("assetAsBuiltRepositoryImpl") AssetCallbackRepository assetAsBuiltCallbackRepository, @Qualifier("assetAsPlannedRepositoryImpl") - AssetCallbackRepository assetAsPlannedCallbackRepository, IrsResponseAssetMapper assetMapperFactory) { + AssetCallbackRepository assetAsPlannedCallbackRepository, + IrsResponseAssetMapper assetMapperFactory, ObjectMapper objectMapper) { this.traceabilityProperties = traceabilityProperties; this.assetAsBuiltCallbackRepository = assetAsBuiltCallbackRepository; this.assetAsPlannedCallbackRepository = assetAsPlannedCallbackRepository; this.jobClient = jobClient; this.assetMapperFactory = assetMapperFactory; + this.objectMapper = objectMapper; } @Override @@ -97,7 +103,6 @@ public void handleJobFinishedCallback(String jobId, String state) { if (jobCompleted(jobResponseIRS.jobStatus())) { List assets = assetMapperFactory.toAssetBaseList(jobResponseIRS); - assets.forEach(assetBase -> { if (assetBase.getBomLifecycle() == AS_BUILT) { saveOrUpdateAssets(assetAsBuiltCallbackRepository, assetBase); @@ -110,6 +115,7 @@ public void handleJobFinishedCallback(String jobId, String state) { void saveOrUpdateAssets(AssetCallbackRepository repository, AssetBase asset) { try { + enrichAssetBaseByContractAgreements(repository, asset); repository.save(asset); } catch (DataIntegrityViolationException ex) { //retry save in case of ERROR: duplicate key value violates unique constraint "asset_pkey" @@ -118,6 +124,25 @@ void saveOrUpdateAssets(AssetCallbackRepository repository, AssetBase asset) { } } + private void enrichAssetBaseByContractAgreements(AssetCallbackRepository repository, AssetBase asset) { + Optional byId = repository.findById(asset.getId()); + List agreementsToAdd = new ArrayList<>(); + byId.ifPresent(assetBase -> agreementsToAdd.addAll(assetBase.getContractAgreements())); + try { + log.info("Found the following existing contractAgreements {}", this.objectMapper.writeValueAsString(agreementsToAdd)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + + ContractType contractType = asset.getSemanticDataModel().isAsBuilt() ? ContractType.ASSET_AS_BUILT : ContractType.ASSET_AS_PLANNED; + agreementsToAdd.add(ContractAgreement.toDomain(asset.getLatestContractAgreementId(), asset.getId(), contractType)); + asset.setContractAgreements(agreementsToAdd); + try { + log.info("Found the following NEW contractAgreements {}", this.objectMapper.writeValueAsString(agreementsToAdd)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } public static boolean jobCompleted(JobStatus jobStatus) { return JOB_STATUS_COMPLETED.equals(jobStatus.state()); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/model/response/factory/IrsResponseAssetMapper.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/model/response/factory/IrsResponseAssetMapper.java index 73635d4240..88aeb82d17 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/model/response/factory/IrsResponseAssetMapper.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/irs/model/response/factory/IrsResponseAssetMapper.java @@ -56,6 +56,7 @@ public class IrsResponseAssetMapper implements AssetBaseMappers { private final ObjectMapper objectMapper; private final BpnService bpnService; + @Override public List toAssetBaseList(IRSResponse irsResponse) { Map> descriptionMap = extractRelationshipToDescriptionMap(irsResponse); @@ -65,6 +66,7 @@ public List toAssetBaseList(IRSResponse irsResponse) { if (tombstones != null) { log.info("Found {} tombstones", tombstones.size()); } + List submodelAssets = new ArrayList<>(irsResponse .submodels() .stream() @@ -74,7 +76,7 @@ public List toAssetBaseList(IRSResponse irsResponse) { AssetBase assetBase = mapper.get().extractSubmodel(irsSubmodel); assetBase.setOwner(getOwner(assetBase, irsResponse)); assetBase.setIdShort(getShortId(irsResponse.shells(), assetBase.getId())); - assetBase.setContractAgreementId(getContractAgreementId(irsResponse.shells(), assetBase.getId())); + assetBase.setLatestContractAgreementId(getContractAgreementId(irsResponse.shells(), assetBase.getId())); assetBase.setManufacturerId(getManufacturerId(irsResponse, assetBase)); assetBase.setManufacturerName(bpnService.findByBpn(assetBase.getManufacturerId())); enrichUpwardAndDownwardDescriptions(descriptionMap, assetBase); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/model/AssetBaseEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/model/AssetBaseEntity.java index 2e4f775d1d..1f20d05675 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/model/AssetBaseEntity.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/assets/infrastructure/base/model/AssetBaseEntity.java @@ -58,5 +58,4 @@ public class AssetBaseEntity { private String importNote; private String policyId; private String tombstone; - private String contractAgreementId; } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ApplicationStartupConfig.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ApplicationStartupConfig.java index 307456303d..e8f244df41 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ApplicationStartupConfig.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/common/config/ApplicationStartupConfig.java @@ -25,20 +25,20 @@ import org.eclipse.tractusx.traceability.contracts.application.service.ContractService; import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; +import org.eclipse.tractusx.traceability.notification.application.contract.model.CreateNotificationContractRequest; +import org.eclipse.tractusx.traceability.notification.application.contract.model.NotificationMethod; +import org.eclipse.tractusx.traceability.notification.application.contract.model.NotificationType; import org.eclipse.tractusx.traceability.notification.domain.contract.EdcNotificationContractService; import org.eclipse.tractusx.traceability.policies.domain.PolicyRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.annotation.Profile; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; -import java.time.Instant; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.stream.Stream; import static org.eclipse.tractusx.traceability.common.config.ApplicationProfiles.NOT_INTEGRATION_TESTS; import static org.eclipse.tractusx.traceability.notification.domain.contract.EdcNotificationContractService.NOTIFICATION_CONTRACTS; @@ -49,21 +49,12 @@ @Profile(NOT_INTEGRATION_TESTS) public class ApplicationStartupConfig { private final PolicyRepository policyRepository; - private final AssetBaseService asPlannedService; - private final AssetBaseService asBuiltService; - private final ContractService contractService; @Autowired public ApplicationStartupConfig(PolicyRepository policyRepository, - @Qualifier("assetAsBuiltServiceImpl") AssetBaseService asBuiltService, - @Qualifier("assetAsPlannedServiceImpl") AssetBaseService asPlannedService, - EdcNotificationContractService edcNotificationContractService, - ContractService contractService) { + EdcNotificationContractService edcNotificationContractService) { this.policyRepository = policyRepository; - this.asPlannedService = asPlannedService; - this.asBuiltService = asBuiltService; this.edcNotificationContractService = edcNotificationContractService; - this.contractService = contractService; } private final EdcNotificationContractService edcNotificationContractService; @@ -99,56 +90,4 @@ public void createNotificationContracts() { executor.shutdown(); } - @EventListener(ApplicationReadyEvent.class) - public void insertIntoContractAgreements() { - ExecutorService executor = Executors.newSingleThreadExecutor(); - executor.execute(() -> { - log.info("on ApplicationReadyEvent insert into contracts."); - try { - log.info("Method yourMethod() started."); - - List asBuilt = asBuiltService.findAll(); - List asPlanned = asPlannedService.findAll(); - - log.info("Retrieved assets: asBuilt={}, asPlanned={}", asBuilt, asPlanned); - - List 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 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 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); - } - log.info("on ApplicationReadyEvent insert into contracts successfully done."); - }); - executor.shutdown(); - } - - } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/mapper/ContractResponseMapper.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/mapper/ContractResponseMapper.java index 11b6366329..4a8c366fea 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/mapper/ContractResponseMapper.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/mapper/ContractResponseMapper.java @@ -38,6 +38,7 @@ public static PageResult from(PageResult contractPag public static ContractResponse from(Contract contract) { return ContractResponse.builder() .contractId(contract.getContractId()) + .globalAssetId(contract.getGlobalAssetId()) .contractType(ContractTypeResponse.valueOf(contract.getType().name())) .state(contract.getState()) .counterpartyAddress(contract.getCounterpartyAddress()) diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/rest/ContractsController.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/rest/ContractsController.java index d79e28cb26..7948189046 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/rest/ContractsController.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/rest/ContractsController.java @@ -27,25 +27,31 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; -import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.traceability.common.model.PageResult; import org.eclipse.tractusx.traceability.common.request.PageableFilterRequest; import org.eclipse.tractusx.traceability.contracts.application.mapper.ContractResponseMapper; -import org.eclipse.tractusx.traceability.contracts.application.service.ContractService; +import org.eclipse.tractusx.traceability.contracts.application.service.ContractServiceReadOnly; import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +@Slf4j @RestController -@RequiredArgsConstructor @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPERVISOR')") @Tag(name = "Contracts") @RequestMapping(path = "/contracts", produces = "application/json", consumes = "application/json") public class ContractsController { - private final ContractService contractService; + + private final ContractServiceReadOnly contractServiceReadOnly; + + public ContractsController(@Qualifier("contractViewServiceImpl") ContractServiceReadOnly contractServiceReadOnly) { + this.contractServiceReadOnly = contractServiceReadOnly; + } @Operation(operationId = "contracts", summary = "All contract agreements for all assets", @@ -90,7 +96,7 @@ public class ContractsController { mediaType = "application/json", schema = @Schema(example = "{\"message\": \"Internal server error.\"}")))}) @PostMapping public PageResult getContracts(@Valid @RequestBody PageableFilterRequest pageableFilterRequest) { - PageResult contracts = contractService.getContracts(pageableFilterRequest); + PageResult contracts = contractServiceReadOnly.getContracts(pageableFilterRequest); return ContractResponseMapper.from(contracts); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractService.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractService.java index 33d97874bf..1240960771 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractService.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractService.java @@ -28,9 +28,9 @@ import java.util.List; public interface ContractService { - PageResult getContracts(PageableFilterRequest pageableFilterRequest); void saveContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException; void saveAll(List contractAgreements); + } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractServiceReadOnly.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractServiceReadOnly.java new file mode 100644 index 0000000000..6c142181cd --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/application/service/ContractServiceReadOnly.java @@ -0,0 +1,27 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.application.service; + +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; + +public interface ContractServiceReadOnly { + PageResult getContracts(PageableFilterRequest pageableFilterRequest); +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/Contract.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/Contract.java index c9d90b705a..e41789d1a1 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/Contract.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/Contract.java @@ -22,11 +22,8 @@ import lombok.Builder; import lombok.Data; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity; -import java.time.Instant; import java.time.OffsetDateTime; -import java.util.List; @Slf4j @AllArgsConstructor @@ -40,17 +37,7 @@ public class Contract { private String state; private String policy; private ContractType type; + private String globalAssetId; - 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 toEntityList(List contracts, ContractType contractType) { - return contracts.stream().map(contract -> Contract.toEntity(contract, contractType)).toList(); - } } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/ContractAgreement.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/ContractAgreement.java index 8437ffd696..f894977d2e 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/ContractAgreement.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/model/ContractAgreement.java @@ -20,11 +20,12 @@ import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; -import jakarta.persistence.Id; import lombok.Builder; import lombok.Getter; import lombok.Setter; -import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsBuiltEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsPlannedEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementBaseEntity; import java.time.Instant; import java.util.List; @@ -34,23 +35,64 @@ @Builder public class ContractAgreement { - @Id private String id; private String contractAgreementId; @Enumerated(EnumType.STRING) private ContractType type; private Instant created; + private Instant updated; + private String globalAssetId; - public static ContractAgreementEntity toEntity(ContractAgreement contractAgreement) { - return ContractAgreementEntity.builder() + public static ContractAgreementBaseEntity toEntity(ContractAgreement contractAgreement) { + return ContractAgreementBaseEntity.builder() .created(contractAgreement.getCreated()) - .id(contractAgreement.getId()) .contractAgreementId(contractAgreement.getContractAgreementId()) .type(contractAgreement.getType()) + .updated(contractAgreement.getUpdated()) + .globalAssetId(contractAgreement.getGlobalAssetId()) .build(); } - public static List toEntityList(List contractAgreementList) { + public static List toEntityList(List contractAgreementList) { return contractAgreementList.stream().map(ContractAgreement::toEntity).toList(); } + + public static ContractAgreement toDomain(String contractAgreementId, String globalAssetId, ContractType contractType) { + return ContractAgreement.builder() + .contractAgreementId(contractAgreementId) + .type(contractType) + .globalAssetId(globalAssetId) + .created(Instant.now()) + .updated(Instant.now()) + .build(); + } + + + public static List fromAsBuiltEntityToContractAgreements(List contractAgreementAsBuiltEntities) { + + return contractAgreementAsBuiltEntities.stream() + .map(contractAgreement -> ContractAgreement + .builder() + .contractAgreementId(contractAgreement.getContractAgreementId()) + .id(contractAgreement.getId()) + .type(contractAgreement.getType()) + .created(contractAgreement.getCreated()) + .updated(contractAgreement.getUpdated()) + .build()) + .toList(); + } + + public static List fromAsPlannedEntityToContractAgreements(List contractAgreementAsPlannedEntities) { + + return contractAgreementAsPlannedEntities.stream() + .map(contractAgreement -> ContractAgreement + .builder() + .contractAgreementId(contractAgreement.getContractAgreementId()) + .id(contractAgreement.getId()) + .type(contractAgreement.getType()) + .created(contractAgreement.getCreated()) + .updated(contractAgreement.getUpdated()) + .build()) + .toList(); + } } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepository.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepository.java index a28d42a6bc..d7c24c0534 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepository.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepository.java @@ -19,20 +19,15 @@ 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.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity; -import org.springframework.data.domain.Pageable; import java.util.List; -public interface ContractRepository { +public interface ContractRepository { - PageResult getContractsByPageable(Pageable pageable, SearchCriteria searchCriteria); void saveAllContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException; - void saveAll(List contractAgreements); + void saveAll(List contractAgreements); + } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepositoryReadOnly.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepositoryReadOnly.java new file mode 100644 index 0000000000..54ededcb3b --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/repository/ContractRepositoryReadOnly.java @@ -0,0 +1,29 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.domain.repository; + +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.springframework.data.domain.Pageable; + +public interface ContractRepositoryReadOnly { + + PageResult getContractsByPageable(Pageable pageable, SearchCriteria searchCriteria); +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/AbstractContractBaseService.java similarity index 70% rename from tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractServiceImpl.java rename to tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/AbstractContractBaseService.java index 9173b5efb5..0ce47f35e8 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractServiceImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/AbstractContractBaseService.java @@ -18,7 +18,6 @@ ********************************************************************************/ 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; @@ -28,36 +27,26 @@ 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.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.domain.repository.ContractRepository; import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Component; import java.util.List; -@RequiredArgsConstructor -@Component -public class ContractServiceImpl implements ContractService { - private final ContractRepository contractRepository; +public abstract class AbstractContractBaseService implements ContractService { - private final ContractFieldMapper contractFieldMapper; + protected abstract ContractRepository getContractRepository(); - @Override - public PageResult getContracts(PageableFilterRequest pageableFilterRequest) { - Pageable pageable = OwnPageable.toPageable(pageableFilterRequest.getOwnPageable(), contractFieldMapper); - SearchCriteria searchCriteria = pageableFilterRequest.getSearchCriteriaRequestParam().toSearchCriteria(contractFieldMapper); - - return contractRepository.getContractsByPageable(pageable, searchCriteria); - } + protected abstract ContractFieldMapper getContractFieldMapper(); @Override public void saveContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException { - contractRepository.saveAllContractAgreements(contractAgreementIds, contractType); + getContractRepository().saveAllContractAgreements(contractAgreementIds, contractType); } @Override public void saveAll(List contractAgreements) { - contractRepository.saveAll(ContractAgreement.toEntityList(contractAgreements)); + getContractRepository().saveAll(ContractAgreement.toEntityList(contractAgreements)); } } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractAsBuiltServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractAsBuiltServiceImpl.java new file mode 100644 index 0000000000..5204a31ac9 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractAsBuiltServiceImpl.java @@ -0,0 +1,46 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.domain.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.traceability.contracts.application.mapper.ContractFieldMapper; +import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepository; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractAsBuiltRepositoryImpl; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class ContractAsBuiltServiceImpl extends AbstractContractBaseService { + + private final ContractAsBuiltRepositoryImpl contractAsBuiltRepository; + private final ContractFieldMapper contractFieldMapper; + + @Override + protected ContractRepository getContractRepository() { + return contractAsBuiltRepository; + } + + @Override + protected ContractFieldMapper getContractFieldMapper() { + return contractFieldMapper; + } + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractAsPlannedServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractAsPlannedServiceImpl.java new file mode 100644 index 0000000000..64254316bc --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractAsPlannedServiceImpl.java @@ -0,0 +1,46 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.domain.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.traceability.contracts.application.mapper.ContractFieldMapper; +import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepository; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractAsPlannedRepositoryImpl; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class ContractAsPlannedServiceImpl extends AbstractContractBaseService { + + private final ContractAsPlannedRepositoryImpl contractAsPlannedRepository; + private final ContractFieldMapper contractFieldMapper; + + @Override + protected ContractRepository getContractRepository() { + return contractAsPlannedRepository; + } + + @Override + protected ContractFieldMapper getContractFieldMapper() { + return contractFieldMapper; + } + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractNotificationServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractNotificationServiceImpl.java new file mode 100644 index 0000000000..127d323c84 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractNotificationServiceImpl.java @@ -0,0 +1,46 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.domain.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.traceability.contracts.application.mapper.ContractFieldMapper; +import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepository; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractNotificationRepositoryImpl; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class ContractNotificationServiceImpl extends AbstractContractBaseService { + + private final ContractNotificationRepositoryImpl contractNotificationRepository; + private final ContractFieldMapper contractFieldMapper; + + @Override + protected ContractRepository getContractRepository() { + return contractNotificationRepository; + } + + @Override + protected ContractFieldMapper getContractFieldMapper() { + return contractFieldMapper; + } + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractViewServiceImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractViewServiceImpl.java new file mode 100644 index 0000000000..f4f5d799d1 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/domain/service/ContractViewServiceImpl.java @@ -0,0 +1,49 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.domain.service; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.traceability.common.model.PageResult; +import org.eclipse.tractusx.traceability.common.model.SearchCriteria; +import org.eclipse.tractusx.traceability.common.request.OwnPageable; +import org.eclipse.tractusx.traceability.common.request.PageableFilterRequest; +import org.eclipse.tractusx.traceability.contracts.application.mapper.ContractFieldMapper; +import org.eclipse.tractusx.traceability.contracts.application.service.ContractServiceReadOnly; +import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractViewRepositoryImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@RequiredArgsConstructor +public class ContractViewServiceImpl implements ContractServiceReadOnly { + + private final ContractViewRepositoryImpl contractViewRepository; + private final ContractFieldMapper contractFieldMapper; + + @Override + public PageResult getContracts(PageableFilterRequest pageableFilterRequest) { + Pageable pageable = OwnPageable.toPageable(pageableFilterRequest.getOwnPageable(), contractFieldMapper); + SearchCriteria searchCriteria = pageableFilterRequest.getSearchCriteriaRequestParam().toSearchCriteria(contractFieldMapper); + return contractViewRepository.getContractsByPageable(pageable, searchCriteria); + } + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementAsBuiltEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementAsBuiltEntity.java new file mode 100644 index 0000000000..8f6379e585 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementAsBuiltEntity.java @@ -0,0 +1,82 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; +import org.eclipse.tractusx.traceability.assets.infrastructure.asbuilt.model.AssetAsBuiltEntity; +import org.eclipse.tractusx.traceability.assets.infrastructure.asbuilt.model.AssetAsBuiltViewEntity; +import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; + +import java.time.Instant; +import java.util.List; + +import static org.apache.commons.collections4.ListUtils.emptyIfNull; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@SuperBuilder +@Table(name = "contract_agreement_as_built") +public class ContractAgreementAsBuiltEntity extends ContractAgreementBaseEntity { + @ManyToOne + @JoinColumn(name = "globalAssetId", referencedColumnName = "id", insertable = false, updatable = false) + private AssetAsBuiltEntity assetAsBuilt; + + @ManyToOne + @JoinColumn(name = "globalAssetId", referencedColumnName = "id", insertable = false, updatable = false) + private AssetAsBuiltViewEntity assetAsBuiltView; + + public static ContractAgreementAsBuiltEntity from(Contract contract, ContractType contractType) { + return ContractAgreementAsBuiltEntity.builder() + .globalAssetId(contract.getGlobalAssetId()) + .contractAgreementId(contract.getContractId()) + .type(contractType) + .created(Instant.now()) + .build(); + } + + + + public static List fromList(List contracts, ContractType contractType) { + return emptyIfNull(contracts).stream().map(contract -> ContractAgreementAsBuiltEntity.from(contract, contractType)).toList(); + } + + public static ContractAgreementAsBuiltEntity fromDomainToEntity(ContractAgreement contractAgreement) { + return ContractAgreementAsBuiltEntity.builder() + .globalAssetId(contractAgreement.getGlobalAssetId()) + .contractAgreementId(contractAgreement.getContractAgreementId()) + .type(contractAgreement.getType()) + .created(contractAgreement.getCreated()) + .updated(contractAgreement.getUpdated()).build(); + } + + public static List fromDomainToEntityList(List contractAgreements) { + return emptyIfNull(contractAgreements).stream().map(ContractAgreementAsBuiltEntity::fromDomainToEntity).toList(); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementAsPlannedEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementAsPlannedEntity.java new file mode 100644 index 0000000000..d7d2a41408 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementAsPlannedEntity.java @@ -0,0 +1,75 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; +import org.eclipse.tractusx.traceability.assets.infrastructure.asplanned.model.AssetAsPlannedEntity; +import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; + +import java.time.Instant; +import java.util.List; + +import static org.apache.commons.collections4.ListUtils.emptyIfNull; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@SuperBuilder +@Table(name = "contract_agreement_as_planned") +public class ContractAgreementAsPlannedEntity extends ContractAgreementBaseEntity { + + @ManyToOne + @JoinColumn(name = "globalAssetId", referencedColumnName = "id", insertable = false, updatable = false) + private AssetAsPlannedEntity assetAsPlanned; + + public static ContractAgreementAsPlannedEntity from(Contract contract, ContractType contractType) { + return ContractAgreementAsPlannedEntity.builder() + .globalAssetId(contract.getGlobalAssetId()) + .contractAgreementId(contract.getContractId()) + .type(contractType) + .created(Instant.now()) + .build(); + } + + public static List fromList(List contracts, ContractType contractType) { + return emptyIfNull(contracts).stream().map(contract -> ContractAgreementAsPlannedEntity.from(contract, contractType)).toList(); + } + + public static ContractAgreementAsPlannedEntity fromDomainToEntity(ContractAgreement contractAgreement) { + return ContractAgreementAsPlannedEntity.builder() + .contractAgreementId(contractAgreement.getContractAgreementId()) + .type(contractAgreement.getType()) + .created(contractAgreement.getCreated()) + .updated(contractAgreement.getUpdated()).build(); + } + + public static List fromDomainToEntityList(List contractAgreements) { + return emptyIfNull(contractAgreements).stream().map(ContractAgreementAsPlannedEntity::fromDomainToEntity).toList(); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementBaseEntity.java similarity index 68% rename from tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementEntity.java rename to tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementBaseEntity.java index 19a4e08fba..04cf268817 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementEntity.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementBaseEntity.java @@ -18,17 +18,19 @@ ********************************************************************************/ package org.eclipse.tractusx.traceability.contracts.infrastructure.model; -import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import jakarta.persistence.Table; +import jakarta.persistence.MappedSuperclass; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.experimental.SuperBuilder; import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; +import org.hibernate.annotations.GenericGenerator; import java.time.Instant; import java.util.List; @@ -36,29 +38,18 @@ @Getter @Setter @NoArgsConstructor -@Entity @SuperBuilder -@Table(name = "contract_agreement") -public class ContractAgreementEntity { +@MappedSuperclass +public class ContractAgreementBaseEntity { @Id + @GeneratedValue(generator = "uuid") + @GenericGenerator(name = "uuid", strategy = "uuid2") private String id; + private String globalAssetId; private String contractAgreementId; @Enumerated(EnumType.STRING) private ContractType type; private Instant created; - - - public static ContractAgreement toDomain(ContractAgreementEntity contractAgreement) { - return ContractAgreement.builder() - .created(contractAgreement.getCreated()) - .id(contractAgreement.getId()) - .contractAgreementId(contractAgreement.getContractAgreementId()) - .type(contractAgreement.getType()) - .build(); - } - - public static List toDomainList(List contractAgreementList) { - return contractAgreementList.stream().map(ContractAgreementEntity::toDomain).toList(); - } + private Instant updated; } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementNotificationEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementNotificationEntity.java new file mode 100644 index 0000000000..53f0e8a2a3 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementNotificationEntity.java @@ -0,0 +1,63 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; +import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; + +import java.time.Instant; +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@SuperBuilder +@Table(name = "contract_agreement_notification") +public class ContractAgreementNotificationEntity extends ContractAgreementBaseEntity { + public static ContractAgreementNotificationEntity from(Contract contract, ContractType contractType) { + return ContractAgreementNotificationEntity.builder() + .globalAssetId(null) + .contractAgreementId(contract.getContractId()) + .type(contractType) + .created(Instant.now()) + .build(); + } + + public static List fromList(List contracts, ContractType contractType) { + return contracts.stream().map(contract -> ContractAgreementNotificationEntity.from(contract, contractType)).toList(); + } + + + public static ContractAgreementNotificationEntity fromDomainToEntity(ContractAgreement contractAgreement) { + return ContractAgreementNotificationEntity.builder() + .contractAgreementId(contractAgreement.getContractAgreementId()) + .globalAssetId(contractAgreement.getGlobalAssetId()) + .type(contractAgreement.getType()) + .created(contractAgreement.getCreated()) + .updated(contractAgreement.getUpdated()).build(); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementViewEntity.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementViewEntity.java new file mode 100644 index 0000000000..d0cec3cded --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/model/ContractAgreementViewEntity.java @@ -0,0 +1,53 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; +import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement; +import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType; + +import java.time.Instant; +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@SuperBuilder +@Table(name = "contract_agreement_full") +public class ContractAgreementViewEntity extends ContractAgreementBaseEntity { + public static ContractAgreementViewEntity from(Contract contract, ContractType contractType) { + return ContractAgreementViewEntity.builder() + .contractAgreementId(contract.getContractId()) + .globalAssetId(contract.getGlobalAssetId()) + .type(contractType) + .created(Instant.now()) + .build(); + } + + public static List fromList(List contracts, ContractType contractType) { + return contracts.stream().map(contract -> ContractAgreementViewEntity.from(contract, contractType)).toList(); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementAsBuiltSpecification.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementAsBuiltSpecification.java new file mode 100644 index 0000000000..35c57f5ded --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementAsBuiltSpecification.java @@ -0,0 +1,42 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +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.ContractAgreementAsBuiltEntity; +import org.jetbrains.annotations.NotNull; +import org.springframework.data.jpa.domain.Specification; + +public class ContractAgreementAsBuiltSpecification extends BaseSpecification implements Specification { + + public ContractAgreementAsBuiltSpecification(SearchCriteriaFilter criteria) { + super(criteria); + } + + @Override + public Predicate toPredicate(@NotNull Root root, @NotNull CriteriaQuery query, @NotNull CriteriaBuilder builder) { + return createPredicate(getSearchCriteriaFilter(), root, builder); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementAsPlannedSpecification.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementAsPlannedSpecification.java new file mode 100644 index 0000000000..dfaca1280b --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementAsPlannedSpecification.java @@ -0,0 +1,42 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +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.ContractAgreementAsPlannedEntity; +import org.jetbrains.annotations.NotNull; +import org.springframework.data.jpa.domain.Specification; + +public class ContractAgreementAsPlannedSpecification extends BaseSpecification implements Specification { + + public ContractAgreementAsPlannedSpecification(SearchCriteriaFilter criteria) { + super(criteria); + } + + @Override + public Predicate toPredicate(@NotNull Root root, @NotNull CriteriaQuery query, @NotNull CriteriaBuilder builder) { + return createPredicate(getSearchCriteriaFilter(), root, builder); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementNotificationSpecification.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementNotificationSpecification.java new file mode 100644 index 0000000000..1f031ad17e --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementNotificationSpecification.java @@ -0,0 +1,42 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +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.ContractAgreementNotificationEntity; +import org.jetbrains.annotations.NotNull; +import org.springframework.data.jpa.domain.Specification; + +public class ContractAgreementNotificationSpecification extends BaseSpecification implements Specification { + + public ContractAgreementNotificationSpecification(SearchCriteriaFilter criteria) { + super(criteria); + } + + @Override + public Predicate toPredicate(@NotNull Root root, @NotNull CriteriaQuery query, @NotNull CriteriaBuilder builder) { + return createPredicate(getSearchCriteriaFilter(), root, builder); + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractSpecification.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementViewSpecification.java similarity index 82% rename from tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractSpecification.java rename to tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementViewSpecification.java index e533c0dd0c..9880af24f6 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractSpecification.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAgreementViewSpecification.java @@ -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.ContractAgreementEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementViewEntity; import org.jetbrains.annotations.NotNull; import org.springframework.data.jpa.domain.Specification; -public class ContractSpecification extends BaseSpecification implements Specification { +public class ContractAgreementViewSpecification extends BaseSpecification implements Specification { - public ContractSpecification(SearchCriteriaFilter criteria) { + public ContractAgreementViewSpecification(SearchCriteriaFilter criteria) { super(criteria); } @Override - public Predicate toPredicate(@NotNull Root root, @NotNull CriteriaQuery query, @NotNull CriteriaBuilder builder) { + public Predicate toPredicate(@NotNull Root root, @NotNull CriteriaQuery query, @NotNull CriteriaBuilder builder) { return createPredicate(getSearchCriteriaFilter(), root, builder); } } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAsBuiltRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAsBuiltRepositoryImpl.java new file mode 100644 index 0000000000..e0f6eb109e --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAsBuiltRepositoryImpl.java @@ -0,0 +1,67 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException; +import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractAgreementService; +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.ContractAgreementAsBuiltEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementBaseEntity; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@Slf4j +public class ContractAsBuiltRepositoryImpl extends ContractRepositoryImplBase implements ContractRepository { + + private final JpaContractAgreementAsBuiltRepository contractAgreementRepository; + + public ContractAsBuiltRepositoryImpl(EdcContractAgreementService edcContractAgreementService, ObjectMapper objectMapper, JpaContractAgreementAsBuiltRepository contractAgreementRepository) { + super(edcContractAgreementService, objectMapper); + this.contractAgreementRepository = contractAgreementRepository; + } + + @Override + public void saveAllContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException { + + List contractAgreementEntities = contractAgreementIds.stream() + .map(contractAgreementId -> ContractAgreementAsBuiltEntity.builder() + .contractAgreementId(contractAgreementId) + .type(contractType) + .build()) + .map(ContractAgreementBaseEntity.class::cast) + .toList(); + + List contracts = fetchEdcContractAgreements(contractAgreementEntities); + List contractAgreementsUpdated = ContractAgreementAsBuiltEntity.fromList(contracts, contractType); + contractAgreementRepository.saveAll(contractAgreementsUpdated); + } + + @Override + public void saveAll(List contractAgreements) { + contractAgreementRepository.saveAll(contractAgreements); + } + + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAsPlannedRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAsPlannedRepositoryImpl.java new file mode 100644 index 0000000000..c58eeda18d --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractAsPlannedRepositoryImpl.java @@ -0,0 +1,72 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException; +import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractAgreementService; +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.ContractAgreementAsPlannedEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementBaseEntity; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.stream.Collectors; + +@Component +@Slf4j +public class ContractAsPlannedRepositoryImpl extends ContractRepositoryImplBase implements ContractRepository { + + private final JpaContractAgreementAsPlannedRepository contractAgreementRepository; + + public ContractAsPlannedRepositoryImpl(EdcContractAgreementService edcContractAgreementService, ObjectMapper objectMapper, JpaContractAgreementAsPlannedRepository contractAgreementRepository) { + super(edcContractAgreementService, objectMapper); + this.contractAgreementRepository = contractAgreementRepository; + } + + @Override + public void saveAllContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException { + + List contractAgreementEntities = contractAgreementIds.stream() + .map(contractAgreementId -> ContractAgreementAsPlannedEntity.builder() + .contractAgreementId(contractAgreementId) + .type(contractType) + .build()) + .collect(Collectors.toList()); + + List baseEntities = contractAgreementEntities + .stream() + .map(entity -> (ContractAgreementBaseEntity) entity) + .toList(); + + + List contracts = fetchEdcContractAgreements(baseEntities); + List contractAgreementsUpdated = ContractAgreementAsPlannedEntity.fromList(contracts, contractType); + contractAgreementRepository.saveAll(contractAgreementsUpdated); + } + + @Override + public void saveAll(List contractAgreements) { + contractAgreementRepository.saveAll(contractAgreements); + } + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractNotificationRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractNotificationRepositoryImpl.java new file mode 100644 index 0000000000..1c7e92b3d1 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractNotificationRepositoryImpl.java @@ -0,0 +1,66 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException; +import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractAgreementService; +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.ContractAgreementBaseEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementNotificationEntity; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@Slf4j +public class ContractNotificationRepositoryImpl extends ContractRepositoryImplBase implements ContractRepository { + + private final JpaContractAgreementNotificationRepository contractAgreementRepository; + + public ContractNotificationRepositoryImpl(EdcContractAgreementService edcContractAgreementService, ObjectMapper objectMapper, EdcContractAgreementService edcContractAgreementService1, JpaContractAgreementNotificationRepository contractAgreementRepository) { + super(edcContractAgreementService, objectMapper); + this.contractAgreementRepository = contractAgreementRepository; + } + + @Override + public void saveAllContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException { + + List contractAgreementEntities = contractAgreementIds.stream() + .map(contractAgreementId -> ContractAgreementNotificationEntity.builder() + .contractAgreementId(contractAgreementId) + .type(contractType) + .build()) + .map(ContractAgreementBaseEntity.class::cast) + .toList(); + + List contracts = fetchEdcContractAgreements(contractAgreementEntities); + List contractAgreementsUpdated = ContractAgreementNotificationEntity.fromList(contracts, contractType); + contractAgreementRepository.saveAll(contractAgreementsUpdated); + } + + @Override + public void saveAll(List contractAgreements) { + contractAgreementRepository.saveAll(contractAgreements); + } + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractRepositoryImplBase.java similarity index 57% rename from tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractRepositoryImpl.java rename to tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractRepositoryImplBase.java index 32bcc62aae..5129117b3a 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractRepositoryImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractRepositoryImplBase.java @@ -20,24 +20,16 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.RequiredArgsConstructor; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.tuple.ImmutablePair; import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractAgreementNegotiationResponse; import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractAgreementsResponse; import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException; import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractAgreementService; -import org.eclipse.tractusx.traceability.common.model.PageResult; -import org.eclipse.tractusx.traceability.common.model.SearchCriteria; -import org.eclipse.tractusx.traceability.common.repository.BaseSpecification; -import org.eclipse.tractusx.traceability.contracts.domain.exception.ContractException; 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.ContractAgreementEntity; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.jpa.domain.Specification; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementBaseEntity; import org.springframework.stereotype.Component; import java.time.Instant; @@ -50,65 +42,18 @@ import java.util.Objects; import java.util.stream.Collectors; -import static org.apache.commons.collections4.ListUtils.emptyIfNull; - @Component -@RequiredArgsConstructor +@AllArgsConstructor @Slf4j -public class ContractRepositoryImpl implements ContractRepository { +public class ContractRepositoryImplBase { private final EdcContractAgreementService edcContractAgreementService; - private final JpaContractAgreementRepository contractAgreementRepository; private final ObjectMapper objectMapper; - @Override - public PageResult getContractsByPageable(Pageable pageable, SearchCriteria searchCriteria) { - try { - List contractAgreementSpecifications = emptyIfNull(searchCriteria.getSearchCriteriaFilterList()).stream() - .map(ContractSpecification::new) - .toList(); - Specification specification = BaseSpecification.toSpecification(contractAgreementSpecifications); - Page contractAgreementEntities = contractAgreementRepository.findAll(specification, pageable); - - 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(contractAgreementEntities.getContent()), - contractAgreementEntities.getPageable().getPageNumber(), - contractAgreementEntities.getTotalPages(), - contractAgreementEntities.getPageable().getPageSize(), - contractAgreementEntities.getTotalElements()); - - } catch (ContractAgreementException e) { - throw new ContractException(e); - } - - } - @Override - public void saveAllContractAgreements(List contractAgreementIds, ContractType contractType) throws ContractAgreementException { - - List contractAgreementEntities = contractAgreementIds.stream() - .map(contractAgreementId -> ContractAgreementEntity.builder() - .contractAgreementId(contractAgreementId) - .type(contractType) - .build()) - .collect(Collectors.toList()); - - List contracts = fetchEdcContractAgreements(contractAgreementEntities); - List contractAgreementsUpdated = Contract.toEntityList(contracts, contractType); - contractAgreementRepository.saveAll(contractAgreementsUpdated); - } + public List fetchEdcContractAgreements(List contractAgreementEntities) throws ContractAgreementException { + List contractAgreementIds = contractAgreementEntities.stream().filter(Objects::nonNull).map(ContractAgreementBaseEntity::getContractAgreementId).filter(Objects::nonNull).toList(); - @Override - public void saveAll(List contractAgreements) { - contractAgreementRepository.saveAll(contractAgreements); - } - - private List fetchEdcContractAgreements(List contractAgreementEntities) throws ContractAgreementException { - List contractAgreementIds = contractAgreementEntities.stream().filter(Objects::nonNull).map(ContractAgreementEntity::getContractAgreementId).toList(); log.info("Trying to fetch contractAgreementIds from EDC: " + contractAgreementIds); List contractAgreements = edcContractAgreementService.getContractAgreements(contractAgreementIds); @@ -116,7 +61,11 @@ private List fetchEdcContractAgreements(List validateContractAgreements(contractAgreementIds, contractAgreements); Map contractTypes = contractAgreementEntities.stream() - .collect(Collectors.toMap(ContractAgreementEntity::getContractAgreementId, ContractAgreementEntity::getType)); + .collect(Collectors.toMap( + ContractAgreementBaseEntity::getContractAgreementId, + ContractAgreementBaseEntity::getType, + (existing, replacement) -> existing // retain existing value if duplicate key is encountered + )); Map contractNegotiations = contractAgreements.stream() .map(agreement -> new ImmutablePair<>(agreement.contractAgreementId(), @@ -127,8 +76,16 @@ private List fetchEdcContractAgreements(List return contractAgreements.stream().map(contractAgreement -> { try { + + String globalAssetId = contractAgreementEntities.stream() + .filter(contractAgreementViewEntity -> contractAgreementViewEntity.getContractAgreementId() == null || contractAgreement.contractAgreementId() == null || contractAgreementViewEntity.getContractAgreementId().equals(contractAgreement.contractAgreementId())) + .findFirst() + .map(ContractAgreementBaseEntity::getGlobalAssetId) + .orElse(null); + return Contract.builder() .contractId(contractAgreement.contractAgreementId()) + .globalAssetId(globalAssetId) .counterpartyAddress(contractNegotiations.get(contractAgreement.contractAgreementId()).counterPartyAddress()) .creationDate(OffsetDateTime.ofInstant(Instant.ofEpochSecond(contractAgreement.contractSigningDate()), ZoneId.systemDefault())) .state(contractNegotiations.get(contractAgreement.contractAgreementId()).state()) @@ -143,11 +100,16 @@ private List fetchEdcContractAgreements(List } private void validateContractAgreements(List contractAgreementIds, List contractAgreements) { + if (contractAgreementIds == null || contractAgreements == null) { + log.warn("Either contractAgreementIds or contractAgreements is null."); + return; + } + ArrayList givenList = new ArrayList<>(contractAgreementIds); Collections.sort(givenList); - List expectedList = contractAgreements.stream() - .map(EdcContractAgreementsResponse::contractAgreementId) + List expectedList = contractAgreements.stream().map(EdcContractAgreementsResponse::contractAgreementId) + .filter(Objects::nonNull)// Ensure no null values are mapped .sorted() .toList(); log.info("EDC responded with the following contractAgreementIds: " + expectedList); diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractViewRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractViewRepositoryImpl.java new file mode 100644 index 0000000000..36b13de488 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/ContractViewRepositoryImpl.java @@ -0,0 +1,84 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException; +import org.eclipse.tractusx.irs.edc.client.contract.service.EdcContractAgreementService; +import org.eclipse.tractusx.traceability.common.model.PageResult; +import org.eclipse.tractusx.traceability.common.model.SearchCriteria; +import org.eclipse.tractusx.traceability.common.repository.BaseSpecification; +import org.eclipse.tractusx.traceability.contracts.domain.exception.ContractException; +import org.eclipse.tractusx.traceability.contracts.domain.model.Contract; +import org.eclipse.tractusx.traceability.contracts.domain.repository.ContractRepositoryReadOnly; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementBaseEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementViewEntity; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.stereotype.Component; + +import java.util.List; + +import static org.apache.commons.collections4.ListUtils.emptyIfNull; + +@Component +@Slf4j +public class ContractViewRepositoryImpl extends ContractRepositoryImplBase implements ContractRepositoryReadOnly { + + private final JpaContractAgreementViewRepository contractAgreementRepository; + + public ContractViewRepositoryImpl(EdcContractAgreementService edcContractAgreementService, + ObjectMapper objectMapper, + JpaContractAgreementViewRepository contractAgreementRepository) { + super(edcContractAgreementService, objectMapper); + this.contractAgreementRepository = contractAgreementRepository; + } + + @Override + public PageResult getContractsByPageable(Pageable pageable, SearchCriteria searchCriteria) { + try { + List contractAgreementSpecifications = emptyIfNull(searchCriteria.getSearchCriteriaFilterList()).stream() + .map(ContractAgreementViewSpecification::new) + .toList(); + Specification specification = BaseSpecification.toSpecification(contractAgreementSpecifications); + Page contractAgreementEntities = contractAgreementRepository.findAll(specification, pageable); + + 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); + } + List baseEntities = contractAgreementEntities.getContent() + .stream() + .map(entity -> (ContractAgreementBaseEntity) entity) + .toList(); + + return new PageResult<>(fetchEdcContractAgreements(baseEntities), + contractAgreementEntities.getPageable().getPageNumber(), + contractAgreementEntities.getTotalPages(), + contractAgreementEntities.getPageable().getPageSize(), + contractAgreementEntities.getTotalElements()); + + } catch (ContractAgreementException e) { + throw new ContractException(e); + } + + } +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementAsBuiltRepository.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementAsBuiltRepository.java new file mode 100644 index 0000000000..8e6bfe35ba --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementAsBuiltRepository.java @@ -0,0 +1,29 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsBuiltEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface JpaContractAgreementAsBuiltRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementAsPlannedRepository.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementAsPlannedRepository.java new file mode 100644 index 0000000000..3780680257 --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementAsPlannedRepository.java @@ -0,0 +1,29 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementAsPlannedEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface JpaContractAgreementAsPlannedRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementNotificationRepository.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementNotificationRepository.java new file mode 100644 index 0000000000..f69f3fff5f --- /dev/null +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementNotificationRepository.java @@ -0,0 +1,29 @@ +/******************************************************************************** + * Copyright (c) 2024 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; + +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementNotificationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface JpaContractAgreementNotificationRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementRepository.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementViewRepository.java similarity index 85% rename from tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementRepository.java rename to tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementViewRepository.java index 135b246d1f..05d7796533 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementRepository.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/contracts/infrastructure/repository/JpaContractAgreementViewRepository.java @@ -18,12 +18,12 @@ ********************************************************************************/ package org.eclipse.tractusx.traceability.contracts.infrastructure.repository; -import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementEntity; +import org.eclipse.tractusx.traceability.contracts.infrastructure.model.ContractAgreementViewEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Repository; @Repository -public interface JpaContractAgreementRepository extends JpaRepository, JpaSpecificationExecutor { +public interface JpaContractAgreementViewRepository extends JpaRepository, JpaSpecificationExecutor { } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/domain/base/service/NotificationsEDCFacade.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/domain/base/service/NotificationsEDCFacade.java index 160ea88345..881b292509 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/domain/base/service/NotificationsEDCFacade.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/domain/base/service/NotificationsEDCFacade.java @@ -65,7 +65,6 @@ @Slf4j @Component -@RequiredArgsConstructor @Transactional(dontRollbackOn = {ContractNegotiationException.class, NoCatalogItemException.class, SendNotificationException.class, NoEndpointDataReferenceException.class}) public class NotificationsEDCFacade { @@ -75,12 +74,28 @@ public class NotificationsEDCFacade { private final EdcProperties edcProperties; - @Qualifier(EDC_NOTIFICATION_TEMPLATE) + private final RestTemplate edcNotificationTemplate; private final EDCCatalogFacade edcCatalogFacade; private final ContractNegotiationService contractNegotiationService; private final EndpointDataReferenceStorage endpointDataReferenceStorage; - private final ContractService contractService; + private final ContractService contractNotificationServiceImpl; + + public NotificationsEDCFacade(ObjectMapper objectMapper, + EdcProperties edcProperties, + @Qualifier(EDC_NOTIFICATION_TEMPLATE) RestTemplate edcNotificationTemplate, + EDCCatalogFacade edcCatalogFacade, + ContractNegotiationService contractNegotiationService, + EndpointDataReferenceStorage endpointDataReferenceStorage, + @Qualifier("contractNotificationServiceImpl") ContractService contractNotificationServiceImpl) { + this.objectMapper = objectMapper; + this.edcProperties = edcProperties; + this.edcNotificationTemplate = edcNotificationTemplate; + this.edcCatalogFacade = edcCatalogFacade; + this.contractNegotiationService = contractNegotiationService; + this.endpointDataReferenceStorage = endpointDataReferenceStorage; + this.contractNotificationServiceImpl = contractNotificationServiceImpl; + } public static final String CX_TAXO_QUALITY_INVESTIGATION_RECEIVE = "https://w3id.org/catenax/taxonomy#ReceiveQualityInvestigationNotification"; public static final String CX_TAXO_QUALITY_INVESTIGATION_UPDATE = "https://w3id.org/catenax/taxonomy#UpdateQualityInvestigationNotification"; @@ -102,7 +117,7 @@ public void startEdcTransfer( notificationMessage.setContractAgreementId(contractAgreementId); try { - contractService.saveContractAgreements(List.of(contractAgreementId), ContractType.NOTIFICATION); + contractNotificationServiceImpl.saveContractAgreements(List.of(contractAgreementId), ContractType.NOTIFICATION); } catch (Exception e) { log.warn("Could not save contractAgreementId for notification {}", e.getMessage()); } diff --git a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/infrastructure/notification/repository/NotificationRepositoryImpl.java b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/infrastructure/notification/repository/NotificationRepositoryImpl.java index 7ceae9ac47..e294dc9761 100644 --- a/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/infrastructure/notification/repository/NotificationRepositoryImpl.java +++ b/tx-backend/src/main/java/org/eclipse/tractusx/traceability/notification/infrastructure/notification/repository/NotificationRepositoryImpl.java @@ -68,7 +68,6 @@ public class NotificationRepositoryImpl implements NotificationRepository { private final JpaNotificationRepository jpaNotificationRepository; private final JpaNotificationMessageRepository jpaNotificationMessageRepository; private final JpaAssetAsBuiltRepository assetsAsBuiltRepository; - private final Clock clock; @PersistenceContext diff --git a/tx-backend/src/main/resources/db/migration/R__create_contract_agreement_view.sql b/tx-backend/src/main/resources/db/migration/R__create_contract_agreement_view.sql index 13de99dcf4..01a0c7d66e 100644 --- a/tx-backend/src/main/resources/db/migration/R__create_contract_agreement_view.sql +++ b/tx-backend/src/main/resources/db/migration/R__create_contract_agreement_view.sql @@ -1,2 +1,33 @@ --- Drop the view if it exists -DROP VIEW IF EXISTS contract_agreement_view; +CREATE VIEW contract_agreement_full AS +SELECT + id, + contract_agreement_id, + global_asset_id, + type, + created, + updated, + 'contract_agreement_as_built' AS source_table +FROM + contract_agreement_as_built +UNION ALL +SELECT + id, + contract_agreement_id, + global_asset_id, + type, + created, + updated, + 'contract_agreement_notification' AS source_table +FROM + contract_agreement_notification +UNION ALL +SELECT + id, + contract_agreement_id, + global_asset_id, + type, + created, + updated, + 'contract_agreement_as_planned' AS source_table +FROM + contract_agreement_as_planned; diff --git a/tx-backend/src/main/resources/db/migration/V26__create_contract_agreements_notification.sql b/tx-backend/src/main/resources/db/migration/V26__create_contract_agreements_notification.sql new file mode 100644 index 0000000000..715a31d0d6 --- /dev/null +++ b/tx-backend/src/main/resources/db/migration/V26__create_contract_agreements_notification.sql @@ -0,0 +1,22 @@ +ALTER TABLE contract_agreement RENAME TO contract_agreement_as_built; + +ALTER TABLE contract_agreement_as_built + ADD COLUMN updated TIMESTAMP; + +CREATE TABLE IF NOT EXISTS contract_agreement_notification ( + id VARCHAR(255) PRIMARY KEY, + contract_agreement_id VARCHAR(255), + created TIMESTAMP, + type VARCHAR(255), + updated TIMESTAMP); + + +CREATE TABLE IF NOT EXISTS contract_agreement_as_planned ( + id VARCHAR(255) PRIMARY KEY, + contract_agreement_id VARCHAR(255), + created TIMESTAMP, + type VARCHAR(255), + updated TIMESTAMP); + + + diff --git a/tx-backend/src/main/resources/db/migration/V27__add_contract_agreements_to_ref_tables.sql b/tx-backend/src/main/resources/db/migration/V27__add_contract_agreements_to_ref_tables.sql new file mode 100644 index 0000000000..516d6a8d50 --- /dev/null +++ b/tx-backend/src/main/resources/db/migration/V27__add_contract_agreements_to_ref_tables.sql @@ -0,0 +1,8 @@ +ALTER TABLE contract_agreement_as_built + ADD COLUMN global_asset_id varchar; + +ALTER TABLE contract_agreement_as_planned + ADD COLUMN global_asset_id varchar; + +ALTER TABLE contract_agreement_notification + ADD COLUMN global_asset_id varchar; diff --git a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/AssetAsBuiltControllerSearchValuesIT.java b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/AssetAsBuiltControllerSearchValuesIT.java index 038285e15e..add602369a 100644 --- a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/AssetAsBuiltControllerSearchValuesIT.java +++ b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/AssetAsBuiltControllerSearchValuesIT.java @@ -144,34 +144,6 @@ void givenBusinessPartnerLowercase_whenCallSearchableValues_thenProperResponse() "BPNL00000003B5MJ")); } - @Test - void givenContractAgreementId_whenCallSearchableValues_thenProperResponse() throws JoseException { - // given - assetsSupport.defaultAssetsStored(); - String fieldName = "contractAgreementId"; - String resultLimit = "100"; - String startWith = "abc1"; - - // then - given() - .header(oAuth2Support.jwtAuthorization(ADMIN)) - .contentType(ContentType.JSON) - .body(asJson(Map.of("fieldName", fieldName, "size", resultLimit, "startWith", startWith))) - .log().all() - .when() - .post("/api/assets/as-built/searchable-values") - .then() - .log().all() - .statusCode(200) - .assertThat() - .body(".", Matchers.containsInRelativeOrder( - "abc1", - "abc10", - "abc11", - "abc12", - "abc13")); - } - @Test void givenBusinessPartnerMixedCase_whenCallSearchableValues_thenProperResponse() throws JoseException { // given diff --git a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/infrastructure/base/IrsCallbackControllerIT.java b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/infrastructure/base/IrsCallbackControllerIT.java index d532610526..73608c19d2 100644 --- a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/infrastructure/base/IrsCallbackControllerIT.java +++ b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/assets/infrastructure/base/IrsCallbackControllerIT.java @@ -25,6 +25,7 @@ import org.eclipse.tractusx.traceability.integration.IntegrationTestSpecification; import org.eclipse.tractusx.traceability.integration.common.support.AssetsSupport; import org.eclipse.tractusx.traceability.integration.common.support.BpnSupport; +import org.eclipse.tractusx.traceability.integration.common.support.ContractRepositoryProvider; import org.eclipse.tractusx.traceability.integration.common.support.IrsApiSupport; import org.eclipse.tractusx.traceability.integration.common.support.repository.AssetAsBuiltSupportRepository; import org.eclipse.tractusx.traceability.integration.common.support.repository.BpnSupportRepository; @@ -54,9 +55,12 @@ class IrsCallbackControllerIT extends IntegrationTestSpecification { @Autowired AssetAsBuiltSupportRepository assetAsBuiltSupportRepository; + @Autowired + ContractRepositoryProvider contractRepositoryProvider; + @Test - void givenNoAssets_whenCallbackReceived_thenSaveThem() throws JoseException { + void givenAssets_whenCallbackReceived_thenSaveThemAndStoreContractAgreementId() throws JoseException { // given bpnSupport.providesBpdmLookup(); @@ -81,7 +85,9 @@ void givenNoAssets_whenCallbackReceived_thenSaveThem() throws JoseException { assertThat(bpnSupportRepository.findAll()).hasSize(1); assetsSupport.assertAssetAsBuiltSize(16); assetsSupport.assertAssetAsPlannedSize(0); - String contractAgreementId = given() + + // Make the API call and store the response + given() .header(oAuth2Support.jwtAuthorization(JwtRole.ADMIN)) .contentType(ContentType.JSON) .log().all() @@ -90,9 +96,7 @@ void givenNoAssets_whenCallbackReceived_thenSaveThem() throws JoseException { .get("/api/assets/as-built/{assetId}") .then() .log().all() - .statusCode(200) - .extract().path("contractAgreementId"); - assertThat(contractAgreementId).isNotEmpty(); + .statusCode(200); } @Test diff --git a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetRepositoryProvider.java b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetRepositoryProvider.java index 78eebc3622..dc5afc0411 100644 --- a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetRepositoryProvider.java +++ b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetRepositoryProvider.java @@ -20,6 +20,7 @@ import org.eclipse.tractusx.traceability.assets.domain.asbuilt.repository.AssetAsBuiltRepository; import org.eclipse.tractusx.traceability.assets.domain.asplanned.repository.AssetAsPlannedRepository; +import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.AssetCallbackRepository; import org.eclipse.tractusx.traceability.assets.infrastructure.base.irs.model.response.factory.IrsResponseAssetMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetsSupport.java b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetsSupport.java index da6558e0d4..0a67761201 100644 --- a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetsSupport.java +++ b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/AssetsSupport.java @@ -19,18 +19,12 @@ package org.eclipse.tractusx.traceability.integration.common.support; import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.traceability.assets.domain.base.model.AssetBase; import org.eclipse.tractusx.traceability.assets.infrastructure.asbuilt.model.AssetAsBuiltEntity; -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.ContractAgreementEntity; -import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.time.Instant; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; @Slf4j @Component @@ -40,7 +34,7 @@ public class AssetsSupport { AssetRepositoryProvider assetRepositoryProvider; @Autowired - ContractRepository contractRepository; + ContractRepositoryProvider contractRepositoryProvider; @Autowired BpnSupport bpnSupport; @@ -56,36 +50,10 @@ public String emptyText() { public void defaultAssetsStored() { oAuth2ApiSupport.oauth2ApiReturnsTechnicalUserToken(); bpnSupport.providesBpdmLookup(); - assetRepositoryProvider.assetAsBuiltRepository().saveAll(assetRepositoryProvider.testdataProvider().readAndConvertAssetsForTests()); - - List mergedAgreements = extractContractAgreementByAssets(); - - contractRepository.saveAll(mergedAgreements); - + List assetBases = assetRepositoryProvider.testdataProvider().readAndConvertAssetsForTests(); + assetRepositoryProvider.assetAsBuiltRepository().saveAll(assetBases); } - private @NotNull List extractContractAgreementByAssets() { - List contractAgreementIdsAsBuilt = assetRepositoryProvider.assetAsBuiltRepository().findAll().stream().map(asBuiltAsset -> ContractAgreementEntity - .builder() - .type(ContractType.ASSET_AS_BUILT) - .contractAgreementId(asBuiltAsset.getContractAgreementId()) - .id(asBuiltAsset.getId()) - .created(Instant.now()) - .build()).collect(Collectors.toUnmodifiableList()); - - List contractAgreementIdsAsPlanned = assetRepositoryProvider.assetAsPlannedRepository().findAll().stream().map(asPlannedAsset -> ContractAgreementEntity - .builder() - .type(ContractType.ASSET_AS_BUILT) - .contractAgreementId(asPlannedAsset.getContractAgreementId()) - .id(asPlannedAsset.getId()) - .created(Instant.now()) - .build()).collect(Collectors.toUnmodifiableList()); - - List mergedAgreements = Stream.concat(contractAgreementIdsAsBuilt.stream(), contractAgreementIdsAsPlanned.stream()) - .toList(); - mergedAgreements.forEach(contractAgreementView -> log.info(contractAgreementView.getContractAgreementId())); - return mergedAgreements; - } public AssetAsBuiltEntity findById(String id) { return AssetAsBuiltEntity.from(assetRepositoryProvider.assetAsBuiltRepository.getAssetById(id)); @@ -98,7 +66,8 @@ public void tractionBatteryCodeAssetsStored() { public void defaultAssetsAsPlannedStored() { bpnSupport.providesBpdmLookup(); - assetRepositoryProvider.assetAsPlannedRepository().saveAll(assetRepositoryProvider.testdataProvider().readAndConvertAssetsAsPlannedForTests()); + List assetBases = assetRepositoryProvider.testdataProvider().readAndConvertAssetsAsPlannedForTests(); + assetRepositoryProvider.assetAsPlannedRepository().saveAll(assetBases); } public void assertAssetAsBuiltSize(int size) { diff --git a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/ContractRepositoryProvider.java b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/ContractRepositoryProvider.java new file mode 100644 index 0000000000..c34a5db5e0 --- /dev/null +++ b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/common/support/ContractRepositoryProvider.java @@ -0,0 +1,24 @@ +package org.eclipse.tractusx.traceability.integration.common.support; + +import lombok.Getter; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractAsBuiltRepositoryImpl; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractAsPlannedRepositoryImpl; +import org.eclipse.tractusx.traceability.contracts.infrastructure.repository.ContractNotificationRepositoryImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Getter +@Component +public class ContractRepositoryProvider { + + @Autowired + ContractAsBuiltRepositoryImpl contractAsBuiltRepository; + + @Autowired + ContractNotificationRepositoryImpl contractNotificationRepository; + + @Autowired + ContractAsPlannedRepositoryImpl contractAsPlannedRepository; + + +} diff --git a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/contracts/ContractsControllerIT.java b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/contracts/ContractsControllerIT.java index fff7cbd8c4..f7eacc02a3 100644 --- a/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/contracts/ContractsControllerIT.java +++ b/tx-backend/src/test/java/org/eclipse/tractusx/traceability/integration/contracts/ContractsControllerIT.java @@ -45,30 +45,6 @@ class ContractsControllerIT extends IntegrationTestSpecification { @Autowired EdcSupport edcSupport; - @Test - void shouldReturnContracts() throws JoseException { - //GIVEN - edcSupport.edcWillReturnContractAgreements(); - edcSupport.edcWillReturnContractAgreementNegotiation(); - assetsSupport.defaultAssetsStored(); - - //WHEN - PageResult contractResponsePageResult = given() - .header(oAuth2Support.jwtAuthorization(ADMIN)) - .contentType(ContentType.JSON) - .log().all() - .when() - .body(new PageableFilterRequest()) - .post("/api/contracts") - .then() - .log().all() - .statusCode(200) - .extract().body().as(new TypeRef<>() { - }); - //THEN - assertThat(contractResponsePageResult.content()).isNotEmpty(); - assertThat(contractResponsePageResult.content().get(0).getPolicy()).isNotEmpty(); - } @Test void shouldReturnOnlyOneContract() throws JoseException { @@ -83,32 +59,7 @@ void shouldReturnOnlyOneContract() throws JoseException { .contentType(ContentType.JSON) .log().all() .when() - .body(PageableFilterRequest.builder().searchCriteriaRequestParam(SearchCriteriaRequestParam.builder().filter(List.of("id,EQUAL,urn:uuid:d387fa8e-603c-42bd-98c3-4d87fef8d2bb,AND")).build()).build()) - .post("/api/contracts") - .then() - .log().all() - .statusCode(200) - .extract().body().as(new TypeRef<>() { - }); - //THEN - assertThat(contractResponsePageResult.content()).isNotEmpty(); - assertThat(contractResponsePageResult.content().get(0).getCounterpartyAddress()).isNotEmpty(); - } - - @Test - void shouldReturnContractsWithNotificationType() throws JoseException { - //GIVEN - edcSupport.edcWillReturnOnlyOneContractAgreement(); - edcSupport.edcWillReturnContractAgreementNegotiation(); - assetsSupport.defaultAssetsStored(); - - //WHEN - PageResult contractResponsePageResult = given() - .header(oAuth2Support.jwtAuthorization(ADMIN)) - .contentType(ContentType.JSON) - .log().all() - .when() - .body(PageableFilterRequest.builder().searchCriteriaRequestParam(SearchCriteriaRequestParam.builder().filter(List.of("contractType,EQUAL,ASSET_AS_BUILT,AND")).build()).build()) + .body(PageableFilterRequest.builder().searchCriteriaRequestParam(SearchCriteriaRequestParam.builder().filter(List.of("contractId,STARTS_WITH,abc1,AND")).build()).build()) .post("/api/contracts") .then() .log().all() diff --git a/tx-backend/testdata/CX_Testdata_MessagingTest_v0.0.14.json b/tx-backend/testdata/CX_Testdata_MessagingTest_v0.0.14.json index 6385c0a5a1..2c8c028b95 100644 --- a/tx-backend/testdata/CX_Testdata_MessagingTest_v0.0.14.json +++ b/tx-backend/testdata/CX_Testdata_MessagingTest_v0.0.14.json @@ -16,7 +16,7 @@ "odrl:and": [ { "@type": "Constraint", - "odrl:leftOperand": "cx-policy:FrameworkAgreement", + "odrl:leftOperand": "https://w3id.org/catenax/policy/FrameworkAgreement", "odrl:operator": { "@id": "odrl:eq" }, @@ -24,7 +24,7 @@ }, { "@type": "Constraint", - "odrl:leftOperand": "cx-policy:UsagePurpose", + "odrl:leftOperand": "https://w3id.org/catenax/policy/UsagePurpose", "odrl:operator": { "@id": "odrl:eq" }, diff --git a/tx-models/src/main/java/assets/response/base/response/AssetBaseResponse.java b/tx-models/src/main/java/assets/response/base/response/AssetBaseResponse.java index f31f142043..83d7f899cb 100644 --- a/tx-models/src/main/java/assets/response/base/response/AssetBaseResponse.java +++ b/tx-models/src/main/java/assets/response/base/response/AssetBaseResponse.java @@ -95,8 +95,4 @@ public class AssetBaseResponse { } """) private String tombstone; - - - @Schema(example = "TODO") - private String contractAgreementId; } diff --git a/tx-models/src/main/java/contract/response/ContractResponse.java b/tx-models/src/main/java/contract/response/ContractResponse.java index dd169666e4..8722e3d825 100644 --- a/tx-models/src/main/java/contract/response/ContractResponse.java +++ b/tx-models/src/main/java/contract/response/ContractResponse.java @@ -48,5 +48,6 @@ public class ContractResponse { @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; + private String globalAssetId; }