use of eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace in project hale by halestudio.
the class StatusPage method addControls.
@Override
protected void addControls(boolean loggedIn) {
super.addControls(loggedIn);
final String workspaceId = getPageParameters().get(PARAMETER_WORKSPACE).toOptionalString();
if (workspaceId == null || workspaceId.isEmpty()) {
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Workspace ID not specified.");
}
try {
workspaces.getWorkspaceFolder(workspaceId);
} catch (FileNotFoundException e) {
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Workspace does not exist.");
}
final IModel<TransformationWorkspace> workspace = new LoadableDetachableModel<TransformationWorkspace>() {
private static final long serialVersionUID = 2600444242247550094L;
@Override
protected TransformationWorkspace load() {
return new TransformationWorkspace(workspaceId);
}
};
// job panel
final Serializable family = AbstractTransformationJob.createFamily(workspaceId);
final JobPanel jobs = new JobPanel("jobs", family, true);
add(jobs);
// status
final Label status = new Label("status", new LoadableDetachableModel<String>() {
private static final long serialVersionUID = -4351763182104835300L;
@Override
protected String load() {
if (workspace.getObject().isTransformationFinished()) {
if (workspace.getObject().isTransformationSuccessful()) {
return "Transformation completed.";
} else {
return "Transformation failed.";
}
} else {
if (Job.getJobManager().find(family).length > 0) {
return "Transformation is running:";
} else {
return "No transformation running.";
}
}
}
});
status.setOutputMarkupId(true);
add(status);
// result
final WebMarkupContainer result = new WebMarkupContainer("result");
result.setOutputMarkupId(true);
add(result);
final WebMarkupContainer update = new WebMarkupContainer("update") {
private static final long serialVersionUID = -2591480922683644827L;
@Override
public boolean isVisible() {
return workspace.getObject().isTransformationFinished();
}
};
result.add(update);
// result : report
File reportFile = workspace.getObject().getReportFile();
DownloadLink report = new DownloadLink("log", reportFile, reportFile.getName());
update.add(report);
// result : file list
IModel<? extends List<File>> resultFilesModel = new LoadableDetachableModel<List<File>>() {
private static final long serialVersionUID = -7971872898614031331L;
@Override
protected List<File> load() {
return Arrays.asList(workspace.getObject().getTargetFolder().listFiles());
}
};
final ListView<File> fileList = new ListView<File>("file", resultFilesModel) {
private static final long serialVersionUID = -8045643864251639540L;
@Override
protected void populateItem(ListItem<File> item) {
// download link
DownloadLink download = new DownloadLink("download", item.getModelObject(), item.getModelObject().getName());
item.add(download);
// file name
download.add(new Label("name", item.getModelObject().getName()));
}
};
update.add(fileList);
// leaseEnd
String leaseEnd;
try {
leaseEnd = workspaces.getLeaseEnd(workspaceId).toString(DateTimeFormat.mediumDateTime());
} catch (IOException e) {
leaseEnd = "unknown";
}
add(new Label("leaseEnd", leaseEnd));
boolean transformationFinished = workspace.getObject().isTransformationFinished();
if (transformationFinished) {
// disable job timer
jobs.getTimer().stopOnNextUpdate();
} else {
// timer
add(new AbstractAjaxTimerBehavior(Duration.milliseconds(1500)) {
private static final long serialVersionUID = -3726349470723024150L;
@Override
protected void onTimer(AjaxRequestTarget target) {
if (workspace.getObject().isTransformationFinished()) {
// update status and result
target.add(status);
target.add(result);
// stop timers
stop(target);
jobs.getTimer().stopOnNextUpdate();
}
}
});
}
}
use of eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace 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