001package com.box.sdk;
002
003import java.util.ArrayList;
004import java.util.Date;
005import java.util.List;
006
007import com.box.sdk.internal.utils.JsonUtils;
008import com.eclipsesource.json.JsonObject;
009import com.eclipsesource.json.JsonValue;
010
011/**
012 * Represents a signer in BoxSignRequest.
013 */
014public class BoxSignRequestSigner extends BoxJSONObject {
015    private String email;
016    private BoxSignRequestSignerRole role;
017    private Boolean isInPerson;
018    private Integer order;
019    private String embedUrlExternalUserId;
020    private Boolean hasViewedEmail;
021    private Boolean hasViewedDocument;
022    private BoxSignerDecision signerDecision;
023    private List<BoxSignerInput> inputs;
024    private String embedUrl;
025    private BoxAPIConnection api;
026
027    /**
028     * Constructs a BoxSignRequestSigner with an email.
029     *
030     * @param email of signer.
031     */
032    public BoxSignRequestSigner(String email) {
033        this.email = email;
034    }
035
036    /**
037     * Construct a BoxSignRequestSigner.
038     *
039     * @param jsonObject the parsed JSON object.
040     * @param api        the API connection to be used to fetch interacted item
041     */
042    public BoxSignRequestSigner(JsonObject jsonObject, BoxAPIConnection api) {
043        super(jsonObject);
044        this.api = api;
045    }
046
047    /**
048     * Gets the email address of the signer.
049     *
050     * @return email address of the signer.
051     */
052    public String getEmail() {
053        return this.email;
054    }
055
056    /**
057     * Gets the role of the signer.
058     *
059     * @return role of the signer.
060     */
061    public BoxSignRequestSignerRole getRole() {
062        return this.role;
063    }
064
065    /**
066     * Gets the flag that when used in combination with an embed url on the sender. After the sender signs,
067     * they will be redirected to the next InPerson signer.
068     *
069     * @return true if is in person signer, otherwise false.
070     */
071    public boolean getIsInPerson() {
072        return this.isInPerson;
073    }
074
075    /**
076     * Gets the order of signer.
077     *
078     * @return order of signer.
079     */
080    public int getOrder() {
081        return this.order;
082    }
083
084    /**
085     * Gets the user id for this signer in external application responsible
086     * for authentication when accessing the embed url.
087     *
088     * @return embed url external user id.
089     */
090    public String getEmbedUrlExternalUserId() {
091        return this.embedUrlExternalUserId;
092    }
093
094    /**
095     * Gets the flag indicating if signer has viewed the sign request email.
096     *
097     * @return true if the signer has viewed the sign request email, otherwise false.
098     */
099    public boolean getHasViewedEmail() {
100        return this.hasViewedEmail;
101    }
102
103    /**
104     * Gets the flag indicating if signer has viewed the document.
105     *
106     * @return true if the signer has viewed the document, otherwise false.
107     */
108    public boolean getHasViewedDocument() {
109        return this.hasViewedDocument;
110    }
111
112    /**
113     * Gets the final decision made by signer.
114     *
115     * @return final decision made by signer.
116     */
117    public BoxSignerDecision getSignerDecision() {
118        return this.signerDecision;
119    }
120
121    /**
122     * Gets the inputs created by a signer on a sign request.
123     *
124     * @return list of inputs created by a signer on a sign request.
125     */
126    public List<BoxSignerInput> getInputs() {
127        return this.inputs;
128    }
129
130    /**
131     * Gets the url to direct signer to for signing.
132     *
133     * @return url to direct signer to for signing.
134     */
135    public String getEmbedUrl() {
136        return this.embedUrl;
137    }
138
139    /**
140     * Sets the email address of the signer.
141     *
142     * @param email address of the signer.
143     * @return this BoxSignRequestSigner object for chaining.
144     */
145    public BoxSignRequestSigner setEmail(String email) {
146        this.email = email;
147        return this;
148    }
149
150    /**
151     * Sets the role of the signer. If role is not set it's FinalCopyReader by default.
152     *
153     * @param role of the signer.
154     * @return this BoxSignRequestSigner object for chaining.
155     */
156    public BoxSignRequestSigner setRole(BoxSignRequestSignerRole role) {
157        this.role = role;
158        return this;
159    }
160
161    /**
162     * Gets the flag that is used in combination with an embed url for a the sender. After the sender signs,
163     * they will be redirected to the next InPerson signer.
164     *
165     * @return true if is in person signer, otherwise false.
166     */
167    public Boolean getInPerson() {
168        return this.isInPerson;
169    }
170
171    /**
172     * Sets the flag that is used in combination with an embed url for a the sender. After the sender signs,
173     * they will be redirected to the next InPerson signer.
174     *
175     * @param isInPerson flag.
176     * @return this BoxSignRequestSigner object for chaining.
177     */
178    public BoxSignRequestSigner setInPerson(Boolean isInPerson) {
179        this.isInPerson = isInPerson;
180        return this;
181    }
182
183    /**
184     * Sets the order of signer.
185     *
186     * @param order of signer.
187     * @return this BoxSignRequestSigner object for chaining.
188     */
189    public BoxSignRequestSigner setOrder(Integer order) {
190        this.order = order;
191        return this;
192    }
193
194    /**
195     * Sets the user id for this signer in external application responsible
196     * for authentication when accessing the embed url.
197     *
198     * @param embedUrlExternalUserId for this signer in external application responsible
199     *                               for authentication when accessing the embed url.
200     * @return this BoxSignRequestSigner object for chaining.
201     */
202    public BoxSignRequestSigner setEmbedUrlExternalUserId(String embedUrlExternalUserId) {
203        this.embedUrlExternalUserId = embedUrlExternalUserId;
204        return this;
205    }
206
207    /**
208     * Represents a final decision made by signer (type and time the decision was made).
209     */
210    public class BoxSignerDecision extends BoxJSONObject {
211        private BoxSignRequestSignerDecisionType type;
212        private Date finalizedAt;
213
214        /**
215         * Constructs a BoxSignerDecision object using an already parsed JSON object.
216         *
217         * @param jsonObject the parsed JSON object.
218         */
219        public BoxSignerDecision(JsonObject jsonObject) {
220            super(jsonObject);
221        }
222
223        /**
224         * Gets the type of decision made by signer.
225         *
226         * @return type of decision made by signer.
227         */
228        public BoxSignRequestSignerDecisionType getType() {
229            return this.type;
230        }
231
232        /**
233         * Gets the date/time that the decision was made.
234         *
235         * @return date/time that the decision was made.
236         */
237        public Date getFinalizedAt() {
238            return this.finalizedAt;
239        }
240
241        /**
242         * {@inheritDoc}
243         */
244        @Override
245        void parseJSONMember(JsonObject.Member member) {
246            JsonValue value = member.getValue();
247            String memberName = member.getName();
248            try {
249                if (memberName.equals("type")) {
250                    this.type = BoxSignRequestSignerDecisionType.fromJSONString(value.asString());
251                } else if (memberName.equals("finalized_at")) {
252                    this.finalizedAt = BoxDateFormat.parse(value.asString());
253                }
254            } catch (Exception e) {
255                throw new BoxDeserializationException(memberName, value.toString(), e);
256            }
257        }
258    }
259
260    /**
261     * Represents an input created by a signer on a sign request.
262     */
263    public class BoxSignerInput extends BoxJSONObject {
264        private String documentTagId;
265        private String textValue;
266        private boolean checkboxValue;
267        private Date dateValue;
268        private BoxSignRequestInputType type;
269        private int pageIndex;
270
271        /**
272         * Constructs a BoxSignerInput object using an already parsed JSON object.
273         *
274         * @param jsonObject the parsed JSON object.
275         */
276        public BoxSignerInput(JsonObject jsonObject) {
277            super(jsonObject);
278        }
279
280        /**
281         * Gets the reference of the id of a particular tag added to the content
282         * of the files being used to create the sign request.
283         *
284         * @return document tag id.
285         */
286        public String getDocumentTagId() {
287            return this.documentTagId;
288        }
289
290        /**
291         * Gets the text prefill value.
292         *
293         * @return text prefill value.
294         */
295        public String getTextValue() {
296            return this.textValue;
297        }
298
299        /**
300         * Gets the checkbox prefill value.
301         *
302         * @return checkbox prefill value.
303         */
304        public boolean getIsCheckboxValue() {
305            return this.checkboxValue;
306        }
307
308        /**
309         * Gets the date prefill value.
310         *
311         * @return date prefill value.
312         */
313        public Date getDateValue() {
314            return this.dateValue;
315        }
316
317        /**
318         * Gets the type of input.
319         *
320         * @return type of input.
321         */
322        public BoxSignRequestInputType getType() {
323            return this.type;
324        }
325
326        /**
327         * Gets the index of page that input is on.
328         *
329         * @return index of page that input is on.
330         */
331        public int getPageIndex() {
332            return this.pageIndex;
333        }
334
335        /**
336         * {@inheritDoc}
337         */
338        @Override
339        void parseJSONMember(JsonObject.Member member) {
340            JsonValue value = member.getValue();
341            String memberName = member.getName();
342            try {
343                if ("documentTagId".equals(memberName)) {
344                    this.documentTagId = value.asString();
345                } else if ("text_value".equals(memberName)) {
346                    this.textValue = value.asString();
347                } else if ("checkbox_value".equals(memberName)) {
348                    this.checkboxValue = value.asBoolean();
349                } else if ("date_value".equals(memberName)) {
350                    this.dateValue = BoxDateFormat.parse(value.asString());
351                } else if ("type".equals(memberName)) {
352                    this.type = BoxSignRequestInputType.fromJSONString(value.asString());
353                } else if ("page_index".equals(memberName)) {
354                    this.pageIndex = value.asInt();
355                }
356            } catch (Exception e) {
357                throw new BoxDeserializationException(memberName, value.toString(), e);
358            }
359        }
360    }
361
362    /**
363     * {@inheritDoc}
364     */
365    @Override
366    void parseJSONMember(JsonObject.Member member) {
367        JsonValue value = member.getValue();
368        String memberName = member.getName();
369        try {
370            if ("email".equals(memberName)) {
371                this.email = value.asString();
372            } else if ("role".equals(memberName)) {
373                this.role = BoxSignRequestSignerRole.fromJSONString(value.asString());
374            } else if ("is_in_person".equals(memberName)) {
375                this.isInPerson = value.asBoolean();
376            } else if ("order".equals(memberName)) {
377                this.order = value.asInt();
378            } else if ("embed_url_external_user_id".equals(memberName)) {
379                this.embedUrlExternalUserId = value.asString();
380            } else if ("has_viewed_email".equals(memberName)) {
381                this.hasViewedEmail = value.asBoolean();
382            } else if ("has_viewed_document".equals(memberName)) {
383                this.hasViewedDocument = value.asBoolean();
384            } else if ("signer_decision".equals(memberName)) {
385                JsonObject signerDecisionJSON = value.asObject();
386                BoxSignerDecision signerDecision = new BoxSignerDecision(signerDecisionJSON);
387                this.signerDecision = signerDecision;
388            } else if ("inputs".equals(memberName)) {
389                List<BoxSignerInput> inputs = new ArrayList<BoxSignerInput>();
390                for (JsonValue inputJSON : value.asArray()) {
391                    BoxSignerInput input = new BoxSignerInput(inputJSON.asObject());
392                    inputs.add(input);
393                }
394                this.inputs = inputs;
395            } else if ("embed_url".equals(memberName)) {
396                this.embedUrl = value.asString();
397            }
398        } catch (Exception e) {
399            throw new BoxDeserializationException(memberName, value.toString(), e);
400        }
401    }
402
403    /**
404     * Gets a JSON object representing this class.
405     *
406     * @return the JSON object representing this class.
407     */
408    public JsonObject getJSONObject() {
409        JsonObject jsonObj = new JsonObject();
410        JsonUtils.addIfNotNull(jsonObj, "email", this.email);
411        JsonUtils.addIfNotNull(jsonObj, "role", this.role);
412        JsonUtils.addIfNotNull(jsonObj, "is_in_person", this.isInPerson);
413        JsonUtils.addIfNotNull(jsonObj, "order", this.order);
414        JsonUtils.addIfNotNull(jsonObj, "embed_url_external_user_id", this.embedUrlExternalUserId);
415
416        return jsonObj;
417    }
418
419    /**
420     * Type of decision made by signer.
421     */
422    public enum BoxSignRequestSignerDecisionType {
423
424        /**
425         * Signed decision.
426         */
427        Signed("signed"),
428
429        /**
430         * Declined decision.
431         */
432        Declined("declined");
433
434        private final String jsonValue;
435
436        private BoxSignRequestSignerDecisionType(String jsonValue) {
437            this.jsonValue = jsonValue;
438        }
439
440        static BoxSignRequestSignerDecisionType fromJSONString(String jsonValue) {
441            if ("signed".equals(jsonValue)) {
442                return Signed;
443            } else if ("declined".equals(jsonValue)) {
444                return Declined;
445            }
446            throw new IllegalArgumentException("The provided JSON value isn't a valid "
447                    + "BoxSignRequestSignerDecisionType.");
448        }
449    }
450
451    /**
452     * Represents a type of input.
453     */
454    public enum BoxSignRequestInputType {
455
456        /**
457         * Signature input.
458         */
459        Signature("signature"),
460
461        /**
462         * Text input.
463         */
464        Text("text"),
465
466        /**
467         * Checkbox input.
468         */
469        Checkbox("checkbox"),
470
471        /**
472         * Date input.
473         */
474        Date("date");
475
476        private final String jsonValue;
477
478        private BoxSignRequestInputType(String jsonValue) {
479            this.jsonValue = jsonValue;
480        }
481
482        static BoxSignRequestInputType fromJSONString(String jsonValue) {
483            if ("signature".equals(jsonValue)) {
484                return Signature;
485            } else if ("text".equals(jsonValue)) {
486                return Text;
487            } else if ("checkbox".equals(jsonValue)) {
488                return Checkbox;
489            } else if ("date".equals(jsonValue)) {
490                return Date;
491            }
492            throw new IllegalArgumentException("The provided JSON value isn't a valid "
493                    + "BoxSignRequestInputType.");
494        }
495    }
496}
497
498