/**
   * 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 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);
    }
  }
  /**
   * @throws ConverterException
   * @see SynchronousConverter#convert(RepresentationObject)
   */
  public ConversionResult convert(RepresentationObject representation)
      throws RepresentationAlreadyConvertedException, InvalidRepresentationException,
          WrongRepresentationTypeException, WrongRepresentationSubtypeException,
          ConverterException {

    UUID uuid = UUID.randomUUID();
    File finalDirectory = new File(getCacheDirectory(), uuid.toString());

    StringBuffer report = new StringBuffer();

    LocalRepresentationObject localRepresentation = null;
    try {

      localRepresentation = downloadRepresentationToLocalDisk(representation);

      logger.trace("Representation downloaded " + localRepresentation);

    } catch (DownloaderException e) {
      logger.debug("Exception downloading representation files - " + e.getMessage(), e);
      throw new ConverterException(
          "Exception downloading representation files - " + e.getMessage(), e);
    } catch (IOException e) {
      logger.debug("Exception downloading representation files - " + e.getMessage(), e);
      throw new ConverterException(
          "Exception downloading representation files - " + e.getMessage(), e);
    }

    File tempDirectory = null;
    try {

      tempDirectory = TempDir.createUniqueDirectory("convertedRep");

      logger.debug("Saving converted representation files to " + tempDirectory);

    } catch (IOException e) {
      logger.debug("Error creating directory for converted representation - " + e.getMessage(), e);
      throw new ConverterException(
          "Error creating directory for converted representation - " + e.getMessage(), e);
    }

    LocalRepresentationObject convertedRepresentation = null;
    try {
      // Create a new RepresentationObject that is a copy of source
      // RepresentationObject
      convertedRepresentation = new LocalRepresentationObject(tempDirectory, localRepresentation);

      // Convert Root File
      RepresentationFile convertedRootFile =
          convertRootFile(localRepresentation.getRootFile(), tempDirectory, report);

      convertedRepresentation.setRootFile(convertedRootFile);

      String subtype = RepresentationBuilder.getRepresentationSubtype(convertedRepresentation);
      convertedRepresentation.setSubType(subtype);

    } catch (IOException e) {
      logger.debug("Error creating directory for converted representation - " + e.getMessage(), e);
      throw new ConverterException(
          "Error creating directory for converted representation - " + e.getMessage(), e);
    } catch (CommandException e) {
      logger.debug("Error converting representation - " + e.getMessage(), e);
      throw new ConverterException("Error converting representation - " + e.getMessage(), e);
    }

    try {

      moveToFinalDirectory(convertedRepresentation, finalDirectory);

    } catch (IOException e) {
      logger.debug("Error moving representation to cache - " + e.getMessage(), e);
      throw new ConverterException("Error moving representation to cache - " + e.getMessage(), e);
    }

    EventPreservationObject eventPO = new EventPreservationObject();
    eventPO.setOutcome("success");
    eventPO.setOutcomeDetailNote("converter outcome details");
    eventPO.setOutcomeDetailExtension(report.toString());

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

    return new ConversionResult(convertedRepresentation, eventPO, getAgent());
  }