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