Search in sources :

Example 1 with LocatableInputSupplier

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