From 36afcc836c584595fc19afcda7b06e29fc8924ee Mon Sep 17 00:00:00 2001 From: MarcoE Date: Thu, 25 Jul 2024 16:25:12 +0200 Subject: [PATCH] Importazione immagini --- .../product/importaz/ArticoliImporter.java | 6 +- .../importaz/service/ProductServices.java | 170 +++++++++++------- 2 files changed, 107 insertions(+), 69 deletions(-) diff --git a/ems-engine/src/main/java/it/integry/ems/product/importaz/ArticoliImporter.java b/ems-engine/src/main/java/it/integry/ems/product/importaz/ArticoliImporter.java index f8847bd892..fdd3b085e3 100644 --- a/ems-engine/src/main/java/it/integry/ems/product/importaz/ArticoliImporter.java +++ b/ems-engine/src/main/java/it/integry/ems/product/importaz/ArticoliImporter.java @@ -26,8 +26,9 @@ public class ArticoliImporter extends BaseEntityImporter> imple ProductServices importDistinteMorganteService = ContextLoader.getCurrentWebApplicationContext().getBean(ProductServices.class); return importDistinteMorganteService.importDistinteMorgante((String) requestDto.getRawContent(), type, format); case REPO_IMAGES: + case FOTO: ProductServices importArticoliRepoImagesServices = ContextLoader.getCurrentWebApplicationContext().getBean(ProductServices.class); - return importArticoliRepoImagesServices.importAarticoliFromREPOIMAGES(type, format, requestDto.getPathFile()); + return importArticoliRepoImagesServices.importFotoArticoli(type, format, requestDto); case APULIA: return ContextLoader.getCurrentWebApplicationContext().getBean(ProductServices.class).importArticoliApulia(type, format, requestDto.getRawContent()); default: @@ -45,7 +46,8 @@ public class ArticoliImporter extends BaseEntityImporter> imple MORGANTE("MORGANTE"), MORGANTE_DISTINTE("MORGANTE_DISTINTE"), REPO_IMAGES("REPO_IMAGES"), - APULIA("APULIA"); + APULIA("APULIA"), + FOTO("FOTO"); private String text; diff --git a/ems-engine/src/main/java/it/integry/ems/product/importaz/service/ProductServices.java b/ems-engine/src/main/java/it/integry/ems/product/importaz/service/ProductServices.java index fc9288b57b..f53d798814 100644 --- a/ems-engine/src/main/java/it/integry/ems/product/importaz/service/ProductServices.java +++ b/ems-engine/src/main/java/it/integry/ems/product/importaz/service/ProductServices.java @@ -5,6 +5,7 @@ import com.annimon.stream.Stream; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import it.integry.common.var.CommonConstants; +import it.integry.ems.Import.dto.ImportRequestDTO; import it.integry.ems.datasource.DataSource; import it.integry.ems.product.importaz.dto.ArticoliMorganteDTO; import it.integry.ems.product.importaz.dto.ContrattiVenditaDTO; @@ -62,22 +63,22 @@ public class ProductServices { private Logger logger = LogManager.getLogger(); - public List saveArtDialogo (MtbAart mtbAart) throws Exception { + public List saveArtDialogo(MtbAart mtbAart) throws Exception { List entityList = new ArrayList<>(); mtbAart.setOperation(OperationType.INSERT_OR_UPDATE); entityList.add(mtbAart); - for (MtbAartBarCode b: mtbAart.getMtbAartBarCode()) { + for (MtbAartBarCode b : mtbAart.getMtbAartBarCode()) { b.setOperation(OperationType.INSERT_OR_UPDATE); } - if (mtbAart.getMtbLisa() != null ) - for (MtbLisa l: mtbAart.getMtbLisa()) { + if (mtbAart.getMtbLisa() != null) + for (MtbLisa l : mtbAart.getMtbLisa()) { l.setOperation(OperationType.INSERT_OR_UPDATE); entityList.add(l); } - List entitites = importAnagListiniService.importAnagListinoAcq(entityList, "V" , null, null); + List entitites = importAnagListiniService.importAnagListinoAcq(entityList, "V", null, null); return entitites; } @@ -1014,47 +1015,82 @@ public class ProductServices { } - public List importAarticoliFromREPOIMAGES(String typeImport, String formatImport, String filePath) throws Exception { - List entitiesReturn = new ArrayList(); + public List importFotoArticoli(String type, String format, ImportRequestDTO requestDto) throws Exception { + List mtbAartList = new ArrayList<>(); - Connection conn = multiDBTransactionManager.getPrimaryDatasource().getConnection(); + Map setup = setupGest.getImportSetupSection(multiDBTransactionManager.getPrimaryConnection(), type, format); + String filePath = UtilityString.isNullOrEmpty(requestDto.getPathFile()) ? setup.get("FILE_FILTER") : requestDto.getPathFile(); + + if (filePath == null) { + mtbAartList.add( + prepareEntityForImportFoto(requestDto.getFileName()) + ); + } else { + File directory = new File(filePath); + + if (directory.exists() && directory.isDirectory()) { + File[] files = directory.listFiles(); + + if (files != null) { + for (File file : files) { + if (file.isFile()) { + mtbAartList.add( + prepareEntityForImportFoto(file.getName()) + ); + + } + } + } else { + throw new Exception("Cartella vuota"); + } + } + } + + if (mtbAartList.isEmpty()) { + throw new Exception("Nessuna immagine salvata"); + } + + return entityProcessor.processEntityList(mtbAartList, multiDBTransactionManager, true); + } + + private MtbAart prepareEntityForImportFoto(String fileName) throws Exception { + List validExtensions = Arrays.asList(".jpg", ".jpeg", ".png", ".gif"); + int extensionIndex = fileName.lastIndexOf("."); + String codMart; + + if (extensionIndex >= 0 && validExtensions.contains(fileName.substring(extensionIndex))) { + codMart = fileName.substring(0, extensionIndex); + } else { + return null; + } - String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1); - String codMart = fileName.substring(0, fileName.lastIndexOf(".jpg")); String[] arg = codMart.split("-"); - if (arg != null && arg.length > 0) codMart = arg[0]; + if (arg.length > 0) codMart = arg[0]; MtbAartLink searchArtLink = new MtbAartLink(); searchArtLink.setCodMart(codMart); + searchArtLink.setPathLink(fileName); searchArtLink.setOperation(OperationType.SELECT); - searchArtLink.setNativeSql("select * from mtb_aart_link where cod_mart = " + UtilityDB.valueToString(codMart) + "and path_link = " + UtilityDB.valueToString(fileName)); List artLinkResult = entityProcessor.processEntity(searchArtLink, multiDBTransactionManager); + MtbAartLink mtbAartLink = new MtbAartLink(); + mtbAartLink.setCodMart(codMart); + mtbAartLink.setPathLink(fileName); + mtbAartLink.setDescrizioneLink("Immagine prodotto " + codMart); if (artLinkResult == null || artLinkResult.isEmpty()) { - MtbAartLink mtbAartLink = new MtbAartLink(); - mtbAartLink.setCodMart(codMart); - mtbAartLink.setPathLink(fileName); - mtbAartLink.setDescrizioneLink("Immagine prodotto " + codMart); mtbAartLink.setOperation(OperationType.INSERT); - - MtbAart mtbAart = new MtbAart(); - mtbAart.setCodMart(codMart); - mtbAart.setOperation(OperationType.NO_OP); - mtbAart.getMtbAartLink().add(mtbAartLink); - - entityProcessor.processEntity(mtbAart, true, multiDBTransactionManager); - entitiesReturn.add(mtbAart); + } else { + mtbAartLink.setOperation(OperationType.UPDATE); } - /* if ( mtbAart != null){ - MtbAartLink mtbAartLink= Stream.of(mtbAart.getMtbAartLink()).filter(x-> x.getCodMart().equalsIgnoreCase(codMart) && x.getPathLink().equalsIgnoreCase(fileName)).single(); + MtbAart mtbAart = new MtbAart(); + mtbAart.setCodMart(codMart); + mtbAart.setOperation(OperationType.NO_OP); + mtbAart.getMtbAartLink().add(mtbAartLink); - } - */ - - return entitiesReturn; + return mtbAart; } public List saveSalesAgreement(ContrattiVenditaDTO contrattiVenditaDTO) throws Exception { @@ -1194,23 +1230,23 @@ public class ProductServices { String sql = ( - "WITH lisa as (\n" + - "select atb_forn.cod_alis\n" + - "from atb_forn inner join atb_list on atb_forn.cod_alis = atb_list.cod_alis\n" + - "where atb_forn.cod_anag = '[cod_anag]'\n" + - "and atb_list.part_iva_forn is null\n" + - "union\n" + - "select vtb_dest.cod_alis\n" + - "from vtb_dest inner join atb_list on vtb_dest.cod_alis = atb_list.cod_alis\n" + - "where vtb_dest.cod_anag = '[cod_anag]'\n" + - "and atb_list.part_iva_forn is null ),\n" + - "barcode as (\n" + - "select l.cod_mart\n" + - "from dbo.getListinoAcquisto(null, null, '[cod_art_for]', null, 'N', null) l\n" + - "inner join lisa on lisa.cod_alis = l.cod_alis\n" + - "where l.tipo_variazione <> 'D' and l.cod_mart is not null )\n" + - "select cod_mart from barcode") - .replace("[cod_anag]",codAnag ) + "WITH lisa as (\n" + + "select atb_forn.cod_alis\n" + + "from atb_forn inner join atb_list on atb_forn.cod_alis = atb_list.cod_alis\n" + + "where atb_forn.cod_anag = '[cod_anag]'\n" + + "and atb_list.part_iva_forn is null\n" + + "union\n" + + "select vtb_dest.cod_alis\n" + + "from vtb_dest inner join atb_list on vtb_dest.cod_alis = atb_list.cod_alis\n" + + "where vtb_dest.cod_anag = '[cod_anag]'\n" + + "and atb_list.part_iva_forn is null ),\n" + + "barcode as (\n" + + "select l.cod_mart\n" + + "from dbo.getListinoAcquisto(null, null, '[cod_art_for]', null, 'N', null) l\n" + + "inner join lisa on lisa.cod_alis = l.cod_alis\n" + + "where l.tipo_variazione <> 'D' and l.cod_mart is not null )\n" + + "select cod_mart from barcode") + .replace("[cod_anag]", codAnag) .replace("[cod_art_for]", codArtFor); String codMart = UtilityDB.executeSimpleQueryOnlyFirstRowFirstColumn(multiDBTransactionManager.getPrimaryConnection(), sql); @@ -1218,28 +1254,28 @@ public class ProductServices { if (!UtilityString.isNullOrEmpty(codMart)) { sql = "WITH barcode_new as (SELECT DISTINCT cod_barre \n" + - "FROM (\n" + - "VALUES ('" + StringUtils.join(barcode, "'),('") + "')\n" + - ") AS a(cod_barre)),\n" + - "barcode as (\n" + - "SELECT mvw_barcode.cod_mart, mvw_barcode.cod_barre\n" + - "FROM mvw_barcode \n" + - "WHERE mvw_barcode.cod_mart = " + UtilityDB.valueToString(codMart) + " \n" + - "union all\n" + - "SELECT mtb_comp.cod_mart, mvw_barcode.cod_barre\n" + - "FROM mvw_barcode inner join mtb_comp on mvw_barcode.cod_mart = mtb_comp.cod_mart\n" + - "WHERE mtb_comp.cod_comp = " + UtilityDB.valueToString(codMart) + "\n) " + - "SELECT DISTINCT CASE WHEN LEFT(barcode_new.cod_barre, 5) = '00000' THEN substring(barcode_new.cod_barre, 6, 8) ELSE barcode_new.cod_barre END as cod_barre\n" + - "FROM barcode_new \n" + - "WHERE not EXISTS(SELECT * FROM barcode WHERE barcode.cod_barre = CASE WHEN LEFT(barcode_new.cod_barre, 5) = '00000' THEN substring(barcode_new.cod_barre, 6, 8) ELSE barcode_new.cod_barre END )"; + "FROM (\n" + + "VALUES ('" + StringUtils.join(barcode, "'),('") + "')\n" + + ") AS a(cod_barre)),\n" + + "barcode as (\n" + + "SELECT mvw_barcode.cod_mart, mvw_barcode.cod_barre\n" + + "FROM mvw_barcode \n" + + "WHERE mvw_barcode.cod_mart = " + UtilityDB.valueToString(codMart) + " \n" + + "union all\n" + + "SELECT mtb_comp.cod_mart, mvw_barcode.cod_barre\n" + + "FROM mvw_barcode inner join mtb_comp on mvw_barcode.cod_mart = mtb_comp.cod_mart\n" + + "WHERE mtb_comp.cod_comp = " + UtilityDB.valueToString(codMart) + "\n) " + + "SELECT DISTINCT CASE WHEN LEFT(barcode_new.cod_barre, 5) = '00000' THEN substring(barcode_new.cod_barre, 6, 8) ELSE barcode_new.cod_barre END as cod_barre\n" + + "FROM barcode_new \n" + + "WHERE not EXISTS(SELECT * FROM barcode WHERE barcode.cod_barre = CASE WHEN LEFT(barcode_new.cod_barre, 5) = '00000' THEN substring(barcode_new.cod_barre, 6, 8) ELSE barcode_new.cod_barre END )"; List mtbAartBarCodes = new ResultSetMapper().mapQuerySetToList(multiDBTransactionManager.getPrimaryConnection(), sql, MtbAartBarCode.class, OperationType.INSERT); - if (mtbAartBarCodes != null && mtbAartBarCodes.size() > 0 ) { + if (mtbAartBarCodes != null && mtbAartBarCodes.size() > 0) { MtbAart mtbAart = new MtbAart() - .setCodMart(codMart) - .setMtbAartBarCode(mtbAartBarCodes); + .setCodMart(codMart) + .setMtbAartBarCode(mtbAartBarCodes); entityList.add(mtbAart); } @@ -1248,7 +1284,7 @@ public class ProductServices { } } - if ( entityList != null && entityList.size() > 0 ) { + if (entityList != null && entityList.size() > 0) { List entityBases = entityProcessor.processEntityList(entityList, true); UtilityEntity.throwEntitiesException(entityBases); return entityBases; @@ -1256,10 +1292,10 @@ public class ProductServices { return null; } - private List getElencoBarcode(String elencoBarcode){ + private List getElencoBarcode(String elencoBarcode) { String[] splitString = elencoBarcode.split("(?<=\\G.{13})"); List barcode = Stream.of(splitString) - .filterNot(s->s.equalsIgnoreCase("0000000000000")) + .filterNot(s -> s.equalsIgnoreCase("0000000000000")) .distinct() .toList();