/**
   * 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);
        }
      }
    }
  }
  /**
   * Create a preservation event the plan execution task.
   *
   * @param originalROPID the PID of the original representation.
   * @param roPID the new representation PID.
   * @param planExecutionDetails The plan execution details.
   * @param reportItem the {@link ReportItem}.
   * @return the PID of the created {@link EventPreservationObject}.
   * @throws ExecutePlanException if an error occurs creating the {@link EventPreservationObject}.
   */
  private String createPreservationEvent(
      final String originalROPID,
      final String roPID,
      final String planExecutionDetails,
      final ReportItem reportItem)
      throws ExecutePlanException {

    // Create agent
    final AgentPreservationObject agentPO = new AgentPreservationObject();
    agentPO.setAgentType(AgentPreservationObject.PRESERVATION_AGENT_TYPE_MIGRATOR);
    agentPO.setAgentName(getName() + "/" + getVersion() + " - SCAPE plan");

    try {

      final EventPreservationObject eventPO = new EventPreservationObject();

      eventPO.setEventType(EventPreservationObject.PRESERVATION_EVENT_TYPE_MIGRATION);
      eventPO.setOutcome("success");
      eventPO.setOutcomeDetailNote("Plan workflow output");
      eventPO.setOutcomeDetailExtension(planExecutionDetails);

      logger.debug("Event is " + eventPO);

      // Register derivation event
      final String epoPID =
          this.rodaClient
              .getIngestService()
              .registerDerivationEvent(originalROPID, roPID, eventPO, agentPO, true);

      logger.info("Event registration finnished. Derivation event is " + epoPID);

      reportItem.addAttribute(new Attribute("Register event - event PID", epoPID));

      return epoPID;

    } catch (NoSuchRODAObjectException e) {
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new ExecutePlanException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    } catch (IngestException e) {
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new ExecutePlanException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    } catch (RemoteException e) {
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new ExecutePlanException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    } catch (RODAClientException e) {
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new ExecutePlanException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    }
  }
  private String ingestRepresentation(RepresentationObject rObject) throws IngestException {

    String roPID = null;
    try {

      roPID = this.ingestService.createRepresentationObject(rObject);
      rObject.setPid(roPID);

      logger.info("RepresentationObject created with PID " + roPID);

    } catch (NoSuchRODAObjectException e) {
      logger.debug("Error creating representation object - " + e.getMessage(), e);
      throw new IngestException("Error creating representation object - " + e.getMessage(), e);
    } catch (RemoteException e) {
      logger.debug("Error creating representation object - " + e.getMessage(), e);
      throw new IngestException("Error creating representation object - " + e.getMessage(), e);
    }

    try {

      // Upload root file
      this.rodaUploader.uploadRepresentationFile(roPID, rObject.getRootFile());

      logger.info(
          "Root file "
              + rObject.getRootFile().getId()
              + " of representation "
              + roPID
              + " uploaded successfully.");

      // Upload part files
      if (rObject.getPartFiles() != null) {

        for (RepresentationFile partFile : rObject.getPartFiles()) {

          this.rodaUploader.uploadRepresentationFile(roPID, partFile);

          logger.info(
              "Part file "
                  + partFile.getId()
                  + " of representation "
                  + roPID
                  + " uploaded successfully.");
        }
      }

      return roPID;

    } catch (FileNotFoundException e) {
      logger.debug("Error accessing representation file - " + e.getMessage(), e);

      try {
        logger.warn("Ingest of new representation failed. Removing created object " + roPID);

        this.ingestService.removeObjects(new String[] {roPID});

      } catch (RemoteException e1) {
        logger.warn(
            "Error removing representation " + roPID + " - " + e1.getMessage() + ". IGNORING", e1);
      }

      throw new IngestException("Error accessing representation file - " + e.getMessage(), e);
    } catch (UploadException e) {

      logger.debug("Error uploading representation file - " + e.getMessage(), e);

      try {
        logger.warn("Ingest of new representation failed. Removing created object " + roPID);

        this.ingestService.removeObjects(new String[] {roPID});

      } catch (RemoteException e1) {
        logger.warn(
            "Error removing representation " + roPID + " - " + e1.getMessage() + ". IGNORING", e1);
      }

      throw new IngestException("Error uploading representation file - " + e.getMessage(), e);
    }
  }
  private ReportItem executeOn(String representationPID) throws PluginException {

    ReportItem reportItem = new ReportItem("Convertion of representation " + representationPID);
    reportItem.addAttribute(new Attribute("start datetime", DateParser.getIsoDate(new Date())));

    RepresentationObject originalRObject = null;
    try {

      reportItem.addAttribute(
          new Attribute(
              "Download original representation - start datetime",
              DateParser.getIsoDate(new Date())));

      // Get original representation from RODA Core
      originalRObject = this.browserService.getRepresentationObject(representationPID);

      logger.info("Original representation is " + originalRObject);

      reportItem.addAttribute(
          new Attribute(
              "Download original representation - finnish datetime",
              DateParser.getIsoDate(new Date())));

    } catch (BrowserException e) {
      logger.debug(
          "Error accessing representation " + representationPID + " - " + e.getMessage(), e);
      throw new PluginException(
          "Error accessing representation " + representationPID + " - " + e.getMessage(),
          e,
          reportItem);
    } catch (NoSuchRODAObjectException e) {
      logger.debug(
          "Error accessing representation " + representationPID + " - " + e.getMessage(), e);
      throw new PluginException(
          "Error accessing representation " + representationPID + " - " + e.getMessage(),
          e,
          reportItem);
    } catch (RemoteException e) {
      logger.debug(
          "Error accessing representation " + representationPID + " - " + e.getMessage(), e);
      throw new PluginException(
          "Error accessing representation " + representationPID + " - " + e.getMessage(),
          e,
          reportItem);
    }

    // Download representation files and verify if the files are already
    // normalised

    if (isRepresentationConverted(originalRObject)) {
      // No need to call the convert plugin

      try {

        String normalizedROPID =
            this.ingestService.setDONormalizedRepresentation(
                originalRObject.getDescriptionObjectPID(), originalRObject.getPid());

        logger.info(
            "Marked representation "
                + normalizedROPID //$NON-NLS-1$
                + " as normalized"); //$NON-NLS-1$

        reportItem.addAttributes(
            new Attribute("Action", "Representation was marked as normalized"));

      } catch (NoSuchRODAObjectException e) {
        reportItem.addAttributes(
            new Attribute(
                "Error",
                "Error setting representation status to normalized (" + e.getMessage() + ")"));
        throw new PluginException(
            "Error setting representation status to normalized - " + e.getMessage(), e, reportItem);
      } catch (IngestException e) {
        reportItem.addAttributes(
            new Attribute(
                "Error",
                "Error setting representation status to normalized (" + e.getMessage() + ")"));
        throw new PluginException(
            "Error setting representation status to normalized - " + e.getMessage(), e, reportItem);
      } catch (RemoteException e) {
        RODAException exception = RODAClient.parseRemoteException(e);
        reportItem.addAttributes(
            new Attribute(
                "Error",
                "Error setting representation status to normalized (" + e.getMessage() + ")"));
        throw new PluginException(
            "Error setting representation status to normalized - " + exception.getMessage(),
            exception,
            reportItem);
      }

      reportItem.addAttributes(
          new Attribute("finnish datetime", DateParser.getIsoDate(new Date())));

      return reportItem;

    } else {

      // Representation is not normalized and we need to call the
      // converter. Continue...

    }

    ConversionResult convertResult = null;
    RepresentationObject convertedRObject = null;
    try {

      reportItem.addAttribute(
          new Attribute("Conversion - start datetime", DateParser.getIsoDate(new Date())));

      logger.info("Converting representation " + originalRObject.getPid());

      // Calling converter
      convertResult = convert(originalRObject);
      convertedRObject = convertResult.getRepresentation();

      logger.info("Convert " + originalRObject.getPid() + " finished");

      reportItem.addAttribute(
          new Attribute("Conversion - finnish datetime", DateParser.getIsoDate(new Date())));
      reportItem.addAttribute(
          new Attribute(
              "Conversion - converted representation",
              convertResult.getRepresentation().toString()));

      if (convertResult.getMigrationEvent() == null) {

        logger.warn("Migration event is null");

      } else {

        logger.info(
            "Conversion outcome is "
                + convertResult.getMigrationEvent().getOutcomeDetailExtension());

        logger.info("Converted representation is " + convertedRObject);

        reportItem.addAttribute(
            new Attribute(
                "Conversion - outcome details",
                convertResult.getMigrationEvent().getOutcomeDetailExtension()));
      }

    } catch (RepresentationConverterException e) {
      logger.debug(
          "Error converting representation " + representationPID + " - " + e.getMessage(), e);
      e.setReportItem(reportItem);
      throw e;
    }

    RepresentationObject writtenRO = null;
    String roPID = null;
    try {

      // Create temporary directory
      File temporaryDirectory = TempDir.createUniqueTemporaryDirectory("rep");

      reportItem.addAttributes(
          new Attribute(
              "Download converted representation - temporary directory",
              temporaryDirectory.toString()),
          new Attribute(
              "Download converted representation - start datetime",
              DateParser.getIsoDate(new Date())));

      logger.info("Writting converted representation to " + temporaryDirectory);

      // Write representation to temporary directory
      writtenRO =
          this.migratorClient.writeRepresentationObject(convertedRObject, temporaryDirectory);

      logger.info("Representation written");

      reportItem.addAttribute(
          new Attribute(
              "Download converted representation - finnish datetime",
              DateParser.getIsoDate(new Date())));

      reportItem.addAttribute(
          new Attribute("Ingest - start datetime", DateParser.getIsoDate(new Date())));

      logger.info("Ingesting converted representation");

      if (isNormalization()) {
        logger.info(
            "This is a normalization process. Setting representation status to "
                + RepresentationObject.STATUS_NORMALIZED);
        writtenRO.setStatuses(new String[] {RepresentationObject.STATUS_NORMALIZED});
      } else {
        logger.info(
            "This is NOT a normalization process. Setting representation status to "
                + RepresentationObject.STATUS_ALTERNATIVE);
        writtenRO.setStatuses(new String[] {RepresentationObject.STATUS_ALTERNATIVE});
      }

      // Ingest converted representation
      roPID = ingestRepresentation(writtenRO);

      logger.info("Representation ingested with PID " + roPID);

      reportItem.addAttribute(
          new Attribute("Ingest - finnish datetime", DateParser.getIsoDate(new Date())));
      reportItem.addAttribute(new Attribute("Ingest - ingested representation PID", roPID));

    } catch (MigratorClientException e) {
      logger.debug("Error downloading converted representation - " + e.getMessage(), e);
      throw new PluginException(
          "Error downloading converted representation  - " + e.getMessage(), e, reportItem);
    } catch (IOException e) {
      logger.debug("Error downloading converted representation - " + e.getMessage(), e);
      throw new PluginException(
          "Error downloading converted representation  - " + e.getMessage(), e, reportItem);
    } catch (IngestException e) {
      logger.debug("Error downloading converted representation - " + e.getMessage(), e);
      throw new PluginException(
          "Error downloading converted representation  - " + e.getMessage(), e, reportItem);
    }

    AgentPreservationObject agentPO = null;
    try {

      logger.info("Registering derivation event");

      reportItem.addAttribute(
          new Attribute("Register event - start datetime", DateParser.getIsoDate(new Date())));

      // Getting converter Agent
      AgentPreservationObject migratorAgent = getConverter().getAgent();

      reportItem.addAttribute(
          new Attribute("Register event - converter agent", migratorAgent.toString()));

      agentPO = new AgentPreservationObject();
      agentPO.setAgentType(AgentPreservationObject.PRESERVATION_AGENT_TYPE_MIGRATOR);
      agentPO.setAgentName(getName() + "/" + getVersion() + " - " + migratorAgent.getAgentName());

      logger.info("Agent is " + agentPO);

      reportItem.addAttribute(new Attribute("Register event - event agent", agentPO.toString()));

    } catch (Exception e) {
      // getConverter().getAgent();
      logger.debug("Error getting converter agent - " + e.getMessage(), e);

      // Delete roPID
      // delete(roPID);
      // try {
      // logger
      // .warn("Ingest of new representation failed. Removing created object "
      // + roPID);
      //
      // this.ingestService.removeObjects(new String[] { roPID });
      //
      // } catch (RemoteException e1) {
      // logger.warn("Error removing representation " + roPID + " - "
      // + e1.getMessage() + ". IGNORING", e1);
      // }

      throw new PluginException("Error getting converter agent - " + e.getMessage(), e, reportItem);
    }

    try {

      EventPreservationObject eventPO = convertResult.getMigrationEvent();

      if (eventPO == null) {
        eventPO = new EventPreservationObject();
      }

      if (isNormalization()) {
        eventPO.setEventType(EventPreservationObject.PRESERVATION_EVENT_TYPE_NORMALIZATION);
      } else {
        eventPO.setEventType(EventPreservationObject.PRESERVATION_EVENT_TYPE_MIGRATION);
      }
      if (StringUtils.isBlank(eventPO.getOutcome())) {
        eventPO.setOutcome("success");
      }
      if (StringUtils.isBlank(eventPO.getOutcomeDetailNote())) {
        eventPO.setOutcomeDetailNote("Converter details");
      }
      if (StringUtils.isBlank(eventPO.getOutcomeDetailExtension())) {
        eventPO.setOutcomeDetailExtension("no details");
      }

      logger.info("Event is " + eventPO);

      // reportItem.addAttribute(new Attribute(
      // "Register event - event outcome details", eventPO
      // .getOutcomeDetailExtension()));

      logger.info("Calling registerDerivationEvent(...)");

      // Register derivation event
      String epoPID =
          this.ingestService.registerDerivationEvent(
              originalRObject.getPid(), roPID, eventPO, agentPO, getParameterMakeObjectsActive());

      logger.info("Event registration finnished. Derivation event is " + epoPID);

      reportItem.addAttributes(
          new Attribute("Register event - event PID", epoPID),
          new Attribute("finnish datetime", DateParser.getIsoDate(new Date())));

      return reportItem;

    } catch (NoSuchRODAObjectException e) {
      // registerDerivationEvent(...)
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new PluginException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    } catch (IngestException e) {
      // registerDerivationEvent(...)
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new PluginException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    } catch (RemoteException e) {
      // registerDerivationEvent(...)
      logger.debug("Error registering convertion event - " + e.getMessage(), e);
      throw new PluginException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    }
  }