Skip to content

Commit

Permalink
Merge pull request eclipse-tractusx#149 from FraunhoferISST/feat/stoc…
Browse files Browse the repository at this point in the history
…kview-add-new-fields

Feat: add bpns and bpna to StockView
  • Loading branch information
tom-rm-meyer-ISST authored Jan 4, 2024
2 parents 0d84082 + 540315d commit 83da1b9
Show file tree
Hide file tree
Showing 23 changed files with 842 additions and 214 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -151,6 +151,7 @@ private void createOwnPartnerEntity() {
private void setupCustomerRole() throws JsonProcessingException {
Partner supplierPartner = createAndGetSupplierPartner();
Material semiconductorMaterial = getNewSemiconductorMaterialForCustomer();
Partner mySelf = partnerService.getOwnPartnerEntity();

semiconductorMaterial = materialService.create(semiconductorMaterial);
log.info(String.format("Created material: %s", semiconductorMaterial));
Expand Down Expand Up @@ -197,13 +198,25 @@ private void setupCustomerRole() throws JsonProcessingException {
semiconductorMaterial,
5,
MeasurementUnit.piece,
"BPNS4444444444XX",
mySelf.getSites().first().getBpns(),
LocationIdTypeEnum.B_P_N_S,
new Date()
);
materialStockEntity = materialStockService.create(materialStockEntity);
log.info(String.format("Created materialStock: %s", materialStockEntity));

// Create Product Stock
ProductStock productStockEntity = new ProductStock(
centralControlUnitEntity,
20,
MeasurementUnit.piece,
mySelf.getSites().first().getBpns(),
LocationIdTypeEnum.B_P_N_S,
new Date(),
nonScenarioCustomer
);
productStockEntity = productStockService.create(productStockEntity);
log.info(String.format("Created productStock: %s", productStockEntity));

// Create PartnerProductStock
semiconductorMaterial = materialService.findByOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber());
Expand All @@ -222,7 +235,8 @@ private void setupCustomerRole() throws JsonProcessingException {
log.info("SAMM-DTO:\n" + objectMapper.writeValueAsString(productStockSammDto));

log.info("Own Street and Number: " + variablesService.getOwnDefaultStreetAndNumber());
Partner mySelf = partnerService.getOwnPartnerEntity();

log.info(mySelf.toString());
var builder = MaterialItemStock.builder();
var materialItemStock = builder.partner(supplierPartner)
.material(semiconductorMaterial)
Expand All @@ -240,7 +254,7 @@ private void setupCustomerRole() throws JsonProcessingException {
Address address = new Address("BPNA4444444444DD", "Feldweg 1", "54545 Neustadt", "Germany");
newSite.getAddresses().add(address);
mySelf.getSites().add(newSite);
partnerService.update(mySelf);
mySelf = partnerService.update(mySelf);

builder = MaterialItemStock.builder();
var otherMaterialItemStock =
Expand Down Expand Up @@ -272,7 +286,7 @@ private void setupCustomerRole() throws JsonProcessingException {
log.info("Created SAMM\n" + node.toPrettyString());

mySelf.getSites().remove(newSite);
partnerService.update(mySelf);
mySelf = partnerService.update(mySelf);
ItemStockSamm itemStockSAMM = new ItemStockSamm();
itemStockSAMM.setMaterialNumberSupplier(semiconductorMatNbrSupplier);
itemStockSAMM.setMaterialNumberCustomer(semiconductorMatNbrCustomer);
Expand Down Expand Up @@ -319,6 +333,7 @@ private void setupCustomerRole() throws JsonProcessingException {
private void setupSupplierRole() {
Partner customerPartner = createAndGetCustomerPartner();
Material semiconductorMaterial = getNewSemiconductorMaterialForSupplier();
Partner mySelf = partnerService.getOwnPartnerEntity();

semiconductorMaterial = materialService.create(semiconductorMaterial);
log.info(String.format("Created product: %s", semiconductorMaterial));
Expand All @@ -344,7 +359,7 @@ private void setupSupplierRole() {
semiconductorMaterial,
20,
MeasurementUnit.piece,
"BPNS1234567890ZZ",
mySelf.getSites().first().getBpns(),
LocationIdTypeEnum.B_P_N_S,
new Date(),
customerPartner
Expand Down Expand Up @@ -419,6 +434,8 @@ private Partner createAndGetNonScenarioCustomer() {
"Non-Scenario Customer",
"http://nonscenario-customer.com/api/v1/dsp",
"BPNL2222222222RR",
"BPNS2222222222XY",
"Non Scneario Site",
"BPNA2222222222XZ",
"Fichtenweg 23",
"65432 Waldhausen",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -168,11 +168,7 @@ public List<Partner> findAllCustomerPartnersForMaterialId(String ownMaterialNumb

@Override
public List<Partner> findAllSupplierPartnersForMaterialId(String ownMaterialNumber) {
var searchResult = materialRepository.findById(ownMaterialNumber);
if (searchResult.isPresent()) {
return mprService.findAllSuppliersForMaterial(searchResult.get());
}
return List.of();
return mprService.findAllSuppliersForOwnMaterialNumber(ownMaterialNumber);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -28,19 +28,20 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.tractusx.puris.backend.common.api.logic.service.VariablesService;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Site;
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.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.domain.model.Stock;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.*;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum;
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.ProductStockRequestApiService;
Expand All @@ -54,6 +55,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -90,6 +92,9 @@ public class StockViewController {
@Autowired
private ModelMapper modelMapper;

@Autowired
private VariablesService variablesService;

private final Pattern materialPattern = Pattern.compile(Material.MATERIAL_NUMBER_REGEX);

@GetMapping("materials")
Expand Down Expand Up @@ -166,6 +171,7 @@ public ProductStockDto updateProductStocks(@RequestBody ProductStockDto productS
}

existingProductStock.setQuantity(productStockDto.getQuantity());
existingProductStock.setMeasurementUnit(productStockDto.getMeasurementUnit());
existingProductStock.setLastUpdatedOn(new Date());

existingProductStock = productStockService.update(existingProductStock);
Expand All @@ -180,13 +186,40 @@ private ProductStockDto convertToDto(ProductStock entity) {
var materialPartnerRelation =
mprService.find(entity.getMaterial().getOwnMaterialNumber(), entity.getAllocatedToCustomerPartner().getUuid());
dto.getMaterial().setMaterialNumberCustomer(materialPartnerRelation.getPartnerMaterialNumber());

Partner myself = partnerService.getOwnPartnerEntity();
setBpnaAndBpnsOnStockDtoBasedOnPartner(entity, dto, myself);

return dto;
}

private ProductStock convertToEntity(ProductStockDto dto) {
ProductStock productStock = modelMapper.map(dto, ProductStock.class);
Material material = materialService.findByOwnMaterialNumber(dto.getMaterial().getMaterialNumberSupplier());
productStock.setMaterial(material);

PartnerDto allocationPartner = dto.getAllocatedToPartner();

Partner existingPartner;
if(allocationPartner.getUuid() != null){

existingPartner = partnerService.findByUuid(allocationPartner.getUuid());
}else{
existingPartner = partnerService.findByBpnl(allocationPartner.getBpnl());
}

if (existingPartner == null){
throw new IllegalStateException(String.format(
"Partner for uuid %s and bpnl %s could not be found",
allocationPartner.getUuid(),
allocationPartner.getBpnl())
);
}
productStock.setAllocatedToCustomerPartner(existingPartner);

// always set to bpna and find corresponding site
productStock.setLocationId(dto.getStockLocationBpna());
productStock.setLocationIdType(LocationIdTypeEnum.B_P_N_A);
return productStock;
}

Expand Down Expand Up @@ -225,6 +258,7 @@ public MaterialStockDto updateMaterialStocks(@RequestBody MaterialStockDto mater
}

existingMaterialStock.setQuantity(materialStockDto.getQuantity());
existingMaterialStock.setMeasurementUnit(materialStockDto.getMeasurementUnit());
existingMaterialStock.setLastUpdatedOn(new Date());

existingMaterialStock = materialStockService.update(existingMaterialStock);
Expand All @@ -236,13 +270,26 @@ private MaterialStockDto convertToDto(MaterialStock entity) {
MaterialStockDto dto = modelMapper.map(entity, MaterialStockDto.class);
dto.getMaterial().setMaterialNumberCx(entity.getMaterial().getMaterialNumberCx());
dto.getMaterial().setMaterialNumberCustomer(entity.getMaterial().getOwnMaterialNumber());

dto.setStockLocationBpns(variablesService.getOwnDefaultBpns());
dto.setStockLocationBpna(variablesService.getOwnDefaultBpna());
log.info(dto.toString());

Partner myself = partnerService.getOwnPartnerEntity();
setBpnaAndBpnsOnStockDtoBasedOnPartner(entity, dto, myself);

return dto;
}

private MaterialStock convertToEntity(MaterialStockDto dto) {
MaterialStock stock = modelMapper.map(dto, MaterialStock.class);
stock.getMaterial().setOwnMaterialNumber(dto.getMaterial().getMaterialNumberCustomer());
return stock;
MaterialStock materialStock = modelMapper.map(dto, MaterialStock.class);
materialStock.getMaterial().setOwnMaterialNumber(dto.getMaterial().getMaterialNumberCustomer());

// always set to bpna and find corresponding site
materialStock.setLocationId(dto.getStockLocationBpna());
materialStock.setLocationIdType(LocationIdTypeEnum.B_P_N_A);

return materialStock;
}

@GetMapping("partner-product-stocks")
Expand Down Expand Up @@ -270,9 +317,39 @@ private PartnerProductStockDto convertToDto(PartnerProductStock entity) {
entity.getSupplierPartner().getUuid());
dto.getMaterial().setMaterialNumberSupplier(materialPartnerRelation.getPartnerMaterialNumber());

Partner customerPartner = partnerService.findByBpnl(entity.getSupplierPartner().getBpnl());
setBpnaAndBpnsOnStockDtoBasedOnPartner(entity, dto, customerPartner);

return dto;
}

/**
* helper method to lookup and set the corresponding bpna or bpns on dto
*
* @param entity Stock to set the data from
* @param dto StockDto to set the data on
* @param partner holding the stock
*/
private void setBpnaAndBpnsOnStockDtoBasedOnPartner(Stock entity, StockDto dto, Partner partner){
if (entity.getLocationIdType() == LocationIdTypeEnum.B_P_N_A){
Optional<Site> siteForAddress = partner.getSites()
.stream().filter(site -> site.getAddresses().stream().anyMatch(addr -> addr.getBpna().equals(entity.getLocationId()))).findFirst();

if (!siteForAddress.isEmpty()){
dto.setStockLocationBpns(siteForAddress.get().getBpns());
dto.setStockLocationBpna(entity.getLocationId());
}
}else{
dto.setStockLocationBpns(entity.getLocationId());
Optional<Site> siteOfPartner = partner.getSites().stream().filter(site -> site.getBpns().equals(entity.getLocationId())).findFirst();
if(!siteOfPartner.isEmpty()){
dto.setStockLocationBpna(siteOfPartner.get().getAddresses().first().getBpna());
}else{
throw new IllegalStateException(String.format("Partner %s with Site %s has no Address.", partner.getBpnl(), siteOfPartner.get().getBpns()));
}
}
}

@GetMapping("customer")
@Operation(description = "Returns a list of all Partners that are ordering the given material")
@ApiResponses(value = {
Expand All @@ -288,6 +365,21 @@ public ResponseEntity<List<PartnerDto>> getCustomerPartnersOrderingMaterial(@Req
.collect(Collectors.toList()));
}

@GetMapping("supplier")
@Operation(description = "Returns a list of all Partners that are supplying the given material")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "400", description = "Invalid parameter")
})
public ResponseEntity<List<PartnerDto>> getSupplierPartnersSupplyingMaterial(@RequestParam String ownMaterialNumber) {
if(!materialPattern.matcher(ownMaterialNumber).matches()) {
return new ResponseEntity<>(HttpStatusCode.valueOf(400));
}
return ResponseEntity.ok(partnerService.findAllSupplierPartnersForMaterialId(ownMaterialNumber).stream()
.map(this::convertToDto)
.collect(Collectors.toList()));
}

@GetMapping("update-partner-product-stock")
@Operation(description = "For the given material, all known suppliers will be requested to report their" +
"current product-stocks. The response body contains a list of those supplier partners that were sent a request." +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -27,7 +27,6 @@
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;

Expand All @@ -36,9 +35,9 @@
@AllArgsConstructor
public class MaterialStockDto extends StockDto {

public MaterialStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String locationId,
LocationIdTypeEnum locationIdType, Date lastUpdatedOn) {
super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn);
public MaterialStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String stockLocationBpns,
String stockLocationBpna, Date lastUpdatedOn) {
super(material, quantity, measurementUnit, stockLocationBpns, stockLocationBpna, lastUpdatedOn);
this.setType(DT_StockTypeEnum.MATERIAL);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -28,7 +28,6 @@
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;

Expand All @@ -39,9 +38,9 @@ public class PartnerProductStockDto extends StockDto {

private PartnerDto supplierPartner;

public PartnerProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String locationId,
LocationIdTypeEnum locationIdType, PartnerDto supplierPartner, Date lastUpdatedOn) {
super(material, quantity, measurementUnit, locationId, locationIdType, lastUpdatedOn);
public PartnerProductStockDto(MaterialDto material, double quantity, MeasurementUnit measurementUnit, String stockLocationBpns,
String stockLocationBpna, PartnerDto supplierPartner, Date lastUpdatedOn) {
super(material, quantity, measurementUnit, stockLocationBpns, stockLocationBpna, lastUpdatedOn);
this.setType(DT_StockTypeEnum.PRODUCT);
this.supplierPartner = supplierPartner;
}
Expand Down
Loading

0 comments on commit 83da1b9

Please sign in to comment.