Search in sources :

Example 16 with FileIOSupplier

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

the class ArchiveProjectReader method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    // copy resources to a temporary directory
    tempDir = Files.createTempDir();
    IOUtils.extract(tempDir, getSource().getInput());
    // create the project file via XMLProjectReader
    File baseFile = new File(tempDir, "project.halex");
    ProjectReader reader;
    if (!baseFile.exists()) {
        // look for project file
        log.debug("Default project file not found, looking for other candidates.");
        String candidate = ProjectIO.findProjectFile(tempDir);
        if (candidate != null) {
            log.info(MessageFormat.format("Loading {0} as project file from archive", candidate));
            baseFile = new File(tempDir, candidate);
        }
        reader = HaleIO.findIOProvider(ProjectReader.class, new FileIOSupplier(baseFile), candidate);
        if (reader == null) {
            reporter.error(new IOMessageImpl("Could not find reader for project file " + candidate, null));
            reporter.setSuccess(false);
            return reporter;
        }
    } else {
        // default project file
        reader = new XMLProjectReader();
    }
    LocatableInputSupplier<InputStream> tempSource = new FileIOSupplier(baseFile);
    // save old save configuration
    LocatableInputSupplier<? extends InputStream> oldSource = getSource();
    setSource(tempSource);
    reader.setSource(tempSource);
    reader.setProjectFiles(getProjectFiles());
    IOReport report = reader.execute(progress);
    Project readProject = reader.getProject();
    if (readProject != null) {
        /*
			 * Because the original source is only available here, update the
			 * project's resource paths here.
			 * 
			 * The only drawback is that the UILocationUpdater cannot be used.
			 */
        LocationUpdater updater = new LocationUpdater(readProject, tempSource.getLocation());
        // update resources
        // resources are made absolute (else they can't be found afterwards)
        updater.updateProject(false);
    }
    // set the real source
    setSource(oldSource);
    // set the read project
    setProjectChecked(readProject, reporter);
    return report;
}
Also used : Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) LocationUpdater(eu.esdihumboldt.hale.common.core.io.project.util.LocationUpdater) InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File) ProjectReader(eu.esdihumboldt.hale.common.core.io.project.ProjectReader)

Example 17 with FileIOSupplier

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

the class MsAccessDataReaderTestSuit method readSchema.

/**
 * Reads a schema from a MsAccess database file.
 *
 * @param sourceFile the file of the source database.
 * @return the schema
 * @throws Exception any exception thrown by {@link MsAccessSchemaReader}
 */
public Schema readSchema(File sourceFile) throws Exception {
    MsAccessSchemaReader schemaReader = new MsAccessSchemaReader();
    schemaReader.setSource(new FileIOSupplier(sourceFile));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_USER, Value.of(USER_NAME));
    schemaReader.setParameter(JDBCSchemaReader.PARAM_PASSWORD, Value.of(PASSWORD));
    IOReport report = schemaReader.execute(new LogProgressIndicator());
    assertTrue(report.isSuccess());
    return schemaReader.getSchema();
}
Also used : IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) LogProgressIndicator(eu.esdihumboldt.hale.common.core.io.impl.LogProgressIndicator) MsAccessSchemaReader(eu.esdihumboldt.hale.io.jdbc.msaccess.MsAccessSchemaReader)

Example 18 with FileIOSupplier

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

the class SpatiaLiteInstanceWriter method loadConfiguration.

@Override
public void loadConfiguration(Map<String, Value> configuration) {
    super.loadConfiguration(configuration);
    Value target = configuration.get(PARAM_TARGET);
    if (target != null && !target.isEmpty()) {
        File file = new File(URI.create(target.as(String.class)));
        setTarget(new FileIOSupplier(file));
    }
}
Also used : Value(eu.esdihumboldt.hale.common.core.io.Value) FileIOSupplier(eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier) File(java.io.File)

Example 19 with FileIOSupplier

use of eu.esdihumboldt.hale.common.core.io.supplier.FileIOSupplier 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 20 with FileIOSupplier

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

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