Skip to content

Commit

Permalink
Merge pull request #7 from axonivy-market/arznei
Browse files Browse the repository at this point in the history
Arznei
  • Loading branch information
ivy-rew authored Oct 24, 2023
2 parents 7fc5c8a + dd3a192 commit cf2fdee
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -12,6 +13,7 @@
import com.axonivy.util.excel.importer.EntityClassReader;

import ch.ivyteam.ivy.environment.IvyTest;
import ch.ivyteam.ivy.scripting.dataclass.IEntityClassField;

@IvyTest
public class TestEntityClassCreator {
Expand All @@ -26,6 +28,23 @@ void readToEntity(@TempDir Path dir) throws IOException {
var entity = reader.getEntity(path);
assertThat(entity).isNotNull();
}

@Test
void readGermanized(@TempDir Path dir) throws Exception {
Path path = dir.resolve("Arzneimittel.xlsx");
TstRes.loadTo(path, "ArzneimittelLight.xlsx");

var entity = reader.getEntity(path);
assertThat(entity).isNotNull();
List<String> fields = entity.getFields().stream().map(IEntityClassField::getName).toList();
for(String field : fields) {
assertThat(field)
.as("no whitespaces")
.doesNotContain(" ")
.doesNotContain("(")
.doesNotContain("ä");
}
}

@BeforeEach
void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ void loadDataToEntity(@TempDir Path dir) throws IOException, CoreException {
customer.getResource().delete(true, new NullProgressMonitor());
}
}

@Test
void loadArznei(@TempDir Path dir) throws IOException, CoreException {
Path path = dir.resolve("meds.xlsx");
TstRes.loadTo(path, "ArzneimittelLight.xlsx");

Workbook wb = ExcelLoader.load(path);
Sheet customerSheet = wb.getSheetAt(0);

IEntityClass customer = reader.toEntity(customerSheet, "meds");
try {
customer.save(new NullProgressMonitor());
Class<?> entity = loader.createTable(customer);
assertThat(unit.findAll(entity)).isEmpty();
loader.load(customerSheet, customer);
List<?> records = unit.findAll(entity);
assertThat(records).hasSizeGreaterThanOrEqualTo(2);
} finally {
customer.getResource().delete(true, new NullProgressMonitor());
}
}

@BeforeEach
void setup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ private void withIdField(IEntityClass entity) {
}

private String fieldName(String colName) {
colName = colName.replaceAll(" ", "");
if (StringUtils.isAllUpperCase(colName)) {
return colName.toLowerCase();
}
colName = colName.replaceAll("\\W", "");
colName = colName.replaceAll("[^\\p{ASCII}]", "");
return StringUtils.uncapitalize(colName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@ public void load(Sheet sheet, IEntityClass entity) {
}

private void insertCallValuesAsParameter(List<? extends IEntityClassField> fields, Row row, Query insert) {
Iterator<Cell> cells = row.cellIterator();
int c = 0;
c++; // consumed by 'id'
while(cells.hasNext()) {
Cell cell = cells.next();
IEntityClassField field = fields.get(c);
String column = field.getName();
insert.setParameter(column, getValue(cell));
int c = -1;
for(var field : fields) {
c++;
if (field.getName().equals("id")) {
continue;
}
Cell cell = row.getCell(c);
insert.setParameter(field.getName(), getValue(cell));
}
}

Expand Down Expand Up @@ -93,6 +92,9 @@ public Class<?> createTable(IEntityClass entity) {
}

private Object getValue(Cell cell) {
if (cell == null) {
return null;
}
if (cell.getCellType() == CellType.NUMERIC) {
return cell.getNumericCellValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<p:messages />
<p:panelGrid columns="1" layout="grid" styleClass="ui-panelgrid-blank ui-fluid">

<p:dataTable id="entities" var="entity" value="#{data.entries}">
<p:dataTable id="entities" var="entity" value="#{data.entries}" paginator="true" rows="100">
<p:column headerText="id">
<h:outputText value="#{entity.id}"/>
</p:column>
Expand Down

0 comments on commit cf2fdee

Please sign in to comment.