Skip to content

Commit

Permalink
feat: select multiple feature types for exporting to Excel
Browse files Browse the repository at this point in the history
HALE studio supports selecting multiple feature types for export to Excel.
Each selected feature type is added as a separate sheet in a single Excel workbook.

ING-3941
  • Loading branch information
emanuelaepure10 committed Jul 17, 2023
1 parent 9336139 commit 9d77ac2
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2014 Data Harmonisation Panel
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Data Harmonisation Panel <http://www.dhpanel.eu>
*/

package eu.esdihumboldt.hale.io.csv.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import eu.esdihumboldt.hale.common.core.io.Value;
import eu.esdihumboldt.hale.common.instance.io.InstanceWriter;
import eu.esdihumboldt.hale.io.csv.InstanceTableIOConstants;
import eu.esdihumboldt.hale.ui.io.IOWizard;
import eu.esdihumboldt.hale.ui.io.config.AbstractConfigurationPage;

/**
* Configuration page for exporting of instances
*
* @author Emanuela Epure
*/
public class CommonInstanceExportConfigurationPage
extends AbstractConfigurationPage<InstanceWriter, IOWizard<InstanceWriter>> {

protected Button solveNestedProperties;
protected Button useSchema;
protected Composite page;

/**
* @param title
*
*/
public CommonInstanceExportConfigurationPage(String title) {
super(title);
setTitle("Additonal Export Options");
setDescription("Select if nested properties should be solved and a type");
}

/**
* @see eu.esdihumboldt.hale.ui.io.config.AbstractConfigurationPage#enable()
*/
@Override
public void enable() {
// not required
}

/**
* @see eu.esdihumboldt.hale.ui.io.config.AbstractConfigurationPage#disable()
*/
@Override
public void disable() {
// not required
}

/**
* @see eu.esdihumboldt.hale.ui.io.IOWizardPage#updateConfiguration(eu.esdihumboldt.hale.common.core.io.IOProvider)
*/
@Override
public boolean updateConfiguration(InstanceWriter provider) {
provider.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES,
Value.of(solveNestedProperties.getSelection()));
provider.setParameter(InstanceTableIOConstants.USE_SCHEMA,
Value.of(useSchema.getSelection()));
return true;
}

/**
* @see eu.esdihumboldt.hale.ui.HaleWizardPage#createContent(org.eclipse.swt.widgets.Composite)
*/
@Override
protected void createContent(Composite page) {
this.page = page;

page.setLayout(new GridLayout(1, false));

solveNestedProperties = new Button(page, SWT.CHECK);
solveNestedProperties.setText("Solve nested properties");
solveNestedProperties.setSelection(true);

useSchema = new Button(page, SWT.CHECK);
useSchema.setText("Use the source schema for the order of the exported columns");
useSchema.setSelection(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
Expand All @@ -34,22 +32,16 @@
import eu.esdihumboldt.hale.common.schema.model.TypeDefinition;
import eu.esdihumboldt.hale.io.csv.InstanceTableIOConstants;
import eu.esdihumboldt.hale.ui.common.definition.selector.TypeDefinitionSelector;
import eu.esdihumboldt.hale.ui.io.IOWizard;
import eu.esdihumboldt.hale.ui.io.config.AbstractConfigurationPage;
import eu.esdihumboldt.hale.ui.service.instance.InstanceService;

/**
* Configuration page for exporting of instances
*
* @author Patrick Lieb
*/
public class InstanceExportConfigurationPage
extends AbstractConfigurationPage<InstanceWriter, IOWizard<InstanceWriter>> {
public class InstanceExportConfigurationPage extends CommonInstanceExportConfigurationPage {

protected Button solveNestedProperties;
protected Button useSchema;
private TypeDefinitionSelector typeSelector;
protected Composite page;

private final ViewerFilter validTypesToSelect = new ViewerFilter() {

Expand Down Expand Up @@ -102,10 +94,7 @@ public void disable() {
*/
@Override
public boolean updateConfiguration(InstanceWriter provider) {
provider.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES,
Value.of(solveNestedProperties.getSelection()));
provider.setParameter(InstanceTableIOConstants.USE_SCHEMA,
Value.of(useSchema.getSelection()));
super.updateConfiguration(provider);
provider.setParameter(InstanceTableIOConstants.EXPORT_TYPE,
Value.of(typeSelector.getSelectedObject().getName().toString()));
return true;
Expand All @@ -116,17 +105,7 @@ public boolean updateConfiguration(InstanceWriter provider) {
*/
@Override
protected void createContent(Composite page) {
this.page = page;

page.setLayout(new GridLayout(1, false));

solveNestedProperties = new Button(page, SWT.CHECK);
solveNestedProperties.setText("Solve nested properties");
solveNestedProperties.setSelection(true);

useSchema = new Button(page, SWT.CHECK);
useSchema.setText("Use the source schema for the order of the exported columns");
useSchema.setSelection(true);
super.createContent(page);

final Label label = new Label(page, SWT.NONE);
label.setText("Choose your Type you want to export:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public class InstanceTableIOConstants {
/**
* Parameter for exporting empty feature types to XLS Export
*/
public static final String EXPORT_EMPTY_FEATURETYPES = "emptyFeatureTypes";
public static final String EXPORT_IGNORE_EMPTY_FEATURETYPES = "ignoreEmptyFeaturetypes";

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.InstanceUtil;
import eu.esdihumboldt.hale.common.schema.model.Schema;
import eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace;
import eu.esdihumboldt.hale.common.test.TestUtil;
import eu.esdihumboldt.hale.io.csv.InstanceTableIOConstants;
import eu.esdihumboldt.hale.io.csv.reader.CommonSchemaConstants;
import eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader;
import eu.esdihumboldt.hale.io.xls.reader.XLSSchemaReader;
import eu.esdihumboldt.hale.io.xls.test.writer.XLSInstanceWriterTestUtil;
import eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter;
import junit.framework.TestCase;

Expand Down Expand Up @@ -57,10 +59,20 @@ public void test() {
IContentType contentType = HalePlatform.getContentTypeManager()
.getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
writer.setParameter(InstanceTableIOConstants.USE_SCHEMA, Value.of(true));
writer.setParameter(InstanceTableIOConstants.EXPORT_IGNORE_EMPTY_FEATURETYPES,
Value.of(false));
writer.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of("ItemType"));

File tempDir = Files.createTempDir();
File tempFile = new File(tempDir, "data.xls");
writer.setInstances(instances);
try {
Schema schema = XLSInstanceWriterTestUtil.createExampleSchema();
DefaultSchemaSpace ss = new DefaultSchemaSpace();
ss.addSchema(schema);
writer.setTargetSchema(ss);

// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tempFile));
writer.setContentType(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import eu.esdihumboldt.hale.common.core.io.report.IOReport;
import eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier;
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection;
import eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection;
import eu.esdihumboldt.hale.common.schema.model.Schema;
import eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace;
import eu.esdihumboldt.hale.common.test.TestUtil;
import eu.esdihumboldt.hale.io.csv.InstanceTableIOConstants;
import eu.esdihumboldt.hale.io.xls.writer.XLSInstanceWriter;
Expand Down Expand Up @@ -94,10 +96,16 @@ public void testWriteSimpleSchemaColOrder() throws Exception {
.getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(true));
writer.setParameter(InstanceTableIOConstants.USE_SCHEMA, Value.of(true));
writer.setParameter(InstanceTableIOConstants.EXPORT_IGNORE_EMPTY_FEATURETYPES,
Value.of(false));
writer.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of("city"));

File tmpFile = tmpFolder.newFile("excelTestWriteSimpleSchema.xls");

writer.setInstances(instance);
DefaultSchemaSpace ss = new DefaultSchemaSpace();
ss.addSchema(schema);
writer.setTargetSchema(ss);
// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tmpFile));
writer.setContentType(contentType);
Expand All @@ -115,6 +123,7 @@ public void testWriteSimpleSchemaColOrder() throws Exception {
checkSheetName(sheet, "city");
checkFirstDataRow(sheet, firstDataRow);
checkHeaderOrder(sheet, header);
tmpFolder.delete();
}

/**
Expand All @@ -124,7 +133,6 @@ public void testWriteSimpleSchemaColOrder() throws Exception {
*/
@Test
public void testWriteComplexSchema() throws Exception {

TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_COMPLEX);
// alternative the data could be generated by iterating through the
Expand All @@ -140,10 +148,17 @@ public void testWriteComplexSchema() throws Exception {
.getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(true));
writer.setParameter(InstanceTableIOConstants.USE_SCHEMA, Value.of(false));
writer.setParameter(InstanceTableIOConstants.EXPORT_IGNORE_EMPTY_FEATURETYPES,
Value.of(true));
writer.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of("person"));

File tmpFile = tmpFolder.newFile("excelTestWriteComplexSchema.xls");

writer.setInstances(example.getSourceInstances());
Schema schema = XLSInstanceWriterTestUtil.createExampleSchema();
DefaultSchemaSpace ss = new DefaultSchemaSpace();
ss.addSchema(schema);
writer.setTargetSchema(ss);
// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tmpFile));
writer.setContentType(contentType);
Expand All @@ -160,6 +175,7 @@ public void testWriteComplexSchema() throws Exception {
checkHeader(sheet, header);
checkSheetName(sheet, "person");
checkFirstDataRow(sheet, firstDataRow);
tmpFolder.delete();
}

/**
Expand All @@ -171,7 +187,6 @@ public void testWriteComplexSchema() throws Exception {
*/
@Test
public void testWriteNotNestedProperties() throws Exception {

TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_COMPLEX);
// alternative the data could be generated by iterating through the
Expand All @@ -185,10 +200,17 @@ public void testWriteNotNestedProperties() throws Exception {
.getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
writer.setParameter(InstanceTableIOConstants.USE_SCHEMA, Value.of(false));
writer.setParameter(InstanceTableIOConstants.EXPORT_IGNORE_EMPTY_FEATURETYPES,
Value.of(true));
writer.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of("person"));

File tmpFile = tmpFolder.newFile("excelNotNestedProperties.xls");

writer.setInstances(example.getSourceInstances());
Schema schema = XLSInstanceWriterTestUtil.createExampleSchema();
DefaultSchemaSpace ss = new DefaultSchemaSpace();
ss.addSchema(schema);
writer.setTargetSchema(ss);
// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tmpFile));
writer.setContentType(contentType);
Expand All @@ -204,12 +226,13 @@ public void testWriteNotNestedProperties() throws Exception {

checkHeader(sheet, header);
checkSheetName(sheet, "person");

checkFirstDataRow(sheet, firstDataRow);
tmpFolder.delete();
}

/**
* Test - write data of complex schema and analyze result
* Test - write data for 2 example and create a common excel with multiple
* sheets
*
* @throws Exception , if an error occurs
*/
Expand All @@ -219,7 +242,7 @@ public void testExportMultiFeatureToExcel() throws Exception {
TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_COMPLEX);
TransformationExample example2 = TransformationExamples
.getExample(TransformationExamples.CARDINALITY_MOVE);
.getExample(TransformationExamples.CARDINALITY_MERGE_1);
examples.add(example.getTargetInstances());
examples.add(example2.getTargetInstances());

Expand All @@ -229,26 +252,35 @@ public void testExportMultiFeatureToExcel() throws Exception {
.getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
writer.setParameter(InstanceTableIOConstants.USE_SCHEMA, Value.of(false));
writer.setParameter(InstanceTableIOConstants.EXPORT_EMPTY_FEATURETYPES, Value.of(true));
writer.setParameter(InstanceTableIOConstants.EXPORT_IGNORE_EMPTY_FEATURETYPES,
Value.of(false));
writer.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of("person" + "," + "t1"));

File tmpFile = tmpFolder.newFile("excelWith2Sheets.xls");
// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tmpFile));
writer.setContentType(contentType);

for (InstanceCollection instanceCollection : examples) {
writer.setInstances(instanceCollection);
}
System.out.println("XLSInstanceWriterTest.testExportMultiFeatureToExcel()");
Schema schema = XLSInstanceWriterTestUtil.createExampleSchema();
DefaultSchemaSpace ss = new DefaultSchemaSpace();
ss.addSchema(schema);
writer.setTargetSchema(ss);

InstanceCollection multiInstanceCollection = new MultiInstanceCollection(examples);
writer.setInstances(multiInstanceCollection);

IOReport report = writer.execute(null);
assertTrue(report.isSuccess());

Workbook wb;
// https: // poi.apache.org/components/spreadsheet/quick-guide.html#FileInputStream
try (POIFSFileSystem fs = new POIFSFileSystem(tmpFile)) {
wb = new HSSFWorkbook(fs.getRoot(), true);
}
int sheetNumber = wb.getNumberOfSheets();
assertEquals(examples.size(), sheetNumber);
Sheet sheet = wb.getSheetAt(0);
assertEquals(1, sheetNumber);
tmpFolder.delete();
}

/**
Expand Down
Loading

0 comments on commit 9d77ac2

Please sign in to comment.