Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prototype: create an entity class out of an excel sheet #40

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions excel-connector-test/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions excel-connector-test/.project
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>ch.ivyteam.ivy.project.IvyProjectNature</nature>
Expand All @@ -45,5 +55,6 @@
<nature>org.eclipse.jem.beaninfo.BeanInfoNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
</natures>
</projectDescription>
9 changes: 9 additions & 0 deletions excel-connector-test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: excel-connector-test
Bundle-SymbolicName: excel-connector-test
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: excel.connector.test
Export-Package: com.axonivy.connector.excel.test
Require-Bundle: ch.ivyteam.ivy.scripting.dataclass
7 changes: 7 additions & 0 deletions excel-connector-test/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source.. = src/,\
src_hd/,\
src_wsproc/,\
src_dataClasses/,\
src_test/
bin.includes = META-INF/,\
.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.axonivy.connector.excel.test;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.axonivy.connector.excel.EntityClassReader;

import ch.ivyteam.ivy.environment.IvyTest;

@IvyTest
public class TestEntityClassCreator {

@Test
void readToEntity(@TempDir Path dir) throws IOException {
Path path = dir.resolve("customers.xlsx");
try(InputStream is = TestEntityClassCreator.class.getResourceAsStream("sample.xlsx")) {
Files.copy(is, path, StandardCopyOption.REPLACE_EXISTING);
}

var reader = new EntityClassReader();
var entity = reader.getEntity(path.toFile().toString());
assertThat(entity).isNotNull();
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run me to create the 'customers' entity out of the given static sample.xlsx
entityClassCreator


}
Binary file not shown.
67 changes: 34 additions & 33 deletions excel-connector/.classpath
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**/*.ivyClass|**/*.mod|**/*.rddescriptor|**/*.xhtml" kind="src" path="src_hd">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src_wsproc">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src_dataClasses">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="con" path="IVY_CONTAINER"/>
<classpathentry kind="con" path="WEBAPP_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**/*.ivyClass|**/*.mod|**/*.rddescriptor|**/*.xhtml" kind="src" path="src_hd">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src_wsproc">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src_dataClasses">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="con" path="IVY_CONTAINER"/>
<classpathentry kind="con" path="WEBAPP_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions excel-connector/.project
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>ch.ivyteam.ivy.project.IvyProjectNature</nature>
Expand All @@ -45,5 +55,6 @@
<nature>org.eclipse.jem.beaninfo.BeanInfoNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
</natures>
</projectDescription>
9 changes: 9 additions & 0 deletions excel-connector/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: excel-connector
Bundle-SymbolicName: excel-connector
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Automatic-Module-Name: excel.connector
Export-Package: com.axonivy.connector.excel
Require-Bundle: ch.ivyteam.ivy.scripting.dataclass
6 changes: 6 additions & 0 deletions excel-connector/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source.. = src/,\
src_hd/,\
src_wsproc/,\
src_dataClasses/
bin.includes = META-INF/,\
.
123 changes: 123 additions & 0 deletions excel-connector/src/com/axonivy/connector/excel/EntityClassReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.axonivy.connector.excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.scripting.dataclass.IDataClassManager;
import ch.ivyteam.ivy.scripting.dataclass.IEntityClass;
import ch.ivyteam.ivy.scripting.dataclass.IProjectDataClassManager;

public class EntityClassReader {

private final IProjectDataClassManager manager;

@SuppressWarnings("restriction")
public EntityClassReader() {
this(IDataClassManager.instance().getProjectDataModelFor(Ivy.wfCase().getProcessModelVersion()));
}

public EntityClassReader(IProjectDataClassManager manager) {
this.manager = manager;
}

@SuppressWarnings("restriction")
public IEntityClass getEntity(String filePath) {
Iterator<Row> rowIterator = loadRowIterator(filePath);
List<String> headerCells = getHeaderCells(rowIterator);
String dataName = StringUtils.substringBeforeLast(new File(filePath).getName(), ".");
String fqName = manager.getDefaultNamespace()+"."+dataName;
if (manager.findDataClass(fqName) != null) {
throw new RuntimeException("entity "+fqName+" already exists");
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you may choose more appropriately how to treat existing classes ... overwrite? delete and forget? ask the user?

var entity = manager.createEntityClass(fqName, null);
addContentCellsToRecordset(entity, headerCells, rowIterator);
entity.save();
return entity;
}

private static Iterator<Row> loadRowIterator(String filePath) {
if (filePath.endsWith(".xls")) {
HSSFWorkbook workbook = openXls(filePath);
HSSFSheet sheet = workbook.getSheetAt(0);
return sheet.rowIterator();
} else {
XSSFWorkbook workbook = openXlsx(filePath);
XSSFSheet sheet = workbook.getSheetAt(0);
return sheet.rowIterator();
}
}

private static HSSFWorkbook openXls(String filePath) {
try (InputStream input = new FileInputStream(filePath);
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook workbook = new HSSFWorkbook(fs);) {
return workbook;
} catch (Exception ex) {
throw new RuntimeException("Could not read excel file from " + filePath, ex);
}
}

private static XSSFWorkbook openXlsx(String filePath) {
try (FileInputStream input = new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(input);) {
return workbook;
} catch (Exception ex) {
throw new RuntimeException("Could not read excel file from " + filePath, ex);
}
}

private static List<String> getHeaderCells(Iterator<Row> rowIterator) {
List<String> headerCells = new ArrayList<String>();
if (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
headerCells.add(cell.getStringCellValue());
}
}
return headerCells;
}

private static void addContentCellsToRecordset(IEntityClass entity, List<String> names, Iterator<Row> rowIterator) {
if (!rowIterator.hasNext()) {
return;
}
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
int cellNo = 0;
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
var name = StringUtils.uncapitalize(names.get(cellNo));
switch (cell.getCellType()) {
case NUMERIC:
entity.addField(name, Float.class.getName());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe on the created field we should also apply some magic: e.g define the max length of the string field or whatever

break;
case STRING:
entity.addField(name, String.class.getName());
break;
case BOOLEAN:
entity.addField(name, Boolean.class.getName());
break;
default:
entity.addField(name, String.class.getName());
break;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle more types?

cellNo++;
}
}
}