private void createStoreThumb(
     String dsIrodsPath,
     int width,
     int height,
     String dsname,
     boolean exists,
     List<String> thumbRels)
     throws Exception {
   String resultPath = runConvertScaleStage(dsIrodsPath, "PNG", width, height);
   String resultURI =
       ((AbstractIrodsObjectEnhancementService) service).makeIrodsURIFromPath(resultPath);
   if (!exists) {
     String message = "adding thumbnail";
     client.addManagedDatastream(
         pid,
         dsname,
         false,
         message,
         Collections.<String>emptyList(),
         "Thumbnail Image",
         false,
         "image/png",
         resultURI);
   } else {
     String message = "updating thumbnail";
     client.modifyDatastreamByReference(
         pid,
         dsname,
         false,
         message,
         new ArrayList<String>(),
         "Thumbnail Image",
         "image/png",
         null,
         null,
         resultURI);
   }
   PID newDSPID = new PID(pid.getPid() + "/" + dsname);
   if (thumbRels == null || !thumbRels.contains(newDSPID.getURI())) {
     client.setExclusiveTripleRelation(
         pid, CDRProperty.thumb.getPredicate(), CDRProperty.thumb.getNamespace(), newDSPID);
   }
   ((AbstractIrodsObjectEnhancementService) service).deleteIRODSFile(resultPath);
 }
 @Override
 public String getTargetID() {
   return pid.getPid();
 }
  @Override
  public Element call() throws EnhancementException {
    Element result = null;
    LOG.debug("Called thumbnail enhancement service for {}", pid);

    String surrogateDsUri = null;
    String surrogateDsId = null;
    PID surrogatePid = null;

    String dsLocation = null;
    String dsIrodsPath = null;

    try {
      // enqueues objects that use this one as a surrogate.
      List<PID> usesMeForSurrogate =
          this.service.getTripleStoreQueryService().fetchPIDsSurrogateFor(pid);
      for (PID usesMe : usesMeForSurrogate) {
        this.service
            .getMessageDirector()
            .direct(
                new EnhancementMessage(
                    usesMe,
                    JMSMessageUtil.servicesMessageNamespace,
                    JMSMessageUtil.ServicesActions.APPLY_SERVICE.getName(),
                    ThumbnailEnhancementService.class.getName()));
        this.service
            .getMessageDirector()
            .direct(
                new EnhancementMessage(
                    usesMe,
                    JMSMessageUtil.servicesMessageNamespace,
                    JMSMessageUtil.ServicesActions.APPLY_SERVICE.getName(),
                    SolrUpdateEnhancementService.class.getName()));
      }

      // get sourceData data stream IDs
      List<String> surrogateDSIDs = this.service.getTripleStoreQueryService().getSurrogateData(pid);
      if (surrogateDSIDs == null || surrogateDSIDs.size() < 1) {
        throw new EnhancementException(pid, "Cannot find a suitable DSID for making a thumbnail.");
      }
      surrogateDsUri = surrogateDSIDs.get(0);
      surrogateDsId = surrogateDsUri.substring(surrogateDsUri.lastIndexOf("/") + 1);
      surrogatePid = new PID(surrogateDsUri.substring(0, surrogateDsUri.lastIndexOf("/")));

      Document foxml = this.retrieveFoxml();
      Document surrogateFoxml;
      // Only retrieve the surrogate FOXML if the surrogate is a different object
      if (surrogatePid.equals(message.getPid())) {
        surrogateFoxml = foxml;
      } else {
        surrogateFoxml = client.getObjectXML(surrogatePid);
      }

      Element newestSourceDS =
          FOXMLJDOMUtil.getMostRecentDatastream(
              ContentModelHelper.Datastream.getDatastream(surrogateDsId), surrogateFoxml);

      if (newestSourceDS == null)
        throw new EnhancementException(
            "Specified source or surrogate datastream "
                + surrogateDsUri
                + " was not found, the object "
                + this.pid.getPid()
                + " is most likely invalid",
            Severity.UNRECOVERABLE);

      dsLocation =
          newestSourceDS
              .getChild("contentLocation", JDOMNamespaceUtil.FOXML_NS)
              .getAttributeValue("REF");

      LOG.debug("Source DS location: {}", dsLocation);
      if (dsLocation != null) {
        dsIrodsPath = client.getIrodsPath(dsLocation);
        LOG.debug("Making 2 Thumbnails..");

        List<String> thumbRels =
            FOXMLJDOMUtil.getRelationValues(
                ContentModelHelper.CDRProperty.thumb.getPredicate(),
                JDOMNamespaceUtil.CDR_NS,
                FOXMLJDOMUtil.getRelsExt(foxml));
        {
          String dsname = ContentModelHelper.Datastream.THUMB_SMALL.getName();
          boolean exists = client.getDatastream(pid, dsname) != null;
          createStoreThumb(dsIrodsPath, 64, 64, dsname, exists, thumbRels);
        }

        {
          String dsname = ContentModelHelper.Datastream.THUMB_LARGE.getName();
          boolean exists = client.getDatastream(pid, dsname) != null;
          createStoreThumb(dsIrodsPath, 128, 128, dsname, exists, thumbRels);
        }
      }
    } catch (EnhancementException e) {
      throw e;
    } catch (FileSystemException e) {
      throw new EnhancementException(e, Severity.FATAL);
    } catch (NotFoundException e) {
      throw new EnhancementException(e, Severity.UNRECOVERABLE);
    } catch (FedoraException e) {
      throw new EnhancementException(
          "Thumbnail Enhancement failed to process, pid: "
              + pid.getPid()
              + " surrogateDS: "
              + surrogateDsId,
          e,
          Severity.RECOVERABLE);
    } catch (Exception e) {
      throw new EnhancementException(
          "Thumbnail Enhancement failed to process, pid "
              + pid.getPid()
              + " surrogateDS: "
              + surrogateDsId,
          e,
          Severity.UNRECOVERABLE);
    }

    return result;
  }