Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.

Commit 75c953f

Browse files
author
Chris Paul
committed
Add support for editable merge fields. Closes #8
1 parent ee50d2b commit 75c953f

2 files changed

Lines changed: 104 additions & 13 deletions

File tree

src/main/java/com/hellosign/sdk/resource/TemplateSignatureRequest.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
*/
2626

2727
import java.io.Serializable;
28+
import java.util.ArrayList;
2829
import java.util.HashMap;
2930
import java.util.List;
3031
import java.util.Map;
3132

33+
import org.json.JSONArray;
34+
3235
import com.hellosign.sdk.HelloSignException;
36+
import com.hellosign.sdk.resource.support.CustomField;
3337
import com.hellosign.sdk.resource.support.Signer;
3438

3539
/**
@@ -57,7 +61,7 @@ public class TemplateSignatureRequest extends AbstractRequest {
5761
// fields are stored, so we can support this association.
5862
private Map<String, Signer> signers = new HashMap<String, Signer>();
5963
private Map<String, String> ccs = new HashMap<String, String>();
60-
private Map<String, String> customFields = new HashMap<String, String>();
64+
private List<CustomField> customFields = new ArrayList<CustomField>();
6165

6266
public TemplateSignatureRequest() {
6367
super();
@@ -169,13 +173,25 @@ public void removeSignerByEmail(String email) throws HelloSignException {
169173
}
170174
}
171175

176+
/**
177+
* Add the custom field to this request. This is useful for specifying
178+
* a pre-filled value and/or a field editor.
179+
* @param field CustomField
180+
*/
181+
public void addCustomField(CustomField field) {
182+
customFields.add(field);
183+
}
184+
172185
/**
173186
* Adds the value to fill in for a custom field with the given field name.
174187
* @param fieldName String field name to be filled in
175188
* @param value String value
176189
*/
177190
public void setCustomFieldValue(String fieldName, String value) {
178-
customFields.put(fieldName, value);
191+
CustomField f = new CustomField();
192+
f.setName(fieldName);
193+
f.setValue(value);
194+
customFields.add(f);
179195
}
180196

181197
/**
@@ -184,6 +200,18 @@ public void setCustomFieldValue(String fieldName, String value) {
184200
* @return Map
185201
*/
186202
public Map<String, String> getCustomFields() {
203+
Map<String, String> fields = new HashMap<String, String>();
204+
for (CustomField f : customFields) {
205+
fields.put(f.getName(), f.getValue());
206+
}
207+
return fields;
208+
}
209+
210+
/**
211+
* Returns a list of CustomField objects for this template.
212+
* @return List of CustomFields
213+
*/
214+
public List<CustomField> getCustomFieldsList() {
187215
return customFields;
188216
}
189217

@@ -193,14 +221,20 @@ public Map<String, String> getCustomFields() {
193221
* @param fields Map
194222
*/
195223
public void setCustomFields(Map<String, String> fields) {
196-
customFields = fields;
224+
clearCustomFields();
225+
for (String key : fields.keySet()) {
226+
CustomField f = new CustomField();
227+
f.setName(key);
228+
f.setValue(fields.get(key));
229+
customFields.add(f);
230+
}
197231
}
198232

199233
/**
200234
* Clears the current custom fields for this request.
201235
*/
202236
public void clearCustomFields() {
203-
customFields = new HashMap<String, String>();
237+
customFields = new ArrayList<CustomField>();
204238
}
205239

206240
/**
@@ -320,11 +354,12 @@ public Map<String, Serializable> getPostFields() throws HelloSignException {
320354
+ "[" + role + "][" + TEMPLATE_CCS_EMAIL + "]",
321355
ccz.get(role));
322356
}
323-
Map<String, String> customFields = getCustomFields();
324-
for (String fieldName : customFields.keySet()) {
325-
fields.put(TEMPLATE_CUSTOM_FIELDS
326-
+ "[" + fieldName + "]",
327-
customFields.get(fieldName));
357+
if (customFields.size() > 0) {
358+
JSONArray array = new JSONArray();
359+
for (CustomField f : customFields) {
360+
array.put(f.getJSONObject());
361+
}
362+
fields.put(TEMPLATE_CUSTOM_FIELDS, array.toString());
328363
}
329364
if (isTestMode()) {
330365
fields.put(REQUEST_TEST_MODE, true);

src/main/java/com/hellosign/sdk/resource/support/CustomField.java

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hellosign.sdk.resource.support;
22

3+
import org.json.JSONException;
4+
35
/**
46
* The MIT License (MIT)
57
*
@@ -41,9 +43,12 @@
4143
*/
4244
public class CustomField extends AbstractResource {
4345

44-
public static final String CUSTOM_FIELD_NAME = "name";
45-
public static final String CUSTOM_FIELD_TYPE = "type";
46-
public static final String CUSTOM_FIELD_API_ID = "api_id";
46+
public static final String CUSTOM_FIELD_NAME = "name";
47+
public static final String CUSTOM_FIELD_TYPE = "type";
48+
public static final String CUSTOM_FIELD_API_ID = "api_id";
49+
public static final String CUSTOM_FIELD_VALUE = "value";
50+
public static final String CUSTOM_FIELD_EDITOR = "editor";
51+
public static final String CUSTOM_FIELD_REQUIRED = "required";
4752

4853
public CustomField() {
4954
super();
@@ -56,6 +61,13 @@ public CustomField(JSONObject json) throws HelloSignException {
5661
public String getName() {
5762
return getString(CUSTOM_FIELD_NAME);
5863
}
64+
/**
65+
* Set the name of this custom field.
66+
* @param name String
67+
*/
68+
public void setName(String name) {
69+
set(CUSTOM_FIELD_NAME, name);
70+
}
5971
public FieldType getType() {
6072
return FieldType.valueOf(getString(CUSTOM_FIELD_TYPE));
6173
}
@@ -65,7 +77,51 @@ public String getTypeString() {
6577
}
6678
return null;
6779
}
80+
/**
81+
* Set the type for this custom field.
82+
* @param type FieldType
83+
*/
84+
public void setType(FieldType type) {
85+
set(CUSTOM_FIELD_TYPE, type.toString());
86+
}
87+
public String getValue() {
88+
return getString(CUSTOM_FIELD_VALUE);
89+
}
90+
/**
91+
* Set the value for this field.
92+
* @param value String
93+
*/
94+
public void setValue(String value) {
95+
set(CUSTOM_FIELD_VALUE, value);
96+
}
6897
public String getApiId() {
6998
return getString(CUSTOM_FIELD_API_ID);
7099
}
71-
}
100+
/**
101+
* Set the API ID for this custom field.
102+
* @param apiId String
103+
*/
104+
public void setApiId(String apiId) {
105+
set(CUSTOM_FIELD_API_ID, apiId);
106+
}
107+
public String getEditor() {
108+
return getString(CUSTOM_FIELD_EDITOR);
109+
}
110+
/**
111+
* Specify what signer role may edit this field.
112+
* @param editor String
113+
*/
114+
public void setEditor(String editor) {
115+
set(CUSTOM_FIELD_EDITOR, editor);
116+
}
117+
public Boolean isRequired() {
118+
return getBoolean(CUSTOM_FIELD_REQUIRED);
119+
}
120+
/**
121+
* Specify whether this field is required.
122+
* @param isRequired Boolean
123+
*/
124+
public void setIsRequired(Boolean isRequired) {
125+
set(CUSTOM_FIELD_REQUIRED, isRequired);
126+
}
127+
}

0 commit comments

Comments
 (0)