Skip to content

Commit

Permalink
fixup: feat: add multiple selection to CSV writer
Browse files Browse the repository at this point in the history
Same approach as for XSL has been implemented for the CSV writer. In the wizard of selecting a feature type has been implemented the multi selection table and the same number of .csv files are exported as the selection.

ING-3987
  • Loading branch information
emanuelaepure10 committed Sep 8, 2023
1 parent 01a8d22 commit 63167b2
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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;
Expand All @@ -39,6 +41,7 @@
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;
Expand Down Expand Up @@ -74,6 +77,7 @@ public static void waitForServices() {
@Test
public void testWriteSimpleSchema() throws Exception {

Schema schema = CSVInstanceWriterTestUtil.createExampleSchema();
TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_ASSIGN);
// alternative the data could be generated by iterating through the
Expand All @@ -86,10 +90,11 @@ public void testWriteSimpleSchema() throws Exception {
int numberOfRows = 3;
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()));
Value.of(sep), null, null, example.getSourceInstances(), schema, qname));

CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
List<String[]> rows = reader.readAll();
Expand Down Expand Up @@ -146,10 +151,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));

CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
List<String[]> rows = reader.readAll();
Expand Down Expand Up @@ -195,6 +201,7 @@ public void testWriteSimpleSchemaColOrder() throws Exception {
@Test
public void testWriteSimpleSchemaDelimiter() throws Exception {

Schema schema = CSVInstanceWriterTestUtil.createExampleSchema();
TransformationExample example = TransformationExamples
.getExample(TransformationExamples.SIMPLE_ASSIGN);

Expand All @@ -209,10 +216,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));

CSVReader reader = new CSVReader(new FileReader(tmpFile), sep, quo, esc);
List<String[]> rows = reader.readAll();
Expand Down Expand Up @@ -248,7 +257,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
Expand All @@ -262,9 +271,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));

CSVReader reader = new CSVReader(new FileReader(tmpFile), sep);
List<String[]> rows = reader.readAll();
Expand Down Expand Up @@ -298,7 +308,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) throws Exception {
// set instances to xls instance writer
InstanceWriter writer = new CSVInstanceWriter();
IContentType contentType = HalePlatform.getContentTypeManager()
Expand All @@ -309,7 +320,13 @@ 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));
writer.setContentType(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,30 @@
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;
import eu.esdihumboldt.hale.common.instance.io.InstanceWriter;
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;

/**
Expand All @@ -43,33 +51,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<TypeDefinition> instanceSourceTypes = instanceService
.getInstanceTypes(DataSet.SOURCE);
if (instanceSourceTypes.contains(element)) {
return true;
}

Set<TypeDefinition> 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;

/**
*
Expand All @@ -96,34 +81,18 @@ 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)
*/
@Override
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();

Expand All @@ -134,26 +103,129 @@ 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));

selectAll.addSelectionListener(new SelectionAdapter() {

@Override
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());
}
});

typeSelector.getControl().setLayoutData(
GridDataFactory.fillDefaults().grab(true, false).span(1, 1).create());
typeSelector.addSelectionChangedListener(new ISelectionChangedListener() {
featureTypeTable.setCheckStateProvider(new ICheckStateProvider() {

@Override
public void selectionChanged(SelectionChangedEvent event) {
setPageComplete(!(event.getSelection().isEmpty()));
if (typeSelector.getSelectedObject() != null) {
page.layout();
page.pack();
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<TypeDefinition> instanceSourceTypes = instanceService
.getInstanceTypes(DataSet.SOURCE);
if (instanceSourceTypes.contains(element)) {
return false;
}

Set<TypeDefinition> 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);

Object[] elements = featureTypeTable.getCheckedElements();
String param = "";
for (Object el : elements) {
param = param + ((TypeDefinition) el).getName().toString() + ",";
}
provider.setParameter(InstanceTableIOConstants.EXPORT_TYPE, Value.of(param));

return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ public class InstanceTableIOConstants {
*/
public static final String EXPORT_TYPE = "selectedExportType";

/**
* Constant for underscore.
*/
public static final String UNDERSCORE = "_";

/**
* Constant for the shape file extension.
*/
public static final String CSV_EXTENSION = ".csv";

/**
* Constant for the url. Used to create schema for shape file writer.
*/
public static final String URL_STRING = "url";

}
Loading

0 comments on commit 63167b2

Please sign in to comment.