Skip to content

Commit

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

feature(chore):985 Fixed contractagreement view script.
  • Loading branch information
ds-mwesener authored Jun 26, 2024
2 parents 1924b41 + a3935fa commit bef1879
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ public interface AssetBaseService {
List<String> getDistinctFilterValues(String fieldName, String startWith, Integer size, Owner owner, List<String> inAssetIds);

List<String> getAssetIdsInImportState(ImportState... importStates);

List<AssetBase> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public interface AssetRepository {
List<AssetBase> findByImportStateIn(ImportState... importStates);

void updateImportStateAndNoteForAssets(ImportState importState, String importNote, List<String> assetIds);

List<AssetBase> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public List<String> getAssetIdsInImportState(ImportState... importStates) {
return getAssetRepository().findByImportStateIn(importStates).stream().map(AssetBase::getId).toList();
}

@Override
public List<AssetBase> findAll() {
return getAssetRepository().findAll();
}

private boolean isSupportedEnumType(String fieldName) {
return SUPPORTED_ENUM_FIELDS.contains(fieldName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public void updateImportStateAndNoteForAssets(ImportState importState, String im
jpaAssetAsBuiltRepository.saveAll(assets);
}

@Transactional
@Override
public List<AssetBase> findAll() {
return jpaAssetAsBuiltRepository.findAll().stream()
.map(AssetAsBuiltEntity::toDomain).toList();
}

@Override
public Optional<AssetBase> findById(String assetId) {
return jpaAssetAsBuiltRepository.findById(assetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.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;
Expand Down Expand Up @@ -80,6 +81,13 @@ public AssetBase getAssetByChildId(String childId) {
.orElseThrow(() -> new AssetNotFoundException("Child Asset Not Found"));
}

@Transactional
@Override
public List<AssetBase> findAll() {
return jpaAssetAsPlannedRepository.findAll().stream()
.map(AssetAsPlannedEntity::toDomain).toList();
}


@Override
public PageResult<AssetBase> getAssets(Pageable pageable, SearchCriteria searchCriteria) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,55 @@

package org.eclipse.tractusx.traceability.common.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.traceability.policies.domain.PolicyRepository;
import org.eclipse.tractusx.traceability.assets.application.base.service.AssetBaseService;
import org.eclipse.tractusx.traceability.assets.domain.base.model.AssetBase;
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;


@Slf4j
@Component
@Profile(NOT_INTEGRATION_TESTS)
@RequiredArgsConstructor
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 asPlannedService,
@Qualifier("assetAsPlannedServiceImpl") AssetBaseService asBuiltService,
EdcNotificationContractService edcNotificationContractService,
ContractService contractService) {
this.policyRepository = policyRepository;
this.asPlannedService = asPlannedService;
this.asBuiltService = asBuiltService;
this.edcNotificationContractService = edcNotificationContractService;
this.contractService = contractService;
}

private final EdcNotificationContractService edcNotificationContractService;
private static final List<CreateNotificationContractRequest> NOTIFICATION_CONTRACTS = List.of(
new CreateNotificationContractRequest(NotificationType.QUALITY_ALERT, NotificationMethod.UPDATE),
Expand Down Expand Up @@ -82,4 +106,44 @@ 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 {
List<AssetBase> asBuilt = asBuiltService.findAll();
List<AssetBase> asPlanned = asPlannedService.findAll();

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

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

List<ContractAgreement> mergedAgreements = Stream.concat(contractAgreementIdsAsBuilt.stream(), contractAgreementIdsAsPlanned.stream())
.toList();

contractService.saveAll(mergedAgreements);


} catch (Exception exception) {
log.error("Failed to insert contracts: ", exception);
}
log.info("on ApplicationReadyEvent insert into contracts successfully done.");
});
executor.shutdown();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.tractusx.traceability.common.model.PageResult;
import org.eclipse.tractusx.traceability.common.request.PageableFilterRequest;
import org.eclipse.tractusx.traceability.contracts.domain.model.Contract;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractAgreement;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;

import java.util.List;
Expand All @@ -30,4 +31,6 @@ public interface ContractService {
PageResult<Contract> getContracts(PageableFilterRequest pageableFilterRequest);

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

void saveAll(List<ContractAgreement> contractAgreements);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/********************************************************************************
* 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.model;

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.ContractAgreementView;

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

@Getter
@Setter
@Builder
public class ContractAgreement {

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

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

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

import java.util.List;
Expand All @@ -32,4 +33,6 @@ public interface ContractRepository {
PageResult<Contract> getContractsByPageable(Pageable pageable, SearchCriteria searchCriteria);

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

void saveAll(List<ContractAgreementView> contractAgreements);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import org.eclipse.tractusx.traceability.contracts.application.mapper.ContractFieldMapper;
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.infrastructure.model.ContractAgreementView;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

Expand All @@ -54,4 +56,9 @@ public PageResult<Contract> getContracts(PageableFilterRequest pageableFilterReq
public void saveContractAgreements(List<String> contractAgreementIds, ContractType contractType) throws ContractAgreementException {
contractRepository.saveAllContractAgreements(contractAgreementIds, contractType);
}

@Override
public void saveAll(List<ContractAgreement> contractAgreements) {
contractRepository.saveAll(ContractAgreement.toEntityList(contractAgreements));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
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 java.time.Instant;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@Entity
@SuperBuilder
@Table(name = "contract_agreement_view")
@Table(name = "contract_agreement")
public class ContractAgreementView {

@Id
Expand All @@ -45,4 +47,18 @@ public class ContractAgreementView {
@Enumerated(EnumType.STRING)
private ContractType type;
private Instant created;


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

public static List<ContractAgreement> toDomainList(List<ContractAgreementView> contractAgreementList) {
return contractAgreementList.stream().map(ContractAgreementView::toDomain).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
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.ContractAgreementView;
import org.eclipse.tractusx.traceability.contracts.domain.model.ContractType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -102,6 +102,11 @@ public void saveAllContractAgreements(List<String> contractAgreementIds, Contrac
contractAgreementInfoViewRepository.saveAll(contractAgreementViewsUpdated);
}

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

private List<Contract> fetchEdcContractAgreements(List<ContractAgreementView> contractAgreementInfoViews) throws ContractAgreementException {
List<String> contractAgreementIds = contractAgreementInfoViews.stream().map(ContractAgreementView::getContractAgreementId).toList();
log.info("Trying to fetch contractAgreementIds from EDC: " + contractAgreementIds);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
-- Drop the view if it exists
DROP VIEW IF EXISTS contract_agreement_view;

ALTER VIEW contract_agreement_view RENAME COLUMN asset_type TO type;

CREATE OR REPLACE VIEW contract_agreement_view (id, contract_agreement_id, type, created) AS
SELECT *
FROM (
(SELECT assets_as_built.id,
contract_agreement_id,
'ASSET_AS_BUILT' AS type,
created
FROM assets_as_built
WHERE contract_agreement_id IS NOT NULL)
UNION ALL
(SELECT assets_as_planned.id,
contract_agreement_id,
'ASSET_AS_PLANNED' AS type,
created
FROM assets_as_planned
WHERE contract_agreement_id IS NOT NULL)
) results
ORDER BY created DESC;
-- Create the table
CREATE TABLE contract_agreement (
id varchar(255) PRIMARY KEY,
contract_agreement_id varchar(255),
type VARCHAR(255),
created TIMESTAMP
);
Loading

0 comments on commit bef1879

Please sign in to comment.