Ejemplo n.º 1
0
  /**
   * Executes the given workflow in the specified representation file.
   *
   * @param workflowFile the workflow {@link File}
   * @param representationID the representation ID
   * @param fileID the file ID
   * @return a WorkflowExecutionOutput with the output directory and command output.
   * @throws ExecutePlanException if an error occurs during the execution
   */
  private WorkflowExecutionOutput executeWorkflowInFile(
      final File workflowFile, final String representationID, final String fileID)
      throws ExecutePlanException {

    logger.info("Executing workflow in file " + representationID + "/" + fileID);

    File inputValueFile = null;
    try {

      // Write the downloaded stream to a temporary file in the disk
      final InputStream inputStream = rodaDownloader.get(representationID, fileID);
      inputValueFile = File.createTempFile(representationID + "_" + fileID + "_", "");
      IOUtils.copyLarge(inputStream, new FileOutputStream(inputValueFile));

      final String outputDir = inputValueFile.getAbsolutePath() + "_taverna/";

      final String executionOutput =
          CommandUtility.execute(
              taverna_bin,
              "-outputdir",
              outputDir,
              "-inputvalue",
              workflowInputPort,
              inputValueFile.getAbsolutePath(),
              workflowFile.getAbsolutePath());

      logger.info("Workflow executed with sucess!");
      logger.debug("Command output: " + executionOutput);

      return new WorkflowExecutionOutput(outputDir, executionOutput);

    } catch (NoSuchRODAObjectException e) {
      logger.error("Error getting file - " + e.getMessage());
      throw new ExecutePlanException("Error getting file " + fileID + " - " + e.getMessage(), e);
    } catch (DownloaderException e) {
      logger.error("Error getting file - " + e.getMessage());
      throw new ExecutePlanException("Error getting file " + fileID + " - " + e.getMessage(), e);
    } catch (IOException e) {
      logger.error("Error getting file - " + e.getMessage());
      throw new ExecutePlanException("Error getting file " + fileID + " - " + e.getMessage(), e);
    } catch (CommandException e) {
      logger.error("Error executing taverna workflow in file " + fileID + " - " + e.getMessage());
      logger.debug("Command output: " + e.getOutput());
      throw new ExecutePlanException(
          "Error executing taverna workflow in file " + fileID + " - " + e.getMessage(), e);
    } finally {

      if (inputValueFile != null) {
        if (FileUtils.deleteQuietly(inputValueFile)) {
          logger.debug("Deleted temporary file " + inputValueFile);
        } else {
          logger.warn("Error deleting temporary file " + inputValueFile);
        }
      }
    }
  }
  protected String getVersion() throws ConverterException {
    try {

      String version = getClass().getName() + "/" + VERSION + " - ";

      String executableVersion = CommandUtility.execute(this.office2pdfExecutable, "--version");

      return version + executableVersion;

    } catch (CommandException e) {
      logger.warn("Exception getting Doc2Pdf version - " + e.getMessage(), e);
      throw new ConverterException("Exception getting Doc2Pdf version - " + e.getMessage(), e);
    }
  }
  protected RepresentationFile convertRootFile(
      RepresentationFile originalRootFile, File tempDirectory, StringBuffer report)
      throws IOException, CommandException {

    // /tmp/original.../F0
    File originalFile = new File(URI.create(originalRootFile.getAccessURL()));
    // Detect extension by DROID
    String origNameWithExt = originalRootFile.getOriginalName();
    FileFormat detectedFileFormat = FormatUtility.getFileFormat(originalFile, origNameWithExt);
    if ((detectedFileFormat != null)
        && (detectedFileFormat.getExtensions() != null)
        && (detectedFileFormat.getExtensions().length > 0)) {
      origNameWithExt = origNameWithExt + "." + detectedFileFormat.getExtensions()[0];
    }
    // /tmp/original.../originalName.doc
    File originalFileWithExtension = new File(originalFile.getParentFile(), origNameWithExt);
    // mv /tmp/original.../F0 /tmp/original.../originalName.doc
    FileUtils.moveFile(originalFile, originalFileWithExtension);

    // /tmp/converted.../F0
    File convertedFile = new File(tempDirectory, originalFile.getName());
    // /tmp/converted.../originalName.doc.pdf
    File convertedFileWithExtension =
        new File(tempDirectory, originalRootFile.getOriginalName() + this.formatExtension);

    // Execute the command
    List<String> commandArgs = new ArrayList<String>();
    commandArgs.add(this.office2pdfExecutable);
    commandArgs.add(originalFileWithExtension.getAbsolutePath());
    commandArgs.add(convertedFileWithExtension.getAbsolutePath());

    String convertOutput = CommandUtility.execute(commandArgs);

    String commandline = StringUtility.join(commandArgs, " "); // $NON-NLS-1$

    String message;
    if (StringUtils.isBlank(convertOutput)) {
      message =
          String.format(
              "%s: %s => %s%n(Command: %s)%n%n", //$NON-NLS-1$
              originalRootFile.getId(),
              originalRootFile.getOriginalName(),
              convertedFileWithExtension.getName(),
              commandline);
    } else {
      message =
          String.format(
              "%s: %s => %s (%s)%n(Command: %s)%n%n",
              originalRootFile //$NON-NLS-1$
                  .getId(),
              originalRootFile.getOriginalName(),
              convertedFileWithExtension.getName(),
              convertOutput,
              commandline);
    }

    logger.trace(message);
    report.append(message);

    // mv /tmp/converted.../originalName.doc.pdf /tmp/converted.../F0
    FileUtils.moveFile(convertedFileWithExtension, convertedFile);

    RepresentationFile convertedRootFile = new RepresentationFile(originalRootFile);

    // convertedRootFile.setMimetype(format);
    convertedRootFile.importFileFormat(
        FormatUtility.getFileFormat(convertedFile, convertedFileWithExtension.getName()));
    convertedRootFile.setSize(convertedFile.length());
    convertedRootFile.setAccessURL(convertedFile.getAbsolutePath());
    convertedRootFile.setOriginalName(convertedRootFile.getOriginalName() + this.formatExtension);

    return convertedRootFile;
  }