From f8b037df567a87d103062059aedc7b100ae790b4 Mon Sep 17 00:00:00 2001 From: Reguel Wermelinger Date: Fri, 10 Nov 2023 09:04:42 +0100 Subject: [PATCH] trying to stabilize build by eager consumption of entity-manager - fixed method order ... as it runs all fine locally :) --- excel-importer-test/META-INF/MANIFEST.MF | 4 +- excel-importer-test/pom.xml | 15 ++++++- .../util/excel/test/TestEntityDataLoader.java | 1 + .../util/excel/importer/EntityDataLoader.java | 42 ++++++++++--------- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/excel-importer-test/META-INF/MANIFEST.MF b/excel-importer-test/META-INF/MANIFEST.MF index fe70758..151836b 100644 --- a/excel-importer-test/META-INF/MANIFEST.MF +++ b/excel-importer-test/META-INF/MANIFEST.MF @@ -6,5 +6,5 @@ Bundle-Version: 10.0.2.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-17 Automatic-Module-Name: excel.importer.test Export-Package: com.axonivy.util.excel.test -Require-Bundle: ch.ivyteam.ivy.scripting.dataclass, - ch.ivyteam.ivy.dialog.config +Require-Bundle: ch.ivyteam.ivy.scripting.dataclass;resolution:=optional, + ch.ivyteam.ivy.dialog.config;resolution:=optional diff --git a/excel-importer-test/pom.xml b/excel-importer-test/pom.xml index 7fac2e1..dde3008 100644 --- a/excel-importer-test/pom.xml +++ b/excel-importer-test/pom.xml @@ -21,7 +21,7 @@ com.axonivy.ivy.test unit-tester - 10.0.0 + 10.0.14 test @@ -31,9 +31,20 @@ com.axonivy.ivy.ci project-build-plugin - 10.0.6 + 10.0.14 true + + + + org.apache.maven.plugins + maven-surefire-plugin + + alphabetical + + + + diff --git a/excel-importer-test/src_test/com/axonivy/util/excel/test/TestEntityDataLoader.java b/excel-importer-test/src_test/com/axonivy/util/excel/test/TestEntityDataLoader.java index 8d68a77..79ea79f 100644 --- a/excel-importer-test/src_test/com/axonivy/util/excel/test/TestEntityDataLoader.java +++ b/excel-importer-test/src_test/com/axonivy/util/excel/test/TestEntityDataLoader.java @@ -74,6 +74,7 @@ void loadArznei(@TempDir Path dir) throws Exception { @BeforeEach void setup() { this.unit = Ivy.persistence().get("testing"); + unit.createEntityManager().clear(); // eager access this.loader = new EntityDataLoader(unit); this.reader = new EntityClassReader(); } 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 a723054..89c49a6 100644 --- a/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java +++ b/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java @@ -46,36 +46,40 @@ public void load(Sheet sheet, IEntityClass entity, IProgressMonitor monitor) thr rows.next(); // skip header monitor.beginTask("Importing Excel data rows", sheet.getLastRowNum()); - List fields = entity.getFields(); - var query = buildInsertQuery(entity, fields); - EntityManager em = manager.createEntityManager(); JdbcConnectionAccess access = em.unwrap(SessionImpl.class).getJdbcConnectionAccess(); Connection con = access.obtainConnection(); - AtomicInteger rCount = new AtomicInteger(); try { - var stmt = con.prepareStatement(query, Statement.NO_GENERATED_KEYS); - rows.forEachRemaining(row -> { - try { - rCount.incrementAndGet(); - insertCallValuesAsParameter(fields, row, stmt); - stmt.addBatch(); - } catch (SQLException ex) { - throw new RuntimeException(ex); - } - }); + var stmt = loadRows(entity, rows, con); stmt.executeBatch(); con.commit(); } catch (Exception ex) { - LOGGER.error("warn "+ex); + LOGGER.error("failed to load rows "+ex); } finally { - System.out.println("inserted " + rCount + " rows"); access.releaseConnection(con); em.close(); } } - private void insertCallValuesAsParameter(List fields, Row row, PreparedStatement stmt) throws SQLException { + private PreparedStatement loadRows(IEntityClass entity, Iterator rows, Connection con) throws SQLException { + AtomicInteger rCount = new AtomicInteger(); + List fields = entity.getFields(); + var query = buildInsertQuery(entity, fields); + var stmt = con.prepareStatement(query, Statement.NO_GENERATED_KEYS); + rows.forEachRemaining(row -> { + try { + rCount.incrementAndGet(); + insertCallValuesAsParameter(fields, row, stmt); + stmt.addBatch(); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } + }); + System.out.println("Generated "+rCount+" inserts"); + return stmt; + } + + private static void insertCallValuesAsParameter(List fields, Row row, PreparedStatement stmt) throws SQLException { int c = 0; for(var field : fields) { if (field.getName().equals("id")) { @@ -88,7 +92,7 @@ private void insertCallValuesAsParameter(List field } } - private String buildInsertQuery(IEntityClass entity, List fields) { + private static String buildInsertQuery(IEntityClass entity, List fields) { String colNames = fields.stream().map(IEntityClassField::getName) .filter(fld -> !fld.equals("id")) .collect(Collectors.joining(",")); @@ -116,7 +120,7 @@ public Class createTable(IEntityClass entity) { return entityClass; } - private Object getValue(Cell cell) { + private static Object getValue(Cell cell) { if (cell == null) { return null; }