/**
   * Create Fast Track Package.
   *
   * @param packageId The id of the package to start FastTrack
   * @param signers The signers to get the signing url
   * @return The signing url
   */
  public String startFastTrack(PackageId packageId, List<FastTrackSigner> signers) {
    String token = getFastTrackToken(packageId, true);
    String path =
        template.urlFor(UrlTemplate.START_FAST_TRACK_PATH).replace("{token}", token).build();

    List<FastTrackRole> roles = new ArrayList<FastTrackRole>();
    for (FastTrackSigner signer : signers) {
      FastTrackRole role =
          FastTrackRoleBuilder.newRoleWithId(signer.getId())
              .withName(signer.getId())
              .withSigner(signer)
              .build();
      roles.add(role);
    }

    String json = Serialization.toJson(roles);
    try {
      String response = client.post(path, json);
      SigningUrl signingUrl = Serialization.fromJson(response, SigningUrl.class);
      return signingUrl.getUrl();
    } catch (RequestException e) {
      throw new EslException("Could not start fast track.", e);
    } catch (Exception e) {
      throw new EslException("Could not start fast track." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get the document's metadata from the package.
   *
   * @param documentPackage The DocumentPackage we want to get document from.
   * @param documentId Id of document to get.
   * @return the document's metadata
   */
  public com.silanis.esl.sdk.Document getDocumentMetadata(
      DocumentPackage documentPackage, String documentId) {
    String path =
        template
            .urlFor(UrlTemplate.DOCUMENT_ID_PATH)
            .replace("{packageId}", documentPackage.getId().getId())
            .replace("{documentId}", documentId)
            .build();

    try {
      String response = client.get(path);
      Document apilDocument = Serialization.fromJson(response, Document.class);

      // Wipe out the members not related to the metadata
      apilDocument.setApprovals(new ArrayList<Approval>());
      apilDocument.setFields(new ArrayList<Field>());
      apilDocument.setPages(new ArrayList<com.silanis.esl.api.model.Page>());

      return new DocumentConverter(
              apilDocument, new DocumentPackageConverter(documentPackage).toAPIPackage())
          .toSDKDocument();
    } catch (RequestException e) {
      throw new EslServerException("Could not get the document's metadata.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get the document's metadata." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get Journal Entries.
   *
   * @param userId The ID of the user whose e-journal entries are being retrieved.
   * @return all of the user's notary e-journal entries.
   */
  public List<com.silanis.esl.sdk.NotaryJournalEntry> getJournalEntries(String userId) {
    List<com.silanis.esl.sdk.NotaryJournalEntry> result =
        new ArrayList<com.silanis.esl.sdk.NotaryJournalEntry>();
    String path =
        template.urlFor(UrlTemplate.NOTARY_JOURNAL_PATH).replace("{userId}", userId).build();

    try {
      String stringResponse = client.get(path);
      Result<com.silanis.esl.api.model.NotaryJournalEntry> apiResponse =
          JacksonUtil.deserialize(
              stringResponse,
              new TypeReference<Result<com.silanis.esl.api.model.NotaryJournalEntry>>() {});
      for (com.silanis.esl.api.model.NotaryJournalEntry apiNotaryJournalEntry :
          apiResponse.getResults()) {
        result.add(
            new NotaryJournalEntryConverter(apiNotaryJournalEntry).toSDKNotaryJournalEntry());
      }
      return result;

    } catch (RequestException e) {
      throw new EslException("Could not get Journal Entries.", e);
    } catch (Exception e) {
      throw new EslException("Could not get Journal Entries." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Returns a Page of DocumentPackages, that last updated in time range, which represents a
   * paginated query response. Important once you have many DocumentPackages.
   *
   * @param status Returned DocumentPackages must have their status set to this value to be included
   *     in the result set
   * @param request Identifying which page of results to return
   * @param from Date range starting from this date included
   * @param to Date range ending of this date included
   * @return List of DocumentPackages that populate the specified page
   */
  public Page<DocumentPackage> getUpdatedPackagesWithinDateRange(
      PackageStatus status, PageRequest request, Date from, Date to) {
    String fromDate = DateHelper.dateToIsoUtcFormat(from);
    String toDate = DateHelper.dateToIsoUtcFormat(to);

    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_LIST_STATUS_DATE_RANGE_PATH)
            .replace("{status}", new PackageStatusConverter(status).toAPIPackageStatus())
            .replace("{from}", Integer.toString(request.getFrom()))
            .replace("{to}", Integer.toString(request.to()))
            .replace("{lastUpdatedStartDate}", fromDate)
            .replace("{lastUpdatedEndDate}", toDate)
            .build();

    try {
      String response = client.get(path);
      Result<Package> results =
          JacksonUtil.deserialize(response, new TypeReference<Result<Package>>() {});
      return convertToPage(results, request);
    } catch (RequestException e) {
      throw new EslServerException("Could not get package list.", e);
    } catch (Exception e) {
      e.printStackTrace();
      throw new EslException("Could not get package list. Exception: " + e.getMessage());
    }
  }
 /**
  * Downloads the documents (in a zip archive) from the package and returns a byte[].
  *
  * @param packageId Id of the DocumentPackage we want to download
  * @return The zipped documents in bytes
  * @throws EslException
  */
 public byte[] downloadZippedDocuments(PackageId packageId) throws EslException {
   String path =
       template.urlFor(UrlTemplate.ZIP_PATH).replace("{packageId}", packageId.getId()).build();
   try {
     return client.getBytes(path).getContents();
   } catch (RequestException e) {
     throw new EslServerException("Could not download the documents to a zip file.", e);
   } catch (Exception e) {
     throw new EslException("Could not download the documents to a zip file.", e);
   }
 }
  /**
   * Get Journal Entries in csv format.
   *
   * @param userId The ID of the user whose e-journal entries are being retrieved.
   * @return all of the user's notary e-journal entries in csv format.
   */
  public DownloadedFile getJournalEntriesAsCSV(String userId) {
    String path =
        template.urlFor(UrlTemplate.NOTARY_JOURNAL_CSV_PATH).replace("{userId}", userId).build();

    try {
      return client.getBytes(path);
    } catch (RequestException e) {
      throw new EslException("Could not get Journal Entries in csv.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get Journal Entries in csv." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Creates a package with roles.
   *
   * @param aPackage
   * @return PackageId
   * @throws com.silanis.esl.sdk.EslException
   */
  public PackageId createPackage(Package aPackage) throws EslException {
    String path = template.urlFor(UrlTemplate.PACKAGE_PATH).build();
    String packageJson = Serialization.toJson(aPackage);

    try {
      String response = client.post(path, packageJson);
      return Serialization.fromJson(response, PackageId.class);
    } catch (RequestException e) {
      throw new EslServerException("Could not create a new package", e);
    } catch (Exception e) {
      throw new EslException("Could not create a new package", e);
    }
  }
  /**
   * Gets the package.
   *
   * @param packageId
   * @return Package
   * @throws EslException
   */
  public Package getApiPackage(String packageId) throws EslException {
    String path =
        template.urlFor(UrlTemplate.PACKAGE_ID_PATH).replace("{packageId}", packageId).build();
    String stringResponse;
    try {
      stringResponse = client.get(path);
    } catch (RequestException e) {
      throw new EslServerException("Could not get package.", e);
    } catch (Exception e) {
      throw new EslException("Could not get package.", e);
    }

    return Serialization.fromJson(stringResponse, Package.class);
  }
 /**
  * Downloads the evidence summary (in PDF) from the package and returns a byte[].
  *
  * @param packageId
  * @return The evidence summary in bytes
  * @throws EslException
  */
 public byte[] downloadEvidenceSummary(PackageId packageId) throws EslException {
   String path =
       template
           .urlFor(UrlTemplate.EVIDENCE_SUMMARY_PATH)
           .replace("{packageId}", packageId.getId())
           .build();
   try {
     return client.getBytes(path).getContents();
   } catch (RequestException e) {
     throw new EslServerException("Could not download the evidence summary.", e);
   } catch (Exception e) {
     throw new EslException("Could not download the evidence summary.", e);
   }
 }
 /**
  * Deletes the document from the package.
  *
  * @param packageId
  * @param documentId
  * @throws EslException
  */
 public void deleteDocument(PackageId packageId, String documentId) throws EslException {
   String path =
       template
           .urlFor(UrlTemplate.DOCUMENT_ID_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{documentId}", documentId)
           .build();
   try {
     client.delete(path);
   } catch (RequestException e) {
     throw new EslServerException("Could not delete document from package.", e);
   } catch (Exception e) {
     throw new EslException("Could not delete document from package.", e);
   }
 }
 public byte[] downloadDocument(PackageId packageId, String documentId) throws EslException {
   String path =
       template
           .urlFor(UrlTemplate.PDF_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{documentId}", documentId)
           .build();
   try {
     return client.getBytesAsOctetStream(path).getContents();
   } catch (RequestException e) {
     throw new EslServerException("Could not download the pdf document.", e);
   } catch (Exception e) {
     throw new EslException("Could not download the pdf document.", e);
   }
 }
  /**
   * Adds a role to the package.
   *
   * @param packageId
   * @param role
   * @return The role added
   * @throws EslException
   */
  public Role addRole(PackageId packageId, Role role) throws EslException {
    String path =
        template.urlFor(UrlTemplate.ROLE_PATH).replace("{packageId}", packageId.getId()).build();

    String roleJson = JacksonUtil.serializeDirty(role);
    String stringResponse;
    try {
      stringResponse = client.post(path, roleJson);
    } catch (RequestException e) {
      throw new EslServerException("Could not add role.", e);
    } catch (Exception e) {
      throw new EslException("Could not add role.", e);
    }
    return Serialization.fromJson(stringResponse, Role.class);
  }
 /**
  * Sends the package.
  *
  * @param packageId
  * @throws EslException
  */
 public void sendPackage(PackageId packageId) throws EslException {
   String path =
       template
           .urlFor(UrlTemplate.PACKAGE_ID_PATH)
           .replace("{packageId}", packageId.getId())
           .build();
   String json = "{\"status\":\"SENT\"}";
   try {
     client.post(path, json);
   } catch (RequestException e) {
     throw new EslServerException("Could not send the package.", e);
   } catch (Exception e) {
     throw new EslException("Could not send the package.", e);
   }
 }
  /**
   * Creates a package and uploads the documents in one step
   *
   * @param aPackage
   * @param documents
   * @return
   * @throws EslException
   */
  public PackageId createPackageOneStep(
      Package aPackage, Collection<com.silanis.esl.sdk.Document> documents) throws EslException {
    String path = template.urlFor(UrlTemplate.PACKAGE_PATH).build();
    String packageJson = Serialization.toJson(aPackage);

    try {
      String response = client.postMultipartPackage(path, documents, packageJson);
      return Serialization.fromJson(response, PackageId.class);

    } catch (RequestException e) {
      throw new EslServerException("Could not create a new package in one-step", e);
    } catch (Exception e) {
      throw new EslException("Could not create a new package in one-step", e);
    }
  }
 /**
  * Unlock a signer which has been locked out due to too many failed authentication attempts.
  *
  * @param signerId If not null, the id of the signer who's status we are to retrieve
  */
 public void unlockSigner(PackageId packageId, String signerId) {
   String path =
       template
           .urlFor(UrlTemplate.ROLE_UNLOCK_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{roleId}", signerId)
           .build();
   try {
     client.post(path, null);
   } catch (RequestException e) {
     throw new EslException("Could not unlock signer.", e);
   } catch (Exception e) {
     throw new EslException("Could not unlock signer." + " Exception: " + e.getMessage());
   }
 }
 /**
  * Deletes a role from the package.
  *
  * @param packageId
  * @param role
  * @throws EslException
  */
 public void deleteRole(PackageId packageId, Role role) throws EslException {
   String path =
       template
           .urlFor(UrlTemplate.ROLE_ID_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{roleId}", role.getId())
           .build();
   try {
     client.delete(path);
   } catch (RequestException e) {
     throw new EslServerException("Could not delete role", e);
   } catch (Exception e) {
     throw new EslException("Could not delete role", e);
   }
 }
  /**
   * Change the package's status to DRAFT.
   *
   * @param packageId
   * @throws EslException
   */
  public void changePackageStatusToDraft(PackageId packageId) throws EslException {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_ID_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      client.put(path, "{\"status\":\"DRAFT\"}");
    } catch (RequestException e) {
      throw new EslServerException("Could not change the package status to DRAFT.", e);
    } catch (Exception e) {
      throw new EslException("Could not change the package status to DRAFT.", e);
    }
  }
 /**
  * Gets the roles for a package.
  *
  * @param packageId
  * @return A list of the roles in the package
  * @throws EslException
  */
 public List<Role> getRoles(PackageId packageId) throws EslException {
   String path =
       template.urlFor(UrlTemplate.ROLE_PATH).replace("{packageId}", packageId.getId()).build();
   String stringResponse;
   try {
     stringResponse = client.get(path);
   } catch (RequestException e) {
     throw new EslServerException(
         "Could not retrieve list of roles for package with id " + packageId.getId(), e);
   } catch (Exception e) {
     throw new EslException(
         "Could not retrieve list of roles for package with id " + packageId.getId(), e);
   }
   return Serialization.fromJson(stringResponse, RoleList.class).getResults();
 }
  private void notifySigner(PackageId packageId, String roleId) {
    String path =
        template
            .urlFor(UrlTemplate.NOTIFY_ROLE_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", roleId)
            .build();

    try {
      client.post(path, null);
    } catch (RequestException e) {
      throw new EslServerException("Could not send email notification.", e);
    } catch (Exception e) {
      throw new EslException("Could not send email notification.  " + e.getMessage());
    }
  }
  private void sendSmsToSigner(PackageId packageId, Role role) {
    String path =
        template
            .urlFor(UrlTemplate.SEND_SMS_TO_SIGNER_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", role.getId())
            .build();

    try {
      client.post(path, null);
    } catch (RequestException e) {
      throw new EslException("Could not send SMS to the signer.", e);
    } catch (Exception e) {
      throw new EslException("Could not send SMS to the signer." + " Exception: " + e.getMessage());
    }
  }
 /**
  * Removes a signer from a package
  *
  * @param packageId The id of the package containing the signer to be deleted
  * @param signerId The role id of the signer to be deleted
  */
 public void removeSigner(PackageId packageId, String signerId) {
   String path =
       template
           .urlFor(UrlTemplate.SIGNER_PATH)
           .replace("{packageId}", packageId.getId())
           .replace("{roleId}", signerId)
           .build();
   try {
     client.delete(path);
     return;
   } catch (RequestException e) {
     throw new EslServerException("Could not delete signer.", e);
   } catch (Exception e) {
     throw new EslException("Could not delete signer." + " Exception: " + e.getMessage());
   }
 }
  /**
   * Deletes the specified package.
   *
   * @param packageId The id of the package to be deleted
   */
  public void deletePackage(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_ID_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      client.delete(path);
    } catch (RequestException e) {
      throw new EslServerException("Unable to delete package.", e);
    } catch (Exception e) {
      e.printStackTrace();
      throw new EslException("Unable to delete package. Exception: " + e.getMessage());
    }
  }
  /**
   * Archive the specified package.
   *
   * @param packageId The id of the package to be archived
   */
  public void archive(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_ID_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    String json = "{\"status\":\"ARCHIVED\"}";
    try {
      client.post(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Unable to archive the package.", e);
    } catch (Exception e) {
      e.printStackTrace();
      throw new EslException("Unable to archive the package. Exception: " + e.getMessage());
    }
  }
  /**
   * Upload documents with external content to the package.
   *
   * @param packageId
   */
  public void addDocumentWithExternalContent(
      String packageId, List<com.silanis.esl.sdk.Document> providerDocuments) {
    String path =
        template.urlFor(UrlTemplate.DOCUMENT_PATH).replace("{packageId}", packageId).build();

    List<Document> apiDocuments = new ArrayList<Document>();
    for (com.silanis.esl.sdk.Document document : providerDocuments) {
      apiDocuments.add(new DocumentConverter(document).toAPIDocumentMetadata());
    }
    try {
      String json = Serialization.toJson(apiDocuments);
      client.post(path, json);
    } catch (RequestException e) {
      throw new EslServerException("Could not upload the documents.", e);
    } catch (Exception e) {
      throw new EslException("Could not upload the documents." + " Exception: " + e.getMessage());
    }
  }
  private String getFastTrackUrl(PackageId packageId, Boolean signing) {
    String path =
        template
            .urlFor(UrlTemplate.FAST_TRACK_URL_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{signing}", signing.toString())
            .build();

    try {
      String json = client.get(path);
      SigningUrl signingUrl = Serialization.fromJson(json, SigningUrl.class);
      return signingUrl.getUrl();
    } catch (RequestException e) {
      throw new EslException("Could not get a fastTrack url.", e);
    } catch (Exception e) {
      throw new EslException("Could not get a fastTrack url." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get thank you dialog content.
   *
   * @param packageId The id of the package to get thank you dialog content.
   * @return thank you dialog content
   */
  public String getThankYouDialogContent(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.THANK_YOU_DIALOG_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      String json = client.get(path);
      Properties thankYouDialogContent = Serialization.fromJson(json, Properties.class);
      return thankYouDialogContent.getProperty("body");
    } catch (RequestException e) {
      throw new EslException("Could not get thank you dialog content.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get thank you dialog content." + " Exception: " + e.getMessage());
    }
  }
  public com.silanis.esl.sdk.Document uploadApiDocument(
      String packageId, String fileName, byte[] fileBytes, Document document) {
    String path =
        template.urlFor(UrlTemplate.DOCUMENT_PATH).replace("{packageId}", packageId).build();

    String documentJson = Serialization.toJson(document);

    try {
      String response = client.postMultipartFile(path, fileName, fileBytes, documentJson);
      com.silanis.esl.api.model.Document uploadedDocument =
          Serialization.fromJson(response, com.silanis.esl.api.model.Document.class);
      return new DocumentConverter(uploadedDocument, getApiPackage(packageId)).toSDKDocument();
    } catch (RequestException e) {
      throw new EslServerException("Could not upload document to package.", e);
    } catch (Exception e) {
      throw new EslException("Could not upload document to package.", e);
    }
  }
  /**
   * Gets the documents from the history list
   *
   * @return
   */
  public List<com.silanis.esl.sdk.Document> getDocuments() {
    String path = template.urlFor(UrlTemplate.PROVIDER_DOCUMENTS).build();

    try {
      String stringResponse = client.get(path);
      List<Document> apiResponse =
          JacksonUtil.deserialize(stringResponse, new TypeReference<List<Document>>() {});
      List<com.silanis.esl.sdk.Document> documents = new ArrayList<com.silanis.esl.sdk.Document>();
      for (Document document : apiResponse) {
        documents.add(new DocumentConverter(document, null).toSDKDocument());
      }
      return documents;
    } catch (RequestException e) {
      throw new EslServerException("Failed to retrieve documents from history List.", e);
    } catch (Exception e) {
      throw new EslException("Failed to retrieve documents from history list.", e);
    }
  }
  private String getSigningUrl(PackageId packageId, Role role) {

    String path =
        template
            .urlFor(UrlTemplate.SIGNER_URL_PATH)
            .replace("{packageId}", packageId.getId())
            .replace("{roleId}", role.getId())
            .build();

    try {
      String response = client.get(path);
      SigningUrl signingUrl = Serialization.fromJson(response, SigningUrl.class);
      return signingUrl.getUrl();
    } catch (RequestException e) {
      throw new EslException("Could not get a signing url.", e);
    } catch (Exception e) {
      throw new EslException("Could not get a signing url." + " Exception: " + e.getMessage());
    }
  }
  /**
   * Get package support configuration.
   *
   * @param packageId The id of the package to get package support configuration.
   * @return package support configuration
   */
  public SupportConfiguration getConfig(PackageId packageId) {
    String path =
        template
            .urlFor(UrlTemplate.PACKAGE_INFORMATION_CONFIG_PATH)
            .replace("{packageId}", packageId.getId())
            .build();

    try {
      String json = client.get(path);
      com.silanis.esl.api.model.SupportConfiguration apiSupportConfiguration =
          Serialization.fromJson(json, com.silanis.esl.api.model.SupportConfiguration.class);
      return new SupportConfigurationConverter(apiSupportConfiguration).toSDKSupportConfiguration();
    } catch (RequestException e) {
      throw new EslException("Could not get support configuration.", e);
    } catch (Exception e) {
      throw new EslException(
          "Could not get support configuration." + " Exception: " + e.getMessage());
    }
  }