diff --git a/common/plugins/eu.esdihumboldt.hale.common.core/src/eu/esdihumboldt/hale/common/core/io/supplier/ByteArrayOutputStreamSupplier.java b/common/plugins/eu.esdihumboldt.hale.common.core/src/eu/esdihumboldt/hale/common/core/io/supplier/ByteArrayOutputStreamSupplier.java
new file mode 100644
index 0000000000..26664e20bf
--- /dev/null
+++ b/common/plugins/eu.esdihumboldt.hale.common.core/src/eu/esdihumboldt/hale/common/core/io/supplier/ByteArrayOutputStreamSupplier.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2023 wetransform GmbH
+ *
+ * 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 .
+ *
+ * Contributors:
+ * wetransform GmbH
+ */
+
+package eu.esdihumboldt.hale.common.core.io.supplier;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+
+/**
+ * I/O supplier based on a {@link ByteArrayOutputStream}
+ *
+ * @author EmanuelaEpure
+ */
+public class ByteArrayOutputStreamSupplier
+ implements LocatableInputSupplier, LocatableOutputSupplier {
+
+ private final ByteArrayOutputStream byteArrayOutputStream;
+
+ /**
+ * Create a ByteArrayOutputStream I/O supplier.
+ *
+ * @param byteArrayOutputStream the byteArrayOutputStream
+ *
+ */
+ public ByteArrayOutputStreamSupplier(ByteArrayOutputStream byteArrayOutputStream) {
+ this.byteArrayOutputStream = byteArrayOutputStream;
+ }
+
+ /**
+ * @see eu.esdihumboldt.util.io.InputSupplier#getInput()
+ */
+ @Override
+ public InputStream getInput() throws IOException {
+ return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
+ }
+
+ /**
+ * @see eu.esdihumboldt.hale.common.core.io.supplier.Locatable#getLocation()
+ */
+ @Override
+ public URI getLocation() {
+ return null;
+ }
+
+ /**
+ * @see eu.esdihumboldt.util.io.OutputSupplier#getOutput()
+ */
+ @Override
+ public OutputStream getOutput() throws IOException {
+ return byteArrayOutputStream;
+ }
+
+ /**
+ * @see eu.esdihumboldt.hale.common.core.io.supplier.LocatableInputSupplier#getUsedLocation()
+ */
+ @Override
+ public URI getUsedLocation() {
+ return null;
+ }
+
+}
diff --git a/io/plugins/eu.esdihumboldt.hale.io.csv.test/src/eu/esdihumboldt/hale/io/csv/writer/CSVInstanceWriterTest.java b/io/plugins/eu.esdihumboldt.hale.io.csv.test/src/eu/esdihumboldt/hale/io/csv/writer/CSVInstanceWriterTest.java
index d8a0dbff61..09655d9d51 100644
--- a/io/plugins/eu.esdihumboldt.hale.io.csv.test/src/eu/esdihumboldt/hale/io/csv/writer/CSVInstanceWriterTest.java
+++ b/io/plugins/eu.esdihumboldt.hale.io.csv.test/src/eu/esdihumboldt/hale/io/csv/writer/CSVInstanceWriterTest.java
@@ -18,11 +18,18 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
+import javax.xml.namespace.QName;
+
import org.eclipse.core.runtime.content.IContentType;
import org.junit.BeforeClass;
import org.junit.Rule;
@@ -35,10 +42,12 @@
import eu.esdihumboldt.hale.common.core.HalePlatform;
import eu.esdihumboldt.hale.common.core.io.Value;
import eu.esdihumboldt.hale.common.core.io.report.IOReport;
+import eu.esdihumboldt.hale.common.core.io.supplier.ByteArrayOutputStreamSupplier;
import eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier;
import eu.esdihumboldt.hale.common.instance.io.InstanceWriter;
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection;
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.internal.CSVSchemaReader;
@@ -73,9 +82,58 @@ public static void waitForServices() {
*/
@Test
public void testWriteSimpleSchema() throws Exception {
+ Schema schema = CSVInstanceWriterTestUtil.createExampleSchema();
+ TransformationExample example = TransformationExamples
+ .getExample(TransformationExamples.SIMPLE_ASSIGN);
+ char sep = ',';
+
+ QName qname = new QName("http://www.opengis.net/om/2.0", "OM_ObservationType");
+ File tmpFile = tmpFolder.newFile("csvTestWriteSimpleSchema.csv");
+
+ assertTrue("Csv Export was not successful.", writeCsvToFile(tmpFile, true, false,
+ Value.of(sep), null, null, example.getSourceInstances(), schema, qname, null));
+
+ CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
+ testWriteSimpleSchemaFromReader(reader);
+ }
+ /**
+ * Test - write simple data, without nested properties and useSchema=false
+ *
+ * @throws Exception , if an error occurs
+ */
+ @Test
+ public void testWriteSimpleSchemaWithByteArrayOutputStream() throws Exception {
+ Schema schema = CSVInstanceWriterTestUtil.createExampleSchema();
TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_ASSIGN);
+ char sep = ',';
+
+ QName qname = new QName("http://www.opengis.net/om/2.0", "OM_ObservationType");
+
+ // Create a ByteArrayOutputStream and write data to it
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+
+ assertTrue("Csv Export was not successful.",
+ writeCsvToFile(null, true, false, Value.of(sep), null, null,
+ example.getSourceInstances(), schema, qname, byteArrayOutputStream));
+
+ // Get the byte array from the ByteArrayOutputStream
+ byte[] writtenData = byteArrayOutputStream.toByteArray();
+
+ // Create a ByteArrayInputStream from the written data
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(writtenData);
+
+ // Wrap the ByteArrayInputStream with an InputStreamReader
+ InputStreamReader inputStreamReader = new InputStreamReader(byteArrayInputStream);
+
+ // Wrap the InputStreamReader with a BufferedReader
+ BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
+ CSVReader reader = new CSVReader(bufferedReader, sep);
+ testWriteSimpleSchemaFromReader(reader);
+ }
+
+ private void testWriteSimpleSchemaFromReader(CSVReader reader) throws IOException {
// alternative the data could be generated by iterating through the
// exemple project's source data:
// eu.esdihumboldt.cst.test/src/testdata/simpleassign/instance1.xml
@@ -84,14 +142,7 @@ public void testWriteSimpleSchema() throws Exception {
// header size
int numberOfColumns = 4; // in the example t1.xsd: elements + attribute
int numberOfRows = 3;
- char sep = ',';
-
- File tmpFile = tmpFolder.newFile("csvTestWriteSimpleSchema.csv");
- assertTrue("Csv Export was not successful.", writeCsvToFile(tmpFile, true, false,
- Value.of(sep), null, null, example.getSourceInstances()));
-
- CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
List rows = reader.readAll();
reader.close();
@@ -146,10 +197,11 @@ public void testWriteSimpleSchemaColOrder() throws Exception {
int numberOfRows = 3; // counting also the header
char sep = ',';
+ QName qname = new QName("http://www.opengis.net/om/2.0", "OM_ObservationType");
File tmpFile = tmpFolder.newFile("csvTestWriteSimpleSchema.csv");
- assertTrue("Csv Export was not successful.",
- writeCsvToFile(tmpFile, true, true, Value.of(sep), null, null, instance));
+ assertTrue("Csv Export was not successful.", writeCsvToFile(tmpFile, true, true,
+ Value.of(sep), null, null, instance, schema, qname, null));
CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
List rows = reader.readAll();
@@ -195,6 +247,7 @@ public void testWriteSimpleSchemaColOrder() throws Exception {
@Test
public void testWriteSimpleSchemaDelimiter() throws Exception {
+ Schema schema = CSVInstanceWriterTestUtil.createExampleSchema();
TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_ASSIGN);
@@ -209,10 +262,12 @@ public void testWriteSimpleSchemaDelimiter() throws Exception {
char quo = '\'';
char esc = '"';
+ QName qname = new QName("http://www.opengis.net/om/2.0", "OM_ObservationType");
File tmpFile = tmpFolder.newFile("csvTestWriteSimpleSchemaDelimiter.csv");
- assertTrue("Csv Export was not successful.", writeCsvToFile(tmpFile, true, false,
- Value.of(sep), Value.of(quo), Value.of(esc), example.getSourceInstances()));
+ assertTrue("Csv Export was not successful.",
+ writeCsvToFile(tmpFile, true, false, Value.of(sep), Value.of(quo), Value.of(esc),
+ example.getSourceInstances(), schema, qname, null));
CSVReader reader = new CSVReader(new FileReader(tmpFile), sep, quo, esc);
List rows = reader.readAll();
@@ -248,7 +303,7 @@ public void testWriteSimpleSchemaDelimiter() throws Exception {
*/
@Test
public void testWriteComplexSchema() throws Exception {
-
+ Schema schema = CSVInstanceWriterTestUtil.createExampleSchema();
TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_COMPLEX);
// alternative the data could be generated by iterating through the
@@ -262,9 +317,10 @@ public void testWriteComplexSchema() throws Exception {
char sep = ',';
File tmpFile = tmpFolder.newFile("csvTestWriteComplexSchema.csv");
+ QName qname = new QName("http://www.opengis.net/om/2.0", "OM_ObservationType");
assertTrue("Csv Export was not successful.", writeCsvToFile(tmpFile, true, false,
- Value.of(sep), null, null, example.getSourceInstances()));
+ Value.of(sep), null, null, example.getSourceInstances(), schema, qname, null));
CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
List rows = reader.readAll();
@@ -298,7 +354,8 @@ public void testWriteComplexSchema() throws Exception {
}
private boolean writeCsvToFile(File tmpFile, boolean solveNestedProperties, boolean useSchema,
- Value sep, Value quo, Value esc, InstanceCollection instances) throws Exception {
+ Value sep, Value quo, Value esc, InstanceCollection instances, Schema schema,
+ QName qname, ByteArrayOutputStream byteArrayOutputStream) throws Exception {
// set instances to xls instance writer
InstanceWriter writer = new CSVInstanceWriter();
IContentType contentType = HalePlatform.getContentTypeManager()
@@ -309,11 +366,23 @@ private boolean writeCsvToFile(File tmpFile, boolean solveNestedProperties, bool
writer.setParameter(CSVSchemaReader.PARAM_SEPARATOR, sep);
writer.setParameter(CSVSchemaReader.PARAM_QUOTE, quo);
writer.setParameter(CSVSchemaReader.PARAM_ESCAPE, esc);
+ writer.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of(qname.getLocalPart()));
writer.setInstances(instances);
+
+ DefaultSchemaSpace ss = new DefaultSchemaSpace();
+ ss.addSchema(schema);
+ writer.setTargetSchema(ss);
+
// write instances to a temporary CSV file
- writer.setTarget(new FileIOSupplier(tmpFile));
+ if (tmpFile != null) {
+ writer.setTarget(new FileIOSupplier(tmpFile));
+ }
+ else {
+ writer.setTarget(new ByteArrayOutputStreamSupplier(byteArrayOutputStream));
+ }
writer.setContentType(contentType);
IOReport report = writer.execute(null);
return report.isSuccess();
}
+
}
diff --git a/io/plugins/eu.esdihumboldt.hale.io.csv.ui/src/eu/esdihumboldt/hale/io/csv/ui/InstanceExportConfigurationPage.java b/io/plugins/eu.esdihumboldt.hale.io.csv.ui/src/eu/esdihumboldt/hale/io/csv/ui/InstanceExportConfigurationPage.java
index 00cd28575e..7f675c2ea6 100644
--- a/io/plugins/eu.esdihumboldt.hale.io.csv.ui/src/eu/esdihumboldt/hale/io/csv/ui/InstanceExportConfigurationPage.java
+++ b/io/plugins/eu.esdihumboldt.hale.io.csv.ui/src/eu/esdihumboldt/hale/io/csv/ui/InstanceExportConfigurationPage.java
@@ -15,17 +15,29 @@
package eu.esdihumboldt.hale.io.csv.ui;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Set;
import org.eclipse.jface.layout.GridDataFactory;
-import org.eclipse.jface.viewers.ISelectionChangedListener;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.jface.viewers.ViewerFilter;
+import org.eclipse.jface.viewers.ArrayContentProvider;
+import org.eclipse.jface.viewers.CheckStateChangedEvent;
+import org.eclipse.jface.viewers.CheckboxTableViewer;
+import org.eclipse.jface.viewers.ICheckStateListener;
+import org.eclipse.jface.viewers.ICheckStateProvider;
+import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.PlatformUI;
import eu.esdihumboldt.hale.common.core.io.Value;
@@ -33,7 +45,6 @@
import eu.esdihumboldt.hale.common.instance.model.DataSet;
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.service.instance.InstanceService;
/**
@@ -43,33 +54,10 @@
*/
public class InstanceExportConfigurationPage extends CommonInstanceExportConfigurationPage {
- private TypeDefinitionSelector typeSelector;
-
- private final ViewerFilter validTypesToSelect = new ViewerFilter() {
-
- @Override
- public boolean select(Viewer viewer, Object parentElement, Object element) {
- if (!(element instanceof TypeDefinition))
- return false;
-
- InstanceService instanceService = PlatformUI.getWorkbench()
- .getService(InstanceService.class);
-
- Set instanceSourceTypes = instanceService
- .getInstanceTypes(DataSet.SOURCE);
- if (instanceSourceTypes.contains(element)) {
- return true;
- }
-
- Set instanceTransformedTypes = instanceService
- .getInstanceTypes(DataSet.TRANSFORMED);
- if (instanceTransformedTypes.contains(element)) {
- return true;
- }
-
- return false;
- }
- };
+ private CheckboxTableViewer featureTypeTable;
+ private Button selectAll = null;
+ private Group chooseFeatureTypes;
+ private Table table;
/**
*
@@ -96,17 +84,6 @@ 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) {
- super.updateConfiguration(provider);
- provider.setParameter(InstanceTableIOConstants.EXPORT_TYPE,
- Value.of(typeSelector.getSelectedObject().getName().toString()));
- return true;
- }
-
/**
* @see eu.esdihumboldt.hale.ui.HaleWizardPage#createContent(org.eclipse.swt.widgets.Composite)
*/
@@ -114,16 +91,11 @@ public boolean updateConfiguration(InstanceWriter provider) {
protected void createContent(Composite page) {
super.createContent(page);
- final Label label = new Label(page, SWT.NONE);
- label.setText("Choose your Type you want to export:");
-
- Label separatorLabel = new Label(page, SWT.NONE);
- separatorLabel.setText("Warning! Feature types with no data are not selectable");
-
- // Set the text colour of the label to yellow
- Color greyLabel = PlatformUI.getWorkbench().getDisplay()
- .getSystemColor(SWT.COLOR_DARK_GRAY);
- separatorLabel.setForeground(greyLabel);
+ GridDataFactory groupData = GridDataFactory.fillDefaults().grab(true, false);
+ chooseFeatureTypes = new Group(page, SWT.NONE);
+ chooseFeatureTypes.setLayout(new GridLayout(1, false));
+ chooseFeatureTypes.setText("Choose your Type you want to export");
+ groupData.applyTo(chooseFeatureTypes);
page.pack();
@@ -134,26 +106,132 @@ protected void createContent(Composite page) {
@Override
protected void onShowPage(boolean firstShow) {
if (firstShow) {
- ViewerFilter[] filters = { validTypesToSelect };
- typeSelector = new TypeDefinitionSelector(page, "Select the corresponding schema type",
- getWizard().getProvider().getTargetSchema(), filters);
+ selectAll = new Button(chooseFeatureTypes, SWT.CHECK);
+ selectAll.setText("Select all");
+ selectAll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
- typeSelector.getControl().setLayoutData(
- GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
- typeSelector.addSelectionChangedListener(new ISelectionChangedListener() {
+ selectAll.addSelectionListener(new SelectionAdapter() {
@Override
- public void selectionChanged(SelectionChangedEvent event) {
- setPageComplete(!(event.getSelection().isEmpty()));
- if (typeSelector.getSelectedObject() != null) {
- page.layout();
- page.pack();
+ public void widgetSelected(SelectionEvent e) {
+ featureTypeTable.setAllChecked(((Button) e.getSource()).getSelection());
+ page.layout();
+ page.pack();
+ setPageComplete(validate());
+ }
+ });
+
+ Label separatorLabel = new Label(page, SWT.NONE);
+ separatorLabel.setText("Warning! Feature types with no data are not selectable");
+
+ // Set the text colour of the label to yellow
+ Color greyLabel = PlatformUI.getWorkbench().getDisplay()
+ .getSystemColor(SWT.COLOR_DARK_GRAY);
+ separatorLabel.setForeground(greyLabel);
+
+ table = new Table(chooseFeatureTypes, SWT.CHECK | SWT.MULTI | SWT.SCROLL_PAGE);
+ table.setHeaderVisible(false);
+ table.setLinesVisible(false);
+ table.setBackground(PlatformUI.getWorkbench().getDisplay()
+ .getSystemColor(SWT.COLOR_LIST_BACKGROUND));
+ GridDataFactory groupData = GridDataFactory.fillDefaults().grab(true, false);
+ groupData.applyTo(table);
+
+ featureTypeTable = new CheckboxTableViewer(table);
+ featureTypeTable.setLabelProvider(new LabelProvider() {
+
+ @Override
+ public String getText(Object element) {
+ return ((TypeDefinition) element).getDisplayName();
+ }
+
+ });
+ featureTypeTable.setContentProvider(ArrayContentProvider.getInstance());
+
+ featureTypeTable.setInput(
+ getWizard().getProvider().getTargetSchema().getMappingRelevantTypes());
+
+ featureTypeTable.addCheckStateListener(new ICheckStateListener() {
+
+ @Override
+ public void checkStateChanged(CheckStateChangedEvent event) {
+ // Programmatic action to toggle the state
+ selectAll.setSelection(
+ featureTypeTable.getCheckedElements().length == featureTypeTable
+ .getTable().getItemCount());
+
+ page.layout();
+ page.pack();
+ setPageComplete(validate());
+ }
+ });
+
+ featureTypeTable.setCheckStateProvider(new ICheckStateProvider() {
+
+ @Override
+ public boolean isChecked(Object element) {
+ if (!(element instanceof TypeDefinition))
+ return false;
+ return checkboxState(element);
+ }
+
+ @Override
+ public boolean isGrayed(Object element) {
+ if (!(element instanceof TypeDefinition))
+ return false;
+ return checkboxState(element);
+ }
+
+ /**
+ * @param element
+ * @return true if the button cannot be selected
+ */
+ private boolean checkboxState(Object element) {
+ InstanceService instanceService = PlatformUI.getWorkbench()
+ .getService(InstanceService.class);
+
+ Set instanceSourceTypes = instanceService
+ .getInstanceTypes(DataSet.SOURCE);
+ if (instanceSourceTypes.contains(element)) {
+ return false;
}
+
+ Set instanceTransformedTypes = instanceService
+ .getInstanceTypes(DataSet.TRANSFORMED);
+ if (instanceTransformedTypes.contains(element)) {
+ return false;
+ }
+ return true;
}
});
}
page.layout();
page.pack();
}
+
+ private boolean validate() {
+ return (featureTypeTable.getCheckedElements().length > 0);
+ }
+
+ /**
+ * @see eu.esdihumboldt.hale.ui.io.IOWizardPage#updateConfiguration(eu.esdihumboldt.hale.common.core.io.IOProvider)
+ */
+ @Override
+ public boolean updateConfiguration(InstanceWriter provider) {
+ super.updateConfiguration(provider);
+
+ List