From 13fb9a871e55e3382af4e55dd89e7657ffa78e5b Mon Sep 17 00:00:00 2001 From: Reguel Wermelinger Date: Fri, 10 Nov 2023 11:38:22 +0100 Subject: [PATCH] stabilize; continue master/detail creation even if import of data failed --- .../util/excel/importer/EntityDataLoader.java | 10 +++++-- .../importer/wizard/ExcelImportProcessor.java | 28 +++++++++++++------ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java b/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java index 89c49a6..e9d1909 100644 --- a/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java +++ b/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java @@ -11,6 +11,7 @@ import javax.persistence.EntityManager; +import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; @@ -65,6 +66,7 @@ private PreparedStatement loadRows(IEntityClass entity, Iterator rows, Conn AtomicInteger rCount = new AtomicInteger(); List fields = entity.getFields(); var query = buildInsertQuery(entity, fields); + LOGGER.info("Prepared insert query: "+query); var stmt = con.prepareStatement(query, Statement.NO_GENERATED_KEYS); rows.forEachRemaining(row -> { try { @@ -75,7 +77,7 @@ private PreparedStatement loadRows(IEntityClass entity, Iterator rows, Conn throw new RuntimeException(ex); } }); - System.out.println("Generated "+rCount+" inserts"); + LOGGER.info("Generated "+rCount+" inserts"); return stmt; } @@ -96,7 +98,11 @@ private static String buildInsertQuery(IEntityClass entity, List !fld.equals("id")) .collect(Collectors.joining(",")); - var query = new StringBuilder("INSERT INTO "+entity.getSimpleName()+" ("+colNames+")\nVALUES ("); + String tableName = entity.getDatabaseTableName(); + if (StringUtils.isBlank(tableName)) { + tableName = entity.getSimpleName(); + } + var query = new StringBuilder("INSERT INTO "+tableName+" ("+colNames+")\nVALUES ("); var params = fields.stream().map(IEntityClassField::getName) .filter(fld -> !fld.equals("id")) .map(f -> ":"+f+"").collect(Collectors.joining(", ")); diff --git a/excel-importer/src/com/axonivy/util/excel/importer/wizard/ExcelImportProcessor.java b/excel-importer/src/com/axonivy/util/excel/importer/wizard/ExcelImportProcessor.java index aa989b3..7ff1b33 100644 --- a/excel-importer/src/com/axonivy/util/excel/importer/wizard/ExcelImportProcessor.java +++ b/excel-importer/src/com/axonivy/util/excel/importer/wizard/ExcelImportProcessor.java @@ -3,6 +3,7 @@ import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; +import java.sql.SQLException; import java.util.List; import org.apache.commons.lang3.StringUtils; @@ -36,6 +37,7 @@ import ch.ivyteam.ivy.project.IIvyProject; import ch.ivyteam.ivy.project.IIvyProjectManager; import ch.ivyteam.ivy.scripting.dataclass.IDataClassManager; +import ch.ivyteam.ivy.scripting.dataclass.IEntityClass; import ch.ivyteam.ivy.scripting.dataclass.IProjectDataClassManager; import ch.ivyteam.ivy.search.restricted.ProjectRelationSearchScope; import ch.ivyteam.util.io.resource.FileResource; @@ -111,14 +113,14 @@ private void importExcel(IProjectDataClassManager manager, FileResource excel, I monitor.setTaskName("Created EntityClass "+entityName); IProcessModelVersion pmv = manager.getProcessModelVersion(); - var persist = pmv.getAdapter(IPersistenceContext.class); - var ivyEntities = persist.get(selectedPersistence); - EntityDataLoader loader = new EntityDataLoader(ivyEntities); - var entityType = loader.createTable(newEntity); - loader.load(sheet, newEntity); - List loaded = ivyEntities.findAll(entityType); - System.out.println("inserted entities "+loaded.size()); - monitor.setTaskName("Loaded Excel rows into Database "+loaded.size()); + int loaded = 0; + try { + var entries = importData(sheet, newEntity, pmv); + loaded = entries.size(); + } catch (Exception ex) { + status = EclipseUtil.createErrorStatus("Loading of Excel data failed", ex); + } + monitor.setTaskName("Loaded Excel rows into Database "+loaded); new DialogCreator().createDialog(newEntity, selectedPersistence); @@ -129,6 +131,16 @@ private void importExcel(IProjectDataClassManager manager, FileResource excel, I ); } + private List importData(Sheet sheet, IEntityClass newEntity, IProcessModelVersion pmv) throws SQLException { + var persist = pmv.getAdapter(IPersistenceContext.class); + var ivyEntities = persist.get(selectedPersistence); + EntityDataLoader loader = new EntityDataLoader(ivyEntities); + var entityType = loader.createTable(newEntity); + loader.load(sheet, newEntity); + List loaded = ivyEntities.findAll(entityType); + return loaded; + } + String getSelectedSourceProjectName() { if (selectedSourceProject == null) { return StringUtils.EMPTY;