Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/main/java/com/hellosign/sdk/resource/TemplateDraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import com.hellosign.sdk.resource.support.Document;
import com.hellosign.sdk.resource.support.types.FieldType;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -145,9 +150,19 @@ public boolean removeSignerRole(String signerRole) throws HelloSignException {
* field.
*/
public void addMergeField(String name, FieldType type) throws HelloSignException {
if (!FieldType.checkbox.equals(type) && !FieldType.text.equals(type)) {
List<FieldType> mergeableFieldTypes = Arrays.asList(
FieldType.CHECKBOX,
FieldType.TEXT
);

if (!mergeableFieldTypes.contains(type)) {
List<String> asStrings = mergeableFieldTypes
.stream()
.map(Enum::toString)
.collect(Collectors.toList());
throw new HelloSignException(
"Only 'text' or 'checkbox' types allowed for merge fields.");
String.format("Only %s types allowed for merge fields.", String.join(",", asStrings))
);
Comment on lines +153 to +165
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was unnecessary since we're not actually adding any other types. I might revert this part.

}
mergeFields.put(name, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setName(String name) {
}

public FieldType getType() {
return FieldType.valueOf(getString(CUSTOM_FIELD_TYPE));
return FieldType.getEnum(getString(CUSTOM_FIELD_TYPE));
}

public String getTypeString() {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/hellosign/sdk/resource/support/FormField.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

/**
* This class represents a HelloSign Form Field object.
*
* @author "Chris Paul (chris@hellosign.com)"
*/
public class FormField extends CustomField {

Expand All @@ -34,7 +32,8 @@ public FormField(JSONObject json) throws HelloSignException {
* @param y int y coordinate location of this field in pixels
* @param height int height of this field in pixels
* @param width int width of this field in pixels
* @param page int page number of the document in which this field will be placed (1-based index)
* @param page int page number of the document in which this field will be placed (1-based
* index)
*/
public FormField(FieldType type, String name, int signer, int x, int y, int height, int width,
int page) {
Expand Down Expand Up @@ -67,8 +66,8 @@ public void setSigner(Integer signer) {
}

/**
* Set the page number for this component. The (x, y) coordinates will be
* relative to the top left corner of that page.
* Set the page number for this component. The (x, y) coordinates will be relative to the top
* left corner of that page.
*
* @param page Integer page number
*/
Expand All @@ -86,8 +85,8 @@ public Integer getPage() {
}

/**
* Set the validation rule for this field. This will force the signer to
* enter data that conforms to the validation rule.
* Set the validation rule for this field. This will force the signer to enter data that
* conforms to the validation rule.
*
* @param type ValidationType
*/
Expand Down Expand Up @@ -125,4 +124,9 @@ public String getValidationTypeString() {
public void setRequired(boolean isRequired) {
setIsRequired(isRequired);
}

@Override
public void setValue(String value) {
throw new UnsupportedOperationException("Setting value is not allowed for form fields, see custom fields.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setValue(Object value) {
}

public FieldType getType() {
return FieldType.valueOf(getString(RESPONSE_DATA_TYPE));
return FieldType.getEnum(getString(RESPONSE_DATA_TYPE));
}

public void setType(FieldType type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package com.hellosign.sdk.resource.support.types;

/**
* Enumeration of allowable field types. These are used in the ResponseData and
* FormField objects.
*
* @author "Chris Paul (chris@hellosign.com)"
*
* Enumeration of allowable field types. These are used in the ResponseData and FormField objects.
*/
public enum FieldType {
text, signature, initials, checkbox, date_signed
CHECKBOX("checkbox"),
CHECKBOX_MERGE("checkbox-merge"),
DATE_SIGNED("date_signed"),
DROPDOWN("dropdown"),
INITIALS("initials"),
RADIO("radio"),
SIGNATURE("signature"),
TEXT("text"),
TEXT_MERGE("text-merge");

public final String value;

FieldType(String value) {
this.value = value;
}

@Override
public String toString() {
return this.value;
}

public static FieldType getEnum(String string) {
for(FieldType type : values())
if(type.value.equals(string)) {
return type;
}
throw new IllegalArgumentException();
}
}
118 changes: 114 additions & 4 deletions src/test/java/com/hellosign/sdk/HelloSignClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ protected String getTestFileAsString(String name) throws FileNotFoundException {
return result;
}

protected File getTestFixture(String name) throws FileNotFoundException {
String path = System.getProperty("file.separator") + this.getClass().getSimpleName()
+ System.getProperty("file.separator") + "Fixtures" + System.getProperty("file.separator") + name;
URL resource = this.getClass().getResource(path);
return new File(resource.getFile());
}

protected void mockResponseCode(int code) {
doReturn(code).when(spy).getLastResponseCode();
}
Expand Down Expand Up @@ -483,7 +490,7 @@ public void testGetTemplate() throws Exception {
assertNotNull(cf.getWidth());
assertNotNull(cf.getHeight());
assertNotNull(cf.isRequired());
if (cf.getType().equals(FieldType.text)) {
if (cf.getType().equals(FieldType.TEXT)) {
assertNotNull(cf.getEstimatedTextLines());
assertNotNull(cf.getEstimatedCharsPerLine());
}
Expand All @@ -498,7 +505,7 @@ public void testGetTemplate() throws Exception {
assertNotNull(ff.getHeight());
assertNotNull(ff.isRequired());
assertNotNull(ff.getSigner());
if (ff.getType().equals(FieldType.text)) {
if (ff.getType().equals(FieldType.TEXT)) {
assertNotNull(ff.getEstimatedTextLines());
assertNotNull(ff.getEstimatedCharsPerLine());
}
Expand Down Expand Up @@ -802,8 +809,8 @@ public void testCreateEmbeddedTemplateDraft() throws Exception {
draft.addSignerRole("Role B", 2);
draft.addCCRole("Lawyer");
draft.addCCRole("Manager");
draft.addMergeField("Full Name", FieldType.text);
draft.addMergeField("Is registered?", FieldType.checkbox);
draft.addMergeField("Full Name", FieldType.TEXT);
draft.addMergeField("Is registered?", FieldType.CHECKBOX);
draft.addMetadata("test_key", "test_value");
EmbeddedRequest embeddedReq = new EmbeddedRequest("034fb51064187cf28e4aad1c2533ad8f",
draft);
Expand Down Expand Up @@ -882,4 +889,107 @@ public void testTemplateFilesUpdateInvalid() throws Exception {
client.updateTemplateFiles("0000000000000000000000000000000000000000", new TemplateDraft(),
null);
}

@Test
public void testSignatureRequestWithFormFields()
throws HelloSignException, FileNotFoundException {
SignatureRequest request = new SignatureRequest();
request.setTitle("NDA with Acme Co.");
request.setSubject("The NDA we talked about");
request.setMessage("Please sign this NDA and then we can discuss more. Let me know if you have any questions.");
request.addSigner("jack@example.com", "Jack");
request.setTestMode(true);

Document nda = new Document();

File file = this.getTestFixture("W9.pdf");
nda.setFile(file);

FormField text = new FormField();
text.setApiId("text_e5e36acbe");
text.setName("Name");
text.setType(FieldType.TEXT);
text.setX(71);
text.setY(94);
text.setWidth(200);
text.setHeight(12);
text.setIsRequired(true);
text.setSigner(0);
text.setPage(1);
nda.addFormField(text);

FormField mergeText = new FormField();
mergeText.setApiId("text_merge_e5e36acbe");
mergeText.setName("Business");
mergeText.setType(FieldType.TEXT_MERGE);
mergeText.setX(70);
mergeText.setY(118);
mergeText.setWidth(200);
mergeText.setHeight(12);
mergeText.setIsRequired(true);
mergeText.setSigner(0);
mergeText.setPage(1);
nda.addFormField(mergeText);

FormField mergeCheckbox = new FormField();
mergeCheckbox.setApiId("checkbox_merge_e5e36acbe");
mergeCheckbox.setName("Tax Class");
mergeCheckbox.setType(FieldType.CHECKBOX_MERGE);
mergeCheckbox.setX(65);
mergeCheckbox.setY(145);
mergeCheckbox.setWidth(9);
mergeCheckbox.setHeight(9);
mergeCheckbox.setIsRequired(true);
mergeCheckbox.setSigner(0);
mergeCheckbox.setPage(1);
nda.addFormField(mergeCheckbox);

FormField checkbox = new FormField();
checkbox.setApiId("checkbox_e5e36acbe");
checkbox.setName("Tax Class");
checkbox.setType(FieldType.CHECKBOX);
checkbox.setX(185);
checkbox.setY(145);
checkbox.setWidth(9);
checkbox.setHeight(9);
checkbox.setIsRequired(true);
checkbox.setSigner(0);
checkbox.setPage(1);
nda.addFormField(checkbox);

FormField signature = new FormField();
signature.setApiId("signature_e5e36acbe");
signature.setName("Sig");
signature.setType(FieldType.SIGNATURE);
signature.setX(130);
signature.setY(520);
signature.setWidth(100);
signature.setHeight(16);
signature.setIsRequired(true);
signature.setSigner(0);
signature.setPage(1);
nda.addFormField(signature);

FormField dateSigned = new FormField();
dateSigned.setApiId("date_e5e36acbe");
dateSigned.setName("Date");
dateSigned.setType(FieldType.DATE_SIGNED);
dateSigned.setX(410);
dateSigned.setY(520);
dateSigned.setWidth(100);
dateSigned.setHeight(16);
dateSigned.setIsRequired(true);
dateSigned.setSigner(0);
dateSigned.setPage(1);
nda.addFormField(dateSigned);

request.addDocument(nda);

Map<String, String> fields = new HashMap<>();
fields.put("checkbox_merge_e5e36acbe", "true");
fields.put("text_merge_e5e36acbe", "This text is merged!?");
request.setCustomFields(fields);

SignatureRequest newRequest = client.sendSignatureRequest(request);
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"signature_request": {
"signature_request_id": "b1f2d21f496b510fc2cc0da0b4d0af7c1687d370",
"test_mode": true,
"title": "NDA with Acme Co.",
"original_title": "The NDA we talked about",
"subject": "The NDA we talked about",
"message": "Please sign this NDA and then we can discuss more. Let me know if you have any questions.",
"metadata": {},
"created_at": 1587430723,
"is_complete": false,
"is_declined": false,
"has_error": false,
"custom_fields": [
{
"name": "Name",
"type": "text",
"required": true,
"api_id": "text_e5e36acbe",
"editor": null,
"value": null
},
{
"name": "Business",
"type": "text",
"required": true,
"api_id": "text_merge_e5e36acbe",
"editor": 0,
"value": "This text is merged!?"
},
{
"name": "Tax Class",
"type": "checkbox",
"required": true,
"api_id": "checkbox_merge_e5e36acbe",
"editor": 0,
"value": true
},
{
"name": "Tax Class",
"type": "checkbox",
"required": true,
"api_id": "checkbox_e5e36acbe",
"editor": null,
"value": false
}
],
"response_data": [],
"signing_url": "https://app.hellosign.com/sign/b1f2d21f496b510fc2cc0da0b4d0af7c1687d370",
"signing_redirect_url": null,
"final_copy_uri": "/v3/signature_request/final_copy/b1f2d21f496b510fc2cc0da0b4d0af7c1687d370",
"files_url": "https://api.hellosign.com/v3/signature_request/files/b1f2d21f496b510fc2cc0da0b4d0af7c1687d370",
"details_url": "https://app.hellosign.com/home/manage?guid=b1f2d21f496b510fc2cc0da0b4d0af7c1687d370",
"requester_email_address": "johnspaetzel@dropbox.com",
"signatures": [
{
"signature_id": "c27d6bf39e0b222abfba400b40a3ba53",
"has_pin": false,
"signer_email_address": "jack@example.com",
"signer_name": "Jack",
"signer_role": null,
"order": null,
"status_code": "awaiting_signature",
"signed_at": null,
"last_viewed_at": null,
"last_reminded_at": null,
"error": null
}
],
"cc_email_addresses": [],
"template_ids": null
}
}