use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSReaderTest method testReadSimple.
/**
* Test - read a sample xls schema and data from same file and sheet (simple
* io test). Check the type, check the properties, check the values of the
* properties, check the datatype of the properties
*
* @throws Exception , if an error occurs
*/
@Test
public void testReadSimple() throws Exception {
// read Schema ###
Schema schema = readXLSSchema("/data/simpleOneSheet.xls", 0, typeName, "java.lang.String,java.lang.String,java.lang.String");
// Test properties and their datatype
TypeDefinition schemaType = schema.getType(QName.valueOf(typeName));
Binding binding;
for (ChildDefinition<?> child : schemaType.getChildren()) {
binding = child.asProperty().getPropertyType().getConstraint(Binding.class);
assertTrue(binding.getBinding().equals(String.class));
}
// Check every property for their existence
for (String propertyName : properties) {
assertEquals(propertyName, schemaType.getChild(QName.valueOf(propertyName)).getDisplayName());
}
// read Instances ###
InstanceCollection instances = readXLSInstances("/data/simpleOneSheet.xls", 0, typeName, true, 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);
}
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class XLSInstanceWriter method execute.
/**
* @see eu.esdihumboldt.hale.common.core.io.impl.AbstractIOProvider#execute(eu.esdihumboldt.hale.common.core.io.ProgressIndicator,
* eu.esdihumboldt.hale.common.core.io.report.IOReporter)
*/
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
boolean solveNestedProperties = getParameter(InstanceTableIOConstants.SOLVE_NESTED_PROPERTIES).as(Boolean.class, false);
// get the parameter to get the type definition
String exportType = getParameter(InstanceTableIOConstants.EXPORT_TYPE).as(String.class);
QName selectedTypeName = null;
if (exportType != null && !exportType.equals("") && !exportType.equals(" ")) {
selectedTypeName = QName.valueOf(exportType);
}
// write xls file
if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xls")) {
wb = new HSSFWorkbook();
} else // write xlsx file
if (getContentType().getId().equals("eu.esdihumboldt.hale.io.xls.xlsx")) {
wb = new XSSFWorkbook();
} else {
reporter.error(new IOMessageImpl("Content type is invalid!", null));
return reporter;
}
cellStyle = XLSCellStyles.getNormalStyle(wb, false);
headerStyle = XLSCellStyles.getHeaderStyle(wb);
// get all instances of the selected Type
InstanceCollection instances = getInstanceCollection(selectedTypeName);
Iterator<Instance> instanceIterator = instances.iterator();
Instance instance = null;
try {
instance = instanceIterator.next();
} catch (NoSuchElementException e) {
reporter.error(new IOMessageImpl("There are no instances for the selected type.", e));
return reporter;
}
List<Instance> remainingInstances = new ArrayList<Instance>();
headerRowStrings = new ArrayList<String>();
// all instances with equal type definitions are stored in an extra
// sheet
TypeDefinition definition = instance.getDefinition();
Sheet sheet = wb.createSheet(definition.getDisplayName());
Row headerRow = sheet.createRow(0);
int rowNum = 1;
Row row = sheet.createRow(rowNum++);
writeRow(row, super.getPropertyMap(instance, headerRowStrings, solveNestedProperties));
while (instanceIterator.hasNext()) {
Instance nextInst = instanceIterator.next();
if (nextInst.getDefinition().equals(definition)) {
row = sheet.createRow(rowNum++);
writeRow(row, super.getPropertyMap(nextInst, headerRowStrings, solveNestedProperties));
} else
remainingInstances.add(nextInst);
}
writeHeaderRow(headerRow, headerRowStrings);
setCellStyle(sheet, headerRowStrings.size());
resizeSheet(sheet);
// write file
FileOutputStream out = new FileOutputStream(getTarget().getLocation().getPath());
wb.write(out);
out.close();
reporter.setSuccess(true);
return reporter;
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class MsAccessDataReaderTestSuit method instanceRaderTest.
/**
* Test - reads a sample MsAccess schema and data.
*
* @throws Exception if an error occurs
*/
public void instanceRaderTest() throws Exception {
// ****** read Schema ******//
Schema schema = readSchema(getSourceTempFilePath());
assertNotNull(schema);
System.out.println("MappingRelevantTypes:" + schema.getMappingRelevantTypes().size());
// assertEquals(1, schema.getMappingRelevantTypes().size());
// Test properties
// TypeDefinition schemaType =
// schema.getMappingRelevantTypes().iterator().next();
// Check every property for their existence
// ****** read Instances ******//
InstanceCollection instances = readInstances(schema, getSourceTempFilePath());
assertTrue(instances.hasSize());
System.out.println("instances size:" + instances.size());
// assertEquals(SOURCE_INSTANCES_COUNT, instances.size());
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class MsSQLServer14Test method testInstanceReader.
/**
* Test instance reader
*
* @throws Exception if error occurred in reading instances
*/
@Test
public void testInstanceReader() throws Exception {
// ****** read Schema ******//
Schema schema = readSchema();
// ****** read Instances ******//
InstanceCollection instances = readInstances(schema);
assertTrue(instances.hasSize());
}
use of eu.esdihumboldt.hale.common.instance.model.InstanceCollection in project hale by halestudio.
the class StreamGmlWriter method writeParts.
/**
* Write the given {@link InstanceCollection}s to multiple files using the
* configured target as a base file name.<br>
* <br>
* Parts can only be written if the configured target is a URI to a local
* file. The output files will be named after the target file, amended by a
* counter. If, for example, the configured target file name is
* <code>output.gml</code>, the files created by this method will be called
* <code>output.0001.gml</code>, <code>output.0002.gml</code>, etc.
*
* @param instanceCollections the parts to write
* @param progress Progress indicator
* @param reporter the reporter to use for the execution report
* @throws IOException if an I/O operation fails
* @see #setTarget(LocatableOutputSupplier)
*/
protected void writeParts(Iterator<InstanceCollection> instanceCollections, ProgressIndicator progress, IOReporter reporter) throws IOException {
final URI location = getTarget().getLocation();
if (location == null) {
reporter.error("Cannot write multiple GML files: Output location unknown");
return;
}
// Can only write multiple instance collection if target is a local file
if (!"file".equals(location.getScheme())) {
reporter.error("Cannot write multiple GML files: Target must be a local file");
return;
}
Path origPath = Paths.get(location).normalize();
String filename;
String extension;
Path targetFolder;
if (origPath.toFile().isDirectory()) {
reporter.error("Cannot write to a directory: Target must a file");
return;
// TODO Support writing to a directory; use parameter to specify
// file name prefix.
} else {
targetFolder = origPath.getParent();
filename = Files.getNameWithoutExtension(origPath.toString());
extension = Files.getFileExtension(origPath.toString());
}
List<URI> filesWritten = new ArrayList<>();
int i = 1;
while (instanceCollections.hasNext()) {
String targetFilename = String.format("%s%s%s.%04d.%s", targetFolder.toString(), File.separator, filename, i++, extension);
File targetFile = new File(targetFilename);
FileOutputStream out = new FileOutputStream(targetFile);
InstanceCollection instances = instanceCollections.next();
write(instances, out, progress, reporter);
filesWritten.add(targetFile.toURI());
}
if (filesWritten.size() > 1) {
setTarget(new MultiLocationOutputSupplier(filesWritten));
} else if (!filesWritten.isEmpty()) {
setTarget(new LocatableOutputSupplier<OutputStream>() {
@Override
public OutputStream getOutput() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public URI getLocation() {
return filesWritten.get(0);
}
});
}
}
Aggregations