Search in sources :

Example 1 with ProjectTransformationEnvironment

use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment 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 2 with ProjectTransformationEnvironment

use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.

the class ProjectHandler method onSuccess.

@Override
protected void onSuccess(EnvironmentManager context, String projectId, File projectFile, Project project, ReportFile reportFile) {
    super.onSuccess(context, projectId, projectFile, project, reportFile);
    if (isEnabled()) {
        // load transformation environment if not yet done
        if (transformationEnvironment == null) {
            try {
                transformationEnvironment = new ProjectTransformationEnvironment(projectId, new FileIOSupplier(projectFile), reportFile);
                // check alignment
                if (transformationEnvironment.getAlignment() == null) {
                    throw new IllegalStateException("Alignment missing or failed to load");
                }
                if (transformationEnvironment.getAlignment().getActiveTypeCells().isEmpty()) {
                    throw new IllegalStateException("Alignment contains no active type relations");
                }
            } catch (Exception e) {
                log.error("Could not load transformation environment for project " + projectId, e);
                status = Status.BROKEN;
                transformationEnvironment = null;
                context.removeEnvironment(projectId);
                // log the exception as report
                Reporter<Message> report = new DefaultReporter<Message>("Load project transformation environment", ProjectIO.ACTION_LOAD_PROJECT, Message.class, false);
                report.error(new MessageImpl(e.getMessage(), e));
                reportFile.publishReport(report);
            }
        } else {
        // XXX somehow check if project was changed?
        }
        if (transformationEnvironment != null) {
            context.addEnvironment(transformationEnvironment);
            status = Status.ACTIVE;
        }
    } else {
        // clear transformation environment
        status = Status.INACTIVE;
        transformationEnvironment = null;
        context.removeEnvironment(projectId);
    }
}
Also used : Message(eu.esdihumboldt.hale.common.core.report.Message) Reporter(eu.esdihumboldt.hale.common.core.report.Reporter) DefaultReporter(eu.esdihumboldt.hale.common.core.report.impl.DefaultReporter) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment) MessageImpl(eu.esdihumboldt.hale.common.core.report.impl.MessageImpl) IOException(java.io.IOException)

Example 3 with ProjectTransformationEnvironment

use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.

the class GenerateDuplicates method loadProject.

private boolean loadProject() throws IOException {
    final AtomicBoolean success = new AtomicBoolean(true);
    LocatableInputSupplier<? extends InputStream> projectIn = new DefaultInputSupplier(context.getProject());
    ProjectTransformationEnvironment env = new ProjectTransformationEnvironment("sample", projectIn, new ReportHandler() {

        @Override
        public void publishReport(Report<?> report) {
            if (report.isSuccess() && report.getErrors().isEmpty()) {
                System.out.println(report.getSummary());
            } else {
                System.err.println("Error loading project: " + report.getSummary());
                success.set(false);
            }
        }
    });
    if (success.get()) {
        this.sourceSchema = env.getSourceSchema();
        this.targetSchema = env.getTargetSchema();
        this.examples = env.getAlignment();
        return true;
    }
    return false;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) ReportHandler(eu.esdihumboldt.hale.common.core.report.ReportHandler) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)

Example 4 with ProjectTransformationEnvironment

use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.

the class CityGMLPropagate method loadProject.

private boolean loadProject() throws IOException {
    final AtomicBoolean success = new AtomicBoolean(true);
    LocatableInputSupplier<? extends InputStream> projectIn = new DefaultInputSupplier(context.getProject());
    ProjectTransformationEnvironment env = new ProjectTransformationEnvironment("sample", projectIn, new ReportHandler() {

        @Override
        public void publishReport(Report<?> report) {
            if (report.isSuccess() && report.getErrors().isEmpty()) {
                System.out.println(report.getSummary());
            } else {
                System.err.println("Error loading project: " + report.getSummary());
                success.set(false);
            }
        }
    });
    if (success.get()) {
        this.sourceSchema = env.getSourceSchema();
        this.targetSchema = env.getTargetSchema();
        this.examples = env.getAlignment();
        return true;
    }
    return false;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) ReportHandler(eu.esdihumboldt.hale.common.core.report.ReportHandler) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)

Example 5 with ProjectTransformationEnvironment

use of eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment in project hale by halestudio.

the class ExecTransformation method loadProject.

private void loadProject() throws IOException {
    status("Loading hale project...");
    env = new ProjectTransformationEnvironment(id, new DefaultInputSupplier(context.getProject()), reportHandler);
}
Also used : DefaultInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier) ProjectTransformationEnvironment(eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)

Aggregations

ProjectTransformationEnvironment (eu.esdihumboldt.hale.common.headless.impl.ProjectTransformationEnvironment)6 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)3 ReportHandler (eu.esdihumboldt.hale.common.core.report.ReportHandler)2 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Alignment (eu.esdihumboldt.hale.common.align.model.Alignment)1 Cell (eu.esdihumboldt.hale.common.align.model.Cell)1 MutableCell (eu.esdihumboldt.hale.common.align.model.MutableCell)1 AbstractIOAdvisor (eu.esdihumboldt.hale.common.core.io.impl.AbstractIOAdvisor)1 FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)1 Message (eu.esdihumboldt.hale.common.core.report.Message)1 Reporter (eu.esdihumboldt.hale.common.core.report.Reporter)1 DefaultReporter (eu.esdihumboldt.hale.common.core.report.impl.DefaultReporter)1 MessageImpl (eu.esdihumboldt.hale.common.core.report.impl.MessageImpl)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