Skip to content

Commit

Permalink
Merge branch 'main' into feat/delivery-information-frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneSchroederLJ authored May 14, 2024
2 parents e726018 + c7eded8 commit 9684945
Show file tree
Hide file tree
Showing 29 changed files with 1,396 additions and 264 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void setupCustomerRole() throws JsonProcessingException {
Partner supplierPartner = createAndGetSupplierPartner();
Material semiconductorMaterial = getNewSemiconductorMaterialForCustomer();
Partner mySelf = partnerService.getOwnPartnerEntity();

semiconductorMaterial.setProductFlag(true);
semiconductorMaterial = materialService.create(semiconductorMaterial);
log.info(String.format("Created material: %s", semiconductorMaterial));
List<Material> materialsFound = materialService.findAllMaterials();
Expand All @@ -161,8 +161,7 @@ private void setupCustomerRole() throws JsonProcessingException {


MaterialPartnerRelation semiconductorPartnerRelation = new MaterialPartnerRelation(semiconductorMaterial,
supplierPartner, semiconductorMatNbrSupplier, true, false);
// semiconductorPartnerRelation.setPartnerCXNumber(semiconductorMatNbrCatenaX);
supplierPartner, semiconductorMatNbrSupplier, true, true);
mprService.create(semiconductorPartnerRelation);
semiconductorPartnerRelation = mprService.find(semiconductorMaterial, supplierPartner);
log.info("Found Relation: " + semiconductorPartnerRelation);
Expand Down Expand Up @@ -220,6 +219,28 @@ private void setupCustomerRole() throws JsonProcessingException {
.build();
reportedMaterialItemStock = reportedMaterialItemStockService.create(reportedMaterialItemStock);
log.info("Created ReportedMaterialItemStock: \n" + reportedMaterialItemStock);

ProductItemStock productItemStock = ProductItemStock.builder()
.partner(supplierPartner)
.material(semiconductorMaterial)
.lastUpdatedOnDateTime(new Date())
.measurementUnit(ItemUnitEnumeration.UNIT_PIECE)
.quantity(33)
.locationBpna(mySelf.getSites().first().getAddresses().first().getBpna())
.locationBpns(mySelf.getSites().first().getBpns())
.build();
productItemStockService.create(productItemStock);

ReportedProductItemStock reportedProductItemStock = ReportedProductItemStock.builder()
.partner(supplierPartner)
.material(semiconductorMaterial)
.lastUpdatedOnDateTime(new Date())
.measurementUnit(ItemUnitEnumeration.UNIT_PIECE)
.quantity(44)
.locationBpns(supplierPartner.getSites().first().getBpns())
.locationBpna(supplierPartner.getSites().first().getAddresses().first().getBpna())
.build();
reportedProductItemStockService.create(reportedProductItemStock);
}

/**
Expand All @@ -228,6 +249,7 @@ private void setupCustomerRole() throws JsonProcessingException {
private void setupSupplierRole() {
Partner customerPartner = createAndGetCustomerPartner();
Material semiconductorMaterial = getNewSemiconductorMaterialForSupplier();
semiconductorMaterial.setMaterialFlag(true);
Partner mySelf = partnerService.getOwnPartnerEntity();

Site secondSite = new Site(
Expand All @@ -247,8 +269,7 @@ private void setupSupplierRole() {
log.info(String.format("Created product: %s", semiconductorMaterial));

MaterialPartnerRelation semiconductorPartnerRelation = new MaterialPartnerRelation(semiconductorMaterial,
customerPartner, semiconductorMatNbrCustomer, false, true);
// semiconductorPartnerRelation.setPartnerCXNumber(semiconductorMatNbrCatenaX);
customerPartner, semiconductorMatNbrCustomer, true, true);
semiconductorPartnerRelation = mprService.create(semiconductorPartnerRelation);

log.info("Created Relation " + semiconductorPartnerRelation);
Expand Down Expand Up @@ -308,6 +329,28 @@ private void setupSupplierRole() {
.build();
reportedProductItemStock = reportedProductItemStockService.create(reportedProductItemStock);
log.info("Created ReportedProductItemStock \n" + reportedProductItemStock);

MaterialItemStock materialItemStock = MaterialItemStock.builder()
.partner(customerPartner)
.material(semiconductorMaterial)
.lastUpdatedOnDateTime(new Date())
.measurementUnit(ItemUnitEnumeration.UNIT_PIECE)
.quantity(400)
.locationBpna(siteLa.getAddresses().stream().findFirst().get().getBpna())
.locationBpns(siteLa.getBpns())
.build();
materialItemStockService.create(materialItemStock);

ReportedMaterialItemStock reportedMaterialItemStock = ReportedMaterialItemStock.builder()
.partner(customerPartner)
.material(semiconductorMaterial)
.lastUpdatedOnDateTime(new Date())
.measurementUnit(ItemUnitEnumeration.UNIT_PIECE)
.quantity(23)
.locationBpna(customerPartner.getSites().first().getAddresses().first().getBpna())
.locationBpns(customerPartner.getSites().first().getBpns())
.build();
reportedMaterialItemStockService.create(reportedMaterialItemStock);
}

/**
Expand Down Expand Up @@ -402,7 +445,6 @@ private Partner createAndGetNonScenarioCustomer() {
private Material getNewSemiconductorMaterialForSupplier() {
Material material = new Material();
material.setOwnMaterialNumber(semiconductorMatNbrSupplier);
material.setMaterialNumberCx(semiconductorMatNbrCatenaX);
material.setProductFlag(true);
material.setName("Semiconductor");
return material;
Expand All @@ -411,7 +453,6 @@ private Material getNewSemiconductorMaterialForSupplier() {
private Material getNewSemiconductorMaterialForCustomer() {
Material material = new Material();
material.setOwnMaterialNumber(semiconductorMatNbrCustomer);
// material.setMaterialNumberCx(semiconductorMatNbrCatenaX);
material.setMaterialFlag(true);
material.setName("Semiconductor");
return material;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@

package org.eclipse.tractusx.puris.backend.common.ddtr.domain.model;

import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.util.HashMap;
import java.util.Map;

@Entity
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -41,10 +39,4 @@ public class DigitalTwinMapping {

private String productTwinId;

@ElementCollection
@CollectionTable(name = "suppliersmap", joinColumns = @JoinColumn(name = "own_mat_nbr", referencedColumnName = "ownMaterialNumber"))
@MapKeyColumn(name = "key")
@Column(name = "value")
private Map<String, String> materialSupplierTwinIds = new HashMap<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.tractusx.puris.backend.common.ddtr.domain.model.DigitalTwinMapping;
import org.eclipse.tractusx.puris.backend.common.ddtr.domain.repository.DigitalTwinMappingRepository;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -37,6 +36,7 @@ public class DigitalTwinMappingService {
@Autowired
private DigitalTwinMappingRepository repository;


public DigitalTwinMapping create(Material material) {
if(repository.findById(material.getOwnMaterialNumber()).isPresent()) {
log.error("DTR Mapping for " + material.getOwnMaterialNumber() + " already exists");
Expand All @@ -63,19 +63,6 @@ public DigitalTwinMapping update(Material material) {
return repository.save(dtm);
}

public DigitalTwinMapping update(MaterialPartnerRelation mpr) {
var searchResult = repository.findById(mpr.getMaterial().getOwnMaterialNumber());
if (searchResult.isEmpty()) {
log.error("DTR Mapping did not exist. Update failed for " + mpr);
return null;
}
var dtm = searchResult.get();
if (mpr.getMaterial().isMaterialFlag() && mpr.isPartnerSuppliesMaterial()) {
dtm.getMaterialSupplierTwinIds().put(mpr.getPartner().getBpnl(), mpr.getPartnerCXNumber());
}
return repository.save(dtm);
}

public DigitalTwinMapping get(String ownMaterialNumber) {
return repository.findById(ownMaterialNumber).orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,84 +103,73 @@ private Response sendDtrGetRequest(List<String> pathSegments, Map<String, String
/**
* Updates an existing product-AAS with all corresponding customer partners.
*
* @param material The given Material
* @param mprs The list of all MaterialProductRelations that exist with customers of the given Material
* @return true, if the DTR signaled a successful registration
* @param material The given Material
* @param mprs The list of all MaterialProductRelations that exist with customers of the given Material
* @return The HTTP response code from the DTR, or null if none was received
*/
public boolean updateProduct(Material material, List<MaterialPartnerRelation> mprs) {
public Integer updateProduct(Material material, List<MaterialPartnerRelation> mprs) {
String twinId = digitalTwinMappingService.get(material).getProductTwinId();
String idAsBase64 = Base64.getEncoder().encodeToString(twinId.getBytes(StandardCharsets.UTF_8));
var body = dtrRequestBodyBuilder.createProductRegistrationRequestBody(material, twinId, mprs);
try (var response = sendDtrPutRequest(body, List.of("api", "v3.0", "shell-descriptors", idAsBase64))) {
var bodyString = response.body().string();
log.info("Response Code " + response.code());
if (response.isSuccessful()) {
return true;
}
log.error("Failure in update for product twin " + material.getOwnMaterialNumber() + "\n" + bodyString);
try (var response = sendDtrPutRequest(body, List.of("api", "v3", "shell-descriptors", idAsBase64))) {
return response.code();
} catch (Exception e) {
log.error("Failure in update for product twin " + material.getOwnMaterialNumber(), e);
}
return false;
return null;
}

/**
* Call this method when a new Material with a product flag was created in your MaterialService - or if a product
* flag was later added to an existing Material.
*
* <p>
* A new AAS will be registered for this Material at your dDTR.
*
* @param material The Material
* @return true, if the DTR signaled a successful registration
* @param material The Material
* @return The HTTP response code from the DTR, or null if none was received
*/
public boolean registerProductAtDtr(Material material) {
public Integer registerProductAtDtr(Material material) {
String twinId = digitalTwinMappingService.get(material).getProductTwinId();
var body = dtrRequestBodyBuilder.createProductRegistrationRequestBody(material, twinId, List.of());
try (var response = sendDtrPostRequest(body, List.of("api", "v3.0", "shell-descriptors"))) {
var bodyString = response.body().string();
if (response.isSuccessful()) {
return true;
}
log.error("Failed to register product at DTR " + material.getOwnMaterialNumber() + "\n" + bodyString);
try (var response = sendDtrPostRequest(body, List.of("api", "v3", "shell-descriptors"))) {
return response.code();
} catch (Exception e) {
log.error("Failed to register product at DTR " + material.getOwnMaterialNumber(), e);
}
return false;
return null;
}

/**
* Call this method when a MaterialPartnerRelation was created or updated it's flag signals that this partner is
* a supplier for the referenced Material.
*
* @param supplierPartnerRelation The MaterialPartnerRelation indicating a supplier for a given Material.
* @return
* @param supplierPartnerRelation The MaterialPartnerRelation indicating a supplier for a given Material.
* @return The HTTP response code from the DTR, or null if none was received
*/
public boolean registerMaterialAtDtr(MaterialPartnerRelation supplierPartnerRelation) {
public Integer registerMaterialAtDtr(MaterialPartnerRelation supplierPartnerRelation) {
var body = dtrRequestBodyBuilder.createMaterialRegistrationRequestBody(supplierPartnerRelation);
try (var response = sendDtrPostRequest(body, List.of("api", "v3.0", "shell-descriptors"))) {
var bodyString = response.body().string();
if(response.isSuccessful()) {
return true;
}
log.error("Failed to register material at DTR " + supplierPartnerRelation.getMaterial().getOwnMaterialNumber() + "\n" + bodyString);
try (var response = sendDtrPostRequest(body, List.of("api", "v3", "shell-descriptors"))) {
return response.code();
} catch (Exception e) {
log.error("Failed to register material at DTR " + supplierPartnerRelation.getMaterial().getOwnMaterialNumber(), e);
}
return false;
return null;
}

public boolean updateMaterialAtDtr(MaterialPartnerRelation supplierPartnerRelation) {
/**
* Updates an existing material-AAS with the Information from the given MaterialPartnerRelation
*
* @param supplierPartnerRelation The MPR that indicates the material and the partner
* @return The HTTP response code from the DTR, or null if none was received
*/
public Integer updateMaterialAtDtr(MaterialPartnerRelation supplierPartnerRelation) {
var body = dtrRequestBodyBuilder.createMaterialRegistrationRequestBody(supplierPartnerRelation);
String idAsBase64 = Base64.getEncoder().encodeToString(supplierPartnerRelation.getPartnerCXNumber().getBytes(StandardCharsets.UTF_8));
try (var response = sendDtrPutRequest(body, List.of("api", "v3.0", "shell-descriptors", idAsBase64))) {
var bodyString = response.body().string();
if(response.isSuccessful()) {
return true;
}
log.error("Failed to register material at DTR " + supplierPartnerRelation.getMaterial().getOwnMaterialNumber() + "\n" + bodyString);
try (var response = sendDtrPutRequest(body, List.of("api", "v3", "shell-descriptors", idAsBase64))) {
return response.code();
} catch (Exception e) {
log.error("Failed to register material at DTR " + supplierPartnerRelation.getMaterial().getOwnMaterialNumber(), e);
}
return false;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public JsonNode createMaterialRegistrationRequestBody(MaterialPartnerRelation ma

submodelDescriptorsArray.add(createSubmodelObject(SubmodelType.ITEM_STOCK.URN_SEMANTIC_ID, href + DirectionCharacteristic.INBOUND + "/", variablesService.getItemStockSubmodelApiAssetId()));
submodelDescriptorsArray.add(createSubmodelObject(SubmodelType.DEMAND.URN_SEMANTIC_ID, href, variablesService.getDemandSubmodelApiAssetId()));
submodelDescriptorsArray.add(createSubmodelObject(SubmodelType.DELIVERY.URN_SEMANTIC_ID, href, variablesService.getDeliverySubmodelApiAssetId()));

log.debug("Created body for material " + material.getOwnMaterialNumber() + "\n" + body.toPrettyString());
return body;
Expand Down
Loading

0 comments on commit 9684945

Please sign in to comment.