diff --git a/excel-importer-product/README.md b/excel-importer-product/README.md index abafdc5..52c649e 100644 --- a/excel-importer-product/README.md +++ b/excel-importer-product/README.md @@ -21,5 +21,7 @@ Imports Excel sheets and transforms it into a full featured web application. In the project, where the Excel data should be managed: 1. Create a persistence unit under `/config/persistence.xml` -2. Add the property, to allow schema changes `hibernate.hbm2ddl.auto=update` +2. Add the properties + 1. to allow schema changes `hibernate.hbm2ddl.auto=create` + 2. to use classic sequence `hibernate.id.new_generator_mappings=false` 3. Set the Data source to a valid database. If there is none, set it up under `/config/databases.yaml` \ No newline at end of file diff --git a/excel-importer-test/config/persistence.xml b/excel-importer-test/config/persistence.xml index 6cfea78..21eb522 100644 --- a/excel-importer-test/config/persistence.xml +++ b/excel-importer-test/config/persistence.xml @@ -5,6 +5,7 @@ + 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 fe04c4b..11764fd 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 @@ -58,18 +58,18 @@ void loadArznei(@TempDir Path dir) throws IOException, CoreException { TstRes.loadTo(path, "ArzneimittelLight.xlsx"); Workbook wb = ExcelLoader.load(path); - Sheet customerSheet = wb.getSheetAt(0); + Sheet medSheet = wb.getSheetAt(0); - IEntityClass customer = reader.toEntity(customerSheet, "meds"); + IEntityClass meds = reader.toEntity(medSheet, "meds"); try { - customer.save(new NullProgressMonitor()); - Class entity = loader.createTable(customer); + meds.save(new NullProgressMonitor()); + Class entity = loader.createTable(meds); assertThat(unit.findAll(entity)).isEmpty(); - loader.load(customerSheet, customer); + loader.load(medSheet, meds); List records = unit.findAll(entity); assertThat(records).hasSizeGreaterThanOrEqualTo(2); } finally { - customer.getResource().delete(true, new NullProgressMonitor()); + meds.getResource().delete(true, new NullProgressMonitor()); } } 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 2680cd9..931b049 100644 --- a/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java +++ b/excel-importer/src/com/axonivy/util/excel/importer/EntityDataLoader.java @@ -19,10 +19,12 @@ import ch.ivyteam.ivy.project.IIvyProjectManager; import ch.ivyteam.ivy.scripting.dataclass.IEntityClass; import ch.ivyteam.ivy.scripting.dataclass.IEntityClassField; +import ch.ivyteam.log.Logger; @SuppressWarnings("restriction") public class EntityDataLoader { + private static final Logger LOGGER = Logger.getLogger(EntityDataLoader.class); private final IIvyEntityManager manager; public EntityDataLoader(IIvyEntityManager manager) { @@ -43,10 +45,14 @@ public void load(Sheet sheet, IEntityClass entity) { try { rows.forEachRemaining(row -> { Query insert = em.createNativeQuery(query); - insert.setParameter("id", rCount.getAndIncrement()); + rCount.incrementAndGet(); insertCallValuesAsParameter(fields, row, insert); - var inserted = insert.executeUpdate(); - System.out.println("updateded "+inserted+" records"); + try { + var inserted = insert.executeUpdate(); + System.out.println("updateded "+inserted+" records"); + } catch (Exception ex) { + LOGGER.error("Failed to insert "+insert); + } }); } finally { System.out.println("inserted " + rCount + " rows"); @@ -56,21 +62,27 @@ public void load(Sheet sheet, IEntityClass entity) { } private void insertCallValuesAsParameter(List fields, Row row, Query insert) { - int c = -1; + int c = 0; for(var field : fields) { - c++; if (field.getName().equals("id")) { continue; } + String name = field.getName(); Cell cell = row.getCell(c); - insert.setParameter(field.getName(), getValue(cell)); + Object value = getValue(cell); + insert.setParameter(name, value); + c++; } } private String buildInsertQuery(IEntityClass entity, List fields) { - String colNames = fields.stream().map(IEntityClassField::getName).collect(Collectors.joining(",")); + String colNames = fields.stream().map(IEntityClassField::getName) + .filter(fld -> !fld.equals("id")) + .collect(Collectors.joining(",")); var query = new StringBuilder("INSERT INTO "+entity.getSimpleName()+" ("+colNames+")\nVALUES ("); - var params = fields.stream().map(IEntityClassField::getName).map(f -> ":"+f+"").collect(Collectors.joining(", ")); + var params = fields.stream().map(IEntityClassField::getName) + .filter(fld -> !fld.equals("id")) + .map(f -> ":"+f+"").collect(Collectors.joining(", ")); query.append(params); query.append(")"); return query.toString();