|
1 | | -import com.fasterxml.jackson.core.JsonProcessingException; |
2 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
| 1 | +import com.signnow.api.document.request.DocumentPostRequest; |
| 2 | +import com.signnow.api.document.request.DocumentPutRequest; |
| 3 | +import com.signnow.api.document.request.data.Field; |
| 4 | +import com.signnow.api.document.request.data.FieldCollection; |
| 5 | +import com.signnow.api.document.response.DocumentPostResponse; |
| 6 | +import com.signnow.api.documentgroup.request.DocumentGroupGetRequest; |
| 7 | +import com.signnow.api.documentgroup.request.DocumentGroupPostRequest; |
| 8 | +import com.signnow.api.documentgroup.request.data.DocumentIdCollection; |
| 9 | +import com.signnow.api.documentgroup.response.DocumentGroupGetResponse; |
| 10 | +import com.signnow.api.documentgroup.response.DocumentGroupPostResponse; |
3 | 11 | import com.signnow.api.documentgroup.request.DocumentGroupRecipientsGetRequest; |
4 | 12 | import com.signnow.api.documentgroup.request.DocumentGroupRecipientsPutRequest; |
5 | 13 | import com.signnow.api.documentgroup.request.data.CcCollection; |
| 14 | +import com.signnow.api.documentgroup.request.data.GeneralReminder; |
| 15 | +import com.signnow.api.documentgroup.request.data.recipient.Attribute; |
| 16 | +import com.signnow.api.documentgroup.request.data.recipient.Document; |
| 17 | +import com.signnow.api.documentgroup.request.data.recipient.DocumentCollection; |
6 | 18 | import com.signnow.api.documentgroup.request.data.recipient.Recipient; |
7 | 19 | import com.signnow.api.documentgroup.request.data.recipient.RecipientCollection; |
8 | 20 | import com.signnow.api.documentgroup.response.DocumentGroupRecipientsGetResponse; |
9 | 21 | import com.signnow.api.documentgroup.response.DocumentGroupRecipientsPutResponse; |
10 | 22 | import com.signnow.core.ApiClient; |
11 | 23 | import com.signnow.core.exception.SignNowApiException; |
12 | 24 | import com.signnow.core.factory.SdkFactory; |
| 25 | +import java.io.File; |
13 | 26 |
|
14 | 27 | public class DocumentGroupRecipientsExample { |
15 | 28 | public static void main(String[] args) { |
16 | | - // Set your actual input data here |
17 | | - // Note: following values are dummy, just for example |
18 | | - // ---------------------------------------------------- |
19 | | - // if it is not specified here, a new Bearer token will be created automatically |
20 | | - String bearerToken = ""; |
21 | | - String documentGroupId = "5d66ca4accdd4ab28f8b2c71001093b5cb3bcb8a"; |
| 29 | + // Fill in your actual data in examples/signnow-example.properties before running |
| 30 | + SignNowExampleData data = new SignNowExampleData(); |
| 31 | + String bearerToken = data.getBearerToken(); |
| 32 | + String pathToDocument = data.getPathToDocument(); |
| 33 | + String firstRecipientRole = data.getFirstRecipientRole(); |
| 34 | + String secondRecipientRole = data.getSecondRecipientRole(); |
| 35 | + String signAction = data.getSignAction(); |
| 36 | + String viewAction = data.getViewAction(); |
| 37 | + // Set your actual input data here, or use these as examples |
| 38 | + String groupName = "Test Document Group Recipients Example"; |
| 39 | + String recipientOrder = "recipient_order"; // at_the_same_time, recipient_order, advanced_order |
| 40 | + int expirationDays = 31; |
22 | 41 |
|
23 | 42 | try { |
24 | 43 | ApiClient client = SdkFactory.createApiClientWithBearerToken(bearerToken); |
25 | 44 |
|
26 | | - // Get recipient |
27 | | - DocumentGroupRecipientsGetRequest request = new DocumentGroupRecipientsGetRequest() |
28 | | - .withDocumentGroupId(documentGroupId); |
29 | | - DocumentGroupRecipientsGetResponse response = (DocumentGroupRecipientsGetResponse) client.send(request) |
30 | | - .getResponse(); |
| 45 | + // Precondition: |
| 46 | + // the document group contains 2 documents both with a field assigned a signer role |
| 47 | + //---------------------------------------------------- |
| 48 | + // Step 1: Upload two documents to become a document group |
| 49 | + DocumentPostRequest request = new DocumentPostRequest(new File(pathToDocument)); |
| 50 | + DocumentPostResponse response = (DocumentPostResponse) client.send(request).getResponse(); |
| 51 | + String documentId1 = response.getId(); |
| 52 | + // Step 2: Add fields with roles to the 1st document |
| 53 | + FieldCollection fields = new FieldCollection(); |
| 54 | + fields.add( |
| 55 | + new Field(205, 18, 122, 12, "text", 0, true, firstRecipientRole, "text_field1", "Reason to sign")); |
| 56 | + DocumentPutRequest putRequest1 = new DocumentPutRequest(fields); |
| 57 | + putRequest1.withDocumentId(documentId1); |
| 58 | + client.send(putRequest1); |
| 59 | + // Step 3: Upload 2nd document |
| 60 | + DocumentPostRequest request2 = new DocumentPostRequest(new File(pathToDocument)); |
| 61 | + DocumentPostResponse response2 = (DocumentPostResponse) client.send(request2).getResponse(); |
| 62 | + String documentId2 = response2.getId(); |
| 63 | + // Step 4: Add fields and roles to the 2nd document |
| 64 | + FieldCollection fields2 = new FieldCollection(); |
| 65 | + fields2.add( |
| 66 | + new Field(220, 24, 142, 14, "text", 0, true, secondRecipientRole, "text_field2", "My reason")); |
| 67 | + DocumentPutRequest putRequest2 = new DocumentPutRequest(fields2); |
| 68 | + putRequest2.withDocumentId(documentId2); |
| 69 | + client.send(putRequest2); |
| 70 | + // Step 5: Create document group |
| 71 | + DocumentIdCollection documentIds = new DocumentIdCollection(); |
| 72 | + documentIds.add(documentId1); |
| 73 | + documentIds.add(documentId2); |
| 74 | + DocumentGroupPostRequest groupRequest = new DocumentGroupPostRequest(documentIds, groupName); |
| 75 | + DocumentGroupPostResponse groupResponse = |
| 76 | + (DocumentGroupPostResponse) client.send(groupRequest).getResponse(); |
| 77 | + String documentGroupId = groupResponse.getId(); |
31 | 78 |
|
32 | | - // Convert to request model and replace email |
33 | | - RecipientCollection recipientCollection = new RecipientCollection(); |
34 | | - for (com.signnow.api.documentgroup.response.data.data.Recipient responseRecipient : response.getData() |
35 | | - .getRecipients()) { |
36 | | - Recipient reqRecipient = convertRecipient(responseRecipient); |
37 | | - if (reqRecipient == null) continue; |
38 | | - |
39 | | - System.out.println("Original name: " + responseRecipient.getName()); |
40 | | - System.out.println("Original email: " + responseRecipient.getEmail()); |
| 79 | + // PUT /v2/document-groups/{document_group_id}/recipients |
| 80 | + DocumentCollection docs1 = new DocumentCollection(); |
| 81 | + // Signer recipient: should review and sign the document |
| 82 | + docs1.add(new Document(documentId1, firstRecipientRole, signAction)); |
| 83 | + Attribute signerAttribute = Attribute.signer(false, true, false, "Please sign", "Signing request", null, null, null); |
| 84 | + Recipient recipient1 = new Recipient("John Doe", "john.doe@example.com", 1, docs1, null, signerAttribute); |
41 | 85 |
|
42 | | - if (reqRecipient.getName().equals("Recipient 1")) { |
43 | | - // Replace email |
44 | | - Recipient updated = new Recipient( |
45 | | - reqRecipient.getName(), |
46 | | - "new.email@example.com", |
47 | | - reqRecipient.getOrder(), |
48 | | - reqRecipient.getDocuments(), |
49 | | - reqRecipient.getEmailGroup(), |
50 | | - reqRecipient.getAttributes()); |
51 | | - recipientCollection.add(updated); |
52 | | - } else { |
53 | | - recipientCollection.add(reqRecipient); |
54 | | - } |
55 | | - } |
| 86 | + DocumentCollection docs2 = new DocumentCollection(); |
| 87 | + // Viewer recipient: no signature, only view the document |
| 88 | + docs2.add(new Document(documentId2, secondRecipientRole, viewAction)); |
| 89 | + Attribute viewerAttribute = Attribute.viewer("Please review", "Document review", false, null, null, null, null); |
| 90 | + Recipient recipient2 = new Recipient("Jane Smith", "jane.smith@example.com", 2, docs2, null, viewerAttribute); |
| 91 | + |
| 92 | + RecipientCollection recipients = new RecipientCollection(); |
| 93 | + recipients.add(recipient1); |
| 94 | + recipients.add(recipient2); |
| 95 | + |
| 96 | + GeneralReminder generalReminder = new GeneralReminder(1, 5, 3); |
56 | 97 |
|
57 | | - // Send PUT request |
58 | 98 | DocumentGroupRecipientsPutRequest putRequest = new DocumentGroupRecipientsPutRequest( |
59 | | - recipientCollection, |
60 | | - new CcCollection()).withDocumentGroupId(documentGroupId); |
| 99 | + recipients, |
| 100 | + new CcCollection(), |
| 101 | + expirationDays, |
| 102 | + generalReminder, |
| 103 | + recipientOrder |
| 104 | + ).withDocumentGroupId(documentGroupId); |
61 | 105 |
|
62 | 106 | DocumentGroupRecipientsPutResponse putResponse = (DocumentGroupRecipientsPutResponse) client.send(putRequest) |
63 | 107 | .getResponse(); |
64 | | - System.out.println("Updated email: " + putResponse.getData().getRecipients().get(0).getEmail()); |
| 108 | + System.out.println("==== PUT ===="); |
| 109 | + System.out.println("Updated recipients:"); |
| 110 | + putResponse.getData().getRecipients().forEach(r -> { |
| 111 | + System.out.println(" Name: " + r.getName() + ", Email: " + r.getEmail()); |
| 112 | + }); |
| 113 | + if (putResponse.getData().getGeneralExpirationDays() != null) { |
| 114 | + System.out.println("General Expiration Days: " + putResponse.getData().getGeneralExpirationDays()); |
| 115 | + } |
| 116 | + if (putResponse.getData().getGeneralReminder() != null) { |
| 117 | + com.signnow.api.documentgroup.response.data.data.GeneralReminder responseReminder = putResponse.getData().getGeneralReminder(); |
| 118 | + System.out.println("General Reminder - After: " + responseReminder.getRemindAfter() |
| 119 | + + ", Before: " + responseReminder.getRemindBefore() |
| 120 | + + ", Repeat: " + responseReminder.getRemindRepeat()); |
| 121 | + } |
| 122 | + if (putResponse.getData().getOrderType() != null) { |
| 123 | + System.out.println("Order Type: " + putResponse.getData().getOrderType()); |
| 124 | + } |
65 | 125 |
|
| 126 | + // GET /v2/document-groups/{document_group_id}/recipients |
| 127 | + System.out.println("==== GET ===="); |
| 128 | + DocumentGroupRecipientsGetRequest getRequest = new DocumentGroupRecipientsGetRequest() |
| 129 | + .withDocumentGroupId(documentGroupId); |
| 130 | + DocumentGroupRecipientsGetResponse getResponse = (DocumentGroupRecipientsGetResponse) client.send(getRequest) |
| 131 | + .getResponse(); |
| 132 | + System.out.println("Recipients:"); |
| 133 | + getResponse.getData().getRecipients().forEach(r -> { |
| 134 | + System.out.println(" Name: " + r.getName() + ", Email: " + r.getEmail()); |
| 135 | + }); |
| 136 | + if (getResponse.getData().getGeneralExpirationDays() != null) { |
| 137 | + System.out.println("General Expiration Days: " + getResponse.getData().getGeneralExpirationDays()); |
| 138 | + } |
| 139 | + if (getResponse.getData().getGeneralReminder() != null) { |
| 140 | + com.signnow.api.documentgroup.response.data.data.GeneralReminder responseReminder = getResponse.getData().getGeneralReminder(); |
| 141 | + System.out.println("General Reminder - After: " + responseReminder.getRemindAfter() |
| 142 | + + ", Before: " + responseReminder.getRemindBefore() |
| 143 | + + ", Repeat: " + responseReminder.getRemindRepeat()); |
| 144 | + } |
| 145 | + if (getResponse.getData().getOrderType() != null) { |
| 146 | + System.out.println("Order Type: " + getResponse.getData().getOrderType()); |
| 147 | + } |
66 | 148 | } catch (SignNowApiException e) { |
67 | 149 | System.out.println("ERROR: " + e.getMessage()); |
68 | 150 | } |
69 | 151 | } |
70 | | - |
71 | | - private static Recipient convertRecipient( |
72 | | - com.signnow.api.documentgroup.response.data.data.Recipient responseRecipient) { |
73 | | - ObjectMapper mapper = new ObjectMapper(); |
74 | | - try { |
75 | | - String json = mapper.writeValueAsString(responseRecipient); |
76 | | - Recipient recipient = mapper.readValue(json, Recipient.class); |
77 | | - |
78 | | - return new Recipient( |
79 | | - recipient.getName(), |
80 | | - recipient.getEmail(), |
81 | | - recipient.getOrder(), |
82 | | - recipient.getDocuments(), |
83 | | - recipient.getEmailGroup(), |
84 | | - recipient.getAttributes()); |
85 | | - |
86 | | - } catch (JsonProcessingException e) { |
87 | | - e.printStackTrace(); |
88 | | - return null; |
89 | | - } |
90 | | - } |
91 | 152 | } |
0 commit comments