-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
70791c4
fe2cb90
79fd85e
b955536
2822f68
a788821
ffece42
d165bdd
4064637
f4fca7e
50a9263
a3cc878
fcfe5a9
d9937aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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(); | ||
} | ||
|
||
} |
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> |
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 |
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/,\ | ||
. |
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"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle more types? |
||
cellNo++; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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