From cf57abe68032d7821e951518ec8c54385b94e7ee Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 31 Jul 2023 17:55:07 +0200 Subject: [PATCH 1/9] fix: frontend now sending materialNumberCustomer to update-partner-product-stock endpoint --- .../stock/controller/ProductStockRequestApiController.java | 1 - frontend/.env | 2 +- frontend/src/views/stock/PartnerStockSFC.vue | 2 +- frontend/src/views/stock/StockTableSFC.vue | 1 + 4 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java index eca4b2ea..9c51c6ad 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java @@ -25,7 +25,6 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.controller.exception.RequestIdAlreadyUsedException; import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; diff --git a/frontend/.env b/frontend/.env index 15dd2485..370ef96a 100644 --- a/frontend/.env +++ b/frontend/.env @@ -7,4 +7,4 @@ VITE_ENDPOINT_MATERIAL_STOCKS=stockView/material-stocks VITE_ENDPOINT_PRODUCT_STOCKS=stockView/product-stocks VITE_ENDPOINT_CUSTOMER=stockView/customer?materialUuid= VITE_ENDPOINT_PARTNER_PRODUCT_STOCKS=stockView/partner-product-stocks -VITE_ENDPOINT_UPDATE_PARTNER_PRODUCT_STOCK=stockView/update-partner-product-stock?materialUuid= +VITE_ENDPOINT_UPDATE_PARTNER_PRODUCT_STOCK=stockView/update-partner-product-stock?ownMaterialNumber= diff --git a/frontend/src/views/stock/PartnerStockSFC.vue b/frontend/src/views/stock/PartnerStockSFC.vue index 2cc18ea9..a8b91c68 100644 --- a/frontend/src/views/stock/PartnerStockSFC.vue +++ b/frontend/src/views/stock/PartnerStockSFC.vue @@ -97,7 +97,7 @@ export default { .catch(err => console.log(err)); }, updateMaterialOrProduct() { - fetch(this.backendURL + this.endpointUpdatePartnerProductStock + this.materialUuid) + fetch(this.backendURL + this.endpointUpdatePartnerProductStock + this.selectedMaterialOrProductId) .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.log(err)); diff --git a/frontend/src/views/stock/StockTableSFC.vue b/frontend/src/views/stock/StockTableSFC.vue index 65eaa98a..ae4a5751 100644 --- a/frontend/src/views/stock/StockTableSFC.vue +++ b/frontend/src/views/stock/StockTableSFC.vue @@ -74,6 +74,7 @@ export default { return { selectedStockId: "", selectedStockUuid: "", + materialNumberCustomer : "" }; }, methods: { From 03cd94e2c0cd107d68b09f14bfd3977e17505e83 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Tue, 1 Aug 2023 08:24:24 +0200 Subject: [PATCH 2/9] fix: request/response endpoint now use requestId as key --- ...{SuccessfullRequestDto.java => SuccessfulRequestDto.java} | 4 ++-- .../stock/controller/ProductStockRequestApiController.java | 4 ++-- .../stock/controller/ProductStockResponseApiController.java | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/{SuccessfullRequestDto.java => SuccessfulRequestDto.java} (94%) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/SuccessfullRequestDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/SuccessfulRequestDto.java similarity index 94% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/SuccessfullRequestDto.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/SuccessfulRequestDto.java index 9cb7cd3f..4195dba9 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/SuccessfullRequestDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/SuccessfulRequestDto.java @@ -30,8 +30,8 @@ @ToString @NoArgsConstructor @AllArgsConstructor -public class SuccessfullRequestDto { +public class SuccessfulRequestDto { - @JsonProperty("request-id") + @JsonProperty("requestId") private UUID requestId; } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java index 9c51c6ad..6cb0bba3 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java @@ -30,7 +30,7 @@ import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.RequestDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfullRequestDto; +import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfulRequestDto; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestApiService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; @@ -128,7 +128,7 @@ public ResponseEntity postRequest(@RequestBody String requestBody) { respondAsyncThread.start(); // if the request has been correctly taken over, return 202 - return ResponseEntity.status(HttpStatusCode.valueOf(202)).body(new SuccessfullRequestDto(requestId)); + return ResponseEntity.status(HttpStatusCode.valueOf(202)).body(new SuccessfulRequestDto(requestId)); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java index 8b9acb11..eda21b77 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java @@ -25,7 +25,7 @@ import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfullRequestDto; +import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfulRequestDto; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.ResponseApiService; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; @@ -39,7 +39,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; @@ -98,7 +97,7 @@ public ResponseEntity postResponse(@RequestBody String body) { responseApiService.consumeResponse(productStockResponseDto); // if the request has been correctly taken over, return 202 - return ResponseEntity.status(HttpStatusCode.valueOf(202)).body(new SuccessfullRequestDto(requestId)); + return ResponseEntity.status(HttpStatusCode.valueOf(202)).body(new SuccessfulRequestDto(requestId)); } } From e8f01c8984b8d0b2700b2412c4727d3b64ef1d8c Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Tue, 1 Aug 2023 19:51:15 +0200 Subject: [PATCH 3/9] feat: added MaterialPartnerRelation and accompanying services --- .../domain/model/MaterialPartnerRelation.java | 85 +++++++ .../MaterialPartnerRelationRepository.java | 22 ++ .../MaterialPartnerRelationService.java | 36 +++ .../MaterialPartnerRelationServiceImpl.java | 211 ++++++++++++++++++ 4 files changed, 354 insertions(+) create mode 100644 backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/MaterialPartnerRelation.java create mode 100644 backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java create mode 100644 backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java create mode 100644 backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/MaterialPartnerRelation.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/MaterialPartnerRelation.java new file mode 100644 index 00000000..a05e956f --- /dev/null +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/MaterialPartnerRelation.java @@ -0,0 +1,85 @@ +package org.eclipse.tractusx.puris.backend.masterdata.domain.model; + +import jakarta.persistence.*; +import lombok.*; + +import java.io.Serializable; +import java.util.Objects; +import java.util.UUID; + +@Entity +@Getter +@Setter +@AllArgsConstructor +public class MaterialPartnerRelation { + + @EmbeddedId + Key key; + + private String partnerMaterialNumber; + private boolean partnerSuppliesMaterial; + private boolean partnerBuysMaterial; + + @ManyToOne + @MapsId("ownMaterialNumber") + @JoinColumn(name = "material_ownMaterialNumber") + Material material; + + @ManyToOne + @MapsId("uuid") + @JoinColumn(name = "partner_uuid") + Partner partner; + + public MaterialPartnerRelation() { + this.key = new Key(); + } + + public MaterialPartnerRelation(Material material, Partner partner, String partnerMaterialNumber, boolean partnerSupplies, boolean partnerBuys) { + this.material = material; + this.partner = partner; + this.key = new Key(material.getOwnMaterialNumber(), partner.getUuid()); + this.partnerMaterialNumber = partnerMaterialNumber; + this.partnerSuppliesMaterial = partnerSupplies; + this.partnerBuysMaterial = partnerBuys; + } + + @Override + public String toString() { + return "MaterialPartnerRelation{" + + "key=" + key + + ", partnerMaterialNumber='" + partnerMaterialNumber + '\'' + + ", partnerSuppliesMaterial=" + partnerSuppliesMaterial + + ", partnerBuysMaterial=" + partnerBuysMaterial + + ", material=" + material.getOwnMaterialNumber() + + ", partner=" + partner.getBpnl() + + '}'; + } + + @Embeddable + @Getter + @Setter + @AllArgsConstructor + @NoArgsConstructor + @ToString + public static class Key implements Serializable { + + @Column(name = "material_ownMaterialNumber") + private String ownMaterialNumber; + + @Column(name = "partner_uuid") + private UUID partnerUuid; + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Key)) return false; + Key key = (Key) o; + return Objects.equals(ownMaterialNumber, key.ownMaterialNumber) && Objects.equals(partnerUuid, key.partnerUuid); + } + + @Override + public int hashCode() { + return Objects.hash(ownMaterialNumber, partnerUuid); + } + } +} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java new file mode 100644 index 00000000..404d481e --- /dev/null +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java @@ -0,0 +1,22 @@ +package org.eclipse.tractusx.puris.backend.masterdata.domain.repository; + +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.UUID; + +@Repository +public interface MaterialPartnerRelationRepository extends JpaRepository { + + List findAllByPartner_Uuid(UUID partnerUuid); + + List findAllByMaterial_OwnMaterialNumber(String ownMaterialNumber); + + List findAllByMaterial_OwnMaterialNumberAndPartnerSuppliesMaterialIsTrue(String ownMaterialNumber); + + List findAllByMaterial_OwnMaterialNumberAndPartnerBuysMaterialIsTrue(String ownMaterialNumber); + + List findAllByPartnerMaterialNumber(String partnerMaterialNumber); +} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java new file mode 100644 index 00000000..0a203570 --- /dev/null +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java @@ -0,0 +1,36 @@ +package org.eclipse.tractusx.puris.backend.masterdata.logic.service; + +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + + +public interface MaterialPartnerRelationService { + MaterialPartnerRelation create(MaterialPartnerRelation materialPartnerRelation); + + MaterialPartnerRelation update(MaterialPartnerRelation materialPartnerRelation); + + MaterialPartnerRelation find(Material material, Partner partner); + + List findAll(); + + Map getBPNL_To_MaterialNumberMap(String ownMaterialNumber); + + MaterialPartnerRelation find(String ownMaterialNumber, UUID partnerUuid); + + List findAllSuppliersForOwnMaterialNumber(String ownMaterialNumber); + + List findAllCustomersForOwnMaterialNumber(String ownMaterialNumber); + + List findAllSuppliersForMaterial(Material material); + + List findAllByPartnerMaterialNumber(String partnerMaterialNumber); + + boolean partnerSuppliesMaterial(Material material, Partner partner); + + boolean partnerOrdersProduct(Material material, Partner partner); +} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java new file mode 100644 index 00000000..bf2825d7 --- /dev/null +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java @@ -0,0 +1,211 @@ +package org.eclipse.tractusx.puris.backend.masterdata.logic.service; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.puris.backend.common.api.logic.service.VariablesService; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.MaterialPartnerRelationRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +@AllArgsConstructor +@Service +@Slf4j +public class MaterialPartnerRelationServiceImpl implements MaterialPartnerRelationService { + + @Autowired + private MaterialPartnerRelationRepository mprRepository; + + @Autowired + private VariablesService variablesService; + + + /** + * Stores the given relation to the database. + * @param materialPartnerRelation + * @return the stored relation or null, if the given relation was already in existence. + */ + @Override + public MaterialPartnerRelation create(MaterialPartnerRelation materialPartnerRelation) { + var searchResult = find(materialPartnerRelation.getMaterial(), materialPartnerRelation.getPartner()); + if (searchResult == null) { + return mprRepository.save(materialPartnerRelation); + } + log.error("Could not create MaterialPartnerRelation, " + materialPartnerRelation.getKey() + " already exists"); + return null; + } + + /** + * Updates an existing MaterialPartnerRelation + * @param materialPartnerRelation + * @return the updated relation or null, if the given relation didn't exist before. + */ + @Override + public MaterialPartnerRelation update(MaterialPartnerRelation materialPartnerRelation) { + var foundEntity = mprRepository.findById(materialPartnerRelation.getKey()); + if (foundEntity.isPresent()) { + return mprRepository.save(materialPartnerRelation); + } + log.error("Could not update MaterialPartnerRelation, " + materialPartnerRelation.getKey() + " didn't exist before"); + return null; + } + + /** + * Find the MaterialPartnerRelation containing the material and the partner. + * @param material + * @param partner + * @return the relation, if it exists or else null; + */ + @Override + public MaterialPartnerRelation find(Material material, Partner partner) { + return find(material.getOwnMaterialNumber(), partner.getUuid()); + } + + /** + * + * @return a list of all existing MaterialPartnerRelations + */ + @Override + public List findAll() { + return mprRepository.findAll(); + } + + /** + * Generates a Map of key-value-pairs. Each key represents the BPNL of a + * partner (and yourself), each corresponding value is the materialNumber + * that the owner of the BPNL is using in his own house to define the given Material. + * @param ownMaterialNumber + * @return a Map with the content described above or an empty map if no entries with the given ownMaterialNumber could be found. + */ + @Override + public Map getBPNL_To_MaterialNumberMap(String ownMaterialNumber) { + var relationsList = mprRepository.findAllByMaterial_OwnMaterialNumber(ownMaterialNumber); + HashMap output = new HashMap<>(); + if (relationsList.isEmpty()) { + return output; + } + output.put(variablesService.getOwnBpnl(), ownMaterialNumber); + for (var relation : relationsList) { + output.put(relation.getPartner().getBpnl(), relation.getPartnerMaterialNumber()); + } + return output; + } + + /** + * Find the MaterialPartnerRelation containing the material with the given + * ownMaterialNumber and the uuid referencing a partner in your database. + * @param ownMaterialNumber + * @param partnerUuid + * @return the relation, if it exists or else null + */ + @Override + public MaterialPartnerRelation find(String ownMaterialNumber, UUID partnerUuid) { + var searchResult = mprRepository.findById(new MaterialPartnerRelation.Key(ownMaterialNumber, partnerUuid)); + if (searchResult.isPresent()) { + return searchResult.get(); + } + return null; + } + + /**Returns a list containing all Partners that are registered as suppliers for + * the material with the given ownMaterialNumber + * + * @param ownMaterialNumber + * @return a list of partners as described above + */ + @Override + public List findAllSuppliersForOwnMaterialNumber(String ownMaterialNumber) { + return mprRepository.findAllByMaterial_OwnMaterialNumberAndPartnerSuppliesMaterialIsTrue(ownMaterialNumber) + .stream() + .map(mpr -> mpr.getPartner()) + .collect(Collectors.toList()); + } + + /**Returns a list containing all Partners that are registered as customers for + * the material with the given ownMaterialNumber + * + * @param ownMaterialNumber + * @return a list of partners as described above + */ + @Override + public List findAllCustomersForOwnMaterialNumber(String ownMaterialNumber) { + return mprRepository.findAllByMaterial_OwnMaterialNumberAndPartnerBuysMaterialIsTrue(ownMaterialNumber) + .stream() + .map(mpr -> mpr.getPartner()) + .collect(Collectors.toList()); + } + + /**Returns a list containing all Partners that are registered as suppliers for + * the material with the given material + * + * @param material + * @return a list of partners as described above + */ + @Override + public List findAllSuppliersForMaterial(Material material) { + return findAllSuppliersForOwnMaterialNumber(material.getOwnMaterialNumber()); + } + + /**Returns a list containing all Partners that are registered as customers for + * the material with the given material + * + * @param material + * @return a list of partners as described above + */ + public List findAllCustomersForMaterial(Material material) { + return findAllCustomersForOwnMaterialNumber(material.getOwnMaterialNumber()); + } + + /** + * Returns a list of all Materials, for which a MaterialPartnerRelation exists, + * where the partner is using the given partnerMaterialNumber. + * @param partnerMaterialNumber + * @return a list of Materials + */ + @Override + public List findAllByPartnerMaterialNumber(String partnerMaterialNumber) { + return mprRepository.findAllByPartnerMaterialNumber(partnerMaterialNumber) + .stream() + .map(mpr -> mpr.getMaterial()) + .collect(Collectors.toList()); + } + + /** + * + * @param material + * @param partner + * @return true, if the given partner is registered as supplier for the given material, else false + */ + @Override + public boolean partnerSuppliesMaterial (Material material, Partner partner) { + if (material.isMaterialFlag()) { + MaterialPartnerRelation mpr = find(material, partner); + return mpr != null && mpr.isPartnerSuppliesMaterial(); + } + return false; + } + + /** + * + * @param material + * @param partner + * @return true, if the given partner is registered as customer for the given material, else false + */ + @Override + public boolean partnerOrdersProduct(Material material, Partner partner) { + if (material.isProductFlag()) { + MaterialPartnerRelation mpr = find(material,partner); + return mpr != null && mpr.isPartnerBuysMaterial(); + } + return false; + } + +} From a0354645cd5627c4406bf1f78036c652cadfc148 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Tue, 1 Aug 2023 19:52:00 +0200 Subject: [PATCH 4/9] refactor: adjustments in existing classes --- .../DataInjectionCommandLineRunner.java | 182 ++++++++++-------- .../api/logic/service/VariablesService.java | 4 + .../masterdata/domain/model/Material.java | 77 +++----- .../masterdata/domain/model/Partner.java | 35 ++-- .../domain/repository/MaterialRepository.java | 9 +- .../domain/repository/PartnerRepository.java | 9 - .../masterdata/logic/dto/MaterialDto.java | 34 ++-- .../masterdata/logic/dto/PartnerDto.java | 33 ---- .../logic/service/MaterialService.java | 15 +- .../logic/service/MaterialServiceImpl.java | 56 ++---- .../logic/service/PartnerService.java | 6 +- .../logic/service/PartnerServiceImpl.java | 46 +++-- .../stock/controller/StockController.java | 39 ++-- .../domain/model/PartnerProductStock.java | 23 ++- .../stock/domain/model/ProductStock.java | 24 +-- .../backend/stock/domain/model/Stock.java | 2 +- .../repository/MaterialStockRepository.java | 3 +- .../PartnerProductStockRepository.java | 10 +- .../repository/ProductStockRepository.java | 6 +- .../logic/adapter/ProductStockSammMapper.java | 71 +++++-- .../logic/service/MaterialStockService.java | 5 +- .../service/MaterialStockServiceImpl.java | 40 +++- .../service/PartnerProductStockService.java | 12 +- .../PartnerProductStockServiceImpl.java | 34 +++- .../ProductStockRequestApiServiceImpl.java | 38 +++- .../ProductStockResponseApiServiceImpl.java | 34 ++-- .../logic/service/ProductStockService.java | 4 +- .../service/ProductStockServiceImpl.java | 52 +++-- 28 files changed, 500 insertions(+), 403 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java index eb44226b..0d14093c 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java @@ -31,7 +31,9 @@ import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.MaterialStock; @@ -72,6 +74,9 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner { @Autowired private PartnerService partnerService; + @Autowired + private MaterialPartnerRelationService mprService; + @Autowired private MaterialStockService materialStockService; @@ -96,6 +101,9 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner { private ObjectMapper objectMapper; + private final String semiconductorMatNbrCustomer = "MNR-7307-AU340474.002"; + private final String semiconductorMatNbrSupplier = "MNR-8101-ID146955.001"; + public DataInjectionCommandLineRunner(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } @@ -121,11 +129,7 @@ public void run(String... args) throws Exception { */ private void setupCustomerRole() throws JsonProcessingException { Partner supplierPartner = createAndGetSupplierPartner(); - Material semiconductorMaterial = getNewSemiconductorMaterial(); - semiconductorMaterial.addPartnerToSuppliedByPartners(supplierPartner); - // adjust flags for customer role - semiconductorMaterial.setMaterialFlag(true); - semiconductorMaterial.setProductFlag(true); + Material semiconductorMaterial = getNewSemiconductorMaterialForCustomer(); semiconductorMaterial = materialService.create(semiconductorMaterial); log.info(String.format("Created material: %s", semiconductorMaterial)); @@ -134,47 +138,60 @@ private void setupCustomerRole() throws JsonProcessingException { log.info(String.format("UUID of supplier partner: %s", supplierPartner.getUuid())); supplierPartner = partnerService.findByUuid(supplierPartner.getUuid()); log.info(String.format("Found supplier partner: %s", supplierPartner)); - log.info(String.format("Relationship to material: %s", supplierPartner.getSuppliesMaterials())); + + + MaterialPartnerRelation semiconductorPartnerRelation = new MaterialPartnerRelation(semiconductorMaterial, + supplierPartner, semiconductorMatNbrSupplier, true, false); + mprService.create(semiconductorPartnerRelation); + semiconductorPartnerRelation = mprService.find(semiconductorMaterial, supplierPartner); + log.info("Found Relation: " + semiconductorPartnerRelation); // customer + material Partner nonScenarioCustomer = createAndGetNonScenarioCustomer(); Material centralControlUnitEntity = getNewCentralControlUnitMaterial(); - centralControlUnitEntity.addPartnerToOrderedByPartners(nonScenarioCustomer); centralControlUnitEntity = materialService.create(centralControlUnitEntity); log.info(String.format("Created Product: %s", centralControlUnitEntity)); - List productsFound = materialService.findAllProducts(); - log.info(String.format("Found Products: %s", productsFound)); - - centralControlUnitEntity = - materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); - log.info(String.format("Found product by materialNumber customer: %s", - centralControlUnitEntity)); - nonScenarioCustomer = partnerService.findByUuid(nonScenarioCustomer.getUuid()); - log.info(String.format("Relationship to product: %s", - nonScenarioCustomer.getOrdersProducts())); - centralControlUnitEntity = - materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); - log.info(String.format("Found product by materialNumber customer: %s", - centralControlUnitEntity)); + MaterialPartnerRelation ccuPartnerRelation = new MaterialPartnerRelation(centralControlUnitEntity, + nonScenarioCustomer, "MNR-4177-C", false, true); + ccuPartnerRelation = mprService.create(ccuPartnerRelation); + log.info("Found Relation: " + ccuPartnerRelation); - Material existingMaterial = - materialService.findByUuid(semiconductorMaterial.getUuid()); - log.info(String.format("Found existingMaterial by uuid: %s", - existingMaterial)); + log.info("All stored Relations: " + mprService.findAll()); - Material existingProduct = - materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); - log.info(String.format("Found existingProduct by customer number: %s", - existingProduct)); - - List existingProducts = - materialService.findAllProducts(); - log.info(String.format("Found existingProducts by product flag true: %s", - existingProducts)); + List productsFound = materialService.findAllProducts(); + log.info(String.format("Found Products: %s", productsFound)); - log.info(String.format("Relationship centralControlUnitEntity -> orderedByPartners: %s", - centralControlUnitEntity.getOrderedByPartners().toString())); +// centralControlUnitEntity = +// materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); +// log.info(String.format("Found product by materialNumber customer: %s", +// centralControlUnitEntity)); +// nonScenarioCustomer = partnerService.findByUuid(nonScenarioCustomer.getUuid()); +// log.info(String.format("Relationship to product: %s", +// nonScenarioCustomer.getOrdersProducts())); +// +// centralControlUnitEntity = +// materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); +// log.info(String.format("Found product by materialNumber customer: %s", +// centralControlUnitEntity)); +// +// Material existingMaterial = +// materialService.findByUuid(semiconductorMaterial.getUuid()); +// log.info(String.format("Found existingMaterial by uuid: %s", +// existingMaterial)); +// +// Material existingProduct = +// materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); +// log.info(String.format("Found existingProduct by customer number: %s", +// existingProduct)); +// +// List existingProducts = +// materialService.findAllProducts(); +// log.info(String.format("Found existingProducts by product flag true: %s", +// existingProducts)); +// +// log.info(String.format("Relationship centralControlUnitEntity -> orderedByPartners: %s", +// centralControlUnitEntity.getOrderedByPartners().toString())); // Create Material Stock MaterialStock materialStockEntity = new MaterialStock( @@ -185,12 +202,12 @@ private void setupCustomerRole() throws JsonProcessingException { ); materialStockEntity = materialStockService.create(materialStockEntity); log.info(String.format("Created materialStock: %s", materialStockEntity)); - List foundMaterialStocks = - materialStockService.findAllByMaterialNumberCustomer(semiconductorMaterial.getMaterialNumberCustomer()); - log.info(String.format("Found materialStock: %s", foundMaterialStocks)); +// List foundMaterialStocks = +// materialStockService.findAllByMaterialNumberCustomer(semiconductorMaterial.getMaterialNumberCustomer()); +// log.info(String.format("Found materialStock: %s", foundMaterialStocks)); // Create PartnerProductStock - semiconductorMaterial = materialService.findByUuid(semiconductorMaterial.getUuid()); + semiconductorMaterial = materialService.findByOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber()); PartnerProductStock partnerProductStockEntity = new PartnerProductStock( semiconductorMaterial, 20, @@ -198,27 +215,35 @@ private void setupCustomerRole() throws JsonProcessingException { new Date(), supplierPartner ); - partnerProductStockEntity = partnerProductStockService.create(partnerProductStockEntity); log.info(String.format("Created partnerProductStock: %s", partnerProductStockEntity)); - ProductStockDto productStockDto = modelMapper.map(partnerProductStockEntity, - ProductStockDto.class); - ProductStockSammDto productStockSammDto = productStockSammMapper.toSamm(productStockDto); - log.info(objectMapper.writeValueAsString(productStockSammDto)); + partnerProductStockEntity = partnerProductStockService.create(partnerProductStockEntity); + ProductStockSammDto productStockSammDto = productStockSammMapper.toSamm(partnerProductStockEntity); + log.info("SAMM-DTO:\n" + objectMapper.writeValueAsString(productStockSammDto)); } /** * Generates an initial set of data for a supplier within the demonstration context. */ private void setupSupplierRole() { Partner customerPartner = createAndGetCustomerPartner(); - Material semiconductorMaterial = getNewSemiconductorMaterial(); - semiconductorMaterial.addPartnerToOrderedByPartners(customerPartner); + Material semiconductorMaterial = getNewSemiconductorMaterialForSupplier(); +// semiconductorMaterial.addPartnerToOrderedByPartners(customerPartner); semiconductorMaterial = materialService.create(semiconductorMaterial); log.info(String.format("Created product: %s", semiconductorMaterial)); + MaterialPartnerRelation semiconductorPartnerRelation = new MaterialPartnerRelation(semiconductorMaterial, + customerPartner, semiconductorMatNbrCustomer, false, true); + semiconductorPartnerRelation = mprService.create(semiconductorPartnerRelation); + + log.info("Created Relation " + semiconductorPartnerRelation); + + semiconductorPartnerRelation = mprService.find(semiconductorMaterial, customerPartner); + + log.info("Found Relation " + semiconductorPartnerRelation); + List materialsFound = materialService.findAllProducts(); log.info(String.format("Found product: %s", materialsFound)); log.info(String.format("Found customer partner: %s", customerPartner)); - log.info(String.format("Relationship to material: %s", customerPartner.getOrdersProducts())); +// log.info(String.format("Relationship to material: %s", customerPartner.getOrdersProducts())); ProductStock productStockEntity = new ProductStock( semiconductorMaterial, @@ -229,13 +254,13 @@ private void setupSupplierRole() { ); productStockEntity = productStockService.create(productStockEntity); log.info(String.format("Created productStock: %s", productStockEntity.toString())); - List foundProductStocks = - productStockService - .findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl( - semiconductorMaterial.getMaterialNumberCustomer(), - customerPartner.getBpnl()); - log.info(String.format("Found productStocks by material number and allocated to customer " + - "bpnl: %s", foundProductStocks)); +// List foundProductStocks = +// productStockService +// .findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl( +// semiconductorMaterial.getMaterialNumberCustomer(), +// customerPartner.getBpnl()); +// log.info(String.format("Found productStocks by material number and allocated to customer " + +// "bpnl: %s", foundProductStocks)); } @@ -247,8 +272,6 @@ private void setupSupplierRole() { private Partner createAndGetCustomerPartner() { Partner customerPartnerEntity = new Partner( "Scenario Customer", - true, - false, "http://sokrates-controlplane:8084/api/v1/ids", "BPNL4444444444XX", "BPNS4444444444XX" @@ -268,8 +291,6 @@ private Partner createAndGetCustomerPartner() { private Partner createAndGetSupplierPartner() { Partner supplierPartnerEntity = new Partner( "Scenario Supplier", - false, - true, "http://plato-controlplane:8084/api/v1/ids", "BPNL1234567890ZZ", "BPNS1234567890ZZ" @@ -289,8 +310,6 @@ private Partner createAndGetSupplierPartner() { private Partner createAndGetNonScenarioCustomer() { Partner nonScenarioCustomer = new Partner( "Non-Scenario Customer", - true, - false, "(None Provided!)>", "BPNL2222222222RR", "BPNL2222222222RR" @@ -302,20 +321,20 @@ private Partner createAndGetNonScenarioCustomer() { return nonScenarioCustomer; } - /** - * creates a new semiconductor Material object. - * Note: this object is not yet stored to the database - * @return a reference to the newly created semiconductor material - */ - private Material getNewSemiconductorMaterial() { - return new Material( - false, - true, - "MNR-7307-AU340474.002", - "MNR-8101-ID146955.001", - null, - "semiconductor" - ); + private Material getNewSemiconductorMaterialForSupplier() { + Material material = new Material(); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + material.setProductFlag(true); + material.setName("semiconductor"); + return material; + } + + private Material getNewSemiconductorMaterialForCustomer() { + Material material = new Material(); + material.setOwnMaterialNumber(semiconductorMatNbrCustomer); + material.setMaterialFlag(true); + material.setName("semiconductor"); + return material; } /** @@ -324,14 +343,11 @@ private Material getNewSemiconductorMaterial() { * @return a reference to the newly created central control unit material */ private Material getNewCentralControlUnitMaterial() { - return new Material( - false, - true, - "MNR-4177-C", - "MNR-4177-S", - null, - "central control unit" - ); + Material material = new Material(); + material.setOwnMaterialNumber("MNR-4177-S"); + material.setProductFlag(true); + material.setName("central control unit"); + return material; } private void createRequest() throws JsonProcessingException { diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/VariablesService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/VariablesService.java index 42c64d59..c6c38165 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/VariablesService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/VariablesService.java @@ -19,9 +19,13 @@ public class VariablesService { @Value("${request.apiassetid}") private String requestApiAssetId; + @Value("${response.apiassetid}") private String responseApiAssetId; + @Value("own.bpnl") + private String ownBpnl; + /** * Returns the asset-id as defined in the properties file for the given api method * under request.apiassetid or response.apiassetid respectively. diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Material.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Material.java index 3e9fcd92..c3a0462a 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Material.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Material.java @@ -21,11 +21,17 @@ */ package org.eclipse.tractusx.puris.backend.masterdata.domain.model; -import jakarta.persistence.*; -import lombok.*; -import org.eclipse.tractusx.puris.backend.stock.domain.model.Stock; - -import java.util.*; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +import java.util.Objects; +import java.util.Set; @Entity @Table(name = "material") @@ -35,36 +41,6 @@ @NoArgsConstructor public class Material { - @Id - @GeneratedValue - private UUID uuid; - - @ManyToMany(fetch = FetchType.EAGER) - @JoinTable( - name = "partner_supplies_product", - joinColumns = @JoinColumn(name = "material_uuid", referencedColumnName = "uuid"), - inverseJoinColumns = @JoinColumn(name = "partner_uuid", referencedColumnName = "uuid") - ) - @ToString.Exclude - @Setter(AccessLevel.NONE) - private Set suppliedByPartners = new HashSet<>(); - ;; - - @ManyToMany(targetEntity = Partner.class, fetch = FetchType.EAGER) - @JoinTable( - name = "partner_orders_product", - joinColumns = @JoinColumn(name = "material_uuid", referencedColumnName = "uuid"), - inverseJoinColumns = @JoinColumn(name = "partner_uuid", referencedColumnName = "uuid") - ) - @ToString.Exclude - @Setter(AccessLevel.NONE) - private Set orderedByPartners = new HashSet<>(); - - @OneToMany(mappedBy = "uuid") - @ToString.Exclude - @Setter(AccessLevel.NONE) - private List materialOnStocks = new ArrayList<>(); - /** * If true, then the Material is a material (input for production / something I buy). *

@@ -79,30 +55,27 @@ public class Material { */ private boolean productFlag; - private String materialNumberCustomer; - - private String materialNumberSupplier; + @Id + private String ownMaterialNumber; private String materialNumberCx; private String name; - public Material(boolean materialFlag, boolean productFlag, String materialNumberCustomer, String materialNumberSupplier, String materialNumberCx, String name) { - this.materialFlag = materialFlag; - this.productFlag = productFlag; - this.materialNumberCustomer = materialNumberCustomer; - this.materialNumberSupplier = materialNumberSupplier; - this.materialNumberCx = materialNumberCx; - this.name = name; - } + @OneToMany(mappedBy = "material") + Set materialPartnerRelations; - public void addPartnerToSuppliedByPartners(Partner supplierPartner) { - this.suppliedByPartners.add(supplierPartner); - supplierPartner.getSuppliesMaterials().add(this); + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Material)) return false; + Material material = (Material) o; + return Objects.equals(ownMaterialNumber, material.ownMaterialNumber); } - public void addPartnerToOrderedByPartners(Partner customerPartner) { - this.orderedByPartners.add(customerPartner); - customerPartner.getOrdersProducts().add(this); + @Override + public int hashCode() { + return Objects.hash(ownMaterialNumber); } + } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Partner.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Partner.java index 34987e04..6110f858 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Partner.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/model/Partner.java @@ -22,7 +22,6 @@ package org.eclipse.tractusx.puris.backend.masterdata.domain.model; import jakarta.persistence.*; -import jakarta.validation.constraints.NotNull; import lombok.*; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; @@ -41,38 +40,40 @@ public class Partner { @GeneratedValue private UUID uuid; private String name; - @NotNull - private boolean actsAsCustomerFlag; - @NotNull - private boolean actsAsSupplierFlag; private String edcUrl; private String bpnl; private String siteBpns; - @ManyToMany(mappedBy = "suppliedByPartners", fetch = FetchType.EAGER) - @Setter(AccessLevel.NONE) - private Set suppliesMaterials = new HashSet<>(); - - @ManyToMany(mappedBy = "orderedByPartners", fetch = FetchType.EAGER) - @Setter(AccessLevel.NONE) - private Set ordersProducts = new HashSet<>(); + @OneToMany(mappedBy = "partner") + private Set materialPartnerRelations; - @OneToMany(mappedBy = "uuid", fetch = FetchType.LAZY) + @OneToMany @ToString.Exclude @Setter(AccessLevel.NONE) private List allocatedProductStocksForCustomer = new ArrayList<>(); - @OneToMany(mappedBy = "uuid") + @OneToMany @ToString.Exclude @Setter(AccessLevel.NONE) private List partnerProductStocks = new ArrayList<>(); - public Partner(String name, boolean actsAsCustomerFlag, boolean actsAsSupplierFlag, String edcUrl, String bpnl, String siteBpns) { + public Partner(String name, String edcUrl, String bpnl, String siteBpns) { this.name = name; - this.actsAsCustomerFlag = actsAsCustomerFlag; - this.actsAsSupplierFlag = actsAsSupplierFlag; this.edcUrl = edcUrl; this.bpnl = bpnl; this.siteBpns = siteBpns; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Partner)) return false; + Partner partner = (Partner) o; + return Objects.equals(uuid, partner.uuid); + } + + @Override + public int hashCode() { + return Objects.hash(uuid); + } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialRepository.java index 4d64547b..8a1a1074 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialRepository.java @@ -26,21 +26,14 @@ import org.springframework.stereotype.Repository; import java.util.List; -import java.util.UUID; @Repository -public interface MaterialRepository extends JpaRepository { +public interface MaterialRepository extends JpaRepository { List findAllByMaterialFlagTrue(); List findAllByProductFlagTrue(); - public List findByMaterialNumberCustomer(String materialNumberCustomer); - public List findByMaterialNumberCx(String materialNumberCx); - public List findByMaterialNumberCustomerAndMaterialFlagTrue(String materialNumberCustomer); - - public List findByMaterialNumberCustomerAndProductFlagTrue(String materialNumberCustomer); - } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/PartnerRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/PartnerRepository.java index caff4624..6d155ae6 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/PartnerRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/PartnerRepository.java @@ -25,21 +25,12 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; import java.util.Optional; import java.util.UUID; @Repository public interface PartnerRepository extends JpaRepository { - List findAllByActsAsCustomerFlagIsTrue(); - - List findAllByActsAsCustomerFlagTrueAndOrdersProducts_Uuid(UUID materialUuid); - - List findAllByActsAsSupplierFlagTrueAndSuppliesMaterials_Uuid(UUID materialUuid); - - List findAllByActsAsSupplierFlagTrue(); - Optional findFirstByBpnl(String bpnl); Optional findFirstBySiteBpns(String siteBpns); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java index 27f487ca..ca59d0cb 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java @@ -36,14 +36,14 @@ public class MaterialDto implements Serializable { private UUID uuid; - @Setter(AccessLevel.NONE) - private Set suppliedByPartners = new HashSet<>(); - - @Setter(AccessLevel.NONE) - private Set orderedByPartners = new HashSet<>(); - - @Setter(AccessLevel.NONE) - private List materialOnStocks = new ArrayList<>(); +// @Setter(AccessLevel.NONE) +// private Set suppliedByPartners = new HashSet<>(); +// +// @Setter(AccessLevel.NONE) +// private Set orderedByPartners = new HashSet<>(); +// +// @Setter(AccessLevel.NONE) +// private List materialOnStocks = new ArrayList<>(); /** * If true, then the Material is a material (input for production / something I buy). @@ -77,14 +77,14 @@ public MaterialDto(boolean materialFlag, boolean productFlag, String materialNum this.name = name; } - public void addSuppliedByPartner(PartnerDto supplierPartner) { - this.suppliedByPartners.add(supplierPartner); - supplierPartner.getSuppliesMaterials().add(this); - } - - public void addOrderedByPartner(PartnerDto customerPartner) { - this.orderedByPartners.add(customerPartner); - customerPartner.getOrdersProducts().add(this); - } +// public void addSuppliedByPartner(PartnerDto supplierPartner) { +// this.suppliedByPartners.add(supplierPartner); +// supplierPartner.getSuppliesMaterials().add(this); +// } +// +// public void addOrderedByPartner(PartnerDto customerPartner) { +// this.orderedByPartners.add(customerPartner); +// customerPartner.getOrdersProducts().add(this); +// } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/PartnerDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/PartnerDto.java index b3a153b0..3c3b0e56 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/PartnerDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/PartnerDto.java @@ -18,23 +18,10 @@ public class PartnerDto implements Serializable { private UUID uuid; private String name; - private boolean actsAsCustomerFlag; - private boolean actsAsSupplierFlag; - private String edcUrl; private String bpnl; private String siteBpns; - @Nullable - @ToString.Exclude - @Setter(AccessLevel.NONE) - private Set suppliesMaterials = new HashSet<>(); - - @Nullable - @ToString.Exclude - @Setter(AccessLevel.NONE) - private Set ordersProducts = new HashSet<>(); - @ToString.Exclude @Setter(AccessLevel.NONE) private List allocatedProductStocksForCustomer = new ArrayList<>(); @@ -43,26 +30,6 @@ public class PartnerDto implements Serializable { @Setter(AccessLevel.NONE) private List partnerProductStocks = new ArrayList<>(); - public PartnerDto(String name, boolean actsAsCustomerFlag, boolean actsAsSupplierFlag, String edcUrl, String bpnl, String siteBpns) { - super(); - this.name = name; - this.actsAsCustomerFlag = actsAsCustomerFlag; - this.actsAsSupplierFlag = actsAsSupplierFlag; - this.edcUrl = edcUrl; - this.bpnl = bpnl; - this.siteBpns = siteBpns; - } - - public void addSuppliedMaterial(MaterialDto suppliedMaterial) { - this.suppliesMaterials.add(suppliedMaterial); - suppliedMaterial.getSuppliedByPartners().add(this); - } - - public void addOrderedProduct(MaterialDto orderedProduct) { - this.ordersProducts.add(orderedProduct); - orderedProduct.getOrderedByPartners().add(this); - } - public void addPartnerProductStock(PartnerProductStockDto partnerProductStockDto) { this.partnerProductStocks.add(partnerProductStockDto); partnerProductStockDto.setSupplierPartner(this); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java index 7c1def3b..faa179f5 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java @@ -22,12 +22,9 @@ package org.eclipse.tractusx.puris.backend.masterdata.logic.service; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; -import org.springframework.stereotype.Service; import java.util.List; -import java.util.UUID; -@Service public interface MaterialService { Material create(Material material); @@ -38,12 +35,16 @@ public interface MaterialService { List findAllProducts(); - Material findByUuid(UUID materialUuid); - Material findByMaterialNumberCx(String materialNumberCx); - Material findMaterialByMaterialNumberCustomer(String materialNumberCustomer); +// Material findByUuid(UUID materialUuid); + + Material findByOwnMaterialNumber(String ownMaterialNumber); + + Material findByMaterialNumberCx(String materialNumberCx); - Material findProductByMaterialNumberCustomer(String materialNumberCustomer); +// Material findMaterialByMaterialNumberCustomer(String materialNumberCustomer); +// +// Material findProductByMaterialNumberCustomer(String materialNumberCustomer); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java index 39566542..6ade631f 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java @@ -29,7 +29,6 @@ import java.util.List; import java.util.Optional; -import java.util.UUID; @Service @Slf4j @@ -40,17 +39,23 @@ public class MaterialServiceImpl implements MaterialService { @Override public Material create(Material material) { - return materialRepository.save(material); + var searchResult = materialRepository.findById(material.getOwnMaterialNumber()); + if (searchResult.isEmpty()) { + return materialRepository.save(material); + } + log.error("Could not create material " + material.getOwnMaterialNumber() + " because it already exists"); + return null; } @Override public Material update(Material material) { Optional existingMaterial = - materialRepository.findById(material.getUuid()); - + materialRepository.findById(material.getOwnMaterialNumber()); if (existingMaterial.isPresent()) { return existingMaterial.get(); - } else return null; + } + log.error("Could not update material " + material.getOwnMaterialNumber() + " because it didn't exist before"); + return null; } @Override @@ -64,13 +69,12 @@ public List findAllProducts() { } @Override - public Material findByUuid(UUID materialUuid) { - Optional foundMaterial = materialRepository.findById(materialUuid); - - if (!foundMaterial.isPresent()) { - return null; + public Material findByOwnMaterialNumber(String ownMaterialNumber) { + var searchResult = materialRepository.findById(ownMaterialNumber); + if (searchResult.isPresent()) { + return searchResult.get(); } - return foundMaterial.get(); + return null; } @Override @@ -86,34 +90,4 @@ public Material findByMaterialNumberCx(String materialNumberCx) { } - @Override - public Material findMaterialByMaterialNumberCustomer(String materialNumberCustomer) { - - List foundMaterial = - materialRepository.findByMaterialNumberCustomerAndMaterialFlagTrue(materialNumberCustomer); - - if (foundMaterial.size() == 0) { - return null; - } - if (foundMaterial.size() > 1) { - log.warn("Found more than one result for materialNumberCx " + materialNumberCustomer); - } - return foundMaterial.get(0); - } - - @Override - public Material findProductByMaterialNumberCustomer(String materialNumberCustomer) { - - List foundProduct = - materialRepository.findByMaterialNumberCustomerAndProductFlagTrue(materialNumberCustomer); - - if (foundProduct.size() == 0) { - return null; - } - if (foundProduct.size() > 1) { - log.warn("Found more than one result for materialNumberCx " + materialNumberCustomer); - } - return foundProduct.get(0); - - } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerService.java index b5e1ebbd..8d91307d 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerService.java @@ -22,21 +22,19 @@ package org.eclipse.tractusx.puris.backend.masterdata.logic.service; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; -import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; -@Service public interface PartnerService { Partner create(Partner partner); Partner findByUuid(UUID partnerUuid); - List findAllCustomerPartnersForMaterialId(UUID materialUuid); + List findAllCustomerPartnersForMaterialId(String ownMaterialNumber); - List findAllSupplierPartnersForMaterialId(UUID materialUUID); + List findAllSupplierPartnersForMaterialId(String ownMaterialNumber); Partner update(Partner partner); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerServiceImpl.java index 2d1369a8..3a59cb00 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/PartnerServiceImpl.java @@ -21,7 +21,9 @@ */ package org.eclipse.tractusx.puris.backend.masterdata.logic.service; +import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.MaterialRepository; import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.PartnerRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,45 +33,65 @@ import java.util.UUID; @Service +@Slf4j public class PartnerServiceImpl implements PartnerService { @Autowired private PartnerRepository partnerRepository; + @Autowired + private MaterialRepository materialRepository; + + @Autowired + private MaterialPartnerRelationService mprService; + @Override public Partner create(Partner partner) { - return partnerRepository.save(partner); + if (partner.getUuid() == null) { + return partnerRepository.save(partner); + } + + var searchResult = partnerRepository.findById(partner.getUuid()); + if(searchResult.isEmpty()) { + return partnerRepository.save(partner); + } + log.error("Could not create Partner " + partner.getBpnl() + " because it already existed before"); + return null; } @Override public Partner findByUuid(UUID partnerUuid) { Optional foundPartner = partnerRepository.findById(partnerUuid); - - if (!foundPartner.isPresent()) { - return null; + if (foundPartner.isPresent()) { + return foundPartner.get(); } - return foundPartner.get(); + return null; } @Override - public List findAllCustomerPartnersForMaterialId(UUID materialUuid) { - return partnerRepository.findAllByActsAsCustomerFlagTrueAndOrdersProducts_Uuid(materialUuid); + public List findAllCustomerPartnersForMaterialId(String ownMaterialNumber) { + return mprService.findAllCustomersForOwnMaterialNumber(ownMaterialNumber); } @Override - public List findAllSupplierPartnersForMaterialId(UUID materialUuid) { - return partnerRepository.findAllByActsAsSupplierFlagTrueAndSuppliesMaterials_Uuid(materialUuid); + public List findAllSupplierPartnersForMaterialId(String ownMaterialNumber) { + var searchResult = materialRepository.findById(ownMaterialNumber); + if (searchResult.isPresent()) { + return mprService.findAllSuppliersForMaterial(searchResult.get()); + } + return List.of(); } @Override public Partner update(Partner partner) { Optional existingPartner = partnerRepository.findById(partner.getUuid()); - if (existingPartner.isPresent()) { - return existingPartner.get(); - } else return null; + return partnerRepository.save(partner); + } + log.error("Could not update Partner " + partner.getBpnl() + " because it didn't exist before"); + return null; } @Override diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java index a69882c9..c03e61d4 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java @@ -32,9 +32,11 @@ import org.eclipse.tractusx.puris.backend.common.api.logic.service.VariablesService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.MaterialDto; import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.PartnerDto; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.MaterialStock; @@ -85,6 +87,9 @@ public class StockController { @Autowired private PartnerService partnerService; + @Autowired + private MaterialPartnerRelationService mprService; + @Autowired private RequestService requestService; @@ -273,11 +278,10 @@ private PartnerProductStockDto convertToDto(PartnerProductStock entity) { @CrossOrigin @GetMapping("customer") @ResponseBody - public List getCustomerPartnersOrderingMaterial(@RequestParam UUID materialUuid) { - List allCustomerPartners = partnerService.findAllCustomerPartnersForMaterialId(materialUuid).stream() + public List getCustomerPartnersOrderingMaterial(@RequestParam String ownMaterialNumber) { + List allCustomerPartners = partnerService.findAllCustomerPartnersForMaterialId(ownMaterialNumber).stream() .map(this::convertToDto) .collect(Collectors.toList()); - return allCustomerPartners; } @@ -286,24 +290,31 @@ public List getCustomerPartnersOrderingMaterial(@RequestParam UUID m @ResponseBody public List triggerPartnerProductStockUpdateForMaterial(@RequestParam String ownMaterialNumber) { - Material materialEntity = materialService.findMaterialByMaterialNumberCustomer(ownMaterialNumber); - - List allSupplierPartnerEntities = - partnerService.findAllSupplierPartnersForMaterialId(materialEntity.getUuid()); + Material materialEntity = materialService.findByOwnMaterialNumber(ownMaterialNumber); + log.info("Found material: " + (materialEntity != null)); + log.info("All materials: " + materialService.findAllMaterials()); + List allSupplierPartnerEntities = mprService.findAllSuppliersForOwnMaterialNumber(ownMaterialNumber); List messageContentDtos = new ArrayList<>(); - // Message Content for all requests - ProductStockRequestForMaterialDto materialDto = new ProductStockRequestForMaterialDto( - materialEntity.getMaterialNumberCustomer(), - materialEntity.getMaterialNumberCx(), - materialEntity.getMaterialNumberSupplier() - ); - messageContentDtos.add(materialDto); for (Partner supplierPartner : allSupplierPartnerEntities) { + MaterialPartnerRelation materialPartnerRelation = mprService.find(materialEntity, supplierPartner); + + if (materialPartnerRelation == null) { + log.error("Missing material-partner-relation for " + materialEntity.getOwnMaterialNumber() + " and " + supplierPartner.getBpnl()); + continue; + } + + ProductStockRequestForMaterialDto materialDto = new ProductStockRequestForMaterialDto( + materialEntity.getOwnMaterialNumber(), + materialEntity.getMaterialNumberCx(), + materialPartnerRelation.getPartnerMaterialNumber() + ); + messageContentDtos.add(materialDto); + String [] data = edcAdapterService.getContractForRequestApi(supplierPartner.getEdcUrl()); if(data == null) { log.error("failed to obtain request api from " + supplierPartner.getEdcUrl()); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/PartnerProductStock.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/PartnerProductStock.java index d7e16581..c4073afb 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/PartnerProductStock.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/PartnerProductStock.java @@ -21,13 +21,11 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.model; -import jakarta.persistence.DiscriminatorValue; -import jakarta.persistence.Entity; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import lombok.ToString; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; @@ -38,12 +36,13 @@ @Entity @DiscriminatorValue("PartnerProductStock") @Getter +@Setter @ToString(callSuper = true) @NoArgsConstructor public class PartnerProductStock extends Stock { - @ManyToOne - @JoinColumn(name = "supplier_partner_uuid") + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "partner_uuid") @NotNull private Partner supplierPartner; @@ -58,9 +57,9 @@ public PartnerProductStock(Material material, double quantity, String atSiteBpnl super(material, quantity, atSiteBpnl, lastUpdatedOn); super.setType(DT_StockTypeEnum.PRODUCT); } - - public void setSupplierPartner(Partner supplierPartner) { - this.supplierPartner = supplierPartner; - supplierPartner.getPartnerProductStocks().add(this); - } -} \ No newline at end of file +// +// public void setSupplierPartner(Partner supplierPartner) { +// this.supplierPartner = supplierPartner; +// supplierPartner.getPartnerProductStocks().add(this); +// } +} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStock.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStock.java index 94e6f0f7..8671a4b2 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStock.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStock.java @@ -21,12 +21,10 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.model; -import jakarta.persistence.DiscriminatorValue; -import jakarta.persistence.Entity; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.Getter; +import lombok.Setter; import lombok.ToString; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; @@ -36,11 +34,15 @@ @Entity @Getter +@Setter @ToString(callSuper = true) @DiscriminatorValue("ProductStock") public class ProductStock extends Stock { - @ManyToOne +// @ManyToOne +// @MapsId("uuid") +// @JoinColumn(name = "partner_uuid") + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "partner_uuid") @ToString.Exclude @NotNull @@ -61,9 +63,9 @@ public ProductStock() { super(); } - public void setAllocatedToCustomerPartner(Partner allocatedToCustomerPartner) { - this.allocatedToCustomerPartner = allocatedToCustomerPartner; - allocatedToCustomerPartner.getAllocatedProductStocksForCustomer().size(); - allocatedToCustomerPartner.getAllocatedProductStocksForCustomer().add(this); - } -} \ No newline at end of file +// public void setAllocatedToCustomerPartner(Partner allocatedToCustomerPartner) { +// this.allocatedToCustomerPartner = allocatedToCustomerPartner; +// allocatedToCustomerPartner.getAllocatedProductStocksForCustomer().size(); +// allocatedToCustomerPartner.getAllocatedProductStocksForCustomer().add(this); +// } +} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/Stock.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/Stock.java index be3a9d28..2e9bd297 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/Stock.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/Stock.java @@ -47,7 +47,7 @@ public class Stock { private UUID uuid; @ManyToOne - @JoinColumn(name = "material_uuid") + @JoinColumn(name = "material_ownMaterialNumber") @NotNull private Material material; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/MaterialStockRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/MaterialStockRepository.java index 756881f0..71a2b17e 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/MaterialStockRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/MaterialStockRepository.java @@ -44,6 +44,7 @@ public interface MaterialStockRepository extends JpaRepository findAllByType(DT_StockTypeEnum stockType); - List findAllByMaterial_MaterialNumberCustomerAndType(String materialNumberCustomer, DT_StockTypeEnum stockType); +// List findAllByMaterial_MaterialNumberCustomerAndType(String materialNumberCustomer, DT_StockTypeEnum stockType); + List findAllByMaterial_OwnMaterialNumberAndType(String ownMaterialNumber, DT_StockTypeEnum stockType); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/PartnerProductStockRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/PartnerProductStockRepository.java index 381bb129..1c726ee2 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/PartnerProductStockRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/PartnerProductStockRepository.java @@ -44,7 +44,13 @@ public interface PartnerProductStockRepository extends JpaRepository findAllByType(DT_StockTypeEnum stockType); - List findAllByMaterial_UuidAndType(UUID materialUuid, DT_StockTypeEnum stockType); +// List findAllByMaterial_UuidAndType(UUID materialUuid, DT_StockTypeEnum stockType); - List findAllByMaterial_UuidAndTypeAndSupplierPartner_Uuid(UUID materialUuid, DT_StockTypeEnum stockType, UUID supplierUuid); + List findAllByMaterial_OwnMaterialNumberAndType(String ownMaterialNumber, DT_StockTypeEnum stockType); + +// List findAllByMaterial_UuidAndTypeAndSupplierPartner_Uuid(UUID materialUuid, DT_StockTypeEnum stockType, UUID supplierUuid); + + List findAllByMaterial_OwnMaterialNumberAndTypeAndSupplierPartner_Uuid(String ownMaterialNumber, DT_StockTypeEnum stockType, UUID supplierUuid); + + List findAllBySupplierPartner_Uuid(UUID uuid); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRepository.java index de1a9317..08f734bf 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRepository.java @@ -43,9 +43,11 @@ public interface ProductStockRepository extends JpaRepository findAllByTypeAndUuid(DT_StockTypeEnum stockType, UUID productStockUuid); + List findAllByMaterial_OwnMaterialNumberAndType(String ownMaterialNumber, DT_StockTypeEnum stockType); + List findAllByType(DT_StockTypeEnum stockType); - List findAllByMaterial_MaterialNumberCustomerAndType(String materialNumberCustomer, DT_StockTypeEnum stockType); +// List findAllByMaterial_MaterialNumberCustomerAndType(String materialNumberCustomer, DT_StockTypeEnum stockType); - List findAllByMaterial_MaterialNumberCustomerAndTypeAndAllocatedToCustomerPartner_Bpnl(String materialNumberCustomer, DT_StockTypeEnum stockType, String allocatedToPartnerBpnl); +// List findAllByMaterial_MaterialNumberCustomerAndTypeAndAllocatedToCustomerPartner_Bpnl(String materialNumberCustomer, DT_StockTypeEnum stockType, String allocatedToPartnerBpnl); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ProductStockSammMapper.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ProductStockSammMapper.java index a5420f8f..248bb980 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ProductStockSammMapper.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ProductStockSammMapper.java @@ -23,13 +23,16 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.MaterialDto; import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.PartnerDto; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; -import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; +import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; +import org.eclipse.tractusx.puris.backend.stock.domain.model.Stock; import org.eclipse.tractusx.puris.backend.stock.logic.dto.PartnerProductStockDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.*; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; @@ -51,15 +54,21 @@ public class ProductStockSammMapper { private MaterialService materialService; @Autowired - private PartnerService partnerService; + private MaterialPartnerRelationService mprService; @Autowired private ModelMapper modelMapper; - public ProductStockSammDto toSamm(ProductStockDto productStockDto) { + /** + * Utility method to serialize a ProductStock or PartnerProduct on the side + * of the responding supplier; + * @param stock MUST be either ProductStock or PartnerProductStock + * @return the corresponding ProductStockSammDto + */ + public ProductStockSammDto toSamm(Stock stock) { GregorianCalendar calendar = new GregorianCalendar(); - calendar.setTime(productStockDto.getLastUpdatedOn()); + calendar.setTime(stock.getLastUpdatedOn()); XMLGregorianCalendar lastUpdatedOn = null; try { lastUpdatedOn = @@ -67,13 +76,13 @@ public ProductStockSammDto toSamm(ProductStockDto productStockDto) { } catch (DatatypeConfigurationException e) { log.error(String.format("Could not create XML Gregorian " + "Calender from PartnerProductStock.lastUpdatedOn: %s", - productStockDto.getLastUpdatedOn().toString())); + stock.getLastUpdatedOn().toString())); return null; } AllocatedStock allocatedStock = new AllocatedStock( - new Quantity(productStockDto.getQuantity(), "unit:piece"), - new LocationId(LocationIdTypeEnum.B_P_N_S, productStockDto.getAtSiteBpnl()) + new Quantity(stock.getQuantity(), "unit:piece"), + new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getAtSiteBpnl()) ); List allocatedStocks = new ArrayList<>(); allocatedStocks.add(allocatedStock); @@ -85,17 +94,42 @@ public ProductStockSammDto toSamm(ProductStockDto productStockDto) { ); List positions = new ArrayList<>(); positions.add(position); + String materialNumberCustomer = null; + String materialNumberSupplier = null; + if (stock instanceof ProductStock) { + // Partner is customer + ProductStock productStock = (ProductStock) stock; + Partner partner = productStock.getAllocatedToCustomerPartner(); + materialNumberCustomer = mprService.find(stock.getMaterial(), partner).getPartnerMaterialNumber(); + materialNumberSupplier = stock.getMaterial().getOwnMaterialNumber(); + } else if (stock instanceof PartnerProductStock) { + // Partner is supplier + PartnerProductStock partnerProductStock = (PartnerProductStock) stock; + Partner partner = partnerProductStock.getSupplierPartner(); + materialNumberSupplier = mprService.find(stock.getMaterial(), partner).getPartnerMaterialNumber(); + materialNumberCustomer = stock.getMaterial().getOwnMaterialNumber(); + } else { + // stock is neither ProductStock nor PartnerProductStock, + // therefore it is not possible to create a ProductStockSammDto + return null; + } return new ProductStockSammDto( positions, - productStockDto.getMaterial().getMaterialNumberCustomer(), - Optional.ofNullable(productStockDto.getMaterial().getMaterialNumberCx()), - Optional.ofNullable(productStockDto.getMaterial().getMaterialNumberSupplier()) + materialNumberCustomer, + Optional.ofNullable(stock.getMaterial().getMaterialNumberCx()), + Optional.ofNullable(materialNumberSupplier) ); } - public PartnerProductStockDto fromSamm(ProductStockSammDto samm) { + /** + * Utility method to deserialize a ProductStockSammDto on the side of the customer + * @param samm a ProductStockSammDto received from a supplier + * @param partner the partner you received the message from + * @return a PartnerProductStockDto + */ + public PartnerProductStockDto fromSamm(ProductStockSammDto samm, Partner partner) { // application currently only supports: // - an AGGREGATED Partner stock // - one Site per Partner @@ -110,9 +144,12 @@ public PartnerProductStockDto fromSamm(ProductStockSammDto samm) { ).sum(); // determine material - Material foundMaterial = - materialService.findProductByMaterialNumberCustomer(samm.getMaterialNumberCustomer()); - MaterialDto foundMaterialDto = modelMapper.map(foundMaterial, MaterialDto.class); + + Material foundMaterial = materialService.findByOwnMaterialNumber(samm.getMaterialNumberCustomer()); + MaterialPartnerRelation materialPartnerRelation = mprService.find(foundMaterial, partner); + MaterialDto foundMaterialDto = new MaterialDto(foundMaterial.isMaterialFlag(), foundMaterial.isProductFlag(), + foundMaterial.getOwnMaterialNumber(), materialPartnerRelation.getPartnerMaterialNumber(), foundMaterial.getMaterialNumberCx(), + foundMaterial.getName()); // find bpns - we use the first one as we currently only have one site per partner. // alternative would be to inject the bpnl of the partner. @@ -122,9 +159,7 @@ public PartnerProductStockDto fromSamm(ProductStockSammDto samm) { .stream().findFirst().get().getAllocatedStocks() .stream().findFirst().get().getSupplierStockLocationId().getLocationId(); - // find partner by bpns - Partner foundPartner = partnerService.findByBpns(atSiteBpns); - PartnerDto supplierPartner = modelMapper.map(foundPartner, PartnerDto.class); + PartnerDto supplierPartner = modelMapper.map(partner, PartnerDto.class); return new PartnerProductStockDto( foundMaterialDto, diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockService.java index 89bcad58..7a69d648 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockService.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.UUID; -@Service public interface MaterialStockService { MaterialStock create(MaterialStock materialStock); @@ -36,7 +35,9 @@ public interface MaterialStockService { MaterialStock findByUuid(UUID materialStockUuid); - List findAllByMaterialNumberCustomer(String materialNumberCustomer); + List findAllByPartnerMaterialNumber(String partnerMaterialNumber); + + List findAllByOwnMaterialNumber(String ownMaterialNumber); MaterialStock update(MaterialStock materialStock); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockServiceImpl.java index f8a67684..91ac2a0c 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/MaterialStockServiceImpl.java @@ -21,22 +21,28 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.stock.domain.model.MaterialStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.repository.MaterialStockRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; - +@Slf4j @Service public class MaterialStockServiceImpl implements MaterialStockService { @Autowired MaterialStockRepository materialStockRepository; + @Autowired + MaterialPartnerRelationService mprService; + @Override public MaterialStock create(MaterialStock materialStock) { return materialStockRepository.save(materialStock); @@ -59,17 +65,37 @@ public MaterialStock findByUuid(UUID materialStockUuid) { } @Override - public List findAllByMaterialNumberCustomer(String materialNumberCustomer) { - return materialStockRepository.findAllByMaterial_MaterialNumberCustomerAndType(materialNumberCustomer, DT_StockTypeEnum.MATERIAL); + public List findAllByPartnerMaterialNumber(String partnerMaterialNumber) { + var materials = mprService.findAllByPartnerMaterialNumber(partnerMaterialNumber); + ArrayList output = new ArrayList<>(); + for (var material : materials) { + output.addAll( + materialStockRepository.findAllByMaterial_OwnMaterialNumberAndType(material.getOwnMaterialNumber(), + DT_StockTypeEnum.MATERIAL)); + } + return output; } @Override - public MaterialStock update(MaterialStock materialStock) { + public List findAllByOwnMaterialNumber(String ownMaterialNumber) { + return materialStockRepository.findAllByMaterial_OwnMaterialNumberAndType(ownMaterialNumber, DT_StockTypeEnum.MATERIAL); + } + @Override + public MaterialStock update(MaterialStock materialStock) { Optional existingStock = materialStockRepository.findById(materialStock.getUuid()); - if (existingStock.isPresent() && existingStock.get().getType() == DT_StockTypeEnum.MATERIAL) { - return existingStock.get(); - } else return null;//TODO error handling: Updated MaterialStock is not material + materialStock = materialStockRepository.save(materialStock); + return materialStock; + } else { + if (existingStock.isPresent()) { + log.error(("update of materialStock " + materialStock.getUuid() + " failed because" + + " existing stock is not of type MATERIAL")); + return null; + } + } + log.error("update of materialStock " + materialStock.getUuid() + " failed because there " + + "is no existing stock stored to the database"); + return null; } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java index bf03b8fc..3d49ca68 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java @@ -21,23 +21,27 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; -@Service public interface PartnerProductStockService { PartnerProductStock create(PartnerProductStock partnerProductStock); List findAll(); - List findAllByMaterialUuid(UUID materialUuid); + List findAllByOwnMaterialNumber(String ownMaterialNumber); PartnerProductStock update(PartnerProductStock partnerProductStock); - List findAllByMaterialUuidAndPartnerUuid(UUID partnerUuid, - UUID materialUuid); + List findAllByOwnMaterialNumberAndPartnerUuid(String ownMaterialNumber, UUID partnerUuid); + + List findAllByPartnerMaterialNumber(Partner partner, String partnerMaterialNumber); + +// List findAllByMaterialUuidAndPartnerUuid(UUID partnerUuid, +// UUID materialUuid); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java index a0169db0..b9cfc173 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java @@ -21,6 +21,9 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; +import lombok.AllArgsConstructor; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.repository.PartnerProductStockRepository; @@ -30,12 +33,15 @@ import java.util.List; import java.util.Optional; import java.util.UUID; - +import java.util.stream.Collectors; @Service +@AllArgsConstructor public class PartnerProductStockServiceImpl implements PartnerProductStockService { - @Autowired - PartnerProductStockRepository partnerProductStockRepository; + + private PartnerProductStockRepository partnerProductStockRepository; + + private MaterialPartnerRelationService mprService; @Override public PartnerProductStock create(PartnerProductStock partnerProductStock) { @@ -48,8 +54,8 @@ public List findAll() { } @Override - public List findAllByMaterialUuid(UUID materialUuid) { - return partnerProductStockRepository.findAllByMaterial_UuidAndType(materialUuid, DT_StockTypeEnum.PRODUCT); + public List findAllByOwnMaterialNumber(String ownMaterialNumber) { + return partnerProductStockRepository.findAllByMaterial_OwnMaterialNumberAndType(ownMaterialNumber, DT_StockTypeEnum.PRODUCT); } @Override @@ -64,7 +70,21 @@ public PartnerProductStock update(PartnerProductStock partnerProductStock) { } @Override - public List findAllByMaterialUuidAndPartnerUuid(UUID partnerUuid, UUID materialUuid) { - return partnerProductStockRepository.findAllByMaterial_UuidAndTypeAndSupplierPartner_Uuid(materialUuid, DT_StockTypeEnum.PRODUCT, partnerUuid); + public List findAllByOwnMaterialNumberAndPartnerUuid(String ownMaterialNumber, UUID partnerUuid) { + return partnerProductStockRepository.findAllByMaterial_OwnMaterialNumberAndTypeAndSupplierPartner_Uuid( + ownMaterialNumber, DT_StockTypeEnum.PRODUCT, partnerUuid); + } + + @Override + public List findAllByPartnerMaterialNumber(Partner partner, String partnerMaterialNumber) { + + var materialsList = mprService.findAllByPartnerMaterialNumber(partnerMaterialNumber); + return partnerProductStockRepository + .findAllBySupplierPartner_Uuid(partner.getUuid()) + .stream() + .filter(pps -> materialsList.contains(pps.getMaterial())) + .collect(Collectors.toList()); + + } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java index 902fc1f2..c7a40640 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java @@ -31,6 +31,7 @@ import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; @@ -67,6 +68,9 @@ public class ProductStockRequestApiServiceImpl implements RequestApiService { @Autowired private MaterialService materialService; + @Autowired + private MaterialPartnerRelationService mprService; + @Autowired private PartnerService partnerService; @@ -121,6 +125,12 @@ public void handleRequest(RequestDto requestDto) { String requestingPartnerBpnl = requestDto.getHeader().getSender(); Partner requestingPartner = partnerService.findByBpnl(requestingPartnerBpnl); + if (requestingPartner == null) { + log.warn("Partner with BPNL " +requestingPartnerBpnl + " is unknown"); + log.warn("Request will not be processed"); + return; + } + String partnerIdsUrl = requestingPartner.getEdcUrl(); if (requestDto.getHeader().getSenderEdc() != null && !partnerIdsUrl.equals(requestDto.getHeader().getSenderEdc())) { @@ -145,8 +155,18 @@ public void handleRequest(RequestDto requestDto) { if (existingMaterial == null) { // if material could not be found via cx number, try to use materialNumberCustomer - existingMaterial = materialService - .findProductByMaterialNumberCustomer(productStockRequestDto.getMaterialNumberCustomer()); + if (productStockRequestDto.getMaterialNumberCustomer() != null) { + var searchResult = mprService.findAllByPartnerMaterialNumber(productStockRequestDto.getMaterialNumberCustomer()); + if (searchResult.size() == 1) { + existingMaterial = searchResult.get(0); + } else { + // MaterialNumberCustomer is ambiguous or unknown + // try to use MaterialNumberSupplier + if (productStockRequestDto.getMaterialNumberSupplier() != null) { + existingMaterial = materialService.findByOwnMaterialNumber(productStockRequestDto.getMaterialNumberSupplier()); + } + } + } } if (existingMaterial == null) { @@ -160,12 +180,11 @@ public void handleRequest(RequestDto requestDto) { requestDto.getHeader().getRequestId())); continue; } else { - log.info("Found requested Material: " + existingMaterial.getMaterialNumberCustomer()); + log.info("Found requested Material: " + existingMaterial.getOwnMaterialNumber()); } - boolean ordersProducts = - existingMaterial.getOrderedByPartners() - .stream().anyMatch( - partner -> partner.getBpnl().equals(requestingPartnerBpnl)); + + + boolean ordersProducts = mprService.partnerOrdersProduct(existingMaterial, requestingPartner); log.info("Requesting entity orders this Material? " + ordersProducts); if (!ordersProducts) { MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); @@ -219,8 +238,9 @@ public void handleRequest(RequestDto requestDto) { } - resultProductStocks.add(productStockSammMapper.toSamm(modelMapper.map(productStock, - ProductStockDto.class))); + log.info("Assembled productStock:\n" + productStock); + + resultProductStocks.add(productStockSammMapper.toSamm(productStock)); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java index 2b6e47f3..70590b5b 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java @@ -30,6 +30,8 @@ import org.eclipse.tractusx.puris.backend.common.api.logic.dto.ResponseDto; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.ResponseApiService; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; import org.eclipse.tractusx.puris.backend.stock.logic.dto.PartnerProductStockDto; @@ -58,6 +60,9 @@ public class ProductStockResponseApiServiceImpl implements ResponseApiService { @Autowired private PartnerProductStockService partnerProductStockService; + @Autowired + private PartnerService partnerService; + @Autowired private ProductStockSammMapper productStockSammMapper; @@ -68,22 +73,27 @@ public class ProductStockResponseApiServiceImpl implements ResponseApiService { public void consumeResponse(ResponseDto responseDto) { ProductStockRequest correspondingProductStockRequest = findCorrespondingRequest(responseDto); - + if (correspondingProductStockRequest == null) { + log.error("Received Response without corresponding request"); + return; + } + Partner partner = partnerService.findByBpnl(responseDto.getHeader().getSender()); for (MessageContentDto messageContentDto : responseDto.getPayload()) { if (messageContentDto instanceof ProductStockSammDto) { - ProductStockSammDto sammDto = (ProductStockSammDto) messageContentDto; PartnerProductStockDto partnerProductStockDto = - productStockSammMapper.fromSamm(sammDto); - + productStockSammMapper.fromSamm(sammDto, partner); // check whether a new PartnerProductStock must be created // or whether an update is sufficient. List existingPartnerProductStocks = - partnerProductStockService.findAllByMaterialUuidAndPartnerUuid( - partnerProductStockDto.getSupplierPartner().getUuid(), - partnerProductStockDto.getMaterial().getUuid() - ); + partnerProductStockService.findAllByOwnMaterialNumberAndPartnerUuid( + partnerProductStockDto.getMaterial().getMaterialNumberCustomer(), + partnerProductStockDto.getSupplierPartner().getUuid()); +// partnerProductStockService.findAllByMaterialUuidAndPartnerUuid( +// partnerProductStockDto.getSupplierPartner().getUuid(), +// partnerProductStockDto.getMaterial().getUuid() +// ); // currently we only accept a one to one mapping of partner - material - stock -site // therefore the can only be one PartnerProductStock @@ -123,13 +133,7 @@ public void consumeResponse(ResponseDto responseDto) { private ProductStockRequest findCorrespondingRequest(ResponseDto responseDto) { UUID requestId = responseDto.getHeader().getRequestId(); - - ProductStockRequest productStockRequestFound = - requestService.findRequestByHeaderUuid(requestId); - - if (productStockRequestFound == null) { - throw new RequestIdNotFoundException(requestId); - } else return productStockRequestFound; + return requestService.findRequestByHeaderUuid(requestId); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockService.java index 959f7edd..650b0103 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockService.java @@ -21,13 +21,13 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; import org.springframework.stereotype.Service; import java.util.List; import java.util.UUID; -@Service public interface ProductStockService { ProductStock create(ProductStock productStock); @@ -38,7 +38,7 @@ public interface ProductStockService { ProductStock findByUuid(UUID productStockUuid); - List findAllByMaterialNumberCustomer(String materialNumberCustomer); + List findAllByMaterialNumberCustomer(String materialNumberCustomer, Partner customerPartner); List findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl(String materialNumberCustomer, String customerBpnl); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockServiceImpl.java index 7f934693..bd7df8d5 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockServiceImpl.java @@ -23,16 +23,22 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.MaterialRepository; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.repository.ProductStockRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; @Service @Slf4j @@ -44,19 +50,25 @@ public class ProductStockServiceImpl implements ProductStockService { @Autowired MaterialRepository materialRepository; + @Autowired + MaterialPartnerRelationService mprService; + + @Autowired + PartnerService partnerService; + @Override public ProductStock create(ProductStock productStock) { // validate, if material is missing - if (productStock.getMaterial() == null || productStock.getMaterial().getUuid() == null){ + if (productStock.getMaterial() == null || productStock.getMaterial().getOwnMaterialNumber() == null){ log.error("Can't create product stock due to missing material or material uuid"); return null; } - Optional existingMaterial = materialRepository.findById(productStock.getMaterial().getUuid()); + Optional existingMaterial = materialRepository.findById(productStock.getMaterial().getOwnMaterialNumber()); if (!existingMaterial.isPresent()) { - log.error(String.format("Material for uuid %s not found", productStock.getMaterial().getUuid())); + log.error(String.format("Material for uuid %s not found", productStock.getMaterial().getOwnMaterialNumber())); return null; } @@ -66,6 +78,11 @@ public ProductStock create(ProductStock productStock) { return null; } + if (!mprService.partnerOrdersProduct(productStock.getMaterial(), productStock.getAllocatedToCustomerPartner())) { + log.error("Partner is not registered als customer for product " + productStock.getMaterial().getOwnMaterialNumber()); + return null; + } + return productStockRepository.save(productStock); } @@ -86,21 +103,29 @@ public ProductStock findByUuid(UUID productStockUuid) { } @Override - public List findAllByMaterialNumberCustomer(String materialNumberCustomer) { - return productStockRepository.findAllByMaterial_MaterialNumberCustomerAndType( - materialNumberCustomer, - DT_StockTypeEnum.PRODUCT); + public List findAllByMaterialNumberCustomer(String materialNumberCustomer, Partner customerPartner) { + List materialsList = mprService.findAllByPartnerMaterialNumber(materialNumberCustomer); + ArrayList output = new ArrayList<>(); + for (var material : materialsList) { + output.addAll(productStockRepository. + findAllByMaterial_OwnMaterialNumberAndType(material.getOwnMaterialNumber(), DT_StockTypeEnum.PRODUCT) + .stream() + .filter(productStock -> productStock.getAllocatedToCustomerPartner().getUuid().equals(customerPartner.getUuid())) + .collect(Collectors.toList())); + } + return output; + } @Override public List findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl( String materialNumberCustomer, String customerBpnl) { - return productStockRepository - .findAllByMaterial_MaterialNumberCustomerAndTypeAndAllocatedToCustomerPartner_Bpnl( - materialNumberCustomer, - DT_StockTypeEnum.PRODUCT, - customerBpnl); + Partner customerPartner = partnerService.findByBpnl(customerBpnl); + if (customerPartner == null) { + return List.of(); + } + return findAllByMaterialNumberCustomer(materialNumberCustomer, customerPartner); } @Override @@ -109,7 +134,8 @@ public ProductStock update(ProductStock productStock) { Optional existingStock = productStockRepository.findById(productStock.getUuid()); if (existingStock.isPresent() && existingStock.get().getType() == DT_StockTypeEnum.PRODUCT) { - return existingStock.get(); + productStock = productStockRepository.save(productStock); + return productStock; } else return null; } From 10a2784b8f8f4f3e30ce17a7b8cddce9c8cf003a Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Wed, 2 Aug 2023 08:46:13 +0200 Subject: [PATCH 5/9] fix: some cleaning up, added flag consitency test for materialpartnerrelation --- .../DataInjectionCommandLineRunner.java | 61 ++++++------------- .../MaterialPartnerRelationRepository.java | 4 ++ .../masterdata/logic/dto/MaterialDto.java | 18 +----- .../MaterialPartnerRelationService.java | 4 ++ .../MaterialPartnerRelationServiceImpl.java | 39 ++++++++++++ 5 files changed, 65 insertions(+), 61 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java index 0d14093c..85c0a41c 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java @@ -41,7 +41,6 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; @@ -159,39 +158,15 @@ private void setupCustomerRole() throws JsonProcessingException { log.info("All stored Relations: " + mprService.findAll()); + List foundPartners = mprService.findAllCustomersForOwnMaterialNumber(centralControlUnitEntity.getOwnMaterialNumber()); + + log.info("Customer Partner for CCU: " + foundPartners); + List productsFound = materialService.findAllProducts(); log.info(String.format("Found Products: %s", productsFound)); -// centralControlUnitEntity = -// materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); -// log.info(String.format("Found product by materialNumber customer: %s", -// centralControlUnitEntity)); -// nonScenarioCustomer = partnerService.findByUuid(nonScenarioCustomer.getUuid()); -// log.info(String.format("Relationship to product: %s", -// nonScenarioCustomer.getOrdersProducts())); -// -// centralControlUnitEntity = -// materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); -// log.info(String.format("Found product by materialNumber customer: %s", -// centralControlUnitEntity)); -// -// Material existingMaterial = -// materialService.findByUuid(semiconductorMaterial.getUuid()); -// log.info(String.format("Found existingMaterial by uuid: %s", -// existingMaterial)); -// -// Material existingProduct = -// materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); -// log.info(String.format("Found existingProduct by customer number: %s", -// existingProduct)); -// -// List existingProducts = -// materialService.findAllProducts(); -// log.info(String.format("Found existingProducts by product flag true: %s", -// existingProducts)); -// -// log.info(String.format("Relationship centralControlUnitEntity -> orderedByPartners: %s", -// centralControlUnitEntity.getOrderedByPartners().toString())); + productsFound = mprService.findAllProductsThatPartnerBuys(nonScenarioCustomer); + log.info("Products that customer buys: " + productsFound); // Create Material Stock MaterialStock materialStockEntity = new MaterialStock( @@ -202,9 +177,7 @@ private void setupCustomerRole() throws JsonProcessingException { ); materialStockEntity = materialStockService.create(materialStockEntity); log.info(String.format("Created materialStock: %s", materialStockEntity)); -// List foundMaterialStocks = -// materialStockService.findAllByMaterialNumberCustomer(semiconductorMaterial.getMaterialNumberCustomer()); -// log.info(String.format("Found materialStock: %s", foundMaterialStocks)); + // Create PartnerProductStock semiconductorMaterial = materialService.findByOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber()); @@ -226,7 +199,7 @@ private void setupCustomerRole() throws JsonProcessingException { private void setupSupplierRole() { Partner customerPartner = createAndGetCustomerPartner(); Material semiconductorMaterial = getNewSemiconductorMaterialForSupplier(); -// semiconductorMaterial.addPartnerToOrderedByPartners(customerPartner); + semiconductorMaterial = materialService.create(semiconductorMaterial); log.info(String.format("Created product: %s", semiconductorMaterial)); @@ -242,8 +215,10 @@ private void setupSupplierRole() { List materialsFound = materialService.findAllProducts(); log.info(String.format("Found product: %s", materialsFound)); - log.info(String.format("Found customer partner: %s", customerPartner)); -// log.info(String.format("Relationship to material: %s", customerPartner.getOrdersProducts())); + + List customerPartners = mprService.findAllCustomersForOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber()); + log.info(String.format("Found customer partners for semiconductor: %s", customerPartners)); + ProductStock productStockEntity = new ProductStock( semiconductorMaterial, @@ -254,13 +229,11 @@ private void setupSupplierRole() { ); productStockEntity = productStockService.create(productStockEntity); log.info(String.format("Created productStock: %s", productStockEntity.toString())); -// List foundProductStocks = -// productStockService -// .findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl( -// semiconductorMaterial.getMaterialNumberCustomer(), -// customerPartner.getBpnl()); -// log.info(String.format("Found productStocks by material number and allocated to customer " + -// "bpnl: %s", foundProductStocks)); + + List foundProductStocks = productStockService. + findAllByMaterialNumberCustomer(semiconductorMatNbrCustomer, customerPartner); + log.info(String.format("Found productStocks by material number and allocated to customer " + + "bpnl: %s", foundProductStocks)); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java index 404d481e..ca97a87a 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java @@ -12,6 +12,10 @@ public interface MaterialPartnerRelationRepository extends JpaRepository findAllByPartner_Uuid(UUID partnerUuid); + List findAllByPartner_UuidAndPartnerSuppliesMaterialIsTrue(UUID partnerUuid); + + List findAllByPartner_UuidAndPartnerBuysMaterialIsTrue(UUID partnerUuid); + List findAllByMaterial_OwnMaterialNumber(String ownMaterialNumber); List findAllByMaterial_OwnMaterialNumberAndPartnerSuppliesMaterialIsTrue(String ownMaterialNumber); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java index ca59d0cb..34c15d12 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/dto/MaterialDto.java @@ -36,14 +36,6 @@ public class MaterialDto implements Serializable { private UUID uuid; -// @Setter(AccessLevel.NONE) -// private Set suppliedByPartners = new HashSet<>(); -// -// @Setter(AccessLevel.NONE) -// private Set orderedByPartners = new HashSet<>(); -// -// @Setter(AccessLevel.NONE) -// private List materialOnStocks = new ArrayList<>(); /** * If true, then the Material is a material (input for production / something I buy). @@ -77,14 +69,6 @@ public MaterialDto(boolean materialFlag, boolean productFlag, String materialNum this.name = name; } -// public void addSuppliedByPartner(PartnerDto supplierPartner) { -// this.suppliedByPartners.add(supplierPartner); -// supplierPartner.getSuppliesMaterials().add(this); -// } -// -// public void addOrderedByPartner(PartnerDto customerPartner) { -// this.orderedByPartners.add(customerPartner); -// customerPartner.getOrdersProducts().add(this); -// } + } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java index 0a203570..d707ff02 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java @@ -16,6 +16,10 @@ public interface MaterialPartnerRelationService { MaterialPartnerRelation find(Material material, Partner partner); + List findAllMaterialsThatPartnerSupplies(Partner partner); + + List findAllProductsThatPartnerBuys(Partner partner); + List findAll(); Map getBPNL_To_MaterialNumberMap(String ownMaterialNumber); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java index bf2825d7..8f470d28 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java @@ -35,6 +35,7 @@ public class MaterialPartnerRelationServiceImpl implements MaterialPartnerRelati */ @Override public MaterialPartnerRelation create(MaterialPartnerRelation materialPartnerRelation) { + flagConsistencyTest(materialPartnerRelation); var searchResult = find(materialPartnerRelation.getMaterial(), materialPartnerRelation.getPartner()); if (searchResult == null) { return mprRepository.save(materialPartnerRelation); @@ -50,6 +51,7 @@ public MaterialPartnerRelation create(MaterialPartnerRelation materialPartnerRel */ @Override public MaterialPartnerRelation update(MaterialPartnerRelation materialPartnerRelation) { + flagConsistencyTest(materialPartnerRelation); var foundEntity = mprRepository.findById(materialPartnerRelation.getKey()); if (foundEntity.isPresent()) { return mprRepository.save(materialPartnerRelation); @@ -58,6 +60,15 @@ public MaterialPartnerRelation update(MaterialPartnerRelation materialPartnerRel return null; } + private void flagConsistencyTest(MaterialPartnerRelation materialPartnerRelation) { + Material material = materialPartnerRelation.getMaterial(); + boolean test = material.isMaterialFlag() && materialPartnerRelation.isPartnerSuppliesMaterial(); + test = test || (material.isProductFlag() && materialPartnerRelation.isPartnerBuysMaterial()); + if (!test) { + log.warn("Flags of " + materialPartnerRelation + " are not consistent with flags of " + material.getOwnMaterialNumber()); + } + } + /** * Find the MaterialPartnerRelation containing the material and the partner. * @param material @@ -69,6 +80,34 @@ public MaterialPartnerRelation find(Material material, Partner partner) { return find(material.getOwnMaterialNumber(), partner.getUuid()); } + /** + * Returns a list of all materials that the given partner supplies to you. + * @param partner the partner + * @return a list of material entities + */ + @Override + public List findAllMaterialsThatPartnerSupplies(Partner partner) { + return mprRepository + .findAllByPartner_UuidAndPartnerSuppliesMaterialIsTrue(partner.getUuid()) + .stream() + .map(mpr -> mpr.getMaterial()) + .collect(Collectors.toList()); + } + + /** + * Returns a list of all products that the given partner buys from you. + * @param partner the partner + * @return a list of product entities + */ + @Override + public List findAllProductsThatPartnerBuys(Partner partner) { + return mprRepository + .findAllByPartner_UuidAndPartnerBuysMaterialIsTrue(partner.getUuid()) + .stream() + .map(mpr -> mpr.getMaterial()) + .collect(Collectors.toList()); + } + /** * * @return a list of all existing MaterialPartnerRelations From 13151e981a78d38ccaec153956e40f6b1b4a5b19 Mon Sep 17 00:00:00 2001 From: --show-origin Date: Wed, 2 Aug 2023 06:06:24 -0700 Subject: [PATCH 6/9] fix(product stock response): product stock quantity was not updated update was not working due to messing up parameter order in partner product stock retrieval --- .../logic/service/PartnerProductStockService.java | 4 ++-- .../service/PartnerProductStockServiceImpl.java | 2 +- .../service/ProductStockResponseApiServiceImpl.java | 12 ++++++++---- frontend/src/views/StockView.vue | 6 +++--- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java index bf03b8fc..825fb2a0 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockService.java @@ -38,6 +38,6 @@ public interface PartnerProductStockService { PartnerProductStock update(PartnerProductStock partnerProductStock); - List findAllByMaterialUuidAndPartnerUuid(UUID partnerUuid, - UUID materialUuid); + List findAllByMaterialUuidAndPartnerUuid(UUID materialUuid, + UUID partnerUuid); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java index a0169db0..40a8b284 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/PartnerProductStockServiceImpl.java @@ -64,7 +64,7 @@ public PartnerProductStock update(PartnerProductStock partnerProductStock) { } @Override - public List findAllByMaterialUuidAndPartnerUuid(UUID partnerUuid, UUID materialUuid) { + public List findAllByMaterialUuidAndPartnerUuid(UUID materialUuid, UUID partnerUuid) { return partnerProductStockRepository.findAllByMaterial_UuidAndTypeAndSupplierPartner_Uuid(materialUuid, DT_StockTypeEnum.PRODUCT, partnerUuid); } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java index 2b6e47f3..ec23ab3c 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java @@ -81,9 +81,9 @@ public void consumeResponse(ResponseDto responseDto) { // or whether an update is sufficient. List existingPartnerProductStocks = partnerProductStockService.findAllByMaterialUuidAndPartnerUuid( - partnerProductStockDto.getSupplierPartner().getUuid(), - partnerProductStockDto.getMaterial().getUuid() - ); + partnerProductStockDto.getMaterial().getUuid(), + partnerProductStockDto.getSupplierPartner().getUuid() + ); // currently we only accept a one to one mapping of partner - material - stock -site // therefore the can only be one PartnerProductStock @@ -104,8 +104,12 @@ public void consumeResponse(ResponseDto responseDto) { log.info(String.format("Created Partner ProductStock from SAMM: %s", createdPartnerProductStock)); } else { + // update quantity only + PartnerProductStock existingPartnerProductStock = existingPartnerProductStocks.get(0); + existingPartnerProductStock.setQuantity(partnerProductStockDto.getQuantity()); + PartnerProductStock updatedPartnerProductStock = - partnerProductStockService.update(existingPartnerProductStocks.get(0)); + partnerProductStockService.update(existingPartnerProductStock); log.info(String.format("Updated Partner ProductStock from SAMM: %s", updatedPartnerProductStock)); } diff --git a/frontend/src/views/StockView.vue b/frontend/src/views/StockView.vue index 360c8c6e..a79cbe49 100644 --- a/frontend/src/views/StockView.vue +++ b/frontend/src/views/StockView.vue @@ -180,12 +180,12 @@ export default { methods: { addOrUpdateStock(changedStock) { if (changedStock.type === "Material") { - var existingMaterialStock = this.bdMaterialStocks.filter( + var existingMaterialStocks = this.bdMaterialStocks.filter( (stock) => (stock.material.uuid === changedStock.materialId) ); - if (existingMaterialStock.length === 1) { // Update existing material stock - var existingMaterialStock = existingMaterialStock[0]; + if (existingMaterialStocks.length === 1) { // Update existing material stock + var existingMaterialStock = existingMaterialStocks[0]; existingMaterialStock.quantity = changedStock.quantity; this.putData(this.backendURL + this.endpointMaterialStocks, existingMaterialStock); From 8ddaef1e7f52bbd84c42e719099ed0875893e747 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Thu, 3 Aug 2023 13:23:10 +0200 Subject: [PATCH 7/9] refactor: moved productstockrequestservice to stock package --- .../backend/DataInjectionCommandLineRunner.java | 6 +++--- .../ProductStockRequestApiController.java | 10 +++++----- .../ProductStockResponseApiController.java | 8 ++++---- .../stock/controller/StockController.java | 16 ++++++++-------- .../ProductStockRequestRepository.java | 2 +- .../ProductStockResponseRepository.java | 2 +- .../ProductStockRequestApiServiceImpl.java | 16 +++++++--------- .../service/ProductStockRequestService.java} | 4 ++-- .../service/ProductStockRequestServiceImpl.java} | 6 +++--- .../ProductStockResponseApiServiceImpl.java | 8 +++----- 10 files changed, 37 insertions(+), 41 deletions(-) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/{common/api => stock}/domain/repository/ProductStockRequestRepository.java (95%) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/{common/api => stock}/domain/repository/ProductStockResponseRepository.java (95%) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/{common/api/logic/service/RequestService.java => stock/logic/service/ProductStockRequestService.java} (96%) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/{common/api/logic/service/RequestServiceImpl.java => stock/logic/service/ProductStockRequestServiceImpl.java} (92%) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java index 85c0a41c..80d82085 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java @@ -29,7 +29,7 @@ import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentErrorDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; @@ -89,7 +89,7 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner { private ProductStockSammMapper productStockSammMapper; @Autowired - private RequestService requestService; + private ProductStockRequestService productStockRequestService; @Value("${puris.demonstrator.role}") @@ -352,7 +352,7 @@ private void createRequest() throws JsonProcessingException { messageHeaderDto, messageContentDtos ); - ProductStockRequest createdProductStockRequest = requestService.createRequest(modelMapper.map(requestDto, + ProductStockRequest createdProductStockRequest = productStockRequestService.createRequest(modelMapper.map(requestDto, ProductStockRequest.class)); log.info(String.format("Created Request: %s", createdProductStockRequest)); log.info(createdProductStockRequest.getPayload().get(0).getClass().toString()); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java index 6cb0bba3..ecb37b83 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java @@ -32,7 +32,7 @@ import org.eclipse.tractusx.puris.backend.common.api.logic.dto.RequestDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfulRequestDto; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestApiService; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequestForMaterial; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; @@ -59,7 +59,7 @@ public class ProductStockRequestApiController { ObjectMapper objectMapper; @Autowired - RequestService requestService; + ProductStockRequestService productStockRequestService; @Autowired RequestApiService requestApiService; @@ -90,7 +90,7 @@ public ResponseEntity postRequest(@RequestBody String requestBody) { UUID requestId = productStockRequestDto.getHeader().getRequestId(); ProductStockRequest productStockRequestFound = - requestService.findRequestByHeaderUuid(requestId); + productStockRequestService.findRequestByHeaderUuid(requestId); if (productStockRequestFound != null) { @@ -108,7 +108,7 @@ public ResponseEntity postRequest(@RequestBody String requestBody) { } newProductStockRequestEntity.setPayload(payload); newProductStockRequestEntity.setState(productStockRequestDto.getState()); - requestService.createRequest(newProductStockRequestEntity); + productStockRequestService.createRequest(newProductStockRequestEntity); log.info("Persisted incoming request " + productStockRequestDto.getHeader().getRequestId()); } catch (Exception e) { log.warn("Failed to persist incoming request " + productStockRequestDto.getHeader().getRequestId()); @@ -136,7 +136,7 @@ public ResponseEntity postRequest(@RequestBody String requestBody) { public ResponseEntity getRequest(@RequestBody JsonNode body) { try { MessageHeaderDto header = objectMapper.convertValue(body.get("header"), MessageHeaderDto.class); - var request = requestService.findRequestByHeaderUuid(header.getRequestId()); + var request = productStockRequestService.findRequestByHeaderUuid(header.getRequestId()); var requestStatus = request.getState(); var jsonResponseBody = objectMapper.createObjectNode(); jsonResponseBody.put("requestId", header.getRequestId().toString()); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java index eda21b77..57f6bcf5 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java @@ -26,7 +26,7 @@ import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfulRequestDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.ResponseApiService; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; @@ -49,7 +49,7 @@ public class ProductStockResponseApiController { @Autowired - RequestService requestService; + ProductStockRequestService productStockRequestService; @Autowired ModelMapper modelMapper; @@ -85,7 +85,7 @@ public ResponseEntity postResponse(@RequestBody String body) { UUID requestId = productStockResponseDto.getHeader().getRequestId(); - ProductStockRequest productStockRequestFound = requestService.findRequestByHeaderUuid(requestId); + ProductStockRequest productStockRequestFound = productStockRequestService.findRequestByHeaderUuid(requestId); if (productStockRequestFound == null) { log.error("Request id " +requestId + " not found"); return ResponseEntity.status(422).build(); @@ -93,7 +93,7 @@ public ResponseEntity postResponse(@RequestBody String body) { log.info("Got response for request Id " + requestId); } - productStockRequestFound = requestService.updateState(productStockRequestFound, DT_RequestStateEnum.COMPLETED); + productStockRequestFound = productStockRequestService.updateState(productStockRequestFound, DT_RequestStateEnum.COMPLETED); responseApiService.consumeResponse(productStockResponseDto); // if the request has been correctly taken over, return 202 diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java index c03e61d4..27b187c1 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java @@ -28,7 +28,7 @@ import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.VariablesService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; @@ -91,7 +91,7 @@ public class StockController { private MaterialPartnerRelationService mprService; @Autowired - private RequestService requestService; + private ProductStockRequestService productStockRequestService; @Autowired private ModelMapper modelMapper; @@ -333,7 +333,7 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara UUID randomUuid = UUID.randomUUID(); // Avoid randomly choosing a UUID that was already used by this customer. - while (requestService.findRequestByHeaderUuid(randomUuid) != null) { + while (productStockRequestService.findRequestByHeaderUuid(randomUuid) != null) { randomUuid = UUID.randomUUID(); } messageHeaderDto.setRequestId(randomUuid); @@ -356,8 +356,8 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara ProductStockRequest productStockRequest = modelMapper.map(requestDto, ProductStockRequest.class); productStockRequest.setState(DT_RequestStateEnum.WORKING); log.debug("Setting request state to " + DT_RequestStateEnum.WORKING); - productStockRequest = requestService.createRequest(productStockRequest); - var test = requestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); + productStockRequest = productStockRequestService.createRequest(productStockRequest); + var test = productStockRequestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); log.debug("Stored in Database " + (test != null) + " " + requestDto.getHeader().getRequestId()); Response response = null; try { @@ -365,18 +365,18 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara response = edcAdapterService.sendDataPullRequest(endpoint, authKey, authCode, requestBody); log.debug(response.body().string()); if(response.code() < 400) { - productStockRequest = requestService.updateState(productStockRequest, DT_RequestStateEnum.REQUESTED); + productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.REQUESTED); log.debug("Sent request and received HTTP Status code " + response.code()); log.debug("Setting request state to " + DT_RequestStateEnum.REQUESTED); } else { log.warn("Receviced HTTP Status Code " + response.code() + " for request " + productStockRequest.getHeader().getRequestId() + " from " + productStockRequest.getHeader().getReceiver()); - productStockRequest = requestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); + productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); } } catch (Exception e) { log.error("Failed to send data pull request to " + supplierPartner.getEdcUrl(), e); - productStockRequest = requestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); + productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); } finally { try { if(response != null) { diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/repository/ProductStockRequestRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java similarity index 95% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/repository/ProductStockRequestRepository.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java index 60470d69..7512a499 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/repository/ProductStockRequestRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java @@ -18,7 +18,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.eclipse.tractusx.puris.backend.common.api.domain.repository; +package org.eclipse.tractusx.puris.backend.stock.domain.repository; import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/repository/ProductStockResponseRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java similarity index 95% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/repository/ProductStockResponseRepository.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java index 0d182e88..07ab0c16 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/repository/ProductStockResponseRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java @@ -18,7 +18,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.eclipse.tractusx.puris.backend.common.api.domain.repository; +package org.eclipse.tractusx.puris.backend.stock.domain.repository; import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java index c7a40640..a6351119 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java @@ -27,7 +27,6 @@ import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.*; import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestApiService; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; @@ -37,7 +36,6 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; import org.modelmapper.ModelMapper; @@ -63,7 +61,7 @@ public class ProductStockRequestApiServiceImpl implements RequestApiService { @Autowired - private RequestService requestService; + private ProductStockRequestService productStockRequestService; @Autowired private MaterialService materialService; @@ -119,8 +117,8 @@ public void handleRequest(RequestDto requestDto) { log.info(String.format("param requestDto %s", requestDto)); requestDto.setState(DT_RequestStateEnum.WORKING); - ProductStockRequest productStockRequestEntity = requestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); - productStockRequestEntity = requestService.updateState(productStockRequestEntity, DT_RequestStateEnum.WORKING); + ProductStockRequest productStockRequestEntity = productStockRequestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); + productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.WORKING); String requestingPartnerBpnl = requestDto.getHeader().getSender(); Partner requestingPartner = partnerService.findByBpnl(requestingPartnerBpnl); @@ -136,7 +134,7 @@ public void handleRequest(RequestDto requestDto) { if (requestDto.getHeader().getSenderEdc() != null && !partnerIdsUrl.equals(requestDto.getHeader().getSenderEdc())) { log.warn("Partner " + requestingPartner.getName() + " is using unknown idsUrl: " + requestDto.getHeader().getSenderEdc()); log.warn("Request will not be processed"); - requestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); + productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); return; } @@ -247,7 +245,7 @@ public void handleRequest(RequestDto requestDto) { var data = edcAdapterService.getContractForResponseApi(partnerIdsUrl); if(data == null) { log.error("Failed to contract response api from " + partnerIdsUrl); - productStockRequestEntity = requestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); + productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); log.info("Request status: \n" + productStockRequestEntity.toString()); return; } @@ -280,10 +278,10 @@ public void handleRequest(RequestDto requestDto) { endpoint, authKey, authCode, requestBody); log.info(response.body().string()); response.body().close(); - productStockRequestEntity = requestService.updateState(productStockRequestEntity, DT_RequestStateEnum.COMPLETED); + productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.COMPLETED); } catch (Exception e) { log.error("Failed to send response to " + responseDto.getHeader().getReceiver(), e); - productStockRequestEntity = requestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); + productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); } finally { log.info("Request status: \n" + productStockRequestEntity.toString()); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/RequestService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java similarity index 96% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/RequestService.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java index 05999cd8..f38341cb 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/RequestService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java @@ -18,7 +18,7 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.eclipse.tractusx.puris.backend.common.api.logic.service; +package org.eclipse.tractusx.puris.backend.stock.logic.service; import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; @@ -30,7 +30,7 @@ * Service providing the interface to the {@link ProductStockRequest} */ @Service -public interface RequestService { +public interface ProductStockRequestService { /** * create a {@link ProductStockRequest} in state REQUESTED. diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/RequestServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java similarity index 92% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/RequestServiceImpl.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java index cfd96a4e..70147070 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/service/RequestServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java @@ -19,11 +19,11 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.eclipse.tractusx.puris.backend.common.api.logic.service; +package org.eclipse.tractusx.puris.backend.stock.logic.service; import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; -import org.eclipse.tractusx.puris.backend.common.api.domain.repository.ProductStockRequestRepository; +import org.eclipse.tractusx.puris.backend.stock.domain.repository.ProductStockRequestRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,7 +31,7 @@ import java.util.UUID; @Service -public class RequestServiceImpl implements RequestService { +public class ProductStockRequestServiceImpl implements ProductStockRequestService { @Autowired private ProductStockRequestRepository productStockRequestRepository; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java index 70590b5b..499187bf 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java @@ -22,13 +22,11 @@ package org.eclipse.tractusx.puris.backend.stock.logic.service; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.controller.exception.RequestIdNotFoundException; import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentErrorDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.ResponseDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.ResponseApiService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; @@ -55,7 +53,7 @@ public class ProductStockResponseApiServiceImpl implements ResponseApiService { @Autowired - private RequestService requestService; + private ProductStockRequestService productStockRequestService; @Autowired private PartnerProductStockService partnerProductStockService; @@ -128,12 +126,12 @@ public void consumeResponse(ResponseDto responseDto) { } // Update status - also only MessageContentErrorDtos would be completed - requestService.updateState(correspondingProductStockRequest, DT_RequestStateEnum.COMPLETED); + productStockRequestService.updateState(correspondingProductStockRequest, DT_RequestStateEnum.COMPLETED); } private ProductStockRequest findCorrespondingRequest(ResponseDto responseDto) { UUID requestId = responseDto.getHeader().getRequestId(); - return requestService.findRequestByHeaderUuid(requestId); + return productStockRequestService.findRequestByHeaderUuid(requestId); } From 739c17d908297dd4581bb32bbcbe570314d75634 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Thu, 3 Aug 2023 17:12:38 +0200 Subject: [PATCH 8/9] refactor: removed apimarshalling service --- .../DataInjectionCommandLineRunner.java | 199 +++++++++--------- .../common/api/domain/model/Message.java | 2 + .../api/domain/model/MessageHeader.java | 2 + .../model/datatype/DT_RequestStateEnum.java | 4 +- .../common/api/logic/dto/MessageDto.java | 4 +- .../common/api/logic/dto/RequestDto.java | 4 +- .../common/api/logic/dto/ResponseDto.java | 2 +- .../ProductStockRequestApiController.java | 63 ++---- .../ProductStockResponseApiController.java | 29 +-- .../stock/controller/StockController.java | 57 ++--- .../domain/model/ProductStockRequest.java | 28 ++- .../model/ProductStockRequestForMaterial.java | 2 +- .../domain/model/ProductStockResponse.java | 44 ++-- .../ProductStockRequestRepository.java | 2 +- .../ProductStockResponseRepository.java | 12 +- .../logic/adapter/ApiMarshallingService.java | 153 -------------- .../logic/dto/ProductStockRequestDto.java | 4 +- .../ProductStockRequestApiServiceImpl.java | 126 +++++------ .../service/ProductStockRequestService.java | 2 +- .../ProductStockRequestServiceImpl.java | 2 +- .../ProductStockResponseApiServiceImpl.java | 29 +-- 21 files changed, 285 insertions(+), 485 deletions(-) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/{common/api => stock}/domain/model/ProductStockRequest.java (75%) rename backend/src/main/java/org/eclipse/tractusx/puris/backend/{common/api => stock}/domain/model/ProductStockResponse.java (61%) delete mode 100644 backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ApiMarshallingService.java diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java index 80d82085..f0acac2f 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java @@ -24,11 +24,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentErrorDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequestForMaterial; import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; @@ -39,11 +39,7 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.MaterialStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; -import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; import org.eclipse.tractusx.puris.backend.stock.logic.service.MaterialStockService; import org.eclipse.tractusx.puris.backend.stock.logic.service.PartnerProductStockService; @@ -54,7 +50,6 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; @@ -95,9 +90,6 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner { @Value("${puris.demonstrator.role}") private String demoRole; - @Autowired - private ApiMarshallingService apiMarshallingService; - private ObjectMapper objectMapper; private final String semiconductorMatNbrCustomer = "MNR-7307-AU340474.002"; @@ -324,98 +316,99 @@ private Material getNewCentralControlUnitMaterial() { } private void createRequest() throws JsonProcessingException { - MessageHeaderDto messageHeaderDto = new MessageHeaderDto(); - messageHeaderDto.setRequestId(UUID.fromString("4979893e-dd6b-43db-b732-6e48b4ba35b3")); - messageHeaderDto.setRespondAssetId("product-stock-response-api"); - messageHeaderDto.setContractAgreementId("some cid"); - messageHeaderDto.setSender("BPNL1234567890ZZ"); - messageHeaderDto.setSenderEdc("http://plato-controlplane:8084/api/v1/ids"); - messageHeaderDto.setReceiver("BPNL4444444444XX"); - messageHeaderDto.setUseCase(DT_UseCaseEnum.PURIS); - messageHeaderDto.setCreationDate(new Date()); - - log.info(objectMapper.writeValueAsString(messageHeaderDto)); - - List messageContentDtos = new ArrayList<>(); - - ProductStockRequestForMaterialDto messageContentDto = new ProductStockRequestForMaterialDto(); - messageContentDto.setMaterialNumberCustomer("CU-MNR"); - messageContentDto.setMaterialNumberSupplier("SU-MNR"); - messageContentDtos.add(messageContentDto); - messageContentDto = new ProductStockRequestForMaterialDto(); - messageContentDto.setMaterialNumberCustomer("OtherCU-MNR"); - messageContentDto.setMaterialNumberSupplier("OtherSU-MNR"); - messageContentDtos.add(messageContentDto); - - ProductStockRequestDto requestDto = new ProductStockRequestDto( - DT_RequestStateEnum.RECEIPT, - messageHeaderDto, - messageContentDtos - ); - ProductStockRequest createdProductStockRequest = productStockRequestService.createRequest(modelMapper.map(requestDto, - ProductStockRequest.class)); - log.info(String.format("Created Request: %s", createdProductStockRequest)); - log.info(createdProductStockRequest.getPayload().get(0).getClass().toString()); - - log.info("Testing ApiMarshallingService:"); - String transformationTest = apiMarshallingService.transformProductStockRequest(requestDto); - log.info("marshalled request to be sent:\n" + transformationTest); - - ProductStockRequestDto productStockRequestDto = apiMarshallingService.transformToProductStockRequestDto(transformationTest); - log.info("unmarshalled the same request as productStockRequestDto: \n" + productStockRequestDto.toString()); - - String sampleResponse = "{\n" + - " \"header\" : {\n" + - " \"requestId\" : \"37be1c8e-e2c3-4fbc-848f-9ee576cecc9f\",\n" + - " \"respondAssetId\" : null,\n" + - " \"creationDate\" : \"2023-07-27T10:56:43.116+00:00\",\n" + - " \"senderEdc\" : \"http://plato-controlplane:8084/api/v1/ids\",\n" + - " \"sender\" : \"BPNL1234567890ZZ\",\n" + - " \"receiver\" : \"BPNL4444444444XX\",\n" + - " \"useCase\" : \"PURIS\",\n" + - " \"contractAgreementId\" : \"product-stock-response-api:c51b78d3-06fa-4539-8d3a-2a0fb19780ba\"\n" + - " },\n" + - " \"content\" : {\n" + - " \"productStock\" : [ {\n" + - " \"positions\" : [ {\n" + - " \"orderPositionReference\" : null,\n" + - " \"lastUpdatedOnDateTime\" : 1690455395379,\n" + - " \"allocatedStocks\" : [ {\n" + - " \"quantityOnAllocatedStock\" : {\n" + - " \"quantityNumber\" : 20.0,\n" + - " \"measurementUnit\" : \"unit:piece\"\n" + - " },\n" + - " \"supplierStockLocationId\" : {\n" + - " \"locationIdType\" : \"BPNS\",\n" + - " \"locationId\" : \"BPNS1234567890ZZ\"\n" + - " }\n" + - " } ]\n" + - " } ],\n" + - " \"materialNumberCustomer\" : \"MNR-7307-AU340474.002\",\n" + - " \"materialNumberCatenaX\" : {\n" + - " \"empty\" : true,\n" + - " \"present\" : false\n" + - " },\n" + - " \"materialNumberSupplier\" : {\n" + - " \"empty\" : false,\n" + - " \"present\" : true\n" + - " }\n" + - " } ]\n" + - " }\n" + - "}\n"; - ProductStockResponseDto productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(sampleResponse); - // insert a MessageContentErrorDto - MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer("Sample MaterialNumber"); - messageContentErrorDto.setError("Sample Error"); - messageContentErrorDto.setMessage("Sample Error Message"); - productStockResponseDto.getPayload().add(messageContentErrorDto); - log.info(productStockResponseDto.toString()); - String productStockResponseString = apiMarshallingService.transformProductStockResponse(productStockResponseDto); - log.info("marshalled sample response: \n" + productStockResponseString); - productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(productStockResponseString); - log.info("unmarshalled header: \n" + productStockResponseDto.getHeader()); - log.info("unmarshalled content: \n" + productStockResponseDto.getPayload()); + + ProductStockRequest request = new ProductStockRequest(); + MessageHeader messageHeader = new MessageHeader(); + messageHeader.setRequestId(UUID.fromString("4979893e-dd6b-43db-b732-6e48b4ba35b3")); + messageHeader.setRespondAssetId("product-stock-response-api"); + messageHeader.setContractAgreementId("some cid"); + messageHeader.setSender("BPNL1234567890ZZ"); + messageHeader.setSenderEdc("http://plato-controlplane:8084/api/v1/ids"); + messageHeader.setReceiver("BPNL4444444444XX"); + messageHeader.setUseCase(DT_UseCaseEnum.PURIS); + messageHeader.setCreationDate(new Date()); + request.setHeader(messageHeader); + + var productStock = request.getContent().getProductStock(); + ProductStockRequestForMaterial rfm = new ProductStockRequestForMaterial("CU-MNR", + null, "SU-MNR"); + productStock.add(rfm); + + rfm = new ProductStockRequestForMaterial("OtherCU-MNR", + null, "OtherSU-MNR"); + productStock.add(rfm); + request.setState(DT_RequestStateEnum.WORKING); + + request = productStockRequestService.createRequest(request); + + + + String stringOutput = objectMapper.writeValueAsString(request); + log.info("SAMPLE-Request\n" + objectMapper.readTree(stringOutput).toPrettyString()); + + var deserializedRequest = objectMapper.readValue(stringOutput, ProductStockRequest.class); + log.info(deserializedRequest.toString()); + + +// log.info("Testing ApiMarshallingService:"); +// String transformationTest = apiMarshallingService.transformProductStockRequest(requestDto); +// log.info("marshalled request to be sent:\n" + transformationTest); +// +// ProductStockRequestDto productStockRequestDto = apiMarshallingService.transformToProductStockRequestDto(transformationTest); +// log.info("unmarshalled the same request as productStockRequestDto: \n" + productStockRequestDto.toString()); +// +// String sampleResponse = "{\n" + +// " \"header\" : {\n" + +// " \"requestId\" : \"37be1c8e-e2c3-4fbc-848f-9ee576cecc9f\",\n" + +// " \"respondAssetId\" : null,\n" + +// " \"creationDate\" : \"2023-07-27T10:56:43.116+00:00\",\n" + +// " \"senderEdc\" : \"http://plato-controlplane:8084/api/v1/ids\",\n" + +// " \"sender\" : \"BPNL1234567890ZZ\",\n" + +// " \"receiver\" : \"BPNL4444444444XX\",\n" + +// " \"useCase\" : \"PURIS\",\n" + +// " \"contractAgreementId\" : \"product-stock-response-api:c51b78d3-06fa-4539-8d3a-2a0fb19780ba\"\n" + +// " },\n" + +// " \"content\" : {\n" + +// " \"productStock\" : [ {\n" + +// " \"positions\" : [ {\n" + +// " \"orderPositionReference\" : null,\n" + +// " \"lastUpdatedOnDateTime\" : 1690455395379,\n" + +// " \"allocatedStocks\" : [ {\n" + +// " \"quantityOnAllocatedStock\" : {\n" + +// " \"quantityNumber\" : 20.0,\n" + +// " \"measurementUnit\" : \"unit:piece\"\n" + +// " },\n" + +// " \"supplierStockLocationId\" : {\n" + +// " \"locationIdType\" : \"BPNS\",\n" + +// " \"locationId\" : \"BPNS1234567890ZZ\"\n" + +// " }\n" + +// " } ]\n" + +// " } ],\n" + +// " \"materialNumberCustomer\" : \"MNR-7307-AU340474.002\",\n" + +// " \"materialNumberCatenaX\" : {\n" + +// " \"empty\" : true,\n" + +// " \"present\" : false\n" + +// " },\n" + +// " \"materialNumberSupplier\" : {\n" + +// " \"empty\" : false,\n" + +// " \"present\" : true\n" + +// " }\n" + +// " } ]\n" + +// " }\n" + +// "}\n"; +// ProductStockResponseDto productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(sampleResponse); +// // insert a MessageContentErrorDto +// MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); +// messageContentErrorDto.setMaterialNumberCustomer("Sample MaterialNumber"); +// messageContentErrorDto.setError("Sample Error"); +// messageContentErrorDto.setMessage("Sample Error Message"); +// productStockResponseDto.getPayload().add(messageContentErrorDto); +// log.info(productStockResponseDto.toString()); +// String productStockResponseString = apiMarshallingService.transformProductStockResponse(productStockResponseDto); +// log.info("marshalled sample response: \n" + productStockResponseString); +// productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(productStockResponseString); +// log.info("unmarshalled header: \n" + productStockResponseDto.getHeader()); +// log.info("unmarshalled content: \n" + productStockResponseDto.getPayload()); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/Message.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/Message.java index dae0bdbc..2aaba866 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/Message.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/Message.java @@ -26,6 +26,8 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import java.util.ArrayList; import java.util.List; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/MessageHeader.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/MessageHeader.java index e4e5b576..a705c165 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/MessageHeader.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/MessageHeader.java @@ -27,6 +27,8 @@ import lombok.Setter; import lombok.ToString; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import java.util.Date; import java.util.UUID; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/datatype/DT_RequestStateEnum.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/datatype/DT_RequestStateEnum.java index 16ea4662..a92ea1c1 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/datatype/DT_RequestStateEnum.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/datatype/DT_RequestStateEnum.java @@ -20,8 +20,8 @@ */ package org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; /** * Enum to track the status of diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/MessageDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/MessageDto.java index 6fb195cf..174fbae0 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/MessageDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/MessageDto.java @@ -27,8 +27,8 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import java.util.ArrayList; import java.util.List; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/RequestDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/RequestDto.java index 69b41e73..cea3c6ed 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/RequestDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/RequestDto.java @@ -24,8 +24,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotNull; import lombok.*; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/ResponseDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/ResponseDto.java index 4dcb6ff6..79077162 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/ResponseDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/logic/dto/ResponseDto.java @@ -24,7 +24,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; /** * Dto for {@link ProductStockResponse} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java index ecb37b83..f38aab02 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockRequestApiController.java @@ -25,26 +25,19 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.RequestDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfulRequestDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestApiService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestApiServiceImpl; import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; -import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequestForMaterial; -import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestDto; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; @Slf4j @@ -62,32 +55,34 @@ public class ProductStockRequestApiController { ProductStockRequestService productStockRequestService; @Autowired - RequestApiService requestApiService; + ProductStockRequestApiServiceImpl requestApiService; @Autowired EdcAdapterService edcAdapterService; - @Autowired - ApiMarshallingService apiMarshallingService; - @PostMapping("request") public ResponseEntity postRequest(@RequestBody String requestBody) { log.info("product-stock/request called: \n" + requestBody); - ProductStockRequestDto productStockRequestDto = null; + ProductStockRequest productStockRequest = null; try { - productStockRequestDto = apiMarshallingService.transformToProductStockRequestDto(requestBody); + productStockRequest = objectMapper.readValue(requestBody, ProductStockRequest.class); } catch (Exception e) { - log.error("Failed to deserialize body of incoming message", e); - return ResponseEntity.status(HttpStatusCode.valueOf(422)).build(); + log.error("Malformed request received"); + return ResponseEntity.status(400).build(); } - if (productStockRequestDto.getHeader() == null || productStockRequestDto.getHeader().getRequestId() == null) { + if (productStockRequest.getHeader() == null || productStockRequest.getHeader().getRequestId() == null) { log.error("No RequestId provided!"); - return ResponseEntity.status(422).build(); + return ResponseEntity.status(400).build(); + } + + if (productStockRequest.getHeader().getSender() == null) { + log.error("No sender attribute in header"); + return ResponseEntity.status(400).build(); } - UUID requestId = productStockRequestDto.getHeader().getRequestId(); + UUID requestId = productStockRequest.getHeader().getRequestId(); ProductStockRequest productStockRequestFound = productStockRequestService.findRequestByHeaderUuid(requestId); @@ -98,32 +93,12 @@ public ResponseEntity postRequest(@RequestBody String requestBody) { return ResponseEntity.status(422).build(); } - productStockRequestDto.setState(DT_RequestStateEnum.RECEIPT); - try { - ProductStockRequest newProductStockRequestEntity = new ProductStockRequest(); - newProductStockRequestEntity.setHeader(modelMapper.map(productStockRequestDto.getHeader(), MessageHeader.class)); - List payload = new ArrayList<>(); - for (var payloadItem : productStockRequestDto.getPayload()) { - payload.add(modelMapper.map(payloadItem, ProductStockRequestForMaterial.class)); - } - newProductStockRequestEntity.setPayload(payload); - newProductStockRequestEntity.setState(productStockRequestDto.getState()); - productStockRequestService.createRequest(newProductStockRequestEntity); - log.info("Persisted incoming request " + productStockRequestDto.getHeader().getRequestId()); - } catch (Exception e) { - log.warn("Failed to persist incoming request " + productStockRequestDto.getHeader().getRequestId()); - } - - // handling the request and responding should be done asynchronously. - final RequestDto threadRequestDto = new RequestDto( - productStockRequestDto.getState(), - productStockRequestDto.getUuid(), - productStockRequestDto.getHeader(), - productStockRequestDto.getPayload() - ); + productStockRequest.setState(DT_RequestStateEnum.RECEIPT); + productStockRequestService.createRequest(productStockRequest); + final ProductStockRequest requestForAsyncThread = productStockRequest; Thread respondAsyncThread = new Thread(() -> { - requestApiService.handleRequest(threadRequestDto); + requestApiService.handleRequest(requestForAsyncThread); }); respondAsyncThread.start(); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java index 57f6bcf5..6248abed 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/ProductStockResponseApiController.java @@ -23,13 +23,12 @@ import java.util.UUID; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.SuccessfulRequestDto; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.ResponseApiService; -import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockResponseApiServiceImpl; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatusCode; @@ -58,32 +57,26 @@ public class ProductStockResponseApiController { ObjectMapper objectMapper; @Autowired - ApiMarshallingService apiMarshallingService; - - private ResponseApiService responseApiService; - - public ProductStockResponseApiController(ResponseApiService responseApiService) { - this.responseApiService = responseApiService; - } + ProductStockResponseApiServiceImpl productStockResponseApiService; @PostMapping("response") public ResponseEntity postResponse(@RequestBody String body) { - ProductStockResponseDto productStockResponseDto = null; + ProductStockResponse productStockResponse = null; try { - productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(body); - log.info(objectMapper.readTree(objectMapper.writeValueAsString(productStockResponseDto)).toPrettyString()); + productStockResponse = objectMapper.readValue(body, ProductStockResponse.class); + log.info(objectMapper.readTree(objectMapper.writeValueAsString(productStockResponse)).toPrettyString()); } catch (Exception e) { log.error("Failed to deserialize body of incoming message", e); return ResponseEntity.status(HttpStatusCode.valueOf(422)).build(); } - if (productStockResponseDto.getHeader() == null || productStockResponseDto.getHeader().getRequestId() == null) { + if (productStockResponse.getHeader() == null || productStockResponse.getHeader().getRequestId() == null) { log.error("No RequestId provided!"); return ResponseEntity.status(422).build(); } - UUID requestId = productStockResponseDto.getHeader().getRequestId(); + UUID requestId = productStockResponse.getHeader().getRequestId(); ProductStockRequest productStockRequestFound = productStockRequestService.findRequestByHeaderUuid(requestId); if (productStockRequestFound == null) { @@ -93,8 +86,8 @@ public ResponseEntity postResponse(@RequestBody String body) { log.info("Got response for request Id " + requestId); } - productStockRequestFound = productStockRequestService.updateState(productStockRequestFound, DT_RequestStateEnum.COMPLETED); - responseApiService.consumeResponse(productStockResponseDto); + productStockRequestService.updateState(productStockRequestFound, DT_RequestStateEnum.COMPLETED); + productStockResponseApiService.consumeResponse(productStockResponse); // if the request has been correctly taken over, return 202 return ResponseEntity.status(HttpStatusCode.valueOf(202)).body(new SuccessfulRequestDto(requestId)); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java index 27b187c1..13bf4e1c 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java @@ -24,10 +24,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.squareup.okhttp.Response; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; +import org.eclipse.tractusx.puris.backend.stock.domain.model.*; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.common.api.logic.service.VariablesService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; @@ -39,10 +39,6 @@ import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; -import org.eclipse.tractusx.puris.backend.stock.domain.model.MaterialStock; -import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; -import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; -import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; import org.eclipse.tractusx.puris.backend.stock.logic.dto.*; import org.eclipse.tractusx.puris.backend.stock.logic.service.MaterialStockService; import org.eclipse.tractusx.puris.backend.stock.logic.service.PartnerProductStockService; @@ -52,7 +48,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; @@ -102,9 +97,6 @@ public class StockController { @Autowired private EdcAdapterService edcAdapterService; - @Autowired - private ApiMarshallingService apiMarshallingService; - @Autowired private VariablesService variablesService; @@ -296,11 +288,10 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara List allSupplierPartnerEntities = mprService.findAllSuppliersForOwnMaterialNumber(ownMaterialNumber); - List messageContentDtos = new ArrayList<>(); - - for (Partner supplierPartner : allSupplierPartnerEntities) { + ProductStockRequest productStockRequest = new ProductStockRequest(); + MaterialPartnerRelation materialPartnerRelation = mprService.find(materialEntity, supplierPartner); if (materialPartnerRelation == null) { @@ -308,12 +299,12 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara continue; } - ProductStockRequestForMaterialDto materialDto = new ProductStockRequestForMaterialDto( + ProductStockRequestForMaterial material = new ProductStockRequestForMaterial( materialEntity.getOwnMaterialNumber(), materialEntity.getMaterialNumberCx(), materialPartnerRelation.getPartnerMaterialNumber() ); - messageContentDtos.add(materialDto); + productStockRequest.getContent().getProductStock().add(material); String [] data = edcAdapterService.getContractForRequestApi(supplierPartner.getEdcUrl()); if(data == null) { @@ -329,45 +320,39 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara } String cid = data[3]; - MessageHeaderDto messageHeaderDto = new MessageHeaderDto(); + MessageHeader messageHeader = new MessageHeader(); UUID randomUuid = UUID.randomUUID(); // Avoid randomly choosing a UUID that was already used by this customer. while (productStockRequestService.findRequestByHeaderUuid(randomUuid) != null) { randomUuid = UUID.randomUUID(); } - messageHeaderDto.setRequestId(randomUuid); - messageHeaderDto.setRespondAssetId(variablesService.getResponseApiAssetId()); - messageHeaderDto.setContractAgreementId(cid); - messageHeaderDto.setSender(ownBpnl); - messageHeaderDto.setSenderEdc(ownEdcIdsUrl); + messageHeader.setRequestId(randomUuid); + messageHeader.setRespondAssetId(variablesService.getResponseApiAssetId()); + messageHeader.setContractAgreementId(cid); + messageHeader.setSender(ownBpnl); + messageHeader.setSenderEdc(ownEdcIdsUrl); // set receiver per partner - messageHeaderDto.setReceiver(supplierPartner.getBpnl()); - messageHeaderDto.setUseCase(DT_UseCaseEnum.PURIS); - messageHeaderDto.setCreationDate(new Date()); - - ProductStockRequestDto requestDto = new ProductStockRequestDto( - DT_RequestStateEnum.REQUESTED, - messageHeaderDto, - messageContentDtos - ); + messageHeader.setReceiver(supplierPartner.getBpnl()); + messageHeader.setUseCase(DT_UseCaseEnum.PURIS); + messageHeader.setCreationDate(new Date()); + - - ProductStockRequest productStockRequest = modelMapper.map(requestDto, ProductStockRequest.class); + productStockRequest.setHeader(messageHeader); productStockRequest.setState(DT_RequestStateEnum.WORKING); - log.debug("Setting request state to " + DT_RequestStateEnum.WORKING); productStockRequest = productStockRequestService.createRequest(productStockRequest); - var test = productStockRequestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); - log.debug("Stored in Database " + (test != null) + " " + requestDto.getHeader().getRequestId()); + var test = productStockRequestService.findRequestByHeaderUuid(productStockRequest.getHeader().getRequestId()); + log.debug("Stored in Database " + (test != null) + " " + productStockRequest.getHeader().getRequestId()); Response response = null; try { - String requestBody = apiMarshallingService.transformProductStockRequest(requestDto); + String requestBody = objectMapper.writeValueAsString(productStockRequest); response = edcAdapterService.sendDataPullRequest(endpoint, authKey, authCode, requestBody); log.debug(response.body().string()); if(response.code() < 400) { productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.REQUESTED); log.debug("Sent request and received HTTP Status code " + response.code()); log.debug("Setting request state to " + DT_RequestStateEnum.REQUESTED); + productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.REQUESTED); } else { log.warn("Receviced HTTP Status Code " + response.code() + " for request " + productStockRequest.getHeader().getRequestId() + " from " + productStockRequest.getHeader().getReceiver()); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/ProductStockRequest.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequest.java similarity index 75% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/ProductStockRequest.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequest.java index c2405a05..7f6bfb74 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/ProductStockRequest.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequest.java @@ -18,16 +18,18 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.eclipse.tractusx.puris.backend.common.api.domain.model; +package org.eclipse.tractusx.puris.backend.stock.domain.model; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; -import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequestForMaterial; import java.util.ArrayList; import java.util.List; @@ -52,10 +54,12 @@ public class ProductStockRequest { * @see DT_RequestStateEnum */ @NotNull + @JsonIgnore private DT_RequestStateEnum state; @Id @GeneratedValue + @JsonIgnore /** * Technical identifier for a Message. */ @@ -67,10 +71,20 @@ public class ProductStockRequest { @Embedded private MessageHeader header; - /** - * List of actual content of the payload. - */ - @ElementCollection - private List payload = new ArrayList<>(); + private ContentWrapper content = new ContentWrapper(); + + @Embeddable + @Getter + @Setter + @ToString + public static class ContentWrapper { + + /** + * List of actual content of the payload. + */ + @ElementCollection + private List productStock = new ArrayList<>(); + + } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequestForMaterial.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequestForMaterial.java index 376a9174..d8b23333 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequestForMaterial.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockRequestForMaterial.java @@ -41,7 +41,7 @@ @Setter @NoArgsConstructor @AllArgsConstructor -@ToString(callSuper = true) +@ToString public class ProductStockRequestForMaterial { @NotNull diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/ProductStockResponse.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockResponse.java similarity index 61% rename from backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/ProductStockResponse.java rename to backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockResponse.java index f86eddef..07962625 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/common/api/domain/model/ProductStockResponse.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/ProductStockResponse.java @@ -18,13 +18,17 @@ * * SPDX-License-Identifier: Apache-2.0 */ -package org.eclipse.tractusx.puris.backend.common.api.domain.model; +package org.eclipse.tractusx.puris.backend.stock.domain.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageContent; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; import java.util.ArrayList; import java.util.List; @@ -36,19 +40,20 @@ * This Response may not be confused with an HTTP response. * Both, the Response and the Request, are called (api) request. */ -@Entity + @Getter @Setter @ToString @NoArgsConstructor public class ProductStockResponse { - @Id - @GeneratedValue - /** - * Technical identifier for a Message. - */ - private UUID uuid; +// @Id +// @GeneratedValue +// @JsonIgnore +// /** +// * Technical identifier for a Message. +// */ +// private UUID uuid; /** * Steering information {@link ProductStockResponse} api message. @@ -56,12 +61,21 @@ public class ProductStockResponse { @Embedded private MessageHeader header; - /** - * List of actual content of the payload. - *

- * May contain also errors. - */ - @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) - private List payload = new ArrayList<>(); +// /** +// * List of actual content of the payload. +// *

+// * May contain also errors. +// */ +// @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) +// private List payload = new ArrayList<>(); + + private ContentWrapper content = new ContentWrapper(); + + @Getter + @Setter + @ToString + public static class ContentWrapper { + List productStocks = new ArrayList<>(); + } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java index 7512a499..19d6608f 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockRequestRepository.java @@ -20,7 +20,7 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.repository; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java index 07ab0c16..67332d1b 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/repository/ProductStockResponseRepository.java @@ -20,7 +20,8 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.repository; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -30,12 +31,7 @@ * Repository to access Responses */ @Repository -public interface ProductStockResponseRepository extends JpaRepository { +public interface ProductStockResponseRepository extends JpaRepository { + - /** - * find the request by the requestUuuid from the message's header - * - * @param headerRequestUuid uuid set by the sending partner in the header - * @return Request - */ } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ApiMarshallingService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ApiMarshallingService.java deleted file mode 100644 index af84d285..00000000 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ApiMarshallingService.java +++ /dev/null @@ -1,153 +0,0 @@ -package org.eclipse.tractusx.puris.backend.stock.logic.adapter; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentErrorDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.Position; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; -import org.modelmapper.ModelMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Optional; - -/** - * A class that marshalls and unmarshalls product stock - * requests. - */ -@Component -@Slf4j -public class ApiMarshallingService { - - @Autowired - ObjectMapper objectMapper; - - @Autowired - ModelMapper modelMapper; - - /** - * This method transforms a ProductStockRequestDto into a String object that - * contains a JSON in accordance with the api definition that can be sent - * to the other party. - * @param productStockRequestDto a request you want to send - * @return a String carrying the information from the input object - */ - public String transformProductStockRequest(ProductStockRequestDto productStockRequestDto) { - var objectNode = objectMapper.createObjectNode(); - objectNode.set("header", objectMapper.convertValue(productStockRequestDto.getHeader(), JsonNode.class)); - var productStockArray = objectMapper.createArrayNode(); - for (var item : productStockRequestDto.getPayload()) { - productStockArray.addPOJO(item); - } - var contentObject = objectMapper.createObjectNode(); - contentObject.put("productStock", productStockArray); - objectNode.put("content", contentObject); - return objectNode.toString(); - } - - /** - * This method transforms a JsonNode received from a customer Partner - * and transforms it into a ProductStockRequestDto - * @param jsonData body from a customer message to the request api - * @return a ProductStockRequestDto carrying the same information - */ - public ProductStockRequestDto transformToProductStockRequestDto(String jsonData) { - try { - JsonNode jsonNode = objectMapper.readValue(jsonData, JsonNode.class); - ProductStockRequestDto productStockRequestDto = new ProductStockRequestDto(); - productStockRequestDto.setHeader(objectMapper.convertValue(jsonNode.get("header"), MessageHeaderDto.class)); - for (var item : jsonNode.get("content").get("productStock") ) { - try { - ProductStockRequestForMaterialDto itemDto = objectMapper.readValue(item.toString(), ProductStockRequestForMaterialDto.class); - productStockRequestDto.getPayload().add(itemDto); - } catch (Exception e) { - log.error("Failed to unmarshall list item: \n" + item.toPrettyString()); - } - } - return productStockRequestDto; - } catch (Exception e) { - log.error(e.getMessage()); - throw new RuntimeException("Unmarshalling of object failed: \n" +jsonData); - } - } - - /** - * This method transforms a ProductStockResponseDto into a String object that - * contains a JSON in accordance with the api definition that can be sent to the - * other party. - * @param responseDto a response you received - * @return a String carrying the information from the input object - */ - public String transformProductStockResponse(ProductStockResponseDto responseDto) { - var objectNode = objectMapper.createObjectNode(); - objectNode.set("header", objectMapper.convertValue(responseDto.getHeader(), JsonNode.class)); - var productStockArray = objectMapper.createArrayNode(); - for (var item : responseDto.getPayload()) { - productStockArray.addPOJO(item); - } - var contentObject = objectMapper.createObjectNode(); - contentObject.set("productStock", productStockArray); - objectNode.set("content", contentObject); - return objectNode.toString(); - } - - /** - * This method transforms a JsonNode received from a customer Partner - * and transforms it into a ProductStockResponseDto - * @param jsonData body from a customer message to the response api - * @return a ProductStockResponseDto carrying the same information - */ - public ProductStockResponseDto transformToProductStockResponseDto(String jsonData) { - try { - JsonNode jsonNode = objectMapper.readValue(jsonData, JsonNode.class); - ProductStockResponseDto responseDto = new ProductStockResponseDto(); - responseDto.setHeader(objectMapper.convertValue(jsonNode.get("header"), MessageHeaderDto.class)); - boolean malformedMessage = responseDto.getHeader().getRequestId() == null || responseDto.getHeader().getSender() == null; - if (malformedMessage) { - throw new RuntimeException("MALFORMED MESSAGE"); - } - for (var item : jsonNode.get("content").get("productStock")) { - try { - MessageContentErrorDto errorDto = objectMapper.readValue(item.toString(), MessageContentErrorDto.class); - responseDto.getPayload().add(errorDto); - } catch (Exception e) { - try { - ProductStockSammDto sammDto = objectMapper.readValue(item.toString(), ProductStockSammDto.class); - responseDto.getPayload().add(sammDto); - } catch (Exception e1) { - // Handle empty optionals manually - try { - Collection positionCollection = new ArrayList<>(); - var positionArray = item.get("positions"); - for (var position : positionArray) { - Position positionDto = objectMapper.readValue(position.toString(), Position.class); - positionCollection.add(positionDto); - } - String materialNumberCustomer = item.get("materialNumberCustomer").asText(); - String materialNumberSupplier = item.get("materialNumberSupplier").asText().length() == 0 - ? null : item.get("materialNumberSupplier").asText(); - String materialNumberCatenaX = item.get("materialNumberCatenaX").asText().length() == 0 - ? null : item.get("materialNumberCatenaX").asText(); - Optional supplier = Optional.ofNullable(materialNumberSupplier); - Optional catenaX = Optional.ofNullable(materialNumberCatenaX); - responseDto.getPayload().add(new ProductStockSammDto(positionCollection, materialNumberCustomer, catenaX, supplier)); - } catch (Exception e2) { - log.error("Failed to unmarshall list item: \n" + item.toPrettyString()); - } - } - } - } - return responseDto; - } catch (Exception e) { - log.error(e.getMessage()); - throw new RuntimeException("Unmarshalling of object failed: \n" +jsonData); - } - } -} diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockRequestDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockRequestDto.java index e0c5f77f..e34d60d6 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockRequestDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockRequestDto.java @@ -24,8 +24,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotNull; import lombok.*; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockResponse; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java index a6351119..d2cc0b56 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java @@ -21,12 +21,13 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.*; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestApiService; import org.eclipse.tractusx.puris.backend.common.edc.logic.service.EdcAdapterService; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; @@ -34,10 +35,10 @@ import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; -import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequestForMaterial; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -58,7 +59,7 @@ */ @Component @Slf4j -public class ProductStockRequestApiServiceImpl implements RequestApiService { +public class ProductStockRequestApiServiceImpl { @Autowired private ProductStockRequestService productStockRequestService; @@ -85,7 +86,7 @@ public class ProductStockRequestApiServiceImpl implements RequestApiService { private ModelMapper modelMapper; @Autowired - private ApiMarshallingService apiMarshallingService; + private ObjectMapper objectMapper; @Value("${edc.dataplane.public.port}") String dataPlanePort; @@ -111,57 +112,46 @@ public static Predicate distinctByKey( return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; } - @Override - public void handleRequest(RequestDto requestDto) { - log.info(String.format("param requestDto %s", requestDto)); - requestDto.setState(DT_RequestStateEnum.WORKING); + public void handleRequest(ProductStockRequest productStockRequest) { - ProductStockRequest productStockRequestEntity = productStockRequestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); - productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.WORKING); - - String requestingPartnerBpnl = requestDto.getHeader().getSender(); + productStockRequest = productStockRequestService.updateState(productStockRequest,DT_RequestStateEnum.WORKING); + String requestingPartnerBpnl = productStockRequest.getHeader().getSender(); Partner requestingPartner = partnerService.findByBpnl(requestingPartnerBpnl); - if (requestingPartner == null) { - log.warn("Partner with BPNL " +requestingPartnerBpnl + " is unknown"); - log.warn("Request will not be processed"); - return; - } - String partnerIdsUrl = requestingPartner.getEdcUrl(); - if (requestDto.getHeader().getSenderEdc() != null && !partnerIdsUrl.equals(requestDto.getHeader().getSenderEdc())) { - log.warn("Partner " + requestingPartner.getName() + " is using unknown idsUrl: " + requestDto.getHeader().getSenderEdc()); + if (productStockRequest.getHeader().getSenderEdc() != null && !partnerIdsUrl.equals(productStockRequest.getHeader().getSenderEdc())) { + log.warn("Partner " + requestingPartner.getName() + " is using unknown idsUrl: " + productStockRequest.getHeader().getSenderEdc()); log.warn("Request will not be processed"); - productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); + productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); return; } - List resultProductStocks = new ArrayList<>(); + List resultProductStocks = new ArrayList<>(); - for (ProductStockRequestForMaterialDto productStockRequestDto : requestDto.getPayload()) { + for (ProductStockRequestForMaterial productStockRequestForMaterial : productStockRequest.getContent().getProductStock()) { // Check if product is known Material existingMaterial = null; - if (productStockRequestDto.getMaterialNumberCatenaX() != null) { + if (productStockRequestForMaterial.getMaterialNumberCatenaX() != null) { // Identify material via cx number, if possible existingMaterial = materialService - .findByMaterialNumberCx(productStockRequestDto.getMaterialNumberCatenaX()); + .findByMaterialNumberCx(productStockRequestForMaterial.getMaterialNumberCatenaX()); } if (existingMaterial == null) { // if material could not be found via cx number, try to use materialNumberCustomer - if (productStockRequestDto.getMaterialNumberCustomer() != null) { - var searchResult = mprService.findAllByPartnerMaterialNumber(productStockRequestDto.getMaterialNumberCustomer()); + if (productStockRequestForMaterial.getMaterialNumberCustomer() != null) { + var searchResult = mprService.findAllByPartnerMaterialNumber(productStockRequestForMaterial.getMaterialNumberCustomer()); if (searchResult.size() == 1) { existingMaterial = searchResult.get(0); } else { // MaterialNumberCustomer is ambiguous or unknown // try to use MaterialNumberSupplier - if (productStockRequestDto.getMaterialNumberSupplier() != null) { - existingMaterial = materialService.findByOwnMaterialNumber(productStockRequestDto.getMaterialNumberSupplier()); + if (productStockRequestForMaterial.getMaterialNumberSupplier() != null) { + existingMaterial = materialService.findByOwnMaterialNumber(productStockRequestForMaterial.getMaterialNumberSupplier()); } } } @@ -169,13 +159,13 @@ public void handleRequest(RequestDto requestDto) { if (existingMaterial == null) { MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer(productStockRequestDto.getMaterialNumberCustomer()); + messageContentErrorDto.setMaterialNumberCustomer(productStockRequestForMaterial.getMaterialNumberCustomer()); messageContentErrorDto.setError("PURIS-01"); messageContentErrorDto.setMessage("Material is unknown."); - resultProductStocks.add(messageContentErrorDto); +// resultProductStocks.add(messageContentErrorDto); log.warn(String.format("No Material found for ID Customer %s in request %s", - productStockRequestDto.getMaterialNumberCustomer(), - requestDto.getHeader().getRequestId())); + productStockRequestForMaterial.getMaterialNumberCustomer(), + productStockRequest.getHeader().getRequestId())); continue; } else { log.info("Found requested Material: " + existingMaterial.getOwnMaterialNumber()); @@ -186,33 +176,33 @@ public void handleRequest(RequestDto requestDto) { log.info("Requesting entity orders this Material? " + ordersProducts); if (!ordersProducts) { MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer(productStockRequestDto.getMaterialNumberCustomer()); + messageContentErrorDto.setMaterialNumberCustomer(productStockRequestForMaterial.getMaterialNumberCustomer()); messageContentErrorDto.setError("PURIS-02"); messageContentErrorDto.setMessage("Partner is not authorized."); - resultProductStocks.add(messageContentErrorDto); +// resultProductStocks.add(messageContentErrorDto); log.warn(String.format("Partner %s is not an ordering Partner of Material " + "found for ID Customer %s in request %s", requestingPartnerBpnl, - productStockRequestDto.getMaterialNumberCustomer(), - requestDto.getHeader().getRequestId())); + productStockRequestForMaterial.getMaterialNumberCustomer(), + productStockRequest.getHeader().getRequestId())); continue; } List productStocks = productStockService .findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl( - productStockRequestDto.getMaterialNumberCustomer(), + productStockRequestForMaterial.getMaterialNumberCustomer(), requestingPartnerBpnl); ProductStock productStock = null; if (productStocks.size() == 0) { MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer(productStockRequestDto.getMaterialNumberCustomer()); + messageContentErrorDto.setMaterialNumberCustomer(productStockRequestForMaterial.getMaterialNumberCustomer()); messageContentErrorDto.setError("PURIS-03"); messageContentErrorDto.setMessage("No Product Stock found."); - resultProductStocks.add(messageContentErrorDto); - log.warn("No Product Stocks of Material " + productStockRequestDto.getMaterialNumberCustomer() - + " found for " + requestDto.getHeader().getSender()); +// resultProductStocks.add(messageContentErrorDto); + log.warn("No Product Stocks of Material " + productStockRequestForMaterial.getMaterialNumberCustomer() + + " found for " + productStockRequest.getHeader().getSender()); continue; } else productStock = productStocks.get(0); @@ -226,8 +216,8 @@ public void handleRequest(RequestDto requestDto) { log.warn(String.format("More than one site is not yet supported per " + "partner. Product Stocks for material ID %s and partner %s in " + "request %s are accumulated", - productStockRequestDto.getMaterialNumberCustomer(), - requestingPartnerBpnl, requestDto.getHeader().getRequestId())); + productStockRequestForMaterial.getMaterialNumberCustomer(), + requestingPartnerBpnl, productStockRequest.getHeader().getRequestId())); } double quantity = productStocks.stream(). @@ -245,8 +235,8 @@ public void handleRequest(RequestDto requestDto) { var data = edcAdapterService.getContractForResponseApi(partnerIdsUrl); if(data == null) { log.error("Failed to contract response api from " + partnerIdsUrl); - productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); - log.info("Request status: \n" + productStockRequestEntity.toString()); + productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); + log.info("Request status: \n" + productStockRequest.toString()); return; } String authKey = data[0]; @@ -254,36 +244,36 @@ public void handleRequest(RequestDto requestDto) { String endpoint = data[2]; String contractId = data[3]; // prepare interface object - MessageHeaderDto messageHeaderDto = new MessageHeaderDto(); - messageHeaderDto.setRequestId(requestDto.getHeader().getRequestId()); - messageHeaderDto.setContractAgreementId(contractId); - messageHeaderDto.setSender(ownBPNL); - messageHeaderDto.setSenderEdc(ownEdcIdsUrl); + MessageHeader messageHeader = new MessageHeader(); + messageHeader.setRequestId(productStockRequest.getHeader().getRequestId()); + messageHeader.setContractAgreementId(contractId); + messageHeader.setSender(ownBPNL); + messageHeader.setSenderEdc(ownEdcIdsUrl); // set receiver per partner - messageHeaderDto.setReceiver(requestDto.getHeader().getSender()); - messageHeaderDto.setUseCase(DT_UseCaseEnum.PURIS); - messageHeaderDto.setCreationDate(new Date()); + messageHeader.setReceiver(productStockRequest.getHeader().getSender()); + messageHeader.setUseCase(DT_UseCaseEnum.PURIS); + messageHeader.setCreationDate(new Date()); - ProductStockResponseDto responseDto = new ProductStockResponseDto(); - responseDto.setHeader(messageHeaderDto); - responseDto.setPayload(resultProductStocks); + ProductStockResponse response = new ProductStockResponse(); + response.setHeader(messageHeader); + response.getContent().setProductStocks(resultProductStocks); if (applyDataplaneWorkaround) { log.info("Applying Dataplane Address Workaround"); endpoint = "http://" + dataPlaneHost + ":" + dataPlanePort + "/api/public"; } try { - String requestBody = apiMarshallingService.transformProductStockResponse(responseDto); - var response = edcAdapterService.sendDataPullRequest( + String requestBody = objectMapper.writeValueAsString(response); + var httpResponse = edcAdapterService.sendDataPullRequest( endpoint, authKey, authCode, requestBody); - log.info(response.body().string()); - response.body().close(); - productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.COMPLETED); + log.info(httpResponse.body().string()); + httpResponse.body().close(); + productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.COMPLETED); } catch (Exception e) { - log.error("Failed to send response to " + responseDto.getHeader().getReceiver(), e); - productStockRequestEntity = productStockRequestService.updateState(productStockRequestEntity, DT_RequestStateEnum.ERROR); + log.error("Failed to send response to " + response.getHeader().getReceiver(), e); + productStockRequest = productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.ERROR); } finally { - log.info("Request status: \n" + productStockRequestEntity.toString()); + log.info("Request status: \n" + productStockRequest.toString()); } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java index f38341cb..e00925ae 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestService.java @@ -20,7 +20,7 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.springframework.stereotype.Service; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java index 70147070..3a084d45 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestServiceImpl.java @@ -21,7 +21,7 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.stock.domain.repository.ProductStockRequestRepository; import org.springframework.beans.factory.annotation.Autowired; diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java index 499187bf..af3f39c6 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockResponseApiServiceImpl.java @@ -22,7 +22,7 @@ package org.eclipse.tractusx.puris.backend.stock.logic.service; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockRequest; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentDto; import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentErrorDto; @@ -31,6 +31,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; +import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; import org.eclipse.tractusx.puris.backend.stock.logic.dto.PartnerProductStockDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; @@ -50,7 +51,7 @@ */ @Component @Slf4j -public class ProductStockResponseApiServiceImpl implements ResponseApiService { +public class ProductStockResponseApiServiceImpl { @Autowired private ProductStockRequestService productStockRequestService; @@ -67,19 +68,18 @@ public class ProductStockResponseApiServiceImpl implements ResponseApiService { @Autowired private ModelMapper modelMapper; - @Override - public void consumeResponse(ResponseDto responseDto) { - ProductStockRequest correspondingProductStockRequest = findCorrespondingRequest(responseDto); + public void consumeResponse(ProductStockResponse response) { + +// ProductStockRequest correspondingProductStockRequest = findCorrespondingRequest(responseDto); + ProductStockRequest correspondingProductStockRequest = productStockRequestService.findRequestByHeaderUuid(response.getHeader().getRequestId()); if (correspondingProductStockRequest == null) { log.error("Received Response without corresponding request"); return; } - Partner partner = partnerService.findByBpnl(responseDto.getHeader().getSender()); - for (MessageContentDto messageContentDto : responseDto.getPayload()) { + Partner partner = partnerService.findByBpnl(response.getHeader().getSender()); + for (ProductStockSammDto sammDto : response.getContent().getProductStocks()) { - if (messageContentDto instanceof ProductStockSammDto) { - ProductStockSammDto sammDto = (ProductStockSammDto) messageContentDto; PartnerProductStockDto partnerProductStockDto = productStockSammMapper.fromSamm(sammDto, partner); // check whether a new PartnerProductStock must be created @@ -88,10 +88,6 @@ public void consumeResponse(ResponseDto responseDto) { partnerProductStockService.findAllByOwnMaterialNumberAndPartnerUuid( partnerProductStockDto.getMaterial().getMaterialNumberCustomer(), partnerProductStockDto.getSupplierPartner().getUuid()); -// partnerProductStockService.findAllByMaterialUuidAndPartnerUuid( -// partnerProductStockDto.getSupplierPartner().getUuid(), -// partnerProductStockDto.getMaterial().getUuid() -// ); // currently we only accept a one to one mapping of partner - material - stock -site // therefore the can only be one PartnerProductStock @@ -117,14 +113,7 @@ public void consumeResponse(ResponseDto responseDto) { log.info(String.format("Updated Partner ProductStock from SAMM: %s", updatedPartnerProductStock)); } - } else if (messageContentDto instanceof MessageContentErrorDto) { - log.error(String.format("Could not receive information: %s", messageContentDto)); - } else - throw new IllegalStateException(String.format("Message Content is unknown: %s", - messageContentDto)); - } - // Update status - also only MessageContentErrorDtos would be completed productStockRequestService.updateState(correspondingProductStockRequest, DT_RequestStateEnum.COMPLETED); } From 539a1896a043a9b71ef597e61a94301abfedb52e Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Fri, 4 Aug 2023 09:35:46 +0200 Subject: [PATCH 9/9] fix: improved logic to identify requested material --- .../stock/controller/StockController.java | 1 - .../ProductStockRequestApiServiceImpl.java | 37 ++++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java index 13bf4e1c..41648300 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/controller/StockController.java @@ -284,7 +284,6 @@ public List triggerPartnerProductStockUpdateForMaterial(@RequestPara Material materialEntity = materialService.findByOwnMaterialNumber(ownMaterialNumber); log.info("Found material: " + (materialEntity != null)); - log.info("All materials: " + materialService.findAllMaterials()); List allSupplierPartnerEntities = mprService.findAllSuppliersForOwnMaterialNumber(ownMaterialNumber); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java index d2cc0b56..aa500712 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/service/ProductStockRequestApiServiceImpl.java @@ -148,24 +148,27 @@ public void handleRequest(ProductStockRequest productStockRequest) { if (searchResult.size() == 1) { existingMaterial = searchResult.get(0); } else { - // MaterialNumberCustomer is ambiguous or unknown - // try to use MaterialNumberSupplier - if (productStockRequestForMaterial.getMaterialNumberSupplier() != null) { - existingMaterial = materialService.findByOwnMaterialNumber(productStockRequestForMaterial.getMaterialNumberSupplier()); + if (productStockRequestForMaterial.getMaterialNumberCustomer() != null) { + if (productStockRequestForMaterial.getMaterialNumberSupplier() == null + || productStockRequestForMaterial.getMaterialNumberSupplier().equals(productStockRequestForMaterial.getMaterialNumberCustomer())) { + // possible case: customer did not define his own material number + // and is using the supplier's material number (see CX 0085) as his own material number. + existingMaterial = materialService.findByOwnMaterialNumber(productStockRequestForMaterial.getMaterialNumberCustomer()); + } else { + // MaterialNumberCustomer is ambiguous or unknown + // try to use MaterialNumberSupplier + if (productStockRequestForMaterial.getMaterialNumberSupplier() != null) { + existingMaterial = materialService.findByOwnMaterialNumber(productStockRequestForMaterial.getMaterialNumberSupplier()); + } + } } } } } if (existingMaterial == null) { - MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer(productStockRequestForMaterial.getMaterialNumberCustomer()); - messageContentErrorDto.setError("PURIS-01"); - messageContentErrorDto.setMessage("Material is unknown."); -// resultProductStocks.add(messageContentErrorDto); - log.warn(String.format("No Material found for ID Customer %s in request %s", - productStockRequestForMaterial.getMaterialNumberCustomer(), - productStockRequest.getHeader().getRequestId())); + log.warn("Material unknown"); + // Material is unknown, error messages in this case currently not supported continue; } else { log.info("Found requested Material: " + existingMaterial.getOwnMaterialNumber()); @@ -175,11 +178,6 @@ public void handleRequest(ProductStockRequest productStockRequest) { boolean ordersProducts = mprService.partnerOrdersProduct(existingMaterial, requestingPartner); log.info("Requesting entity orders this Material? " + ordersProducts); if (!ordersProducts) { - MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer(productStockRequestForMaterial.getMaterialNumberCustomer()); - messageContentErrorDto.setError("PURIS-02"); - messageContentErrorDto.setMessage("Partner is not authorized."); -// resultProductStocks.add(messageContentErrorDto); log.warn(String.format("Partner %s is not an ordering Partner of Material " + "found for ID Customer %s in request %s", requestingPartnerBpnl, @@ -196,11 +194,6 @@ public void handleRequest(ProductStockRequest productStockRequest) { ProductStock productStock = null; if (productStocks.size() == 0) { - MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer(productStockRequestForMaterial.getMaterialNumberCustomer()); - messageContentErrorDto.setError("PURIS-03"); - messageContentErrorDto.setMessage("No Product Stock found."); -// resultProductStocks.add(messageContentErrorDto); log.warn("No Product Stocks of Material " + productStockRequestForMaterial.getMaterialNumberCustomer() + " found for " + productStockRequest.getHeader().getSender()); continue;