Search in sources :

Example 31 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class TransformationWorkspace method transform.

/**
 * Transform the instances provided through the given instance readers and
 * by default stores the result in the {@link #getTargetFolder()}.
 *
 * @param env the transformation environment
 * @param sources the instance readers
 * @param target the configuration of the target instance writer
 * @param customTarget the custom output supplier to use for the target,
 *            <code>null</code> to use the default target in thet
 *            {@link #getTargetFolder()}
 * @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)
 * @throws Exception if launching the transformation fails
 */
public ListenableFuture<Boolean> transform(TransformationEnvironment env, List<InstanceReader> sources, IOConfiguration target, LocatableOutputSupplier<? extends OutputStream> customTarget) throws Exception {
    InstanceWriter writer = (InstanceWriter) HeadlessIO.loadProvider(target);
    // output file
    if (customTarget != null) {
        writer.setTarget(customTarget);
    } else {
        File out = new File(targetFolder, "result." + getFileExtension(writer.getContentType()));
        writer.setTarget(new FileIOSupplier(out));
    }
    ListenableFuture<Boolean> result = Transformation.transform(sources, writer, env, new ReportFile(reportFile), workspace.getName());
    Futures.addCallback(result, new FutureCallback<Boolean>() {

        @Override
        public void onSuccess(Boolean result) {
            try {
                setTransformationSuccess(result);
            } catch (IOException e) {
                log.error("Failed to set transformation success for workspace", e);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            try {
                setTransformationSuccess(false);
            } catch (IOException e) {
                log.error("Failed to set transformation success for workspace", e);
            }
        }
    });
    return result;
}
Also used : InstanceWriter(eu.esdihumboldt.hale.common.instance.io.InstanceWriter) ReportFile(eu.esdihumboldt.hale.common.headless.report.ReportFile) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) IOException(java.io.IOException) ReportFile(eu.esdihumboldt.hale.common.headless.report.ReportFile) File(java.io.File)

Example 32 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class InstanceExportWizard method performValidation.

/**
 * Run the configured validators on the exported instance. May be overriden
 * to customize validation process.
 *
 * @return true if all validations were successful
 */
protected boolean performValidation() {
    boolean success = true;
    for (InstanceValidator validator : validators) {
        // set schemas
        List<? extends Locatable> schemas = getProvider().getValidationSchemas();
        validator.setSchemas(schemas.toArray(new Locatable[schemas.size()]));
        // set service provider
        validator.setServiceProvider(HaleUI.getServiceProvider());
        ExportTarget<?> exportTarget = getSelectTargetPage().getExportTarget();
        if (exportTarget instanceof FileTarget) {
            LocatableOutputSupplier<? extends OutputStream> target = getProvider().getTarget();
            List<String> fileNames = new ArrayList<>();
            if (target instanceof MultiLocationOutputSupplier) {
                for (URI location : ((MultiLocationOutputSupplier) target).getLocations()) {
                    if (!"file".equals(location.getScheme())) {
                        continue;
                    }
                    File targetFile = new File(location);
                    fileNames.add(targetFile.getAbsolutePath());
                }
            } else {
                fileNames.add(((FileTarget<?>) exportTarget).getTargetFileName());
            }
            for (String fileName : fileNames) {
                LocatableInputSupplier<? extends InputStream> source = new FileIOSupplier(new File(fileName));
                validator.setSource(source);
                validator.setContentType(getContentType());
                IOReporter defReport = validator.createReporter();
                // validate and execute provider
                try {
                    // validate configuration
                    validator.validate();
                    IOReport report = execute(validator, defReport);
                    if (report != null) {
                        // add report to report server
                        ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
                        repService.addReport(report);
                        if (report.isSuccess()) {
                            log.info(report.getSummary());
                        } else {
                            log.error(report.getSummary());
                            success = false;
                        }
                    }
                } catch (IOProviderConfigurationException e) {
                    log.error(MessageFormat.format("The validator '{0}' could not be executed", validator.getClass().getCanonicalName()), e);
                    success = false;
                }
            }
        } else {
            log.error("No input can be provided for validation (no file target)");
            success = false;
        }
    }
    if (success) {
        log.userInfo("All validations completed successfully.");
    } else {
        log.userError("There were validation failures. Please check the report for more details.");
    }
    return success;
}
Also used : InstanceValidator(eu.esdihumboldt.hale.common.instance.io.InstanceValidator) ArrayList(java.util.ArrayList) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileTarget(eu.esdihumboldt.hale.ui.io.target.FileTarget) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) MultiLocationOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.MultiLocationOutputSupplier) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) Locatable(eu.esdihumboldt.hale.common.core.io.supplier.Locatable)

Example 33 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class FileSource method getSource.

/**
 * @see AbstractProviderSource#getSource()
 */
@Override
protected LocatableInputSupplier<? extends InputStream> getSource() {
    File file = new File(sourceFile.getStringValue());
    if (file.isAbsolute())
        return new FileIOSupplier(file);
    else {
        URI relativeURI = IOUtils.relativeFileToURI(file);
        File absoluteFile = new File(projectLocation.resolve(relativeURI));
        return new FileIOSupplier(absoluteFile, relativeURI);
    }
}
Also used : FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) URI(java.net.URI)

Example 34 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier in project hale by halestudio.

the class FileTarget method updateConfiguration.

@Override
public boolean updateConfiguration(P provider) {
    try {
        URI uri = new URI(targetFile.getStringValue());
        if (!uri.isAbsolute()) {
            // was a file
            uri = new File(targetFile.getStringValue()).toURI();
        }
        final URI location = uri;
        provider.setTarget(new LocatableOutputSupplier<OutputStream>() {

            @Override
            public OutputStream getOutput() throws IOException {
                File file = new File(location);
                return new FileOutputStream(file);
            // XXX other URIs unsupported for now
            }

            @Override
            public URI getLocation() {
                return location;
            }
        });
        return true;
    } catch (URISyntaxException e) {
    // ignore, assume it's a file
    }
    File file = new File(targetFile.getStringValue());
    provider.setTarget(new FileIOSupplier(file));
    return true;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File)

Aggregations

FileIOSupplier (eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier)34 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)22 File (java.io.File)22 LogProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator)9 IOException (java.io.IOException)9 IContentType (org.eclipse.core.runtime.content.IContentType)8 URI (java.net.URI)6 AlignmentWriter (eu.esdihumboldt.hale.common.align.io.AlignmentWriter)4 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)4 DefaultSchemaSpace (eu.esdihumboldt.hale.common.schema.model.impl.DefaultSchemaSpace)4 NullProgressIndicator (eu.esdihumboldt.hale.common.core.io.impl.NullProgressIndicator)3 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)3 DefaultInputSupplier (eu.esdihumboldt.hale.common.core.io.supplier.DefaultInputSupplier)3 InstanceWriter (eu.esdihumboldt.hale.common.instance.io.InstanceWriter)3 Schema (eu.esdihumboldt.hale.common.schema.model.Schema)3 OutputStream (java.io.OutputStream)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)2 ProjectReader (eu.esdihumboldt.hale.common.core.io.project.ProjectReader)2 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)2