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();
}
}
Aggregations