@Override
public MigrationResult migrate(Alternative alternative, DigitalObject digitalObject) throws PlatoException {
PreservationActionDefinition action = alternative.getAction();
SSHTavernaExecutor tavernaExecutor = new SSHTavernaExecutor();
tavernaExecutor.init();
MigrationResult result = new MigrationResult();
HashMap<String, Object> inputData = new HashMap<String, Object>();
WorkflowDescription workflowDescription = MyExperimentRESTClient.getWorkflow(action.getDescriptor());
workflowDescription.readMetadata();
if (!workflowDescription.getProfile().equals("http://purl.org/DP/components#MigrationAction")) {
result.setSuccessful(false);
result.setReport("The workflow " + action.getUrl() + " is no MigrationAction.");
return result;
}
List<Port> inputPorts = workflowDescription.getInputPorts();
for (Port p : inputPorts) {
if (ComponentConstants.VALUE_SOURCE_OBJECT.equals(p.getValue())) {
inputData.put(p.getName(),
tavernaExecutor.new ByteArraySourceFile(FileUtils.makeFilename(digitalObject.getFullname()),
digitalObject.getData().getData()));
} else if (ComponentConstants.VALUE_PARAMETER.equals(p.getValue()) || p.getValue() == null) {
String value = action.getParamByName(p.getName());
if (value == null) {
value = "";
}
inputData.put(p.getName(), value);
} else {
result.setSuccessful(false);
result.setReport("The workflow " + action.getUrl() + " has unsupported port " + p.getName()
+ " that accepts " + p.getValue());
return result;
}
}
tavernaExecutor.setInputData(inputData);
// Workflow
tavernaExecutor.setWorkflow(action.getUrl());
// Output ports to receive
List<Port> outputPorts = workflowDescription.getOutputPorts();
Set<String> outputPortNames = new HashSet<String>(outputPorts.size());
String targetPathPort = null;
for (Port p : outputPorts) {
outputPortNames.add(p.getName());
if (ComponentConstants.VALUE_TARGET_OBJECT.equals(p.getValue())) {
targetPathPort = p.getName();
}
}
tavernaExecutor.setOutputPorts(outputPortNames);
// Output files
if (targetPathPort == null) {
result.setSuccessful(false);
result.setReport("The workflow " + action.getUrl() + " has not target port.");
return result;
}
HashMap<String, SSHInMemoryTempFile> requestedFiles = new HashMap<String, SSHInMemoryTempFile>(1);
SSHInMemoryTempFile tempFile = new SSHInMemoryTempFile();
requestedFiles.put(targetPathPort, tempFile);
tavernaExecutor.setOutputFiles(requestedFiles);
// Execute
try {
tavernaExecutor.execute();
} catch (IOException e) {
result.setSuccessful(false);
result.setReport("Error connecting to execution server");
log.error("Error connecting to execution server", e);
return result;
} catch (TavernaExecutorException e) {
result.setSuccessful(false);
result.setReport("Error executing taverna workflow");
log.error("Error executing taverna workflow", e);
return result;
}
result.setSuccessful(true);
result.setReport(tavernaExecutor.getOutputDoc());
Map<String, ?> outputFiles = tavernaExecutor.getOutputFiles();
DigitalObject u = new DigitalObject();
for (Entry<String, ?> entry : outputFiles.entrySet()) {
SSHInMemoryTempFile resultFile = (SSHInMemoryTempFile) entry.getValue();
u.getData().setData(resultFile.getData());