private String createDerivedRepresentation(
      final String roPID,
      final Map<String, WorkflowExecutionOutput> outputs,
      final ReportItem reportItem)
      throws ExecutePlanException {

    logger.trace(String.format("createNewRepresentation(%s, %s)", roPID, outputs));

    RepresentationObject roOriginal = null;
    LocalRepresentationObject roLocalDerived = null;
    final StringBuilder sbExecDetails = new StringBuilder();

    try {

      roOriginal = rodaClient.getBrowserService().getRepresentationObject(roPID);

      roLocalDerived = downloadRepresentationToLocalDisk(roOriginal);
      roLocalDerived.setId(DateParser.getIsoDate(new Date()));
      roLocalDerived.setStatuses(new String[] {RepresentationObject.STATUS_NORMALIZED});

      sbExecDetails.append(
          String.format("<planExecutionDetails plan=\"%s\">%n", planFile.getName()));

      // Check if root file was changed
      if (outputs.containsKey(roLocalDerived.getRootFile().getId())) {
        final WorkflowExecutionOutput output = outputs.get(roLocalDerived.getRootFile().getId());
        updateFile(roLocalDerived.getRootFile(), output);
        sbExecDetails.append(
            getPlanExecutionDetailsForFile(roLocalDerived.getRootFile().getId(), output));
      }

      if (roLocalDerived.getPartFiles() != null) {
        for (RepresentationFile rFile : roLocalDerived.getPartFiles()) {
          if (outputs.containsKey(rFile.getId())) {
            final WorkflowExecutionOutput output = outputs.get(rFile.getId());
            updateFile(rFile, output);
            sbExecDetails.append(getPlanExecutionDetailsForFile(rFile.getId(), output));
          }
        }
      }

      sbExecDetails.append(String.format("</planExecutionDetails>%n"));

      roLocalDerived.setType(RepresentationObject.DIGITALIZED_WORK);
      final String subtype = RepresentationBuilder.getRepresentationSubtype(roLocalDerived);
      roLocalDerived.setSubType(subtype);

    } catch (RODAException e) {
      deleteTemporaryLocalRepresentation(roLocalDerived);
      logger.error(e.getMessage(), e);
      throw new ExecutePlanException(e.getMessage(), e);
    } catch (RemoteException e) {
      deleteTemporaryLocalRepresentation(roLocalDerived);
      logger.error(e.getMessage(), e);
      throw new ExecutePlanException(e.getMessage(), e);
    } catch (IOException e) {
      deleteTemporaryLocalRepresentation(roLocalDerived);
      logger.error(e.getMessage(), e);
      throw new ExecutePlanException(e.getMessage(), e);
    }

    String derivedROPID = null;
    try {

      derivedROPID = ingestRepresentation(roLocalDerived);
      reportItem.addAttribute(new Attribute("Derived representation PID", derivedROPID));

    } catch (IngestException e) {

      logger.error("Error ingesting new representation - " + e.getMessage(), e);
      throw new ExecutePlanException("Error ingesting new representation - " + e.getMessage(), e);

    } finally {

      deleteTemporaryLocalRepresentation(roLocalDerived);
    }

    try {

      final String epoPID =
          createPreservationEvent(
              roOriginal.getPid(), derivedROPID, sbExecDetails.toString(), reportItem);
      reportItem.addAttribute(new Attribute("Derivation event PID", epoPID));

    } catch (ExecutePlanException e) {
      logger.debug("Error registering convertion event - " + e.getMessage(), e);

      try {
        logger.warn("Error registering convertion event. Removing created object " + derivedROPID);

        this.rodaClient.getIngestService().removeObjects(new String[] {derivedROPID});

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

      throw new ExecutePlanException(
          "Error registering convertion event - " + e.getMessage(), e, reportItem);
    }

    return derivedROPID;
  }
  /** @param args */
  public static void main(String[] args) {

    try {

      RODAClient rodaClient = null;

      if (args.length == 3) {

        // http://localhost:8180/
        String hostUrl = args[0];
        String username = args[1];
        String password = args[2];
        rodaClient = new RODAClient(new URL(hostUrl), username, password);

      } else {
        System.err.println(
            BrowserSimpleROTest.class.getSimpleName()
                + " protocol://hostname:port/core-service [username password]");
        System.exit(1);
      }

      Browser browserService = rodaClient.getBrowserService();

      System.out.println("\n**************************************");
      System.out.println("Number of Event Preservation Objects");
      System.out.println("**************************************");

      int count = browserService.getSimpleEventPreservationObjectCount(null);
      System.out.println(count + " event preservation objects in the repository");

      System.out.println("\n**************************************");
      System.out.println("Number of Event Preservation Objects (Inactive)");
      System.out.println("**************************************");

      Filter filterInactive = new Filter();
      filterInactive.add(new SimpleFilterParameter("state", RODAObject.STATE_INACTIVE));

      count = browserService.getSimpleEventPreservationObjectCount(filterInactive);
      System.out.println(count + " inactive event preservation objects in the repository");

      SimpleEventPreservationObject[] simpleEPOs =
          browserService.getSimpleEventPreservationObjects(null);

      System.out.println("\n**************************************");
      System.out.println("List of Event Preservation Objects");
      System.out.println("**************************************");

      for (int i = 0; simpleEPOs != null && i < simpleEPOs.length; i++) {
        System.out.println(simpleEPOs[i]);
      }

      if (simpleEPOs != null && simpleEPOs.length > 0) {

        System.out.println("\n*********************************************");
        System.out.println(
            "Getting EventPreservationObject of the first representation ("
                + simpleEPOs[0].getPid()
                + ")");
        System.out.println("*********************************************");

        EventPreservationObject rObject =
            browserService.getEventPreservationObject(simpleEPOs[0].getPid());
        System.out.println(rObject);
      }

    } catch (Throwable e) {
      e.printStackTrace();
      if (e.getCause() != null) {
        System.err.println("Cause exception:");
        e.getCause().printStackTrace();
      }
    }
  }