use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class PartitioningWFSWriter method execute.
@Override
protected IOReport execute(final ProgressIndicator progress, final IOReporter reporter) throws IOProviderConfigurationException, IOException {
progress.begin("Upload to WFS-T", IProgressMonitor.UNKNOWN);
try {
progress.setCurrentTask("Partitioning data");
// create the partitioner
InstanceCollectionPartitioner partitioner = StreamGmlWriter.getPartitioner(this, reporter);
// partition the graph
int threshold = getParameter(PARAM_INSTANCES_THRESHOLD).as(Integer.class, DEFAULT_INSTANCES_THRESHOLD);
try (ResourceIterator<InstanceCollection> parts = partitioner.partition(getInstances(), threshold, reporter)) {
if (partitioner.requiresImmediateConsumption()) {
// handle all parts right here, one after another
int partCount = 0;
boolean failed = false;
if (parts.hasNext()) {
while (parts.hasNext() && !progress.isCanceled()) {
partCount++;
SubtaskProgressIndicator partitionProgress = new SubtaskProgressIndicator(progress);
partitionProgress.begin("Assembling part " + partCount, ProgressIndicator.UNKNOWN);
InstanceCollection part = parts.next();
partitionProgress.end();
progress.setCurrentTask("Upload part " + partCount + ((part.hasSize()) ? (" (" + part.size() + " instances)") : ("")));
IOReport report = uploadInstances(part, reporter, new SubtaskProgressIndicator(progress));
if (!report.isSuccess()) {
failed = true;
reporter.error("Upload of part {0} - {1}", partCount, report.getSummary());
} else {
reporter.info("Upload of part {0} - {1}", partCount, report.getSummary());
}
}
reporter.setSuccess(!failed && reporter.getErrors().isEmpty());
if (!reporter.isSuccess()) {
reporter.setSummary("Errors during upload to WFS-T, please see the report.");
} else {
reporter.setSummary("Successfully uploaded data via WFS-T");
}
} else {
reporter.setSuccess(false);
reporter.setSummary("Partitioning yielded no instances to upload");
}
} else {
// can start requests with separate thread (potentially
// threads, but tests with WFSes show that this usually is
// too much to handle for the service)
int partCount = 0;
final AtomicBoolean failed = new AtomicBoolean();
if (parts.hasNext()) {
ExecutorService requestThread = Executors.newSingleThreadExecutor();
while (parts.hasNext() && !progress.isCanceled()) {
partCount++;
SubtaskProgressIndicator partitionProgress = new SubtaskProgressIndicator(// only used for first
progress);
// partitioning
if (partCount == 1)
partitionProgress.begin("Assembling part " + partCount, ProgressIndicator.UNKNOWN);
// not
final InstanceCollection part = parts.next();
// safe
if (partCount == 1)
partitionProgress.end();
progress.setCurrentTask("Upload part " + partCount + ((part.hasSize()) ? (" (" + part.size() + " instances)") : ("")));
final int currentPart = partCount;
requestThread.submit(new Runnable() {
@Override
public void run() {
try {
IOReport report = uploadInstances(part, reporter, new SubtaskProgressIndicator(progress));
if (!report.isSuccess()) {
failed.set(true);
reporter.error(new IOMessageImpl("Upload of part " + currentPart + " - " + report.getSummary(), null));
} else {
reporter.info(new IOMessageImpl("Upload of part " + currentPart + " - " + report.getSummary(), null));
}
} catch (Exception e) {
failed.set(true);
reporter.error(new IOMessageImpl("Upload of part " + currentPart + " failed", e));
}
}
});
}
// wait for requests completion
requestThread.shutdown();
if (!requestThread.awaitTermination(24, TimeUnit.HOURS)) {
reporter.error(new IOMessageImpl("Timeout reached waiting for completion of WFS requests", null));
}
reporter.setSuccess(!failed.get() && reporter.getErrors().isEmpty());
if (!reporter.isSuccess()) {
reporter.setSummary("Errors during upload to WFS-T, please see the report.");
} else {
reporter.setSummary("Successfully uploaded data via WFS-T");
}
} else {
reporter.setSuccess(false);
reporter.setSummary("Partitioning yielded no instances to upload");
}
}
}
} catch (Exception e) {
reporter.error(new IOMessageImpl("Error during attempt to upload to WFS-T", e));
reporter.setSuccess(false);
} finally {
progress.end();
}
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSInstanceIOTest method test.
/**
* Exports the instances created by
* {@link XLSInstanceWriterTestExamples#createInstanceCollection} into a
* temporary XLS file by executing {@link XLSInstanceWriter#execute}.
* Afterwards, the schema is read by {@link XLSSchemaReader} and the
* instances are loaded by {@link XLSInstanceReader}. Each of the imported
* instances are compared with the original instances. In addtion, a
* different set of instances is compared with the imported instances.
*/
@Test
public void test() {
// set instances to xls instance writer
XLSInstanceWriter writer = new XLSInstanceWriter();
InstanceCollection instances = XLSInstanceWriterTestExamples.createInstanceCollection();
IContentType contentType = HalePlatform.getContentTypeManager().getContentType("eu.esdihumboldt.hale.io.xls.xls");
writer.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
File tempDir = Files.createTempDir();
File tempFile = new File(tempDir, "data.xls");
writer.setInstances(instances);
try {
// write instances to a temporary XLS file
writer.setTarget(new FileIOSupplier(tempFile));
writer.setContentType(contentType);
IOReport report = writer.execute(null);
assertTrue(report.isSuccess());
} catch (IOProviderConfigurationException | IOException e) {
fail("Execution of xls instance writer failed.");
}
// read the schema from the temporary XLS file
XLSSchemaReader schemaReader = new XLSSchemaReader();
schemaReader.setContentType(contentType);
schemaReader.setSource(new FileIOSupplier(tempFile));
schemaReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
schemaReader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
schemaReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
try {
IOReport report = schemaReader.execute(null);
assertTrue(report.isSuccess());
} catch (IOProviderConfigurationException | IOException e1) {
fail("Execution of schema reader failed.");
}
Schema schema = schemaReader.getSchema();
// read the instances form the temporary XLS file
XLSInstanceReader reader = new XLSInstanceReader();
reader.setSourceSchema(schema);
reader.setParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE, Value.of(true));
reader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of("ItemType"));
reader.setParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES, Value.of(false));
// read sheet with index 0 since there is only one sheet
reader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(0));
reader.setContentType(contentType);
reader.setSource(new FileIOSupplier(tempFile));
try {
IOReport report = reader.execute(null);
assertTrue(report.isSuccess());
} catch (IOProviderConfigurationException | IOException e) {
fail("Execution of xls instance reader failed.");
}
// compare size of instance collection
InstanceCollection inst = reader.getInstances();
assertEquals(4, inst.size());
// check if instance collection contains current instance
Iterator<Instance> instanceIt = inst.iterator();
while (instanceIt.hasNext()) {
Instance instance = instanceIt.next();
assertTrue(contains(instances.iterator(), instance));
}
// other instance should be contained in the imported instances
InstanceCollection falseInstances = XLSInstanceWriterTestExamples.createFalseTestInstanceCollection();
instanceIt = inst.iterator();
while (instanceIt.hasNext()) {
Instance instance = instanceIt.next();
assertFalse(contains(falseInstances.iterator(), instance));
}
// delete file and temporary directory
tempFile.delete();
tempDir.delete();
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSReaderTest method testPropertyType.
/**
* Test - check declaration of properties' datatype.
*
* @throws Exception , if an error occurs
*/
@Test
public void testPropertyType() throws Exception {
Schema schema = readXLSSchema("/data/simpleOneSheet.xls", 0, typeName, "java.lang.Integer,java.lang.String,java.lang.String");
// Test property datatype
TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
Binding binding = schemaType.getChildren().iterator().next().asProperty().getPropertyType().getConstraint(Binding.class);
assertTrue("The type is not an Integer.", binding.getBinding().equals(Integer.class));
// ### Instance
InstanceCollection instances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, true, schema);
assertTrue(instances.hasSize());
assertEquals(numberOfInstances, instances.size());
// Check the values of the first (type) instance
Instance instance = instances.iterator().next();
Object[] value = instance.getProperty(QName.valueOf(properties[0]));
assertEquals(Integer.valueOf(dataFirstColumn[0]), value[0]);
assertTrue(value[0] instanceof Integer);
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSReaderTest method testReadBlankCells.
/**
* Test - read xls file and data. Check handled blank cells.
*
* @throws Exception , if an error occurs
*/
@Test
public void testReadBlankCells() throws Exception {
// read schema ###
Schema schema = readXLSSchema("/data/blankEntries.xls", 0, typeName, "java.lang.String,java.lang.String,java.lang.String");
// read instances ###
InstanceCollection instances = readXLSInstances("/data/blankEntries.xls", 0, typeName, true, schema);
// Number of instances should be the same
assertTrue(instances.hasSize());
assertEquals(numberOfInstances, instances.size());
// Check blank property of first type
Instance instance = instances.iterator().next();
Object[] value;
value = instance.getProperty(QName.valueOf(properties[1]));
// There should be no value, therefore the length of the value-array has
// to be
// 0
assertEquals(0, value.length);
// check other values to be correct
value = instance.getProperty(QName.valueOf(properties[0]));
assertEquals(dataFirstColumn[0], value[0]);
value = instance.getProperty(QName.valueOf(properties[2]));
assertEquals(dataFirstColumn[2], value[0]);
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSReaderTest method testReadSameFileDiffSheet.
/**
* Test - read a sample xls schema and data from same file and different
* sheets. Check skip first
*
* @throws Exception , if an error occurs
*/
@Test
public void testReadSameFileDiffSheet() throws Exception {
String sourceLocation = "/data/simpleTwoSheet.xls";
Schema schema = readXLSSchema(sourceLocation, 0, typeName, "java.lang.String,java.lang.String,java.lang.String");
TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
// Check every property for their existence
for (String propertyName : properties) {
assertEquals(propertyName, schemaType.getChild(QName.valueOf(propertyName)).getDisplayName());
}
// Instance Read ###
InstanceCollection instances = readXLSInstances(sourceLocation, 1, typeName, false, schema);
assertTrue(instances.hasSize());
assertEquals(numberOfInstances, instances.size());
// get Type to check property definition (schema and instance
// combination)
TypeDefinition type = instances.iterator().next().getDefinition();
ChildDefinition<?> child = null;
assertEquals(typeName, type.getDisplayName());
for (int i = 0; i < properties.length; i++) {
child = type.getChild(QName.valueOf(properties[i]));
assertEquals(properties[i], child.getDisplayName());
}
// Check the values of the first (type) instance
Instance instance = instances.iterator().next();
Object[] value;
for (int i = 0; i < dataFirstColumn.length; i++) {
value = instance.getProperty(QName.valueOf(properties[i]));
assertEquals(dataFirstColumn[i], value[0]);
assertTrue(value[0] instanceof String);
}
}
Aggregations