Skip to content

Commit a22430e

Browse files
authored
Merge pull request #163 from docusign/webforms-example
Added new web forms example
2 parents 38ff741 + e5d0f10 commit a22430e

File tree

9 files changed

+1077
-118
lines changed

9 files changed

+1077
-118
lines changed

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ For a list of code examples that use the Web Forms API, see the [How-to guides o
131131

132132
#### Multiple code examples, Authorization Code Grant, and JWT Grant:
133133
1. $ `cd <Quickstart folder>`
134-
1. $ `mvn package -Dmaven.test.skip=true`
135-
1. $ `java -Dspring.profiles.active=dev -jar target/code-examples-java-1.0-SNAPSHOT.war`
136-
For Windows:
137-
1. $ `mvn package -D"maven.test.skip"="true"`
138-
1. $ `java -D"spring.profiles.active"="dev" -jar target/code-examples-java-1.0-SNAPSHOT.war`
134+
1. $ `mvn spring-boot:run`
139135

140136
#### Authorization Code Grant embedded signing example:
141137

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<click.version>1.5.0</click.version>
3232
<monitor.version>1.4.0</monitor.version>
3333
<admin.version>2.0.0</admin.version>
34-
<webforms.version>2.0.0-RC1</webforms.version>
34+
<webforms.version>2.1.0</webforms.version>
3535
<iam.version>0.0.1-alpha.1</iam.version>
3636
<swagger-core-version>2.2.22</swagger-core-version>
3737
<jackson-version>2.17.2</jackson-version>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.docusign.controller.webForms.examples;
2+
3+
import com.docusign.DSConfiguration;
4+
import com.docusign.common.WorkArguments;
5+
import com.docusign.controller.eSignature.services.CreateTemplateService;
6+
import com.docusign.controller.webForms.services.CreateRemoteInstanceService;
7+
import com.docusign.core.model.DoneExample;
8+
import com.docusign.core.model.Session;
9+
import com.docusign.core.model.User;
10+
import com.docusign.esign.client.ApiClient;
11+
import com.docusign.esign.client.ApiException;
12+
import com.docusign.esign.model.EnvelopeTemplate;
13+
import com.docusign.esign.model.EnvelopeTemplateResults;
14+
import com.docusign.esign.model.TemplateSummary;
15+
import com.docusign.webforms.model.WebFormInstance;
16+
import com.docusign.webforms.model.WebFormSummaryList;
17+
import org.springframework.stereotype.Controller;
18+
import org.springframework.ui.ModelMap;
19+
import org.springframework.web.bind.annotation.RequestMapping;
20+
import org.springframework.web.servlet.view.RedirectView;
21+
22+
import javax.servlet.http.HttpServletResponse;
23+
import java.io.IOException;
24+
25+
@Controller
26+
@RequestMapping("/web002")
27+
public class WEB002CreateRemoteInstance extends AbstractWebFormsController {
28+
29+
private static final String TEMPLATE_ID = "templateId";
30+
31+
private static final String TEMPLATE_NAME = "Web Form Example Template";
32+
33+
private static final String DOCUMENT_FILE_NAME = "World_Wide_Corp_Web_Form.pdf";
34+
35+
private static final String WEB_FORM_CONFIG = "web-form-config.json";
36+
37+
private final DSConfiguration configuration;
38+
39+
public WEB002CreateRemoteInstance(DSConfiguration config, Session session, User user) {
40+
super(config, "web002", session, user);
41+
this.configuration = config;
42+
}
43+
44+
@Override
45+
protected void onInitModel(WorkArguments args, ModelMap model) throws Exception {
46+
super.onInitModel(args, model);
47+
model.addAttribute(TEMPLATE_ID, session.getWebformTemplateId());
48+
}
49+
50+
@Override
51+
protected Object doWork(
52+
WorkArguments args,
53+
ModelMap model,
54+
HttpServletResponse response) throws ApiException, IOException, com.docusign.webforms.client.ApiException {
55+
if (session.getIsWebFormsInitialRun()) {
56+
handleInitialRun(model);
57+
return new RedirectView("web002");
58+
}
59+
60+
String accountId = session.getAccountId();
61+
var apiClient = createWebFormsApiClient(
62+
config.getWebFormsBasePath(),
63+
user.getAccessToken());
64+
65+
WebFormSummaryList forms = CreateRemoteInstanceService.getFormsByName(
66+
apiClient,
67+
accountId,
68+
TEMPLATE_NAME);
69+
70+
if (forms.getItems() == null || forms.getItems().isEmpty()) {
71+
throw new ApiException(getTextForCodeExampleByApiType().CustomErrorTexts.get(0).ErrorMessage);
72+
}
73+
74+
String formId = forms.getItems().get(0).getId();
75+
76+
WebFormInstance form = CreateRemoteInstanceService.createInstance(
77+
apiClient,
78+
accountId,
79+
formId,
80+
configuration.getSignerEmail(),
81+
configuration.getSignerName());
82+
83+
session.setIsWebFormsInitialRun(true);
84+
DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName)
85+
.withMessage(getTextForCodeExampleByApiType().ResultsPageText
86+
.replaceFirst("\\{0}", form.getEnvelopes().get(0).getId()).replaceFirst("\\{1}", form.getId())
87+
)
88+
.addToModel(model, config);
89+
return DONE_EXAMPLE_PAGE;
90+
}
91+
92+
private void handleInitialRun(ModelMap model) throws ApiException, IOException {
93+
ApiClient apiClient = createESignApiClient(session.getBasePath(), user.getAccessToken());
94+
String accountId = session.getAccountId();
95+
96+
String templateId = findOrCreateTemplate(apiClient, accountId);
97+
session.setWebformTemplateId(templateId);
98+
session.setIsWebFormsInitialRun(false);
99+
100+
model.addAttribute(TEMPLATE_ID, templateId);
101+
CreateRemoteInstanceService.addTemplateIdToForm(WEB_FORM_CONFIG, templateId);
102+
}
103+
104+
private String findOrCreateTemplate(ApiClient apiClient, String accountId) throws ApiException, IOException {
105+
EnvelopeTemplateResults templateResults = CreateTemplateService.searchTemplatesByName(
106+
apiClient,
107+
accountId,
108+
TEMPLATE_NAME);
109+
110+
if (templateResults.getEnvelopeTemplates() != null && !templateResults.getEnvelopeTemplates().isEmpty()) {
111+
EnvelopeTemplate existingTemplate = templateResults.getEnvelopeTemplates().get(0);
112+
return existingTemplate.getTemplateId();
113+
}
114+
115+
TemplateSummary newTemplate = CreateTemplateService.createTemplate(
116+
apiClient,
117+
accountId,
118+
CreateRemoteInstanceService.prepareEnvelopeTemplate(TEMPLATE_NAME, DOCUMENT_FILE_NAME)
119+
);
120+
return newTemplate.getTemplateId();
121+
}
122+
}

src/main/java/com/docusign/controller/webForms/services/CreateAndEmbedFormService.java

Lines changed: 94 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,65 @@
1212
import com.docusign.webforms.model.WebFormValues;
1313

1414
import java.io.IOException;
15-
import java.net.URISyntaxException;
1615
import java.nio.file.Files;
1716
import java.nio.file.Paths;
1817
import java.util.List;
1918
import java.util.Map;
20-
import java.util.Objects;
19+
20+
import org.springframework.core.io.ClassPathResource;
21+
import org.springframework.util.StreamUtils;
2122

2223
public final class CreateAndEmbedFormService {
2324
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);
3030
var option = formManagementApi.new ListFormsOptions();
3131
option.setSearch(search);
3232

3333
return formManagementApi.listForms(userAccessToken, option);
34-
//ds-snippet-end:WebForms1Step3
34+
// ds-snippet-end:WebForms1Step3
3535
}
3636

3737
public static void addTemplateIdToForm(String fileName, String templateId) {
3838
String targetString = "template-id";
3939

4040
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());
4343

44-
String fileContent = new String(Files.readAllBytes(templateFile));
44+
String fileContent = new String(buffer);
4545
String modifiedContent = fileContent.replace(targetString, templateId);
4646

4747
var basePath = System.getProperty("user.dir");
4848
var configFile = Paths.get(basePath, "demo_documents", fileName);
4949

5050
Files.createDirectories(Paths.get(basePath, "demo_documents"));
5151
Files.write(configFile, modifiedContent.getBytes());
52-
} catch (IOException | URISyntaxException ex) {
52+
} catch (IOException ex) {
5353
System.out.println("An error occurred: " + ex.getMessage());
5454
}
5555
}
5656

5757
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 {
6261

63-
//ds-snippet-start:WebForms1Step4
64-
WebFormValues formValues = new WebFormValues();
62+
// ds-snippet-start:WebForms1Step4
63+
WebFormValues formValues = new WebFormValues();
6564

6665
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);
7674
String clientUserId = "1234-5678-abcd-ijkl";
7775

7876
CreateInstanceRequestBody options = new CreateInstanceRequestBody()
@@ -81,91 +79,85 @@ public static WebFormInstance createInstance(
8179
.expirationOffset(24);
8280

8381
return formManagementApi.createInstance(accountId, formId, options);
84-
//ds-snippet-end:WebForms1Step5
82+
// ds-snippet-end:WebForms1Step5
8583
}
8684

8785
public static EnvelopeTemplate prepareEnvelopeTemplate(String templateName, String documentPdf) throws IOException {
8886
Document document = EnvelopeHelpers.createDocumentFromFile(
8987
documentPdf,
9088
"World_Wide_Web_Form",
91-
"1"
92-
);
93-
89+
"1");
90+
9491
Signer signer = new Signer()
95-
.roleName("signer")
96-
.recipientId("1")
97-
.routingOrder("1");
92+
.roleName("signer")
93+
.recipientId("1")
94+
.routingOrder("1");
9895

9996
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"))));
158150

159151
Recipients recipients = new Recipients()
160-
.signers(List.of(signer));
161-
152+
.signers(List.of(signer));
153+
162154
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");
170162
}
171163
}

0 commit comments

Comments
 (0)