From e2b7efa0f368f6d7dab458842348f96e7ffef844 Mon Sep 17 00:00:00 2001 From: --show-origin Date: Fri, 18 Aug 2023 07:01:19 -0700 Subject: [PATCH 1/3] refactor: use m:n relationship on controller level --- .../ProductStockRequestApiController.java | 71 ++---- .../ProductStockResponseApiController.java | 35 ++- .../stock/controller/StockController.java | 205 +++++++++--------- .../stock/logic/dto/FrontendMaterialDto.java | 34 +++ 4 files changed, 178 insertions(+), 167 deletions(-) create mode 100644 backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/FrontendMaterialDto.java 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..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.common.api.logic.service.RequestService; +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 @@ -59,38 +52,40 @@ public class ProductStockRequestApiController { ObjectMapper objectMapper; @Autowired - RequestService requestService; + 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 = - requestService.findRequestByHeaderUuid(requestId); + productStockRequestService.findRequestByHeaderUuid(requestId); if (productStockRequestFound != null) { @@ -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()); - requestService.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(); @@ -136,7 +111,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..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.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; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; +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.stock.logic.service.ProductStockResponseApiServiceImpl; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatusCode; @@ -49,7 +48,7 @@ public class ProductStockResponseApiController { @Autowired - RequestService requestService; + ProductStockRequestService productStockRequestService; @Autowired ModelMapper modelMapper; @@ -58,34 +57,28 @@ 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 = requestService.findRequestByHeaderUuid(requestId); + ProductStockRequest productStockRequestFound = productStockRequestService.findRequestByHeaderUuid(requestId); if (productStockRequestFound == null) { log.error("Request id " +requestId + " not found"); return ResponseEntity.status(422).build(); @@ -93,8 +86,8 @@ public ResponseEntity postResponse(@RequestBody String body) { log.info("Got response for request Id " + requestId); } - productStockRequestFound = requestService.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 a69882c9..35a99b4a 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,35 +24,35 @@ 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.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.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; -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.domain.model.*; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.FrontendMaterialDto; +import org.eclipse.tractusx.puris.backend.stock.logic.dto.MaterialStockDto; +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.service.MaterialStockService; import org.eclipse.tractusx.puris.backend.stock.logic.service.PartnerProductStockService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockService; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; 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.Map; import java.util.UUID; import java.util.stream.Collectors; @@ -86,7 +86,10 @@ public class StockController { private PartnerService partnerService; @Autowired - private RequestService requestService; + private MaterialPartnerRelationService mprService; + + @Autowired + private ProductStockRequestService productStockRequestService; @Autowired private ModelMapper modelMapper; @@ -97,58 +100,42 @@ public class StockController { @Autowired private EdcAdapterService edcAdapterService; - @Autowired - private ApiMarshallingService apiMarshallingService; - @Autowired private VariablesService variablesService; - @Value("${edc.idsUrl}") - private String ownEdcIdsUrl; - - @Value("${own.bpns}") - private String ownBpns; - - @Value("${own.bpnl}") - private String ownBpnl; @CrossOrigin @GetMapping("materials") @ResponseBody - public List getMaterials() { - - List allMaterials = materialService.findAllMaterials().stream() - .map(this::convertToDto) - .collect(Collectors.toList()); - - allMaterials.stream().forEach(m -> System.out.println(m)); - - return allMaterials; - + public List getMaterials() { + return materialService.findAllMaterials() + .stream() + .map(mat -> new FrontendMaterialDto(mat.getOwnMaterialNumber(), mat.getName())) + .collect(Collectors.toList()); } - private MaterialDto convertToDto(Material entity) { - return modelMapper.map(entity, MaterialDto.class); + @CrossOrigin + @GetMapping("materialnumbers-mapping") + @ResponseBody + public Map getMaterialNumbers(@RequestParam String ownMaterialNumber) { + return mprService.getBPNL_To_MaterialNumberMap(ownMaterialNumber); } @CrossOrigin @GetMapping("products") @ResponseBody - public List getProducts() { - - List allProducts = materialService.findAllProducts().stream() - .map(this::convertToDto) - .collect(Collectors.toList()); - - return allProducts; + public List getProducts() { + return materialService.findAllProducts() + .stream() + .map(mat -> new FrontendMaterialDto(mat.getOwnMaterialNumber(), mat.getName())) + .collect(Collectors.toList()); } @CrossOrigin @GetMapping("product-stocks") @ResponseBody public List getProductStocks() { - List allProductStocks = productStockService.findAll().stream() .map(this::convertToDto) .collect(Collectors.toList()); @@ -169,6 +156,7 @@ public ProductStockDto createProductStocks(@RequestBody ProductStockDto productS if (createdProductStock == null){ return null; } + log.info("Created product-stock: " + createdProductStock); ProductStockDto productStockToReturn = convertToDto(createdProductStock); @@ -188,6 +176,7 @@ public ProductStockDto updateProductStocks(@RequestBody ProductStockDto productS existingProductStock.setLastUpdatedOn(new Date()); existingProductStock = productStockService.create(existingProductStock); + log.info("Updated product-stock: " + existingProductStock); ProductStockDto productStockToReturn = convertToDto(existingProductStock); @@ -195,11 +184,19 @@ public ProductStockDto updateProductStocks(@RequestBody ProductStockDto productS } private ProductStockDto convertToDto(ProductStock entity) { - return modelMapper.map(entity, ProductStockDto.class); + ProductStockDto dto = modelMapper.map(entity, ProductStockDto.class); + dto.getMaterial().setMaterialNumberSupplier(entity.getMaterial().getOwnMaterialNumber()); + var materialPartnerRelation = + mprService.find(entity.getMaterial().getOwnMaterialNumber(), entity.getAllocatedToCustomerPartner().getUuid()); + dto.getMaterial().setMaterialNumberCustomer(materialPartnerRelation.getPartnerMaterialNumber()); + return dto; } private ProductStock convertToEntity(ProductStockDto dto) { - return modelMapper.map(dto, ProductStock.class); + ProductStock productStock = modelMapper.map(dto, ProductStock.class); + Material material = materialService.findByOwnMaterialNumber(dto.getMaterial().getMaterialNumberSupplier()); + productStock.setMaterial(material); + return productStock; } @CrossOrigin @@ -209,7 +206,7 @@ public List getMaterialStocks() { List allMaterialStocks = materialStockService.findAll().stream() .map(this::convertToDto) .collect(Collectors.toList()); - + log.info(allMaterialStocks.toString()); return allMaterialStocks; } @@ -233,14 +230,15 @@ public MaterialStockDto createMaterialStocks(@RequestBody MaterialStockDto mater @ResponseBody public MaterialStockDto updateMaterialStocks(@RequestBody MaterialStockDto materialStockDto) { MaterialStock existingMaterialStock = materialStockService.findByUuid(materialStockDto.getUuid()); - if (existingMaterialStock.getUuid() == null) { + if (existingMaterialStock == null || existingMaterialStock.getUuid() == null) { + log.warn("unable to find existing stock, exiting"); return null; } existingMaterialStock.setQuantity(materialStockDto.getQuantity()); existingMaterialStock.setLastUpdatedOn(new Date()); - existingMaterialStock = materialStockService.create(existingMaterialStock); + existingMaterialStock = materialStockService.update(existingMaterialStock); MaterialStockDto productStockToReturn = convertToDto(existingMaterialStock); @@ -248,36 +246,47 @@ public MaterialStockDto updateMaterialStocks(@RequestBody MaterialStockDto mater } private MaterialStockDto convertToDto(MaterialStock entity) { - return modelMapper.map(entity, MaterialStockDto.class); + MaterialStockDto dto = modelMapper.map(entity, MaterialStockDto.class); + dto.getMaterial().setMaterialNumberCx(entity.getMaterial().getMaterialNumberCx()); + dto.getMaterial().setMaterialNumberCustomer(entity.getMaterial().getOwnMaterialNumber()); + return dto; } private MaterialStock convertToEntity(MaterialStockDto dto) { - return modelMapper.map(dto, MaterialStock.class); + MaterialStock stock = modelMapper.map(dto, MaterialStock.class); + stock.getMaterial().setOwnMaterialNumber(dto.getMaterial().getMaterialNumberCustomer()); + return stock; } @CrossOrigin @GetMapping("partner-product-stocks") @ResponseBody - public List getPartnerProductStocks() { - List allPartnerProductStocks = partnerProductStockService.findAll().stream() - .map(this::convertToDto) - .collect(Collectors.toList()); - - return allPartnerProductStocks; + public List getPartnerProductStocks(@RequestParam String ownMaterialNumber) { + return partnerProductStockService. + findAllByOwnMaterialNumber(ownMaterialNumber) + .stream() + .map(this::convertToDto) + .collect(Collectors.toList()); } private PartnerProductStockDto convertToDto(PartnerProductStock entity) { - return modelMapper.map(entity, PartnerProductStockDto.class); + PartnerProductStockDto dto = modelMapper.map(entity, PartnerProductStockDto.class); + dto.getMaterial().setMaterialNumberCx(entity.getMaterial().getMaterialNumberCx()); + dto.getMaterial().setMaterialNumberCustomer(entity.getMaterial().getOwnMaterialNumber()); + var materialPartnerRelation = mprService.find(entity.getMaterial().getOwnMaterialNumber(), + entity.getSupplierPartner().getUuid()); + dto.getMaterial().setMaterialNumberSupplier(materialPartnerRelation.getPartnerMaterialNumber()); + + return dto; } @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,23 +295,29 @@ public List getCustomerPartnersOrderingMaterial(@RequestParam UUID m @ResponseBody public List triggerPartnerProductStockUpdateForMaterial(@RequestParam String ownMaterialNumber) { - Material materialEntity = materialService.findMaterialByMaterialNumberCustomer(ownMaterialNumber); + Material materialEntity = materialService.findByOwnMaterialNumber(ownMaterialNumber); + log.info("Found material: " + (materialEntity != null) + " " + ownMaterialNumber); - List allSupplierPartnerEntities = - partnerService.findAllSupplierPartnersForMaterialId(materialEntity.getUuid()); + List allSupplierPartnerEntities = mprService.findAllSuppliersForOwnMaterialNumber(ownMaterialNumber); + for (Partner supplierPartner : allSupplierPartnerEntities) { - List messageContentDtos = new ArrayList<>(); + ProductStockRequest productStockRequest = new ProductStockRequest(); - // Message Content for all requests - ProductStockRequestForMaterialDto materialDto = new ProductStockRequestForMaterialDto( - materialEntity.getMaterialNumberCustomer(), - materialEntity.getMaterialNumberCx(), - materialEntity.getMaterialNumberSupplier() - ); - messageContentDtos.add(materialDto); + MaterialPartnerRelation materialPartnerRelation = mprService.find(materialEntity, supplierPartner); - for (Partner supplierPartner : allSupplierPartnerEntities) { + if (materialPartnerRelation == null) { + log.error("Missing material-partner-relation for " + materialEntity.getOwnMaterialNumber() + + " and " + supplierPartner.getBpnl()); + continue; + } + + ProductStockRequestForMaterial material = new ProductStockRequestForMaterial( + materialEntity.getOwnMaterialNumber(), + materialEntity.getMaterialNumberCx(), + materialPartnerRelation.getPartnerMaterialNumber() + ); + productStockRequest.getContent().getProductStock().add(material); String [] data = edcAdapterService.getContractForRequestApi(supplierPartner.getEdcUrl()); if(data == null) { @@ -318,54 +333,48 @@ 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 (requestService.findRequestByHeaderUuid(randomUuid) != null) { + 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(variablesService.getOwnBpnl()); + messageHeader.setSenderEdc(variablesService.getOwnEdcIdsUrl()); // 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 = requestService.createRequest(productStockRequest); - var test = requestService.findRequestByHeaderUuid(requestDto.getHeader().getRequestId()); - log.debug("Stored in Database " + (test != null) + " " + requestDto.getHeader().getRequestId()); + productStockRequest = productStockRequestService.createRequest(productStockRequest); + 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 = 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); + productStockRequestService.updateState(productStockRequest, DT_RequestStateEnum.REQUESTED); } else { - log.warn("Receviced HTTP Status Code " + response.code() + " for request " + productStockRequest.getHeader().getRequestId() + log.warn("Received 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/stock/logic/dto/FrontendMaterialDto.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/FrontendMaterialDto.java new file mode 100644 index 00000000..89a55009 --- /dev/null +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/dto/FrontendMaterialDto.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Volkswagen AG + * Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V. + * (represented by Fraunhofer ISST) + * Copyright (c) 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ +package org.eclipse.tractusx.puris.backend.stock.logic.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class FrontendMaterialDto { + String ownMaterialNumber; + String description; +} From 1db974671ef387fa38a4e45fa05fc77b58faa77a Mon Sep 17 00:00:00 2001 From: --show-origin Date: Fri, 18 Aug 2023 07:01:59 -0700 Subject: [PATCH 2/3] refactor: use business identifiers and m:n relationships --- frontend/.env | 4 +-- frontend/src/views/StockView.vue | 30 ++++++++++---------- frontend/src/views/stock/PartnerStockSFC.vue | 28 ++++++++---------- frontend/src/views/stock/StockTableSFC.vue | 17 ++++++----- 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/frontend/.env b/frontend/.env index 370ef96a..5ff918ed 100644 --- a/frontend/.env +++ b/frontend/.env @@ -5,6 +5,6 @@ VITE_ENDPOINT_MATERIALS=stockView/materials VITE_ENDPOINT_PRODUCTS=stockView/products 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_CUSTOMER=stockView/customer?ownMaterialNumber= +VITE_ENDPOINT_PARTNER_PRODUCT_STOCKS=stockView/partner-product-stocks?ownMaterialNumber= VITE_ENDPOINT_UPDATE_PARTNER_PRODUCT_STOCK=stockView/update-partner-product-stock?ownMaterialNumber= diff --git a/frontend/src/views/StockView.vue b/frontend/src/views/StockView.vue index 78844ad8..fe1ad2aa 100644 --- a/frontend/src/views/StockView.vue +++ b/frontend/src/views/StockView.vue @@ -59,8 +59,8 @@ v-model="this.changedStock.materialId" :disabled="this.changedStock.type === 'Product'" > - @@ -71,10 +71,10 @@ id="productSelect" v-model="this.changedStock.productId" :disabled="this.changedStock.type === 'Material'" - @change="onProductChange($event)" + @change="onProductChange(this.changedStock.productId)" > - @@ -157,12 +157,12 @@ export default { }, site: { bpns: "BPNS12345678910ZZZ", - name: "Wolfsburg Hauptwertk", + name: "Wolfsburg Hauptwerk", }, }; }, mounted() { - console.log("backendURL in StockView: " + this.backendURL); + console.log("backendURL in StockView: " + this.backendURL); fetch(this.backendURL + this.endpointMaterials) .then(res => res.json()) .then(data => this.bdMaterials = data) @@ -181,7 +181,7 @@ export default { addOrUpdateStock(changedStock) { if (changedStock.type === "Material") { var existingMaterialStocks = this.bdMaterialStocks.filter( - (stock) => (stock.material.uuid === changedStock.materialId) + (stock) => (stock.material.materialNumberCustomer === changedStock.materialId) ); if (existingMaterialStocks.length === 1) { // Update existing material stock @@ -193,13 +193,13 @@ export default { } else { // Create new material stock // 1. Determine product var existingMaterial = this.bdMaterials.filter( - (m) => m.uuid === changedStock.materialId + (m) => m.materialNumberCustomer === changedStock.materialId )[0]; // 2. Create Stock var newStock = { uuid: "", - material: existingMaterial, + material: { materialNumberCustomer: existingMaterial.ownMaterialNumber } , quantity: changedStock.quantity, unitOfMeasure: existingMaterial.unitOfMeasure, allocatedToCustomerPartner: existingCustomer, @@ -214,7 +214,7 @@ export default { } } else if (changedStock.type === "Product") { var existingProductStocks = this.bdProductStocks.filter( - (stock) => (stock.material.uuid === changedStock.productId) && + (stock) => (stock.material.materialNumberSupplier === changedStock.productId) && (stock.allocatedToCustomerPartner.uuid === changedStock.allocatedToCustomer) ); @@ -227,7 +227,7 @@ export default { } else { // Create new product stock // 1. Determine product var existingProduct = this.bdProducts.filter( - (p) => p.uuid === changedStock.productId + (p) => p.ownMaterialNumber === changedStock.productId )[0]; // 2. Determine partner @@ -238,7 +238,7 @@ export default { // 3. Create Stock newStock = { uuid: "", - material: existingProduct, + material: {materialNumberSupplier: existingProduct.ownMaterialNumber}, quantity: changedStock.quantity, unitOfMeasure: existingProduct.unitOfMeasure, allocatedToCustomerPartner: existingCustomer, @@ -284,8 +284,8 @@ export default { .then(data => console.log(data)) .catch(err => console.log(err)); }, - onProductChange(event) { - fetch(this.backendURL + this.endpointCustomer + event.target.value) + onProductChange(productId) { + fetch(this.backendURL + this.endpointCustomer + productId) .then(res => res.json()) .then(data => this.bdCustomers = data) .catch(err => console.log(err)); diff --git a/frontend/src/views/stock/PartnerStockSFC.vue b/frontend/src/views/stock/PartnerStockSFC.vue index a8b91c68..2c0ef2f2 100644 --- a/frontend/src/views/stock/PartnerStockSFC.vue +++ b/frontend/src/views/stock/PartnerStockSFC.vue @@ -59,7 +59,6 @@ export default { props: { selectedMaterialOrProductId: {type: String, required: true}, - materialUuid: {type: String, required: true}, partnerRole: {type: String, required: true}, }, data() { @@ -73,29 +72,26 @@ export default { created() { if (this.selectedMaterialOrProductId !== "") { if (this.partnerRole === "supplier") { - this.getAvailableMaterials( - this.selectedMaterialOrProductId - ); - } else if (this.partnerRole === "customer") { - this.getAvailableProducts( - this.selectedMaterialOrProductId - ); + this.getAvailableMaterials(); } + // else if (this.partnerRole === "customer") { + // this.getAvailableProducts(); + // } } }, methods: { - getAvailableMaterials(materialId) { - fetch(this.backendURL + this.endpointPartnerProductStocks) - .then(res => res.json()) - .then(data => this.availableMaterialsOrProducts = data) - .catch(err => console.log(err)); - }, - getAvailableProducts(productId) { - fetch(this.backendURL + this.endpointPartnerProductStocks) + getAvailableMaterials() { + fetch(this.backendURL + this.endpointPartnerProductStocks + this.selectedMaterialOrProductId) .then(res => res.json()) .then(data => this.availableMaterialsOrProducts = data) .catch(err => console.log(err)); }, + // getAvailableProducts() { + // fetch(this.backendURL + this.endpointPartnerProductStocks) + // .then(res => res.json()) + // .then(data => this.availableMaterialsOrProducts = data) + // .catch(err => console.log(err)); + // }, updateMaterialOrProduct() { fetch(this.backendURL + this.endpointUpdatePartnerProductStock + this.selectedMaterialOrProductId) .then(res => res.json()) diff --git a/frontend/src/views/stock/StockTableSFC.vue b/frontend/src/views/stock/StockTableSFC.vue index ae4a5751..35282a62 100644 --- a/frontend/src/views/stock/StockTableSFC.vue +++ b/frontend/src/views/stock/StockTableSFC.vue @@ -32,9 +32,9 @@ {{ stock.material.materialNumberCustomer }} {{ stock.material.name }} @@ -45,10 +45,9 @@