Skip to content

Commit

Permalink
stabilize; continue master/detail creation even if import of data failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-rew committed Nov 10, 2023
1 parent f8b037d commit 13fb9a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +66,7 @@ private PreparedStatement loadRows(IEntityClass entity, Iterator<Row> rows, Conn
AtomicInteger rCount = new AtomicInteger();
List<? extends IEntityClassField> 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 {
Expand All @@ -75,7 +77,7 @@ private PreparedStatement loadRows(IEntityClass entity, Iterator<Row> rows, Conn
throw new RuntimeException(ex);
}
});
System.out.println("Generated "+rCount+" inserts");
LOGGER.info("Generated "+rCount+" inserts");
return stmt;
}

Expand All @@ -96,7 +98,11 @@ private static String buildInsertQuery(IEntityClass entity, List<? extends IEnti
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 (");
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(", "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit 13fb9a8

Please sign in to comment.