Search in sources :

Example 1 with AbstractIOAdvisor

use of eu.esdihumboldt.hale.common.core.io.impl.AbstractIOAdvisor 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)

Aggregations

AbstractIOAdvisor (eu.esdihumboldt.hale.common.core.io.impl.AbstractIOAdvisor)1 ProjectTransformationEnvironment (eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)1 DefaultTransformedInstanceValidator (eu.esdihumboldt.hale.common.headless.transform.validate.impl.DefaultTransformedInstanceValidator)1 InstanceReader (eu.esdihumboldt.hale.common.instance.io.InstanceReader)1 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)1 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)1 FilteredInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.FilteredInstanceCollection)1 MultiInstanceCollection (eu.esdihumboldt.hale.common.instance.model.impl.MultiInstanceCollection)1 BrowseOrientInstanceCollection (eu.esdihumboldt.hale.common.instance.orient.storage.BrowseOrientInstanceCollection)1 IOException (java.io.IOException)1