|
27 | 27 | import java.io.File; |
28 | 28 | import java.lang.reflect.Constructor; |
29 | 29 | import java.net.HttpURLConnection; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
30 | 32 |
|
| 33 | +import org.json.JSONArray; |
31 | 34 | import org.json.JSONException; |
32 | 35 | import org.json.JSONObject; |
33 | 36 |
|
|
50 | 53 | import com.hellosign.sdk.resource.support.Signature; |
51 | 54 | import com.hellosign.sdk.resource.support.SignatureRequestList; |
52 | 55 | import com.hellosign.sdk.resource.support.TemplateList; |
| 56 | +import com.hellosign.sdk.resource.support.types.FieldType; |
53 | 57 |
|
54 | 58 | /** |
55 | 59 | * You'll need the HelloSignClient to do just about everything, from creating |
@@ -123,6 +127,8 @@ public class HelloSignClient { |
123 | 127 | public static final String CLIENT_ID = "client_id"; |
124 | 128 | public static final String EMBEDDED_TEMPLATE_SKIP_SIGNER_ROLES = "skip_signer_roles"; |
125 | 129 | public static final String EMBEDDED_TEMPLATE_SKIP_SUBJECT_MESSAGE = "skip_subject_message"; |
| 130 | + public static final String EMBEDDED_TEMPLATE_MERGE_FIELDS = "merge_fields"; |
| 131 | + public static final String EMBEDDED_TEMPLATE_CC_ROLES = "cc_roles"; |
126 | 132 |
|
127 | 133 | private Authentication auth; |
128 | 134 | private HttpClient httpClient; |
@@ -186,7 +192,7 @@ public HelloSignClient(Authentication auth) throws HelloSignException { |
186 | 192 | /** |
187 | 193 | * Allows overriding of the authentication mechanism. Used |
188 | 194 | * mainly for setting an OAuth token/secret. |
189 | | - * @param auth |
| 195 | + * @param auth Authentication used for setting the auth method for this client |
190 | 196 | */ |
191 | 197 | public void setAuthentication(Authentication auth) { |
192 | 198 | this.auth = auth; |
@@ -617,6 +623,8 @@ public boolean deleteTemplate(String templateId) throws HelloSignException { |
617 | 623 | * @param clientId String optional ID of the app which is generating this |
618 | 624 | * new template. Set to null if not used. |
619 | 625 | * @return String ID of the template to be created |
| 626 | + * @throws HelloSignException thrown if there is a problem processing the |
| 627 | + * HTTP request |
620 | 628 | */ |
621 | 629 | public String updateTemplateFiles(String existingTemplateId, TemplateDraft newTemplate, String clientId) |
622 | 630 | throws HelloSignException { |
@@ -832,11 +840,51 @@ public EmbeddedResponse getEmbeddedTemplateEditUrl(String templateId, boolean sk |
832 | 840 | */ |
833 | 841 | public EmbeddedResponse getEmbeddedTemplateEditUrl(String templateId, boolean skipSignerRoles, |
834 | 842 | boolean skipSubjectMessage, boolean testMode) throws HelloSignException { |
| 843 | + return getEmbeddedTemplateEditUrl(templateId, skipSignerRoles, skipSubjectMessage, false, null, null); |
| 844 | + } |
| 845 | + |
| 846 | + /** |
| 847 | + * Big kahuna method for editing an embedded template. This method allows |
| 848 | + * the updating of merge fields and CC roles (both optional parameters). |
| 849 | + * |
| 850 | + * @param templateId String ID of the signature request to embed |
| 851 | + * @param skipSignerRoles true if the edited template should not allow the |
| 852 | + * user to modify the template's signer roles. Defaults to false. |
| 853 | + * @param skipSubjectMessage true if the edited template should not allow |
| 854 | + * the user to modify the template's subject and message. Defaults to |
| 855 | + * false. |
| 856 | + * @param testMode true if this request is a test request. Useful for |
| 857 | + * editing locked templates. |
| 858 | + * @param mergeFields These will overwrite the current merge fields for the |
| 859 | + * embedded template. To remove all merge fields, pass in an empty Map. |
| 860 | + * @param ccRoles These will overwrite the current CC Roles for the embedded |
| 861 | + * template. To remove all CC roles, pass in null or an empty List. |
| 862 | + * @return EmbeddedResponse |
| 863 | + * @throws HelloSignException thrown if there's a problem processing the |
| 864 | + * HTTP request or the JSON response. |
| 865 | + */ |
| 866 | + public EmbeddedResponse getEmbeddedTemplateEditUrl(String templateId, boolean skipSignerRoles, |
| 867 | + boolean skipSubjectMessage, boolean testMode, Map<String, FieldType> mergeFields, List<String> ccRoles) |
| 868 | + throws HelloSignException { |
835 | 869 | String url = BASE_URI + EMBEDDED_EDIT_URL_URI + "/" + templateId; |
836 | | - return new EmbeddedResponse( |
837 | | - httpClient.withAuth(auth).withPostField(EMBEDDED_TEMPLATE_SKIP_SIGNER_ROLES, skipSignerRoles) |
838 | | - .withPostField(EMBEDDED_TEMPLATE_SKIP_SUBJECT_MESSAGE, skipSubjectMessage) |
839 | | - .withPostField(AbstractRequest.REQUEST_TEST_MODE, testMode).post(url).asJson()); |
| 870 | + HttpClient client = httpClient.withAuth(auth) |
| 871 | + .withPostField(EMBEDDED_TEMPLATE_SKIP_SIGNER_ROLES, skipSignerRoles) |
| 872 | + .withPostField(EMBEDDED_TEMPLATE_SKIP_SUBJECT_MESSAGE, skipSubjectMessage) |
| 873 | + .withPostField(AbstractRequest.REQUEST_TEST_MODE, testMode); |
| 874 | + String mergeFieldsStr = TemplateDraft.serializeMergeFields(mergeFields); |
| 875 | + if (mergeFieldsStr != null) { |
| 876 | + client = client.withPostField(EMBEDDED_TEMPLATE_MERGE_FIELDS, mergeFieldsStr); |
| 877 | + } |
| 878 | + if (ccRoles == null || ccRoles.isEmpty()) { |
| 879 | + // Per documentation: https://app.hellosign.com/api/reference#get_embedded_template_edit_url |
| 880 | + client = client.withPostField(EMBEDDED_TEMPLATE_CC_ROLES + "[0]", ""); |
| 881 | + } else { |
| 882 | + for (int i = 0; i < ccRoles.size(); i++) { |
| 883 | + String cc = ccRoles.get(i); |
| 884 | + client = client.withPostField(EMBEDDED_TEMPLATE_CC_ROLES + "[" + i + "]", cc); |
| 885 | + } |
| 886 | + } |
| 887 | + return new EmbeddedResponse(client.post(url).asJson()); |
840 | 888 | } |
841 | 889 |
|
842 | 890 | /** |
|
0 commit comments