Skip to content

Commit

Permalink
trying to stabilize build by eager consumption of entity-manager
Browse files Browse the repository at this point in the history
- fixed method order ... as it runs all fine locally :)
  • Loading branch information
ivy-rew committed Nov 10, 2023
1 parent 571e1ea commit f8b037d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions excel-importer-test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 13 additions & 2 deletions excel-importer-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>com.axonivy.ivy.test</groupId>
<artifactId>unit-tester</artifactId>
<version>10.0.0</version>
<version>10.0.14</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -31,9 +31,20 @@
<plugin>
<groupId>com.axonivy.ivy.ci</groupId>
<artifactId>project-build-plugin</artifactId>
<version>10.0.6</version>
<version>10.0.14</version>
<extensions>true</extensions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends IEntityClassField> 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<? extends IEntityClassField> fields, Row row, PreparedStatement stmt) throws SQLException {
private PreparedStatement loadRows(IEntityClass entity, Iterator<Row> rows, Connection con) throws SQLException {
AtomicInteger rCount = new AtomicInteger();
List<? extends IEntityClassField> 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<? extends IEntityClassField> fields, Row row, PreparedStatement stmt) throws SQLException {
int c = 0;
for(var field : fields) {
if (field.getName().equals("id")) {
Expand All @@ -88,7 +92,7 @@ private void insertCallValuesAsParameter(List<? extends IEntityClassField> field
}
}

private String buildInsertQuery(IEntityClass entity, List<? extends IEntityClassField> fields) {
private static String buildInsertQuery(IEntityClass entity, List<? extends IEntityClassField> fields) {
String colNames = fields.stream().map(IEntityClassField::getName)
.filter(fld -> !fld.equals("id"))
.collect(Collectors.joining(","));
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit f8b037d

Please sign in to comment.