2525 */
2626
2727import java .io .Serializable ;
28+ import java .util .ArrayList ;
2829import java .util .HashMap ;
2930import java .util .List ;
3031import java .util .Map ;
3132
33+ import org .json .JSONArray ;
34+
3235import com .hellosign .sdk .HelloSignException ;
36+ import com .hellosign .sdk .resource .support .CustomField ;
3337import 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 );
0 commit comments