use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.
the class AbstractHandlerTest method loadXMLInstances.
/**
* Load an instance collection from a GML file.
*
* @param schemaLocation the GML application schema location
* @param xmlLocation the GML file location
* @param interpolConfig the interpolation configuration
* @return the instance collection
* @throws IOException if reading schema or instances failed
* @throws IOProviderConfigurationException if the I/O providers were not
* configured correctly
*/
public static InstanceCollection loadXMLInstances(URI schemaLocation, URI xmlLocation, @Nullable ReaderConfiguration interpolConfig) throws IOException, IOProviderConfigurationException {
SchemaReader reader = new XmlSchemaReader();
reader.setSharedTypes(null);
reader.setSource(new DefaultInputSupplier(schemaLocation));
IOReport schemaReport = reader.execute(null);
assertTrue(schemaReport.isSuccess());
Schema sourceSchema = reader.getSchema();
InstanceReader instanceReader = new GmlInstanceReader();
instanceReader.setSource(new DefaultInputSupplier(xmlLocation));
instanceReader.setSourceSchema(sourceSchema);
if (interpolConfig != null) {
interpolConfig.apply(instanceReader);
}
IOReport instanceReport = instanceReader.execute(null);
assertTrue(instanceReport.isSuccess());
return instanceReader.getInstances();
}
use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.
the class AppSchemaFileWriterTest method prepareProvider.
// adapted from DefaultIOAdvisor and subclasses
private static void prepareProvider(IOProvider provider, ProjectInfo projectInfo, URI projectLocation) {
if (provider instanceof ProjectInfoAware) {
ProjectInfoAware pia = (ProjectInfoAware) provider;
pia.setProjectInfo(projectInfo);
pia.setProjectLocation(projectLocation);
}
if (provider instanceof InstanceReader) {
InstanceReader ir = (InstanceReader) provider;
ir.setSourceSchema(sourceSchemaSpace);
}
}
use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.
the class Transformation method transform.
/**
* Transform the instances provided through the given instance readers and
* supply the result to the given instance writer.
*
* @param sources the instance readers
* @param target the target instance writer
* @param environment the transformation environment
* @param reportHandler the report handler
* @param processId the identifier for the transformation process, may be
* <code>null</code> if grouping the jobs to a job family is not
* necessary
* @param validators the instance validators, may be <code>null</code> or
* empty
* @param filterDefinition {@link InstanceFilterDefinition} object as a
* filter may be <code>null</code>
* @return the future representing the successful completion of the
* transformation (note that a successful completion doesn't
* necessary mean there weren't any internal transformation errors)
*/
public static ListenableFuture<Boolean> transform(List<InstanceReader> sources, InstanceWriter target, final TransformationEnvironment environment, final ReportHandler reportHandler, Object processId, Collection<InstanceValidator> validators, InstanceFilterDefinition filterDefinition) {
final IOAdvisor<InstanceReader> loadDataAdvisor = new AbstractIOAdvisor<InstanceReader>() {
/**
* @see IOAdvisor#prepareProvider(IOProvider)
*/
@Override
public void prepareProvider(InstanceReader provider) {
super.prepareProvider(provider);
provider.setSourceSchema(environment.getSourceSchema());
}
/**
* @see AbstractIOAdvisor#updateConfiguration(IOProvider)
*/
@Override
public void updateConfiguration(InstanceReader provider) {
super.updateConfiguration(provider);
if (environment instanceof ProjectTransformationEnvironment) {
// set project CRS manager as CRS provider
/*
* Resource based CRS settings will however not work, as the
* resource identifiers will not match
*/
provider.setCRSProvider(new ProjectCRSManager(provider, null, ((ProjectTransformationEnvironment) environment).getProject()));
}
}
};
loadDataAdvisor.setServiceProvider(environment);
loadDataAdvisor.setActionId(InstanceIO.ACTION_LOAD_SOURCE_DATA);
List<InstanceCollection> sourceList = Lists.transform(sources, new Function<InstanceReader, InstanceCollection>() {
@Override
public InstanceCollection apply(@Nullable InstanceReader input) {
try {
HeadlessIO.executeProvider(input, loadDataAdvisor, null, reportHandler);
// XXX progress?!
} catch (IOException e) {
throw new IllegalStateException("Failed to load source data", e);
}
return input.getInstances();
}
});
// Apply Filter
InstanceCollection sourceCollection = applyFilter(sourceList, filterDefinition);
final TransformationSink targetSink;
try {
targetSink = TransformationSinkExtension.getInstance().createSink(!target.isPassthrough());
targetSink.setTypes(environment.getTargetSchema());
// add validation to sink
// XXX for now default validation if env variable is set
String env = System.getenv("HALE_TRANSFORMATION_INTERNAL_VALIDATION");
if (env != null && env.equalsIgnoreCase("true")) {
targetSink.addValidator(new DefaultTransformedInstanceValidator(reportHandler, environment));
}
} catch (Exception e) {
throw new IllegalStateException("Error creating target sink", e);
}
IOAdvisor<InstanceWriter> saveDataAdvisor = new AbstractIOAdvisor<InstanceWriter>() {
/**
* @see IOAdvisor#prepareProvider(IOProvider)
*/
@Override
public void prepareProvider(InstanceWriter provider) {
super.prepareProvider(provider);
// set target schema
provider.setTargetSchema(environment.getTargetSchema());
// set instances to export
provider.setInstances(targetSink.getInstanceCollection());
}
};
saveDataAdvisor.setServiceProvider(environment);
saveDataAdvisor.setActionId(InstanceIO.ACTION_SAVE_TRANSFORMED_DATA);
saveDataAdvisor.prepareProvider(target);
saveDataAdvisor.updateConfiguration(target);
ExportJob exportJob = new ExportJob(targetSink, target, saveDataAdvisor, reportHandler);
// no validation
ValidationJob validationJob = null;
if (validators != null && !validators.isEmpty()) {
validationJob = new ValidationJob(validators, reportHandler, target, environment);
}
return transform(sourceCollection, targetSink, exportJob, validationJob, environment.getAlignment(), environment.getSourceSchema(), reportHandler, environment, processId);
}
use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.
the class UploadAndTransForm method onSubmit.
/**
* @see Form#onSubmit()
*/
@Override
protected void onSubmit() {
List<FileUpload> uploads = file.getFileUploads();
if (uploads == null || uploads.isEmpty()) {
error("Please specify files to transform.");
return;
}
final TransformationWorkspace workspace = new TransformationWorkspace();
List<InstanceReader> readers = Lists.transform(uploads, new Function<FileUpload, InstanceReader>() {
private int count;
@Override
public InstanceReader apply(@Nullable final FileUpload input) {
/*
* Copy uploaded file to source folder, because the
* input stream retrieved from the FileUpload is
* automatically closed with the end of the request.
*/
File file = new File(workspace.getSourceFolder(), (count++) + "_" + input.getClientFileName());
try {
input.writeTo(file);
} catch (IOException e) {
throw new IllegalStateException("Unable to read uploaded source file", e);
}
InstanceReader reader = null;
try {
LocatableInputSupplier<? extends InputStream> in = new FileIOSupplier(file);
reader = HaleIO.findIOProvider(InstanceReader.class, in, input.getClientFileName());
if (reader != null) {
reader.setSource(in);
}
} catch (Exception e) {
throw new IllegalStateException("Unable to read uploaded source file", e);
}
if (reader == null) {
throw new IllegalStateException("Could not find I/O provider for source file.");
}
return reader;
}
});
try {
workspace.transform(projectId, readers, target.getConfig());
setResponsePage(StatusPage.class, new PageParameters().add(StatusPage.PARAMETER_WORKSPACE, workspace.getId()));
} catch (Exception e) {
log.error("Error launching transformation process", e);
error("Error launching transformation process");
workspace.delete();
}
}
use of eu.esdihumboldt.hale.common.instance.io.InstanceReader in project hale by halestudio.
the class XLSReaderTest method readXLSInstances.
private InstanceCollection readXLSInstances(String sourceLocation, int sheetIndex, String typeName, boolean skipFirst, Schema sourceSchema) throws Exception {
InstanceReader instanceReader = new XLSInstanceReader();
instanceReader.setSource(new DefaultInputSupplier(getClass().getResource(sourceLocation).toURI()));
instanceReader.setParameter(InstanceTableIOConstants.SHEET_INDEX, Value.of(sheetIndex));
instanceReader.setParameter(CommonSchemaConstants.PARAM_TYPENAME, Value.of(typeName));
instanceReader.setParameter(CommonSchemaConstants.PARAM_SKIP_FIRST_LINE, Value.of(skipFirst));
instanceReader.setSourceSchema(sourceSchema);
// Test instances
IOReport report = instanceReader.execute(null);
assertTrue("Data import was not successfull.", report.isSuccess());
return instanceReader.getInstances();
}
Aggregations