|
12 | 12 | import com.docusign.webforms.model.WebFormValues; |
13 | 13 |
|
14 | 14 | import java.io.IOException; |
15 | | -import java.net.URISyntaxException; |
16 | 15 | import java.nio.file.Files; |
17 | 16 | import java.nio.file.Paths; |
18 | 17 | import java.util.List; |
19 | 18 | import java.util.Map; |
20 | | -import java.util.Objects; |
| 19 | + |
| 20 | +import org.springframework.core.io.ClassPathResource; |
| 21 | +import org.springframework.util.StreamUtils; |
21 | 22 |
|
22 | 23 | public final class CreateAndEmbedFormService { |
23 | 24 | public static WebFormSummaryList getForms( |
24 | | - ApiClient apiClient, |
25 | | - String userAccessToken, |
26 | | - String search |
27 | | - ) throws ApiException { |
28 | | - //ds-snippet-start:WebForms1Step3 |
29 | | - FormManagementApi formManagementApi = new FormManagementApi(apiClient); |
| 25 | + ApiClient apiClient, |
| 26 | + String userAccessToken, |
| 27 | + String search) throws ApiException { |
| 28 | + // ds-snippet-start:WebForms1Step3 |
| 29 | + FormManagementApi formManagementApi = new FormManagementApi(apiClient); |
30 | 30 | var option = formManagementApi.new ListFormsOptions(); |
31 | 31 | option.setSearch(search); |
32 | 32 |
|
33 | 33 | return formManagementApi.listForms(userAccessToken, option); |
34 | | - //ds-snippet-end:WebForms1Step3 |
| 34 | + // ds-snippet-end:WebForms1Step3 |
35 | 35 | } |
36 | 36 |
|
37 | 37 | public static void addTemplateIdToForm(String fileName, String templateId) { |
38 | 38 | String targetString = "template-id"; |
39 | 39 |
|
40 | 40 | try { |
41 | | - var templateFilePath = CreateAndEmbedFormService.class.getClassLoader().getResource(fileName); |
42 | | - var templateFile = Paths.get(Objects.requireNonNull(templateFilePath).toURI()); |
| 41 | + ClassPathResource resource = new ClassPathResource(fileName); |
| 42 | + byte[] buffer = StreamUtils.copyToByteArray(resource.getInputStream()); |
43 | 43 |
|
44 | | - String fileContent = new String(Files.readAllBytes(templateFile)); |
| 44 | + String fileContent = new String(buffer); |
45 | 45 | String modifiedContent = fileContent.replace(targetString, templateId); |
46 | 46 |
|
47 | 47 | var basePath = System.getProperty("user.dir"); |
48 | 48 | var configFile = Paths.get(basePath, "demo_documents", fileName); |
49 | 49 |
|
50 | 50 | Files.createDirectories(Paths.get(basePath, "demo_documents")); |
51 | 51 | Files.write(configFile, modifiedContent.getBytes()); |
52 | | - } catch (IOException | URISyntaxException ex) { |
| 52 | + } catch (IOException ex) { |
53 | 53 | System.out.println("An error occurred: " + ex.getMessage()); |
54 | 54 | } |
55 | 55 | } |
56 | 56 |
|
57 | 57 | public static WebFormInstance createInstance( |
58 | | - ApiClient apiClient, |
59 | | - String accountId, |
60 | | - String formId |
61 | | - ) throws ApiException { |
| 58 | + ApiClient apiClient, |
| 59 | + String accountId, |
| 60 | + String formId) throws ApiException { |
62 | 61 |
|
63 | | - //ds-snippet-start:WebForms1Step4 |
64 | | - WebFormValues formValues = new WebFormValues(); |
| 62 | + // ds-snippet-start:WebForms1Step4 |
| 63 | + WebFormValues formValues = new WebFormValues(); |
65 | 64 |
|
66 | 65 | formValues.putAll(Map.of( |
67 | | - "PhoneNumber", "555-555-5555", |
68 | | - "Yes", new String[]{ "Yes" }, |
69 | | - "Company", "Tally", |
70 | | - "JobTitle", "Programmer Writer" |
71 | | - )); |
72 | | - //ds-snippet-end:WebForms1Step4 |
73 | | - |
74 | | - //ds-snippet-start:WebForms1Step5 |
75 | | - FormInstanceManagementApi formManagementApi = new FormInstanceManagementApi(apiClient); |
| 66 | + "PhoneNumber", "555-555-5555", |
| 67 | + "Yes", new String[] { "Yes" }, |
| 68 | + "Company", "Tally", |
| 69 | + "JobTitle", "Programmer Writer")); |
| 70 | + // ds-snippet-end:WebForms1Step4 |
| 71 | + |
| 72 | + // ds-snippet-start:WebForms1Step5 |
| 73 | + FormInstanceManagementApi formManagementApi = new FormInstanceManagementApi(apiClient); |
76 | 74 | String clientUserId = "1234-5678-abcd-ijkl"; |
77 | 75 |
|
78 | 76 | CreateInstanceRequestBody options = new CreateInstanceRequestBody() |
79 | | - .clientUserId(clientUserId) |
80 | | - .formValues(formValues) |
81 | | - .expirationOffset(3600); |
82 | | - |
| 77 | + .clientUserId(clientUserId) |
| 78 | + .formValues(formValues) |
| 79 | + .expirationOffset(3600); |
| 80 | + |
83 | 81 | return formManagementApi.createInstance(accountId, formId, options); |
84 | | - //ds-snippet-end:WebForms1Step5 |
| 82 | + // ds-snippet-end:WebForms1Step5 |
85 | 83 | } |
86 | 84 |
|
87 | 85 | public static EnvelopeTemplate prepareEnvelopeTemplate(String templateName, String documentPdf) throws IOException { |
88 | 86 | Document document = EnvelopeHelpers.createDocumentFromFile( |
89 | 87 | documentPdf, |
90 | 88 | "World_Wide_Web_Form", |
91 | | - "1" |
92 | | - ); |
93 | | - |
| 89 | + "1"); |
| 90 | + |
94 | 91 | Signer signer = new Signer() |
95 | | - .roleName("signer") |
96 | | - .recipientId("1") |
97 | | - .routingOrder("1"); |
| 92 | + .roleName("signer") |
| 93 | + .recipientId("1") |
| 94 | + .routingOrder("1"); |
98 | 95 |
|
99 | 96 | signer.tabs(new Tabs() |
100 | | - .checkboxTabs(List.of( |
101 | | - new Checkbox() |
102 | | - .documentId("1") |
103 | | - .tabLabel("Yes") |
104 | | - .anchorString("/SMS/") |
105 | | - .anchorUnits("pixels") |
106 | | - .anchorXOffset("0") |
107 | | - .anchorYOffset("0") |
108 | | - )) |
109 | | - .signHereTabs(List.of( |
110 | | - new SignHere() |
111 | | - .documentId("1") |
112 | | - .tabLabel("Signature") |
113 | | - .anchorString("/SignHere/") |
114 | | - .anchorUnits("pixels") |
115 | | - .anchorXOffset("20") |
116 | | - .anchorYOffset("10") |
117 | | - )) |
118 | | - .textTabs(List.of( |
119 | | - new Text() |
120 | | - .documentId("1") |
121 | | - .tabLabel("FullName") |
122 | | - .anchorString("/FullName/") |
123 | | - .anchorUnits("pixels") |
124 | | - .anchorXOffset("0") |
125 | | - .anchorYOffset("0"), |
126 | | - new Text() |
127 | | - .documentId("1") |
128 | | - .tabLabel("PhoneNumber") |
129 | | - .anchorString("/PhoneNumber/") |
130 | | - .anchorUnits("pixels") |
131 | | - .anchorXOffset("0") |
132 | | - .anchorYOffset("0"), |
133 | | - new Text() |
134 | | - .documentId("1") |
135 | | - .tabLabel("Company") |
136 | | - .anchorString("/Company/") |
137 | | - .anchorUnits("pixels") |
138 | | - .anchorXOffset("0") |
139 | | - .anchorYOffset("0"), |
140 | | - new Text() |
141 | | - .documentId("1") |
142 | | - .tabLabel("JobTitle") |
143 | | - .anchorString("/Title/") |
144 | | - .anchorUnits("pixels") |
145 | | - .anchorXOffset("0") |
146 | | - .anchorYOffset("0") |
147 | | - )) |
148 | | - .dateSignedTabs(List.of( |
149 | | - new DateSigned() |
150 | | - .documentId("1") |
151 | | - .tabLabel("DateSigned") |
152 | | - .anchorString("/Date/") |
153 | | - .anchorUnits("pixels") |
154 | | - .anchorXOffset("0") |
155 | | - .anchorYOffset("0") |
156 | | - )) |
157 | | - ); |
| 97 | + .checkboxTabs(List.of( |
| 98 | + new Checkbox() |
| 99 | + .documentId("1") |
| 100 | + .tabLabel("Yes") |
| 101 | + .anchorString("/SMS/") |
| 102 | + .anchorUnits("pixels") |
| 103 | + .anchorXOffset("0") |
| 104 | + .anchorYOffset("0"))) |
| 105 | + .signHereTabs(List.of( |
| 106 | + new SignHere() |
| 107 | + .documentId("1") |
| 108 | + .tabLabel("Signature") |
| 109 | + .anchorString("/SignHere/") |
| 110 | + .anchorUnits("pixels") |
| 111 | + .anchorXOffset("20") |
| 112 | + .anchorYOffset("10"))) |
| 113 | + .textTabs(List.of( |
| 114 | + new Text() |
| 115 | + .documentId("1") |
| 116 | + .tabLabel("FullName") |
| 117 | + .anchorString("/FullName/") |
| 118 | + .anchorUnits("pixels") |
| 119 | + .anchorXOffset("0") |
| 120 | + .anchorYOffset("0"), |
| 121 | + new Text() |
| 122 | + .documentId("1") |
| 123 | + .tabLabel("PhoneNumber") |
| 124 | + .anchorString("/PhoneNumber/") |
| 125 | + .anchorUnits("pixels") |
| 126 | + .anchorXOffset("0") |
| 127 | + .anchorYOffset("0"), |
| 128 | + new Text() |
| 129 | + .documentId("1") |
| 130 | + .tabLabel("Company") |
| 131 | + .anchorString("/Company/") |
| 132 | + .anchorUnits("pixels") |
| 133 | + .anchorXOffset("0") |
| 134 | + .anchorYOffset("0"), |
| 135 | + new Text() |
| 136 | + .documentId("1") |
| 137 | + .tabLabel("JobTitle") |
| 138 | + .anchorString("/Title/") |
| 139 | + .anchorUnits("pixels") |
| 140 | + .anchorXOffset("0") |
| 141 | + .anchorYOffset("0"))) |
| 142 | + .dateSignedTabs(List.of( |
| 143 | + new DateSigned() |
| 144 | + .documentId("1") |
| 145 | + .tabLabel("DateSigned") |
| 146 | + .anchorString("/Date/") |
| 147 | + .anchorUnits("pixels") |
| 148 | + .anchorXOffset("0") |
| 149 | + .anchorYOffset("0")))); |
158 | 150 |
|
159 | 151 | Recipients recipients = new Recipients() |
160 | | - .signers(List.of(signer)); |
161 | | - |
| 152 | + .signers(List.of(signer)); |
| 153 | + |
162 | 154 | return new EnvelopeTemplate() |
163 | | - .description("Example template created via the API") |
164 | | - .name(templateName) |
165 | | - .shared("false") |
166 | | - .documents(List.of(document)) |
167 | | - .emailSubject("Please sign this document") |
168 | | - .recipients(recipients) |
169 | | - .status("created"); |
| 155 | + .description("Example template created via the API") |
| 156 | + .name(templateName) |
| 157 | + .shared("false") |
| 158 | + .documents(List.of(document)) |
| 159 | + .emailSubject("Please sign this document") |
| 160 | + .recipients(recipients) |
| 161 | + .status("created"); |
170 | 162 | } |
171 | 163 | } |
0 commit comments