|
| 1 | +package com.docusign.controller.webForms.services; |
| 2 | + |
| 3 | +import com.docusign.controller.eSignature.examples.EnvelopeHelpers; |
| 4 | +import com.docusign.esign.model.*; |
| 5 | +import com.docusign.webforms.api.FormInstanceManagementApi; |
| 6 | +import com.docusign.webforms.api.FormManagementApi; |
| 7 | +import com.docusign.webforms.client.ApiClient; |
| 8 | +import com.docusign.webforms.client.ApiException; |
| 9 | +import com.docusign.webforms.model.*; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public final class CreateRemoteInstanceService { |
| 20 | + |
| 21 | + private static final String ROLE_SIGNER = "signer"; |
| 22 | + |
| 23 | + private static final String DOCUMENT_ID = "1"; |
| 24 | + |
| 25 | + private static final String UNITS = "pixels"; |
| 26 | + |
| 27 | + public static WebFormSummaryList getFormsByName( |
| 28 | + ApiClient apiClient, |
| 29 | + String userAccessToken, |
| 30 | + String name |
| 31 | + ) throws ApiException { |
| 32 | + FormManagementApi formManagementApi = new FormManagementApi(apiClient); |
| 33 | + var option = formManagementApi.new ListFormsOptions(); |
| 34 | + option.setSearch(name); |
| 35 | + |
| 36 | + return formManagementApi.listForms(userAccessToken, option); |
| 37 | + } |
| 38 | + |
| 39 | + public static void addTemplateIdToForm(String fileName, String templateId) { |
| 40 | + try { |
| 41 | + var resourceUrl = CreateRemoteInstanceService.class.getClassLoader().getResource(fileName); |
| 42 | + if (resourceUrl == null) { |
| 43 | + throw new IllegalArgumentException("File not found in resources: " + fileName); |
| 44 | + } |
| 45 | + |
| 46 | + Path templateFile = Paths.get(resourceUrl.toURI()); |
| 47 | + String fileContent = Files.readString(templateFile); |
| 48 | + String modifiedContent = fileContent.replace("template-id", templateId); |
| 49 | + |
| 50 | + Path basePath = Paths.get(System.getProperty("user.dir"), "demo_documents"); |
| 51 | + Files.createDirectories(basePath); |
| 52 | + Files.writeString(basePath.resolve(fileName), modifiedContent); |
| 53 | + } catch (IOException | URISyntaxException ex) { |
| 54 | + System.out.println("An error occurred: " + ex.getMessage()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static WebFormInstance createInstance( |
| 59 | + ApiClient apiClient, |
| 60 | + String accountId, |
| 61 | + String formId, |
| 62 | + String signerEmail, |
| 63 | + String signerName |
| 64 | + ) throws ApiException { |
| 65 | + WebFormValues formValues = new WebFormValues(); |
| 66 | + formValues.putAll(Map.of( |
| 67 | + "PhoneNumber", "555-555-5555", |
| 68 | + "Yes", new String[]{ "Yes" }, |
| 69 | + "Company", "Tally", |
| 70 | + "JobTitle", "Programmer Writer" |
| 71 | + )); |
| 72 | + |
| 73 | + var recipient = new CreateInstanceRequestBodyRecipients() |
| 74 | + .email(signerEmail) |
| 75 | + .name(signerName) |
| 76 | + .roleName(ROLE_SIGNER); |
| 77 | + |
| 78 | + CreateInstanceRequestBody options = new CreateInstanceRequestBody() |
| 79 | + .sendOption(SendOption.NOW) |
| 80 | + .formValues(formValues) |
| 81 | + .recipients(List.of(recipient)); |
| 82 | + |
| 83 | + return new FormInstanceManagementApi(apiClient).createInstance(accountId, formId, options); |
| 84 | + } |
| 85 | + |
| 86 | + public static EnvelopeTemplate prepareEnvelopeTemplate(String templateName, String documentPdf) throws IOException { |
| 87 | + Document document = EnvelopeHelpers.createDocumentFromFile(documentPdf, "World_Wide_Web_Form", DOCUMENT_ID); |
| 88 | + Signer signer = createSigner(); |
| 89 | + Recipients recipients = new Recipients().signers(List.of(signer)); |
| 90 | + |
| 91 | + return new EnvelopeTemplate() |
| 92 | + .description("Example template created via the API") |
| 93 | + .name(templateName) |
| 94 | + .shared("false") |
| 95 | + .documents(List.of(document)) |
| 96 | + .emailSubject("Please sign this document") |
| 97 | + .recipients(recipients) |
| 98 | + .status("created"); |
| 99 | + } |
| 100 | + |
| 101 | + private static Signer createSigner() { |
| 102 | + Signer signer = new Signer() |
| 103 | + .roleName(ROLE_SIGNER) |
| 104 | + .recipientId(DOCUMENT_ID) |
| 105 | + .routingOrder("1"); |
| 106 | + |
| 107 | + signer.tabs(new Tabs() |
| 108 | + .checkboxTabs(List.of( |
| 109 | + new Checkbox() |
| 110 | + .documentId(DOCUMENT_ID) |
| 111 | + .tabLabel("Yes") |
| 112 | + .anchorString("/SMS/") |
| 113 | + .anchorUnits(UNITS) |
| 114 | + .anchorXOffset("0") |
| 115 | + .anchorYOffset("0") |
| 116 | + )) |
| 117 | + .signHereTabs(List.of( |
| 118 | + new SignHere() |
| 119 | + .documentId(DOCUMENT_ID) |
| 120 | + .tabLabel("Signature") |
| 121 | + .anchorString("/SignHere/") |
| 122 | + .anchorUnits(UNITS) |
| 123 | + .anchorXOffset("20") |
| 124 | + .anchorYOffset("10") |
| 125 | + )) |
| 126 | + .textTabs(List.of( |
| 127 | + createText("FullName", "/FullName/"), |
| 128 | + createText("PhoneNumber", "/PhoneNumber/"), |
| 129 | + createText("Company", "/Company/"), |
| 130 | + createText("JobTitle", "/Title/") |
| 131 | + )) |
| 132 | + .dateSignedTabs(List.of( |
| 133 | + new DateSigned() |
| 134 | + .documentId(DOCUMENT_ID) |
| 135 | + .tabLabel("DateSigned") |
| 136 | + .anchorString("/Date/") |
| 137 | + .anchorUnits(UNITS) |
| 138 | + .anchorXOffset("0") |
| 139 | + .anchorYOffset("0") |
| 140 | + )) |
| 141 | + ); |
| 142 | + return signer; |
| 143 | + } |
| 144 | + |
| 145 | + private static Text createText(String tabLabel, String anchor) { |
| 146 | + return new Text() |
| 147 | + .documentId(DOCUMENT_ID) |
| 148 | + .tabLabel(tabLabel) |
| 149 | + .anchorString(anchor) |
| 150 | + .anchorUnits(UNITS) |
| 151 | + .anchorXOffset("0") |
| 152 | + .anchorYOffset("0"); |
| 153 | + } |
| 154 | +} |
0 commit comments