From bd0d5cf9765b70a7743ac51860fbbb7aa4422017 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Fri, 6 Oct 2023 15:51:48 +0200 Subject: [PATCH 1/8] feat: initial commit --- .../stock/controller/StockController.java | 2 +- .../domain/model/PartnerProductStock.java | 5 - .../model/measurement/MeasurementUnit.java | 30 ++++- .../PartnerProductStockRepository.java | 13 ++- .../logic/adapter/ProductStockSammMapper.java | 103 ++++++----------- .../service/PartnerProductStockService.java | 5 + .../PartnerProductStockServiceImpl.java | 9 +- .../ProductStockResponseApiServiceImpl.java | 104 ++++++++++-------- 8 files changed, 145 insertions(+), 126 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 2e21c175..562a1b97 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 @@ -196,7 +196,7 @@ public ProductStockDto updateProductStocks(@RequestBody ProductStockDto productS existingProductStock.setQuantity(productStockDto.getQuantity()); existingProductStock.setLastUpdatedOn(new Date()); - existingProductStock = productStockService.create(existingProductStock); + existingProductStock = productStockService.update(existingProductStock); log.info("Updated product-stock: " + existingProductStock); ProductStockDto productStockToReturn = convertToDto(existingProductStock); 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 cbd0d8c5..92f6724f 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 @@ -60,9 +60,4 @@ public PartnerProductStock(Material material, double quantity, MeasurementUnit m this.setSupplierPartner(supplierPartner); } - public PartnerProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, Date lastUpdatedOn) { - super(material, quantity, measurementUnit, atSiteBpns, lastUpdatedOn); - super.setType(DT_StockTypeEnum.PRODUCT); - } - } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java index 9089d57c..d645fd54 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java @@ -21,6 +21,10 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.model.measurement; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; + public enum MeasurementUnit { gram, @@ -50,6 +54,30 @@ public enum MeasurementUnit { set, pair, page, - kilowattHour + kilowattHour; + + /** + * Returns a json representation needed for de-/serialization of the {@link ProductStockSammDto}. + * For example: "unit:piece" + * + * @return the json representation as String + */ + @JsonValue + public String jsonRepresentation() { + return "unit:" + this; + } + + /** + * Maps the input to a MeasurementUnit by skipping the prefix "unit:" and evaluating the remainder. + * Needed for deserialization of the {@link ProductStockSammDto} + * + * @param value the input String to be parsed + * @return a MeasurementUnit or null + */ + @JsonCreator + public static MeasurementUnit parseStringFromJson(String value) { + value = value.startsWith("unit:") ? value.substring(5) : value; + return MeasurementUnit.valueOf(value); + } } 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 1c726ee2..06b57986 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 @@ -21,9 +21,12 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.repository; +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.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.Stock; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; +import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -44,13 +47,15 @@ public interface PartnerProductStockRepository extends JpaRepository findAllByType(DT_StockTypeEnum stockType); -// List findAllByMaterial_UuidAndType(UUID materialUuid, DT_StockTypeEnum stockType); - 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); + + List findAllBySupplierPartnerAndMaterialAndAtSiteBpnsAndMeasurementUnit( + Partner supplierPartner, + Material material, + String siteBpns, + MeasurementUnit measurementUnit); } 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 d82e54e1..201062ed 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,25 +23,18 @@ 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.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.domain.model.measurement.MeasurementUnit; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.PartnerProductStockDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.*; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; import java.util.*; @Slf4j @@ -66,7 +59,7 @@ public class ProductStockSammMapper { public ProductStockSammDto toSamm(Stock stock) { AllocatedStock allocatedStock = new AllocatedStock( - new Quantity(stock.getQuantity(), "unit:" + stock.getMeasurementUnit()), + new Quantity(stock.getQuantity(), stock.getMeasurementUnit().jsonRepresentation()), new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getAtSiteBpns()) ); List allocatedStocks = new ArrayList<>(); @@ -107,69 +100,45 @@ public ProductStockSammDto toSamm(Stock stock) { ); } - /** - * Utility method to deserialize a ProductStockSammDto on the side of the customer + * Utility method to deserialize a ProductStockSammDto on the side of the customer. + * Since each ProductStockSammDto may contain multiple Positions, this method will + * return a list of PartnerProductStocks, where each Samm-Position is mapped to an + * instance of PartnerProductStock. + * + * The caller of this method has to decide how whether he wants to aggregate some of + * the items of the returned list. Furthermore, he may want to compare the resulting + * list to his current database and then decide which of the resulting PartnerProductStocks + * are an update to existing entities in his database. + * * @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 - - // aggregate quantity - double quantity = samm.getPositions().stream(). - mapToDouble( - pos -> pos.getAllocatedStocks().stream(). - mapToDouble(stock -> - stock.getQuantityOnAllocatedStock().getQuantityNumber() - ).sum() - ).sum(); - - // This is just a quickfix in line with the above-mentioned restriction that - // we are currently supporting only an aggregated stock. It is assumed that all - // stocks are using the same unit. If, for example, one stock uses kilograms and - // another stock uses metric tonnes for the same material, this will of course - // lead to faulty data. - String measurementUnitString = samm.getPositions().stream().findFirst() - .stream().findFirst().get().getAllocatedStocks().stream().findFirst() - .get().getQuantityOnAllocatedStock().getMeasurementUnit(); - measurementUnitString = measurementUnitString.replace("unit:", ""); - MeasurementUnit measurementUnit = MeasurementUnit.valueOf(measurementUnitString); - - // Another quickfix. This will retrieve the lastUpdatedOn timestamp from - // the first position found and apply it to the accumulated stock. - Date lastUpdatedOnDate = samm.getPositions().stream().findFirst() - .get().getLastUpdatedOnDateTime(); - - - // determine material - - 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. - // also we set the bpn here always to the site - String atSiteBpns = - samm.getPositions() - .stream().findFirst().get().getAllocatedStocks() - .stream().findFirst().get().getSupplierStockLocationId().getLocationId(); - - PartnerDto supplierPartner = modelMapper.map(partner, PartnerDto.class); - - return new PartnerProductStockDto( - foundMaterialDto, - quantity, - measurementUnit, - atSiteBpns, - supplierPartner, - lastUpdatedOnDate - ); + public List sammToPartnerProductStocks(ProductStockSammDto samm, Partner partner) { + ArrayList output = new ArrayList<>(); + Material material = materialService.findByOwnMaterialNumber(samm.getMaterialNumberCustomer()); + if(material == null && samm.getMaterialNumberCatenaX().isPresent()) { + materialService.findByMaterialNumberCx(samm.getMaterialNumberCatenaX().get()); + } + for(var position : samm.getPositions()) { + Date lastUpdated = position.getLastUpdatedOnDateTime(); + for(var allocatedStock : position.getAllocatedStocks()) { + double quantity = allocatedStock.getQuantityOnAllocatedStock().getQuantityNumber(); + MeasurementUnit unit = MeasurementUnit.parseStringFromJson( + allocatedStock.getQuantityOnAllocatedStock().getMeasurementUnit()); + String locationId = allocatedStock.getSupplierStockLocationId().getLocationId(); + PartnerProductStock partnerProductStock = new PartnerProductStock( + material, + quantity, + unit, + locationId, + lastUpdated, + partner + ); + output.add(partnerProductStock); + } + } + return output; } } 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 2395b725..615b9cd7 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,8 +21,10 @@ */ package org.eclipse.tractusx.puris.backend.stock.logic.service; +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.stock.domain.model.PartnerProductStock; +import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; import org.springframework.stereotype.Service; import java.util.List; @@ -42,6 +44,9 @@ public interface PartnerProductStockService { List findAllByPartnerMaterialNumber(Partner partner, String partnerMaterialNumber); + List findAllByPartnerAndMaterialAndLocationAndMeasurementUnit(Partner partner, Material material, + String site, MeasurementUnit measurementUnit); + //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 080c840f..69bc62ca 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 @@ -22,12 +22,13 @@ package org.eclipse.tractusx.puris.backend.stock.logic.service; import lombok.AllArgsConstructor; +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.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; +import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; import org.eclipse.tractusx.puris.backend.stock.domain.repository.PartnerProductStockRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @@ -85,4 +86,10 @@ public List findAllByPartnerMaterialNumber(Partner partner, .filter(pps -> materialsList.contains(pps.getMaterial())) .collect(Collectors.toList()); } + + @Override + public List findAllByPartnerAndMaterialAndLocationAndMeasurementUnit(Partner partner, Material material, + String site, MeasurementUnit measurementUnit) { + return partnerProductStockRepository.findAllBySupplierPartnerAndMaterialAndAtSiteBpnsAndMeasurementUnit(partner, material, site, measurementUnit); + } } 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 3afb8c83..49d923a1 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 @@ -28,14 +28,15 @@ 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.domain.model.Stock; 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; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * Service implements the handling of a response for Product Stock @@ -60,61 +61,70 @@ public class ProductStockResponseApiServiceImpl { @Autowired private ProductStockSammMapper productStockSammMapper; - @Autowired - private ModelMapper modelMapper; - 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"); + log.error("Received Response without corresponding request\n" + response); return; } Partner partner = partnerService.findByBpnl(response.getHeader().getSender()); - for (ProductStockSammDto sammDto : response.getContent().getProductStocks()) { - - PartnerProductStockDto partnerProductStockDto = - productStockSammMapper.fromSamm(sammDto, partner); - // check whether a new PartnerProductStock must be created - // or whether an update is sufficient. - List existingPartnerProductStocks = - partnerProductStockService.findAllByOwnMaterialNumberAndPartnerUuid( - partnerProductStockDto.getMaterial().getMaterialNumberCustomer(), - partnerProductStockDto.getSupplierPartner().getUuid()); - - // currently we only accept a one to one mapping of partner - material - stock -site - // therefore the can only be one PartnerProductStock - // if > 0 -> IllegalState - if (existingPartnerProductStocks.size() > 1) { - throw new IllegalStateException(String.format("There exist %d " + - "PartnerProductStocks for material uuid %s and supplier partner uuid " + - "%s", existingPartnerProductStocks.size(), - partnerProductStockDto.getMaterial().getUuid(), - partnerProductStockDto.getSupplierPartner().getUuid())); - } - - // Create or update - if (existingPartnerProductStocks.isEmpty()) { - PartnerProductStock createdPartnerProductStock = - partnerProductStockService.create(modelMapper.map(partnerProductStockDto, - PartnerProductStock.class)); - 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(existingPartnerProductStock); - log.info(String.format("Updated Partner ProductStock from SAMM: %s", - updatedPartnerProductStock)); + if(partner == null) { + log.error("Received response from unknown Partner\n" + response); + return; + } + ArrayList consolidatedStocks = new ArrayList<>(); + for(var sammDto : response.getContent().getProductStocks()) { + List foundStocks = productStockSammMapper.sammToPartnerProductStocks(sammDto, partner); + if(!foundStocks.isEmpty()) { + // Per definition each instance of the ProductStockSammDto contains + // information about exactly one instance of Material. + // If locationIds of any pair of foundStocks do match, + // then these two should be merged. + + // Create lists of Stocks with same location + var groupedByLocations = foundStocks.stream().collect(Collectors.groupingBy(Stock::getAtSiteBpns, Collectors.toList())); + for(var locationGrouping : groupedByLocations.entrySet()) { + // From each grouping with same location, create sub-lists with same MeasurementUnit + var groupedByMeasurementUnit = locationGrouping.getValue().stream().collect(Collectors.groupingBy( + Stock::getMeasurementUnit, Collectors.toList())); + for (var entry : groupedByMeasurementUnit.values()) { + // Aggregate Stocks with same location and MeasurementUnit + if(entry.size() > 1) { + PartnerProductStock baseStock = entry.get(0); + baseStock.setQuantity(0); + PartnerProductStock aggregatedStock = entry.stream().reduce(baseStock, (stockA, stockB) -> { + stockA.setQuantity(stockA.getQuantity() + stockB.getQuantity()); + return stockA; + }); + consolidatedStocks.add(aggregatedStock); + } else { + consolidatedStocks.add(entry.get(0)); + } + } } + } } - // Update status - also only MessageContentErrorDtos would be completed + // Check whether a new PartnerProductStock must be created or whether + // an existing PartnerProductStock gets updated. + for (PartnerProductStock newStockData : consolidatedStocks) { + var existingPartnerProductStocks = partnerProductStockService.findAllByPartnerAndMaterialAndLocationAndMeasurementUnit( + newStockData.getSupplierPartner(), newStockData.getMaterial(), newStockData.getAtSiteBpns(), newStockData.getMeasurementUnit()); + // There should be at most one instance in that list, because we are aggregating them (see above) + if(existingPartnerProductStocks.size()>1) { + log.warn("Found multiple instances of PartnerProductStock: \n" + existingPartnerProductStocks); + } + if(existingPartnerProductStocks.isEmpty()) { + // create new PartnerProductStock in database: + partnerProductStockService.create(newStockData); + } else { + // update existing PartnerProductStock + PartnerProductStock existingPartnerProductStock = existingPartnerProductStocks.get(0); + existingPartnerProductStock.setQuantity(newStockData.getQuantity()); + partnerProductStockService.update(existingPartnerProductStock); + } + } productStockRequestService.updateState(correspondingProductStockRequest, DT_RequestStateEnum.Completed); } From a6970bd330cc73c459bb3a282018db5ea0b6df43 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 9 Oct 2023 08:51:39 +0200 Subject: [PATCH 2/8] refactor: samm now using measurementunit instead of string --- .../stock/logic/adapter/ProductStockSammMapper.java | 5 ++--- .../puris/backend/stock/logic/dto/samm/Quantity.java | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) 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 201062ed..51ce73e3 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 @@ -59,7 +59,7 @@ public class ProductStockSammMapper { public ProductStockSammDto toSamm(Stock stock) { AllocatedStock allocatedStock = new AllocatedStock( - new Quantity(stock.getQuantity(), stock.getMeasurementUnit().jsonRepresentation()), + new Quantity(stock.getQuantity(), stock.getMeasurementUnit()), new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getAtSiteBpns()) ); List allocatedStocks = new ArrayList<>(); @@ -125,8 +125,7 @@ public List sammToPartnerProductStocks(ProductStockSammDto Date lastUpdated = position.getLastUpdatedOnDateTime(); for(var allocatedStock : position.getAllocatedStocks()) { double quantity = allocatedStock.getQuantityOnAllocatedStock().getQuantityNumber(); - MeasurementUnit unit = MeasurementUnit.parseStringFromJson( - allocatedStock.getQuantityOnAllocatedStock().getMeasurementUnit()); + MeasurementUnit unit = allocatedStock.getQuantityOnAllocatedStock().getMeasurementUnit(); String locationId = allocatedStock.getSupplierStockLocationId().getLocationId(); PartnerProductStock partnerProductStock = new PartnerProductStock( material, diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/samm/Quantity.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/samm/Quantity.java index b95a57a1..1c9ae1c8 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/samm/Quantity.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/samm/Quantity.java @@ -25,6 +25,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.NotNull; import lombok.ToString; +import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; import java.util.Objects; @@ -39,12 +40,11 @@ public class Quantity { private Double quantityNumber; @NotNull - //custom: made Curie a String - private String measurementUnit; + private MeasurementUnit measurementUnit; @JsonCreator public Quantity(@JsonProperty(value = "quantityNumber") Double quantityNumber, - @JsonProperty(value = "measurementUnit") String measurementUnit) { + @JsonProperty(value = "measurementUnit") MeasurementUnit measurementUnit) { this.quantityNumber = quantityNumber; this.measurementUnit = measurementUnit; } @@ -63,7 +63,7 @@ public Double getQuantityNumber() { * * @return {@link #measurementUnit} */ - public String getMeasurementUnit() { + public MeasurementUnit getMeasurementUnit() { return this.measurementUnit; } From ca8b6b74d5475de81ae152c271470f3263e3e58b Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 9 Oct 2023 10:10:06 +0200 Subject: [PATCH 3/8] refactor: included location id type in stocks --- .../DataInjectionCommandLineRunner.java | 4 ++ .../stock/domain/model/MaterialStock.java | 6 ++- .../domain/model/PartnerProductStock.java | 7 +-- .../stock/domain/model/ProductStock.java | 11 +++-- .../backend/stock/domain/model/Stock.java | 11 +++-- .../PartnerProductStockRepository.java | 6 ++- .../repository/ProductStockRepository.java | 3 -- .../logic/adapter/ProductStockSammMapper.java | 4 +- .../stock/logic/dto/MaterialStockDto.java | 6 ++- .../logic/dto/PartnerProductStockDto.java | 12 ++--- .../stock/logic/dto/ProductStockDto.java | 10 ++-- .../backend/stock/logic/dto/StockDto.java | 11 +++-- .../service/PartnerProductStockService.java | 6 +-- .../PartnerProductStockServiceImpl.java | 7 ++- .../ProductStockRequestApiServiceImpl.java | 2 +- .../ProductStockResponseApiServiceImpl.java | 49 ++++++++++--------- .../service/ProductStockServiceImpl.java | 2 +- 17 files changed, 89 insertions(+), 68 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 d510f5c2..2a58697b 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 @@ -37,6 +37,7 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.*; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; 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; @@ -199,6 +200,7 @@ private void setupCustomerRole() throws JsonProcessingException { 5, MeasurementUnit.piece, "BPNS4444444444XX", + LocationIdTypeEnum.B_P_N_S, new Date() ); materialStockEntity = materialStockService.create(materialStockEntity); @@ -212,6 +214,7 @@ private void setupCustomerRole() throws JsonProcessingException { 10, MeasurementUnit.piece, supplierPartner.getSites().stream().findFirst().get().getBpns(), + LocationIdTypeEnum.B_P_N_S, new Date(), supplierPartner ); @@ -255,6 +258,7 @@ private void setupSupplierRole() { 20, MeasurementUnit.piece, "BPNS1234567890ZZ", + LocationIdTypeEnum.B_P_N_S, new Date(), customerPartner ); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/MaterialStock.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/MaterialStock.java index 9afa03d7..36365b3a 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/MaterialStock.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/MaterialStock.java @@ -30,6 +30,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; @@ -46,9 +47,10 @@ @NoArgsConstructor public class MaterialStock extends Stock { - public MaterialStock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, + public MaterialStock(Material material, double quantity, MeasurementUnit measurementUnit, String locationId, + LocationIdTypeEnum locationIdType, Date lastUpdatedOn) { - super(material, quantity, measurementUnit, atSiteBpns, lastUpdatedOn); + super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn); super.setType(DT_StockTypeEnum.MATERIAL); } 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 92f6724f..1a0afcc7 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 @@ -31,6 +31,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; @@ -53,9 +54,9 @@ public class PartnerProductStock extends Stock { @NotNull private Partner supplierPartner; - public PartnerProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, - Date lastUpdatedOn, Partner supplierPartner) { - super(material, quantity, measurementUnit, atSiteBpns, lastUpdatedOn); + public PartnerProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String locationId, + LocationIdTypeEnum locationIdType, Date lastUpdatedOn, Partner supplierPartner) { + super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn); super.setType(DT_StockTypeEnum.PRODUCT); this.setSupplierPartner(supplierPartner); } 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 4c854eb1..24012eeb 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 @@ -30,6 +30,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; @@ -51,14 +52,16 @@ public class ProductStock extends Stock { @NotNull private Partner allocatedToCustomerPartner; - public ProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, Date lastUpdatedOn, Partner allocatedToCustomerPartner) { - super(material, quantity, measurementUnit, atSiteBpns, lastUpdatedOn); + public ProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String locationId, + LocationIdTypeEnum locationIdType, Date lastUpdatedOn, Partner allocatedToCustomerPartner) { + super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn); super.setType(DT_StockTypeEnum.PRODUCT); this.setAllocatedToCustomerPartner(allocatedToCustomerPartner); } - public ProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, Date lastUpdatedOn) { - super(material, quantity, measurementUnit, atSiteBpns, lastUpdatedOn); + public ProductStock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, + LocationIdTypeEnum locationIdType, Date lastUpdatedOn) { + super(material, quantity, measurementUnit, atSiteBpns, locationIdType, lastUpdatedOn); super.setType(DT_StockTypeEnum.PRODUCT); } 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 c11f0acf..83396d06 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 @@ -30,6 +30,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; import java.util.UUID; @@ -64,7 +65,10 @@ public class Stock { private MeasurementUnit measurementUnit; @NotNull - private String atSiteBpns; + private String locationId; + + @NotNull + private LocationIdTypeEnum locationIdType; @Enumerated(EnumType.STRING) @NotNull @@ -74,11 +78,12 @@ public class Stock { @NotNull private Date lastUpdatedOn; - public Stock(Material material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, Date lastUpdatedOn) { + public Stock(Material material, double quantity, MeasurementUnit measurementUnit, String locationId, LocationIdTypeEnum locationIdType, Date lastUpdatedOn) { this.material = material; this.quantity = quantity; this.measurementUnit = measurementUnit; - this.atSiteBpns = atSiteBpns; + this.locationId = locationId; + this.locationIdType = locationIdType; this.lastUpdatedOn = lastUpdatedOn; } } 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 06b57986..c4a686fb 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 @@ -27,6 +27,7 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.Stock; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -53,9 +54,10 @@ public interface PartnerProductStockRepository extends JpaRepository findAllBySupplierPartner_Uuid(UUID uuid); - List findAllBySupplierPartnerAndMaterialAndAtSiteBpnsAndMeasurementUnit( + List findAllBySupplierPartnerAndMaterialAndLocationIdAndLocationIdTypeAndMeasurementUnit( Partner supplierPartner, Material material, - String siteBpns, + String locationId, + LocationIdTypeEnum locationIdType, MeasurementUnit measurementUnit); } 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 08f734bf..affd6b61 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 @@ -47,7 +47,4 @@ public interface ProductStockRepository extends JpaRepository findAllByType(DT_StockTypeEnum stockType); -// List findAllByMaterial_MaterialNumberCustomerAndType(String materialNumberCustomer, DT_StockTypeEnum stockType); - -// 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 51ce73e3..e157f878 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 @@ -60,7 +60,7 @@ public ProductStockSammDto toSamm(Stock stock) { AllocatedStock allocatedStock = new AllocatedStock( new Quantity(stock.getQuantity(), stock.getMeasurementUnit()), - new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getAtSiteBpns()) + new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getLocationId()) ); List allocatedStocks = new ArrayList<>(); allocatedStocks.add(allocatedStock); @@ -127,11 +127,13 @@ public List sammToPartnerProductStocks(ProductStockSammDto double quantity = allocatedStock.getQuantityOnAllocatedStock().getQuantityNumber(); MeasurementUnit unit = allocatedStock.getQuantityOnAllocatedStock().getMeasurementUnit(); String locationId = allocatedStock.getSupplierStockLocationId().getLocationId(); + LocationIdTypeEnum locationIdType = allocatedStock.getSupplierStockLocationId().getLocationIdType(); PartnerProductStock partnerProductStock = new PartnerProductStock( material, quantity, unit, locationId, + locationIdType, lastUpdated, partner ); diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/MaterialStockDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/MaterialStockDto.java index 6dcd5a20..ac4d3623 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/MaterialStockDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/MaterialStockDto.java @@ -27,6 +27,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.MaterialDto; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; @@ -35,8 +36,9 @@ @AllArgsConstructor public class MaterialStockDto extends StockDto { - public MaterialStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns) { - super(material, quantity, measurementUnit, atSiteBpns, new Date()); + public MaterialStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String locationId, + LocationIdTypeEnum locationIdType, Date lastUpdatedOn) { + super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn); this.setType(DT_StockTypeEnum.MATERIAL); } } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/PartnerProductStockDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/PartnerProductStockDto.java index 7f41dbd3..6dbee3d9 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/PartnerProductStockDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/PartnerProductStockDto.java @@ -28,6 +28,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.PartnerDto; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; @@ -38,14 +39,9 @@ public class PartnerProductStockDto extends StockDto { private PartnerDto supplierPartner; - public PartnerProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns) { - super(material, quantity, measurementUnit, atSiteBpns, new Date()); - this.setType(DT_StockTypeEnum.PRODUCT); - } - - public PartnerProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, - PartnerDto supplierPartner, Date lastUpdatedOn) { - super(material, quantity, measurementUnit, atSiteBpns, lastUpdatedOn); + public PartnerProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String locationId, + LocationIdTypeEnum locationIdType, PartnerDto supplierPartner, Date lastUpdatedOn) { + super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn); this.setType(DT_StockTypeEnum.PRODUCT); this.supplierPartner = supplierPartner; } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockDto.java index ce806c60..cae6f65d 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/ProductStockDto.java @@ -29,6 +29,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.PartnerDto; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.util.Date; @@ -40,14 +41,9 @@ public class ProductStockDto extends StockDto { private PartnerDto allocatedToCustomerPartner; - public ProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns) { - super(material, quantity, measurementUnit, atSiteBpns, new Date()); - this.setType(DT_StockTypeEnum.PRODUCT); - } - public ProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, - PartnerDto allocatedToCustomerPartner) { - super(material, quantity, measurementUnit, atSiteBpns, new Date()); + LocationIdTypeEnum locationIdType, PartnerDto allocatedToCustomerPartner, Date lastUpdatedOn) { + super(material, quantity, measurementUnit, atSiteBpns, locationIdType, lastUpdatedOn); this.setType(DT_StockTypeEnum.PRODUCT); this.allocatedToCustomerPartner = allocatedToCustomerPartner; } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/StockDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/StockDto.java index 3e8312ca..09fa6747 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/StockDto.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/StockDto.java @@ -29,6 +29,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.logic.dto.MaterialDto; import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import java.io.Serializable; import java.util.Date; @@ -49,17 +50,21 @@ public abstract class StockDto implements Serializable { private MeasurementUnit measurementUnit; - private String atSiteBpns; + private String locationId; + + private LocationIdTypeEnum locationIdType; private DT_StockTypeEnum type; private Date lastUpdatedOn; - public StockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String atSiteBpns, Date lastUpdatedOn) { + public StockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String locationId, + LocationIdTypeEnum locationIdType, Date lastUpdatedOn) { this.material = material; this.quantity = quantity; this.measurementUnit = measurementUnit; - this.atSiteBpns = atSiteBpns; + this.locationId = locationId; + this.locationIdType = locationIdType; this.lastUpdatedOn = lastUpdatedOn; } } 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 615b9cd7..613acb1f 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 @@ -25,6 +25,7 @@ import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import org.springframework.stereotype.Service; import java.util.List; @@ -45,8 +46,5 @@ public interface PartnerProductStockService { List findAllByPartnerMaterialNumber(Partner partner, String partnerMaterialNumber); List findAllByPartnerAndMaterialAndLocationAndMeasurementUnit(Partner partner, Material material, - String site, MeasurementUnit measurementUnit); - - //List findAllByMaterialUuidAndPartnerUuid(UUID materialUuid, - // UUID partnerUuid); + String locationId, LocationIdTypeEnum locationIdType, MeasurementUnit measurementUnit); } 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 69bc62ca..aa6bca31 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 @@ -29,6 +29,7 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.datatype.DT_StockTypeEnum; import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit; import org.eclipse.tractusx.puris.backend.stock.domain.repository.PartnerProductStockRepository; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum; import org.springframework.stereotype.Service; import java.util.List; @@ -89,7 +90,9 @@ public List findAllByPartnerMaterialNumber(Partner partner, @Override public List findAllByPartnerAndMaterialAndLocationAndMeasurementUnit(Partner partner, Material material, - String site, MeasurementUnit measurementUnit) { - return partnerProductStockRepository.findAllBySupplierPartnerAndMaterialAndAtSiteBpnsAndMeasurementUnit(partner, material, site, measurementUnit); + String locationId, LocationIdTypeEnum locationIdType, MeasurementUnit measurementUnit) { + return partnerProductStockRepository. + findAllBySupplierPartnerAndMaterialAndLocationIdAndLocationIdTypeAndMeasurementUnit(partner, material, locationId, + locationIdType, measurementUnit); } } 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 fefebcb1..c1f3a2ed 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 @@ -214,7 +214,7 @@ public void handleRequest(ProductStockRequest productStockRequest) { if (productStocks.size() > 1) { List distinctProductStocks = productStocks.stream() - .filter(distinctByKey(p -> p.getAtSiteBpns())) + .filter(distinctByKey(p -> p.getLocationId())) .collect(Collectors.toList()); if (distinctProductStocks.size() > 1) { log.warn(String.format("More than one site is not yet supported per " + 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 49d923a1..a4843e34 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,7 +30,6 @@ import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStockResponse; import org.eclipse.tractusx.puris.backend.stock.domain.model.Stock; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; -import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -79,27 +78,32 @@ public void consumeResponse(ProductStockResponse response) { if(!foundStocks.isEmpty()) { // Per definition each instance of the ProductStockSammDto contains // information about exactly one instance of Material. - // If locationIds of any pair of foundStocks do match, - // then these two should be merged. - // Create lists of Stocks with same location - var groupedByLocations = foundStocks.stream().collect(Collectors.groupingBy(Stock::getAtSiteBpns, Collectors.toList())); - for(var locationGrouping : groupedByLocations.entrySet()) { - // From each grouping with same location, create sub-lists with same MeasurementUnit - var groupedByMeasurementUnit = locationGrouping.getValue().stream().collect(Collectors.groupingBy( - Stock::getMeasurementUnit, Collectors.toList())); - for (var entry : groupedByMeasurementUnit.values()) { - // Aggregate Stocks with same location and MeasurementUnit - if(entry.size() > 1) { - PartnerProductStock baseStock = entry.get(0); - baseStock.setQuantity(0); - PartnerProductStock aggregatedStock = entry.stream().reduce(baseStock, (stockA, stockB) -> { - stockA.setQuantity(stockA.getQuantity() + stockB.getQuantity()); - return stockA; - }); - consolidatedStocks.add(aggregatedStock); - } else { - consolidatedStocks.add(entry.get(0)); + // If locationIds and -types of any pair of foundStocks do match, + // then these two should be merged if they have the same MeasurementUnit + + // Create lists of Stocks with same locationId + var groupedByLocationIdTypes = foundStocks.stream().collect(Collectors.groupingBy(Stock::getLocationIdType, Collectors.toList())); + for (var locationTypeGrouping : groupedByLocationIdTypes.values()) { + var groupedByLocations = locationTypeGrouping.stream().collect(Collectors.groupingBy(Stock::getLocationId, Collectors.toList())); + // From each grouping with same locationIdType, create sub-lists with same locationId + for (var locationGrouping : groupedByLocations.values()) { + // From each grouping with same location, create sub-lists with same MeasurementUnit + var groupedByMeasurementUnit = locationGrouping.stream().collect(Collectors.groupingBy( + Stock::getMeasurementUnit, Collectors.toList())); + for (var entry : groupedByMeasurementUnit.values()) { + // Aggregate Stocks with same location and MeasurementUnit + if (entry.size() > 1) { + PartnerProductStock baseStock = entry.get(0); + baseStock.setQuantity(0); + PartnerProductStock aggregatedStock = entry.stream().reduce(baseStock, (stockA, stockB) -> { + stockA.setQuantity(stockA.getQuantity() + stockB.getQuantity()); + return stockA; + }); + consolidatedStocks.add(aggregatedStock); + } else { + consolidatedStocks.add(entry.get(0)); + } } } } @@ -110,7 +114,8 @@ public void consumeResponse(ProductStockResponse response) { // an existing PartnerProductStock gets updated. for (PartnerProductStock newStockData : consolidatedStocks) { var existingPartnerProductStocks = partnerProductStockService.findAllByPartnerAndMaterialAndLocationAndMeasurementUnit( - newStockData.getSupplierPartner(), newStockData.getMaterial(), newStockData.getAtSiteBpns(), newStockData.getMeasurementUnit()); + newStockData.getSupplierPartner(), newStockData.getMaterial(), newStockData.getLocationId(), + newStockData.getLocationIdType(), newStockData.getMeasurementUnit()); // There should be at most one instance in that list, because we are aggregating them (see above) if(existingPartnerProductStocks.size()>1) { log.warn("Found multiple instances of PartnerProductStock: \n" + existingPartnerProductStocks); 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 bd7df8d5..3aa5354b 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 @@ -88,7 +88,7 @@ public ProductStock create(ProductStock productStock) { @Override public List findAll() { - return productStockRepository.findAllByType(DT_StockTypeEnum.PRODUCT); + return productStockRepository.findAll(); } @Override From a13e0c3eff0dab598909c199eea40bb099546e3f Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 9 Oct 2023 10:48:31 +0200 Subject: [PATCH 4/8] fix: make sure create methods dont overwrite existing stocks --- .../stock/logic/service/PartnerProductStockServiceImpl.java | 5 +++++ .../stock/logic/service/ProductStockServiceImpl.java | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) 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 aa6bca31..cd7148f3 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 @@ -22,6 +22,7 @@ package org.eclipse.tractusx.puris.backend.stock.logic.service; import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; 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; @@ -38,6 +39,7 @@ import java.util.stream.Collectors; @Service @AllArgsConstructor +@Slf4j public class PartnerProductStockServiceImpl implements PartnerProductStockService { private PartnerProductStockRepository partnerProductStockRepository; @@ -46,6 +48,9 @@ public class PartnerProductStockServiceImpl implements PartnerProductStockServic @Override public PartnerProductStock create(PartnerProductStock partnerProductStock) { + // avoid unintentional overwriting of an existing PartnerProductStock + partnerProductStock.setUuid(null); + return partnerProductStockRepository.save(partnerProductStock); } 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 3aa5354b..da2d03ea 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 @@ -59,6 +59,8 @@ public class ProductStockServiceImpl implements ProductStockService { @Override public ProductStock create(ProductStock productStock) { + // avoid unintentional overwriting of an existing ProductStock + productStock.setUuid(null); // validate, if material is missing if (productStock.getMaterial() == null || productStock.getMaterial().getOwnMaterialNumber() == null){ @@ -67,8 +69,8 @@ public ProductStock create(ProductStock productStock) { } Optional existingMaterial = materialRepository.findById(productStock.getMaterial().getOwnMaterialNumber()); - if (!existingMaterial.isPresent()) { - log.error(String.format("Material for uuid %s not found", productStock.getMaterial().getOwnMaterialNumber())); + if (existingMaterial.isEmpty()) { + log.error(String.format("Material %s not found", productStock.getMaterial().getOwnMaterialNumber())); return null; } From 85754e6b0e411d015c5a181295e3c66670073fa3 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 9 Oct 2023 11:01:34 +0200 Subject: [PATCH 5/8] chore: some cleaning up & dependencies --- DEPENDENCIES_BACKEND | 2 +- backend/DEPENDENCIES | 2 +- .../domain/model/measurement/MeasurementUnit.java | 14 -------------- .../logic/service/ProductStockServiceImpl.java | 1 - 4 files changed, 2 insertions(+), 17 deletions(-) diff --git a/DEPENDENCIES_BACKEND b/DEPENDENCIES_BACKEND index d7ee2ac8..4066cbf6 100644 --- a/DEPENDENCIES_BACKEND +++ b/DEPENDENCIES_BACKEND @@ -24,7 +24,7 @@ maven/mavencentral/jakarta.annotation/jakarta.annotation-api/2.1.1, EPL-2.0 OR G maven/mavencentral/jakarta.inject/jakarta.inject-api/2.0.0, Apache-2.0, approved, clearlydefined maven/mavencentral/jakarta.persistence/jakarta.persistence-api/3.1.0, EPL-2.0 OR BSD-3-Clause AND (EPL-2.0 OR BSD-3-Clause AND BSD-3-Clause), approved, #7696 maven/mavencentral/jakarta.transaction/jakarta.transaction-api/2.0.1, EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0, approved, #7697 -maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, clearlydefined +maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, ee4j.validation maven/mavencentral/jakarta.xml.bind/jakarta.xml.bind-api/4.0.0, BSD-3-Clause, approved, ee4j.jaxb maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.12.22, Apache-2.0, approved, #1810 maven/mavencentral/net.bytebuddy/byte-buddy/1.12.22, Apache-2.0 AND BSD-3-Clause, approved, #1811 diff --git a/backend/DEPENDENCIES b/backend/DEPENDENCIES index d7ee2ac8..4066cbf6 100644 --- a/backend/DEPENDENCIES +++ b/backend/DEPENDENCIES @@ -24,7 +24,7 @@ maven/mavencentral/jakarta.annotation/jakarta.annotation-api/2.1.1, EPL-2.0 OR G maven/mavencentral/jakarta.inject/jakarta.inject-api/2.0.0, Apache-2.0, approved, clearlydefined maven/mavencentral/jakarta.persistence/jakarta.persistence-api/3.1.0, EPL-2.0 OR BSD-3-Clause AND (EPL-2.0 OR BSD-3-Clause AND BSD-3-Clause), approved, #7696 maven/mavencentral/jakarta.transaction/jakarta.transaction-api/2.0.1, EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0, approved, #7697 -maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, clearlydefined +maven/mavencentral/jakarta.validation/jakarta.validation-api/3.0.2, Apache-2.0, approved, ee4j.validation maven/mavencentral/jakarta.xml.bind/jakarta.xml.bind-api/4.0.0, BSD-3-Clause, approved, ee4j.jaxb maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.12.22, Apache-2.0, approved, #1810 maven/mavencentral/net.bytebuddy/byte-buddy/1.12.22, Apache-2.0 AND BSD-3-Clause, approved, #1811 diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java index d645fd54..e2521938 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/domain/model/measurement/MeasurementUnit.java @@ -21,7 +21,6 @@ */ package org.eclipse.tractusx.puris.backend.stock.domain.model.measurement; -import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; @@ -67,17 +66,4 @@ public String jsonRepresentation() { return "unit:" + this; } - /** - * Maps the input to a MeasurementUnit by skipping the prefix "unit:" and evaluating the remainder. - * Needed for deserialization of the {@link ProductStockSammDto} - * - * @param value the input String to be parsed - * @return a MeasurementUnit or null - */ - @JsonCreator - public static MeasurementUnit parseStringFromJson(String value) { - value = value.startsWith("unit:") ? value.substring(5) : value; - return MeasurementUnit.valueOf(value); - } - } 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 da2d03ea..f28ccc37 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,7 +23,6 @@ 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; From ded5b2e49a6b40bd9bb54d5605c621eced167cc4 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Tue, 10 Oct 2023 10:46:46 +0200 Subject: [PATCH 6/8] fix: fixed material identification logic --- .../logic/adapter/ProductStockSammMapper.java | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) 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 e157f878..3f5d8d01 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 @@ -101,15 +101,18 @@ public ProductStockSammDto toSamm(Stock stock) { } /** - * Utility method to deserialize a ProductStockSammDto on the side of the customer. + *

Utility method to deserialize a ProductStockSammDto on the side of the customer. * Since each ProductStockSammDto may contain multiple Positions, this method will * return a list of PartnerProductStocks, where each Samm-Position is mapped to an - * instance of PartnerProductStock. + * instance of PartnerProductStock.

* - * The caller of this method has to decide how whether he wants to aggregate some of + *

The caller of this method has to decide whether he wants to aggregate some of * the items of the returned list. Furthermore, he may want to compare the resulting * list to his current database and then decide which of the resulting PartnerProductStocks - * are an update to existing entities in his database. + * are an update to existing entities in his database.

+ * + *

Please note, that this application currently does NOT evaluate the orderPositionReference of the + * ProductStockSammDto.

* * @param samm a ProductStockSammDto received from a supplier * @param partner the partner you received the message from @@ -117,10 +120,36 @@ public ProductStockSammDto toSamm(Stock stock) { */ public List sammToPartnerProductStocks(ProductStockSammDto samm, Partner partner) { ArrayList output = new ArrayList<>(); - Material material = materialService.findByOwnMaterialNumber(samm.getMaterialNumberCustomer()); - if(material == null && samm.getMaterialNumberCatenaX().isPresent()) { - materialService.findByMaterialNumberCx(samm.getMaterialNumberCatenaX().get()); + Material material = null; + if(samm.getMaterialNumberCatenaX().isPresent()) { + material = materialService.findByMaterialNumberCx(samm.getMaterialNumberCatenaX().get()); + } + if(material == null && samm.getMaterialNumberCustomer() != null) { + material = materialService.findByOwnMaterialNumber(samm.getMaterialNumberCustomer()); } + if(material == null && samm.getMaterialNumberSupplier().isPresent()) { + var materialsFromPartners = mprService.findAllByPartnerMaterialNumber(samm.getMaterialNumberSupplier().get()); + int foundMatches = 0; + for (var foundMaterial : materialsFromPartners) { + if(mprService.partnerSuppliesMaterial(foundMaterial, partner)) { + material = foundMaterial; + foundMatches++; + } + } + if(foundMatches > 1) { + // Ambiguous results found, i.e. this Partner seems to use his MaterialNumber which he transmitted + // in the SammDto for more than one Product. + // Will arbitrarily use the last one found, but issue a warning. + log.warn("Could not unambiguously determine Material via partner material number " + + samm.getMaterialNumberSupplier().get()); + } + } + if(material == null) { + log.error("Could not identify material in SammDto: \n" + samm); + // Abort and return empty list. + return output; + } + for(var position : samm.getPositions()) { Date lastUpdated = position.getLastUpdatedOnDateTime(); for(var allocatedStock : position.getAllocatedStocks()) { From 8d8080ae11b9632c31dd9a1e6656b320e75b4d01 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Tue, 10 Oct 2023 10:52:26 +0200 Subject: [PATCH 7/8] chore: updated dependencies_frontend --- DEPENDENCIES_FRONTEND | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEPENDENCIES_FRONTEND b/DEPENDENCIES_FRONTEND index 4f6d4895..57f7c017 100644 --- a/DEPENDENCIES_FRONTEND +++ b/DEPENDENCIES_FRONTEND @@ -95,7 +95,7 @@ npm/npmjs/-/glob/7.2.3, ISC, approved, clearlydefined npm/npmjs/-/globals/13.16.0, MIT, approved, clearlydefined npm/npmjs/-/good-listener/1.2.2, MIT, approved, clearlydefined npm/npmjs/-/has-flag/4.0.0, MIT, approved, clearlydefined -npm/npmjs/-/has/1.0.3, MIT, approved, clearlydefined +npm/npmjs/-/has/1.0.3, MIT, approved, #10930 npm/npmjs/-/ignore/5.2.0, MIT, approved, #5907 npm/npmjs/-/import-fresh/3.3.0, MIT, approved, clearlydefined npm/npmjs/-/imurmurhash/0.1.4, MIT, approved, clearlydefined From e57191ebdbe42580f658a847e808fb21e8f8878c Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 16 Oct 2023 09:42:00 +0200 Subject: [PATCH 8/8] fix: fixed identification logic --- DEPENDENCIES_FRONTEND | 2 +- .../logic/adapter/ProductStockSammMapper.java | 70 +++++++++++++------ 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/DEPENDENCIES_FRONTEND b/DEPENDENCIES_FRONTEND index 57f7c017..0acae6b3 100644 --- a/DEPENDENCIES_FRONTEND +++ b/DEPENDENCIES_FRONTEND @@ -87,7 +87,7 @@ npm/npmjs/-/flatted/3.2.6, ISC AND (ISC AND MIT), approved, #2430 npm/npmjs/-/fraction.js/4.2.0, MIT, approved, clearlydefined npm/npmjs/-/fs.realpath/1.0.0, ISC, approved, clearlydefined npm/npmjs/-/fsevents/2.3.2, MIT, approved, #2967 -npm/npmjs/-/function-bind/1.1.1, MIT, approved, clearlydefined +npm/npmjs/-/function-bind/1.1.1, MIT, approved, #11063 npm/npmjs/-/functional-red-black-tree/1.0.1, MIT, approved, clearlydefined npm/npmjs/-/glob-parent/5.1.2, ISC, approved, clearlydefined npm/npmjs/-/glob-parent/6.0.2, ISC, approved, clearlydefined 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 3f5d8d01..287fbc2c 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 @@ -35,7 +35,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Optional; @Slf4j @Component @@ -53,22 +56,23 @@ public class ProductStockSammMapper { /** * 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) { AllocatedStock allocatedStock = new AllocatedStock( - new Quantity(stock.getQuantity(), stock.getMeasurementUnit()), - new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getLocationId()) + new Quantity(stock.getQuantity(), stock.getMeasurementUnit()), + new LocationId(LocationIdTypeEnum.B_P_N_S, stock.getLocationId()) ); List allocatedStocks = new ArrayList<>(); allocatedStocks.add(allocatedStock); Position position = new Position( - null, - stock.getLastUpdatedOn(), - allocatedStocks + null, + stock.getLastUpdatedOn(), + allocatedStocks ); List positions = new ArrayList<>(); positions.add(position); @@ -78,7 +82,7 @@ public ProductStockSammDto toSamm(Stock stock) { // Partner is customer ProductStock productStock = (ProductStock) stock; Partner partner = productStock.getAllocatedToCustomerPartner(); - materialNumberCustomer = mprService.find(stock.getMaterial(), partner).getPartnerMaterialNumber(); + materialNumberCustomer = mprService.find(stock.getMaterial(), partner).getPartnerMaterialNumber(); materialNumberSupplier = stock.getMaterial().getOwnMaterialNumber(); } else if (stock instanceof PartnerProductStock) { // Partner is supplier @@ -93,10 +97,10 @@ public ProductStockSammDto toSamm(Stock stock) { } return new ProductStockSammDto( - positions, - materialNumberCustomer, - Optional.ofNullable(stock.getMaterial().getMaterialNumberCx()), - Optional.ofNullable(materialNumberSupplier) + positions, + materialNumberCustomer, + Optional.ofNullable(stock.getMaterial().getMaterialNumberCx()), + Optional.ofNullable(materialNumberSupplier) ); } @@ -114,45 +118,65 @@ public ProductStockSammDto toSamm(Stock stock) { *

Please note, that this application currently does NOT evaluate the orderPositionReference of the * ProductStockSammDto.

* - * @param samm a ProductStockSammDto received from a supplier + * @param samm a ProductStockSammDto received from a supplier * @param partner the partner you received the message from * @return a PartnerProductStockDto */ public List sammToPartnerProductStocks(ProductStockSammDto samm, Partner partner) { ArrayList output = new ArrayList<>(); Material material = null; - if(samm.getMaterialNumberCatenaX().isPresent()) { + if (samm.getMaterialNumberCatenaX().isPresent()) { material = materialService.findByMaterialNumberCx(samm.getMaterialNumberCatenaX().get()); + if (material != null) { + if (!material.getOwnMaterialNumber().equals(samm.getMaterialNumberCustomer())) { + log.warn("Mismatch between MaterialNumberCX and CustomerMaterialNumber!"); + } + if (samm.getMaterialNumberSupplier().isPresent() && + !mprService.findAllByPartnerMaterialNumber(samm.getMaterialNumberSupplier().get()).contains(material)) { + log.warn("Mismatch between MaterialNumberCX and SupplierMaterialNumber!"); + } + } else { + log.warn("Could not identify Material via MaterialNumberCX: " + samm.getMaterialNumberCatenaX()); + } } - if(material == null && samm.getMaterialNumberCustomer() != null) { - material = materialService.findByOwnMaterialNumber(samm.getMaterialNumberCustomer()); - } - if(material == null && samm.getMaterialNumberSupplier().isPresent()) { + if (material == null && samm.getMaterialNumberSupplier().isPresent()) { var materialsFromPartners = mprService.findAllByPartnerMaterialNumber(samm.getMaterialNumberSupplier().get()); int foundMatches = 0; for (var foundMaterial : materialsFromPartners) { - if(mprService.partnerSuppliesMaterial(foundMaterial, partner)) { + if (mprService.partnerSuppliesMaterial(foundMaterial, partner)) { material = foundMaterial; + if (foundMaterial.getOwnMaterialNumber().equals(samm.getMaterialNumberCustomer())) { + // foundMaterial is a definitive match + foundMatches = 1; + break; + } foundMatches++; } } - if(foundMatches > 1) { + if (foundMatches > 1) { // Ambiguous results found, i.e. this Partner seems to use his MaterialNumber which he transmitted // in the SammDto for more than one Product. // Will arbitrarily use the last one found, but issue a warning. - log.warn("Could not unambiguously determine Material via partner material number " + + log.warn("Could not unambiguously determine Material via MaterialNumberSupplier " + samm.getMaterialNumberSupplier().get()); } + if (foundMatches < 1) { + log.warn("Could not identify Material via MaterialNumberSupplier: " + samm.getMaterialNumberSupplier().get()); + } } - if(material == null) { + if (material == null && samm.getMaterialNumberCustomer() != null) { + material = materialService.findByOwnMaterialNumber(samm.getMaterialNumberCustomer()); + } + + if (material == null) { log.error("Could not identify material in SammDto: \n" + samm); // Abort and return empty list. return output; } - for(var position : samm.getPositions()) { + for (var position : samm.getPositions()) { Date lastUpdated = position.getLastUpdatedOnDateTime(); - for(var allocatedStock : position.getAllocatedStocks()) { + for (var allocatedStock : position.getAllocatedStocks()) { double quantity = allocatedStock.getQuantityOnAllocatedStock().getQuantityNumber(); MeasurementUnit unit = allocatedStock.getQuantityOnAllocatedStock().getMeasurementUnit(); String locationId = allocatedStock.getSupplierStockLocationId().getLocationId();