Skip to content

Commit

Permalink
fix: use auto incremented id; avoid self mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ivy-rew committed Oct 25, 2023
1 parent 22b2d67 commit eae14a9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
4 changes: 3 additions & 1 deletion excel-importer-product/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
1 change: 1 addition & 0 deletions excel-importer-test/config/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show-sql" value="true"/>
<property name="hibernate.id.new_generator_mappings" value="false"/>
</properties>
</persistence-unit>
</persistence>
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand All @@ -56,21 +62,27 @@ public void load(Sheet sheet, IEntityClass entity) {
}

private void insertCallValuesAsParameter(List<? extends IEntityClassField> 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<? extends IEntityClassField> 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();
Expand Down

0 comments on commit eae14a9

Please sign in to comment.