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 identification #224

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 @@ -22,6 +22,7 @@
package org.eclipse.tractusx.puris.backend.masterdata.domain.repository;

import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -44,4 +45,12 @@ public interface MaterialPartnerRelationRepository extends JpaRepository<Materia
List<MaterialPartnerRelation> findAllByMaterial_OwnMaterialNumberAndPartnerBuysMaterialIsTrue(String ownMaterialNumber);

List<MaterialPartnerRelation> findAllByPartnerMaterialNumber(String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByPartnerAndPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(Partner partner, String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByPartnerAndPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(Partner partner, String partnerMaterialNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public interface MaterialPartnerRelationService {
boolean partnerSuppliesMaterial(Material material, Partner partner);

boolean partnerOrdersProduct(Material material, Partner partner);

List<MaterialPartnerRelation> findAllBySupplierPartnerMaterialNumber(String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByCustomerPartnerMaterialNumber(String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllBySupplierPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber);

List<MaterialPartnerRelation> findAllByCustomerPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber);
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,24 @@ public boolean partnerOrdersProduct(Material material, Partner partner) {
return false;
}

@Override
public List<MaterialPartnerRelation> findAllBySupplierPartnerMaterialNumber(String partnerMaterialNumber) {
return mprRepository.findAllByPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(partnerMaterialNumber);
}

@Override
public List<MaterialPartnerRelation> findAllByCustomerPartnerMaterialNumber(String partnerMaterialNumber) {
return mprRepository.findAllByPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(partnerMaterialNumber);
}

@Override
public List<MaterialPartnerRelation> findAllBySupplierPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber) {
return mprRepository.findAllByPartnerAndPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(partner, partnerMaterialNumber);
}

@Override
public List<MaterialPartnerRelation> findAllByCustomerPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber) {
return mprRepository.findAllByPartnerAndPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(partner, partnerMaterialNumber);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.eclipse.tractusx.puris.backend.masterdata.logic.service;

import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;

import java.util.List;

Expand All @@ -35,18 +36,68 @@ public interface MaterialService {

List<Material> findAllProducts();



// Material findByUuid(UUID materialUuid);

Material findByOwnMaterialNumber(String ownMaterialNumber);

Material findByMaterialNumberCx(String materialNumberCx);

List<Material> findAll();

// Material findMaterialByMaterialNumberCustomer(String materialNumberCustomer);
//
// Material findProductByMaterialNumberCustomer(String materialNumberCustomer);
/**
* This method will do a best effort attempt to return a Material Entity for the given input arguments.
* All arguments are potentially nullable. But the more arguments you provide, the better.
*
* The material will be selected
* <li>by the materialNumberCx, if possible</li>
* <li>otherwise by the customerMaterialNumber, if possible</li>
* <li>otherwise by the supplierMaterialNumber, if possible</li>
*
* If a materialNumberCx is given and there exists a Material Entity that matches, this Material will
* always be chosen.
*
* Otherwise, an attempt will be made to find a matching Material by the other arguments. In this case,
* if a specific partner is given as an argument, a matching supplierMaterialNumber will only be considered,
* if this is actually this partner's material number.
*
* This method will write warnings to the log, if mismatches or ambiguities were found.
*
* Since this method is only meant to be used from a Customer's Perspective, the customerMaterialNumber will
* be treated as ownMaterialNumber.
*
* @param materialNumberCx the CatenaX - MaterialNumber
* @param customerMatNbr the MaterialNumber on the customer's side
* @param supplierMatNbr the MaterialNumber on the supplier's side
* @param partner the Partner
* @return the Material, or null if no matching material was found
*/
Material findFromCustomerPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner);

/**
* This method will do a best effort attempt to return a Material Entity for the given input arguments.
* All arguments are potentially nullable. But the more arguments you provide, the better.
*
* The material will be selected
* <li>by the materialNumberCx, if possible</li>
* <li>otherwise by the customerMaterialNumber, if possible</li>
* <li>otherwise by the supplierMaterialNumber, if possible</li>
*
* If a materialNumberCx is given and there exists a Material Entity that matches, this Material will
* always be chosen.
*
* Otherwise, an attempt will be made to find a matching Material by the other arguments. In this case,
* if a specific partner is given as an argument, a matching customerMaterialNumber will only be considered,
* if this is actually this partner's material number.
*
* This method will write warnings to the log, if mismatches or ambiguities were found.
*
* Since this method is only meant to be used from a Supplier's Perspective, the supplierMaterialNumber will
* be treated as ownMaterialNumber.
*
* @param materialNumberCx the CatenaX - MaterialNumber
* @param customerMatNbr the MaterialNumber on the customer's side
* @param supplierMatNbr the MaterialNumber on the supplier's side
* @param partner the Partner
* @return the Material, or null if no matching material was found
*/
Material findFromSupplierPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -35,7 +37,10 @@
public class MaterialServiceImpl implements MaterialService {

@Autowired
MaterialRepository materialRepository;
private MaterialRepository materialRepository;

@Autowired
private MaterialPartnerRelationService mprService;

@Override
public Material create(Material material) {
Expand All @@ -50,7 +55,7 @@ public Material create(Material material) {
@Override
public Material update(Material material) {
Optional<Material> existingMaterial =
materialRepository.findById(material.getOwnMaterialNumber());
materialRepository.findById(material.getOwnMaterialNumber());
if (existingMaterial.isPresent()) {
return existingMaterial.get();
}
Expand Down Expand Up @@ -95,4 +100,127 @@ public List<Material> findAll() {
return materialRepository.findAll();
}

@Override
public Material findFromCustomerPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner) {
Material material = null;
if (materialNumberCx != null) {
// Use Material Number CX
material = findByMaterialNumberCx(materialNumberCx);
if (material != null) {
if (customerMatNbr != null && !material.equals(findByOwnMaterialNumber(customerMatNbr))) {
log.warn("Mismatch between " + material + " and " + customerMatNbr);
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
Dismissed Show dismissed Hide dismissed
}
if (partner != null && !mprService.partnerSuppliesMaterial(material, partner)) {
log.warn("Partner " + partner + " does not supply material " + material);
}
}

}
if (material == null && customerMatNbr != null) {
// If previous effort yielded not result, use Customer Material Number
material = findByOwnMaterialNumber(customerMatNbr);
if (material != null && materialNumberCx != null) {
log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material);
Dismissed Show dismissed Hide dismissed
}
if (material != null && partner != null && !mprService.partnerSuppliesMaterial(material, partner)) {
log.warn("Partner " + partner + " does not supply material " + material);
}
}
if (material == null && supplierMatNbr != null) {
// If previous effort yielded not result, use Supplier Material Number
List<Material> materialList;
if (partner != null) {
materialList = mprService.findAllBySupplierPartnerAndPartnerMaterialNumber(partner, supplierMatNbr)
.stream()
.map(MaterialPartnerRelation::getMaterial)
.toList();
} else {
materialList = mprService.findAllBySupplierPartnerMaterialNumber(supplierMatNbr)
.stream()
.map(MaterialPartnerRelation::getMaterial)
.toList();
}

if (!materialList.isEmpty()) {
material = materialList.get(0);
}
if (materialList.size() > 1) {
log.warn("Ambiguous results for supplier partner Material Number " + supplierMatNbr + ", arbitrarily choosing " + material);
Dismissed Show dismissed Hide dismissed
}
if (material != null) {
if (materialNumberCx != null) {
log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material);
Dismissed Show dismissed Hide dismissed
}
if (customerMatNbr != null) {
log.warn("Unknown Customer Material Number " + customerMatNbr + " for Material " + material);
Dismissed Show dismissed Hide dismissed
}
}
}

return material;
}

@Override
public Material findFromSupplierPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner) {
Material material = null;
if (materialNumberCx != null) {
// Use Material Number CX
material = findByMaterialNumberCx(materialNumberCx);
if (material != null) {
if (supplierMatNbr != null && !material.equals(findByOwnMaterialNumber(supplierMatNbr))) {
log.warn("Mismatch between " + material + " and " + supplierMatNbr);
Dismissed Show dismissed Hide dismissed
}
if(partner != null && ! mprService.partnerOrdersProduct(material, partner)) {
log.warn("Partner " + partner + " does not order material " + material);
}
}
}
if (material == null && customerMatNbr != null) {
// If previous effort yielded not result, use Customer Material Number
List<Material> materialList;
if (partner != null) {
materialList = mprService.findAllByCustomerPartnerAndPartnerMaterialNumber(partner, customerMatNbr)
.stream()
.map(MaterialPartnerRelation::getMaterial)
.toList();
} else {
materialList = mprService.findAllByCustomerPartnerMaterialNumber(customerMatNbr)
.stream()
.map(MaterialPartnerRelation::getMaterial)
.toList();
}

if (!materialList.isEmpty()) {
material = materialList.get(0);
}
if (materialList.size() > 1) {
log.warn("Ambiguous results for customer partner Material Number " + customerMatNbr + ", arbitrarily choosing " + material);
Dismissed Show dismissed Hide dismissed
}
if (material != null) {
if (materialNumberCx != null) {
log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material);
Dismissed Show dismissed Hide dismissed
}
if (supplierMatNbr != null && !material.equals(findByOwnMaterialNumber(supplierMatNbr))) {
log.warn("Mismatch between OwnMaterialNumber " + supplierMatNbr + " and " + material);
Dismissed Show dismissed Hide dismissed
}
}
}
if (material == null && supplierMatNbr != null) {
// If previous effort yielded not result, use Supplier Material Number
material = findByOwnMaterialNumber(supplierMatNbr);
if (material != null) {
if (materialNumberCx != null) {
log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material);
Dismissed Show dismissed Hide dismissed
}
if (customerMatNbr != null) {
log.warn("Unknown customer Material Number " + customerMatNbr + " for Material " + material);
Dismissed Show dismissed Hide dismissed
}
if(partner != null && ! mprService.partnerOrdersProduct(material, partner)) {
log.warn("Partner " + partner + " does not order material " + material);
}
}
}
return material;
}

}
Loading
Loading