/**
   * Get representation files and writes them in the local disk
   *
   * @param representation
   * @return
   * @throws IOException
   * @throws DownloaderException
   */
  protected LocalRepresentationObject downloadRepresentationToLocalDisk(
      RepresentationObject representation) throws IOException, DownloaderException {

    File tempDirectory = TempDir.createUniqueDirectory("rodaSourceRep");

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

    LocalRepresentationObject localRepresentation =
        new LocalRepresentationObject(tempDirectory, representation);

    RepresentationFile rootRepFile = representation.getRootFile();
    File rootFile =
        this.rodaDownloader.saveTo(representation.getPid(), rootRepFile.getId(), tempDirectory);
    localRepresentation.getRootFile().setAccessURL(rootFile.toURI().toURL().toString());

    logger.trace("File " + rootRepFile.getId() + " saved to " + rootFile);

    for (RepresentationFile partRepFile : localRepresentation.getPartFiles()) {

      File partFile =
          this.rodaDownloader.saveTo(
              localRepresentation.getPid(), partRepFile.getId(), tempDirectory);

      partRepFile.setAccessURL(partFile.toURI().toURL().toString());

      logger.trace("File " + partRepFile.getId() + " saved to " + partFile);
    }

    return localRepresentation;
  }
  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);
    }
  }