Search in sources :

Example 1 with InstanceReader

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();
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) SchemaReader(eu.esdihumboldt.hale.common.schema.io.SchemaReader) XmlSchemaReader(eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) GmlInstanceReader(eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)

Example 2 with InstanceReader

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);
    }
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) ProjectInfoAware(eu.esdihumboldt.hale.common.core.io.project.ProjectInfoAware)

Example 3 with InstanceReader

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);
}
Also used : InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) BrowseOrientInstanceCollection(eu.esdihumboldt.hale.common.instance.orient.storage.BrowseOrientInstanceCollection) MultiInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection) FilteredInstanceCollection(eu.esdihumboldt.hale.common.instance.model.impl.FilteredInstanceCollection) AbstractIOAdvisor(eu.esdihumboldt.hale.common.core.io.impl.AbstractIOAdvisor) IOException(java.io.IOException) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment) DefaultTransformedInstanceValidator(eu.esdihumboldt.hale.common.headless.transform.validate.impl.DefaultTransformedInstanceValidator) IOException(java.io.IOException) InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader)

Example 4 with InstanceReader

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();
    }
}
Also used : TransformationWorkspace(eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace) InputStream(java.io.InputStream) IOException(java.io.IOException) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) IOException(java.io.IOException) InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) LocatableInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.LocatableInputSupplier) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Example 5 with InstanceReader

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();
}
Also used : InstanceReader(eu.esdihumboldt.hale.common.instance.io.InstanceReader) XLSInstanceReader(eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) XLSInstanceReader(eu.esdihumboldt.hale.io.xls.reader.XLSInstanceReader) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport)

Aggregations

InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)13 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)8 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)7 GmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.GmlInstanceReader)5 SchemaReader (eu.esdihumboldt.hale.common.schema.io.SchemaReader)4 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)4 XmlSchemaReader (eu.esdihumboldt.hale.io.xsd.reader.XmlSchemaReader)4 IOException (java.io.IOException)3 IOProvider (eu.esdihumboldt.hale.common.core.io.IOProvider)2 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)2 XmlInstanceReader (eu.esdihumboldt.hale.io.gml.reader.internal.XmlInstanceReader)2 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 AbstractIOAdvisor (eu.esdihumboldt.hale.common.core.io.impl.AbstractIOAdvisor)1 ProjectInfoAware (eu.esdihumboldt.hale.common.core.io.project.ProjectInfoAware)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 LocatableInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.LocatableInputSupplier)1 ProjectTransformationEnvironment (eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)1 TransformationWorkspace (eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace)1 DefaultTransformedInstanceValidator (eu.esdihumboldt.hale.common.headless.transform.validate.impl.DefaultTransformedInstanceValidator)1 InstanceValidator (eu.esdihumboldt.hale.common.instance.io.InstanceValidator)1