Bulk operation:

- Material import implemented
This commit is contained in:
Jan 2025-09-23 18:30:35 +02:00
parent afb72e4d44
commit ef76bf382f
3 changed files with 78 additions and 2 deletions

View file

@ -207,6 +207,21 @@ public class MaterialRepository {
.collect(Collectors.toList());
}
@Transactional
public void insert(Material material) {
String sql = """
INSERT INTO material (part_number, normalized_part_number, hs_code, name, is_deprecated)
VALUES (?, ?, ?, ?, ?)
""";
jdbcTemplate.update(sql,
material.getPartNumber(),
material.getNormalizedPartNumber(),
material.getHsCode(),
material.getName(),
material.getDeprecated()
);
}
private static class MaterialMapper implements RowMapper<Material> {

View file

@ -3,6 +3,7 @@ package de.avatic.lcc.service.bulk;
import de.avatic.lcc.model.bulk.BulkFileTypes;
import de.avatic.lcc.model.bulk.BulkOperation;
import de.avatic.lcc.repositories.NodeRepository;
import de.avatic.lcc.service.bulk.bulkImport.MaterialBulkImportService;
import de.avatic.lcc.service.bulk.bulkImport.NodeBulkImportService;
import de.avatic.lcc.service.bulk.bulkImport.PackagingBulkImportService;
import de.avatic.lcc.service.excelMapper.*;
@ -30,8 +31,9 @@ public class BulkImportService {
private final NodeTransformer nodeTransformer;
private final NodeBulkImportService nodeBulkImportService;
private final PackagingBulkImportService packagingBulkImportService;
private final MaterialBulkImportService materialBulkImportService;
public BulkImportService(MatrixRateExcelMapper matrixRateExcelMapper, ContainerRateExcelMapper containerRateExcelMapper, MaterialExcelMapper materialExcelMapper, PackagingExcelMapper packagingExcelMapper, NodeExcelMapper nodeExcelMapper, NodeRepository nodeRepository, NodeTransformer nodeTransformer, NodeBulkImportService nodeBulkImportService, PackagingBulkImportService packagingBulkImportService) {
public BulkImportService(MatrixRateExcelMapper matrixRateExcelMapper, ContainerRateExcelMapper containerRateExcelMapper, MaterialExcelMapper materialExcelMapper, PackagingExcelMapper packagingExcelMapper, NodeExcelMapper nodeExcelMapper, NodeRepository nodeRepository, NodeTransformer nodeTransformer, NodeBulkImportService nodeBulkImportService, PackagingBulkImportService packagingBulkImportService, MaterialBulkImportService materialBulkImportService) {
this.matrixRateExcelMapper = matrixRateExcelMapper;
this.containerRateExcelMapper = containerRateExcelMapper;
this.materialExcelMapper = materialExcelMapper;
@ -41,6 +43,7 @@ public class BulkImportService {
this.nodeTransformer = nodeTransformer;
this.nodeBulkImportService = nodeBulkImportService;
this.packagingBulkImportService = packagingBulkImportService;
this.materialBulkImportService = materialBulkImportService;
}
public void processOperation(BulkOperation op) throws IOException {
@ -63,7 +66,7 @@ public class BulkImportService {
break;
case MATERIAL:
var materials = materialExcelMapper.extractSheet(sheet);
// materials.forEach((material) -> materialBulkImportService);
materials.forEach(materialBulkImportService::processMaterialInstructions);
break;
case PACKAGING:
var packaging = packagingExcelMapper.extractSheet(sheet);

View file

@ -0,0 +1,58 @@
package de.avatic.lcc.service.bulk.bulkImport;
import de.avatic.lcc.model.bulk.BulkInstruction;
import de.avatic.lcc.model.bulk.BulkInstructionType;
import de.avatic.lcc.model.materials.Material;
import de.avatic.lcc.repositories.MaterialRepository;
import de.avatic.lcc.service.transformer.generic.MaterialTransformer;
import de.avatic.lcc.util.exception.internalerror.ExcelValidationError;
import org.springframework.stereotype.Service;
@Service
public class MaterialBulkImportService {
private final MaterialRepository materialRepository;
public MaterialBulkImportService(MaterialRepository materialRepository) {
this.materialRepository = materialRepository;
}
private static String normalizePartNumber(String partNumber) {
if (partNumber.length() > 12) throw new IllegalArgumentException("Part number must be less than 12 characters");
return "000000000000".concat(partNumber).substring(partNumber.length());
}
public void processMaterialInstructions(BulkInstruction<Material> instr) {
BulkInstructionType instrType = instr.getType();
Material material = instr.getEntity();
if (instrType == BulkInstructionType.UPDATE) {
updateMaterial(material);
} else if (instrType == BulkInstructionType.DELETE) {
deleteMaterial(material);
}
}
private void updateMaterial(Material material) {
var foundMaterial = materialRepository.getByPartNumber(material.getNormalizedPartNumber());
if (foundMaterial.isEmpty()) {
materialRepository.insert(material);
} else {
material.setId(foundMaterial.get().getId());
materialRepository.update(material);
}
}
private void deleteMaterial(Material material) {
var foundMaterial = materialRepository.getByPartNumber(material.getNormalizedPartNumber());
if (foundMaterial.isEmpty()) {
throw new ExcelValidationError("Unable to delete. Material with part number " + material.getPartNumber() + " not found");
} else {
materialRepository.setDeprecatedById(foundMaterial.get().getId());
}
}
}