Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/material service frontend #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,71 +52,53 @@ 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<Object> 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) {
log.error("RequestId already in use");
return ResponseEntity.status(422).build();
}

productStockRequestDto.setState(DT_RequestStateEnum.RECEIPT);
try {
ProductStockRequest newProductStockRequestEntity = new ProductStockRequest();
newProductStockRequestEntity.setHeader(modelMapper.map(productStockRequestDto.getHeader(), MessageHeader.class));
List<ProductStockRequestForMaterial> 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();

Expand All @@ -136,7 +111,7 @@ public ResponseEntity<Object> postRequest(@RequestBody String requestBody) {
public ResponseEntity<Object> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,7 +48,7 @@
public class ProductStockResponseApiController {

@Autowired
RequestService requestService;
ProductStockRequestService productStockRequestService;

@Autowired
ModelMapper modelMapper;
Expand All @@ -58,43 +57,37 @@ public class ProductStockResponseApiController {
ObjectMapper objectMapper;

@Autowired
ApiMarshallingService apiMarshallingService;

private ResponseApiService responseApiService;

public ProductStockResponseApiController(ResponseApiService responseApiService) {
this.responseApiService = responseApiService;
}
ProductStockResponseApiServiceImpl productStockResponseApiService;


@PostMapping("response")
public ResponseEntity<Object> 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();
} else {
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));
Expand Down
Loading
Loading