|
| 1 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 2 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 3 | +import com.signnow.api.documentgroup.request.DocumentGroupRecipientsGetRequest; |
| 4 | +import com.signnow.api.documentgroup.request.DocumentGroupRecipientsPutRequest; |
| 5 | +import com.signnow.api.documentgroup.request.data.CcCollection; |
| 6 | +import com.signnow.api.documentgroup.request.data.recipient.Recipient; |
| 7 | +import com.signnow.api.documentgroup.request.data.recipient.RecipientCollection; |
| 8 | +import com.signnow.api.documentgroup.response.DocumentGroupRecipientsGetResponse; |
| 9 | +import com.signnow.api.documentgroup.response.DocumentGroupRecipientsPutResponse; |
| 10 | +import com.signnow.core.ApiClient; |
| 11 | +import com.signnow.core.exception.SignNowApiException; |
| 12 | +import com.signnow.core.factory.SdkFactory; |
| 13 | + |
| 14 | +public class DocumentGroupRecipientsExample { |
| 15 | + 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"; |
| 22 | + |
| 23 | + try { |
| 24 | + ApiClient client = SdkFactory.createApiClientWithBearerToken(bearerToken); |
| 25 | + |
| 26 | + // Get recipient |
| 27 | + DocumentGroupRecipientsGetRequest request = new DocumentGroupRecipientsGetRequest() |
| 28 | + .withDocumentGroupId(documentGroupId); |
| 29 | + DocumentGroupRecipientsGetResponse response = (DocumentGroupRecipientsGetResponse) client.send(request) |
| 30 | + .getResponse(); |
| 31 | + |
| 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()); |
| 41 | + |
| 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 | + } |
| 56 | + |
| 57 | + // Send PUT request |
| 58 | + DocumentGroupRecipientsPutRequest putRequest = new DocumentGroupRecipientsPutRequest( |
| 59 | + recipientCollection, |
| 60 | + new CcCollection()).withDocumentGroupId(documentGroupId); |
| 61 | + |
| 62 | + DocumentGroupRecipientsPutResponse putResponse = (DocumentGroupRecipientsPutResponse) client.send(putRequest) |
| 63 | + .getResponse(); |
| 64 | + System.out.println("Updated email: " + putResponse.getData().getRecipients().get(0).getEmail()); |
| 65 | + |
| 66 | + } catch (SignNowApiException e) { |
| 67 | + System.out.println("ERROR: " + e.getMessage()); |
| 68 | + } |
| 69 | + } |
| 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 | +} |
0 commit comments