Skip to content

Commit 0ddb1e3

Browse files
committed
Added new web forms example
1 parent 2016b8f commit 0ddb1e3

File tree

7 files changed

+985
-11
lines changed

7 files changed

+985
-11
lines changed

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+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}

src/main/java/com/docusign/core/model/Session.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,6 @@ public class Session implements Serializable {
7777
private String instanceId;
7878

7979
private Boolean isPKCEWorking = true;
80+
81+
private Boolean isWebFormsInitialRun = true;
8082
}

src/main/resources/web-form-config.json

Lines changed: 673 additions & 1 deletion
Large diffs are not rendered by default.

src/main/webapp/WEB-INF/templates/views/pages/webforms/examples/web001.jsp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55
<c:choose>
66
<c:when test="${templateId == null}">
77
<p>${example.getExampleDescription()}</p>
8-
<p>
9-
API methods used:
10-
<a target='_blank' href="https://developers.docusign.com/docs/esign-rest-api/reference/templates/templates/list/">
11-
Templates::list
12-
</a>, and
13-
<a target='_blank' href="https://developers.docusign.com/docs/esign-rest-api/reference/templates/templates/create/">
14-
Templates::create
15-
</a>.
16-
</p>
8+
<jsp:include page="../../links_to_api_methods.jsp" />
179
<p>
1810
${viewSourceFile}
1911
</p>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2+
<jsp:include page="../../../partials/head.jsp"/>
3+
4+
<h4>${example.getExampleName()}</h4>
5+
<c:choose>
6+
<c:when test="${templateId == null}">
7+
<p>${example.getExampleDescription()}</p>
8+
<jsp:include page="../../links_to_api_methods.jsp" />
9+
10+
<p>
11+
${viewSourceFile}
12+
</p>
13+
<form class="eg" action="" method="post" data-busy="form">
14+
<input type="hidden" name="_csrf" value="${csrfToken}">
15+
<button type="submit" class="btn btn-docu">${launcherTexts.getSubmitButton()}</button>
16+
</form>
17+
</c:when>
18+
<c:when test="${templateId != null}">
19+
<c:forEach var="page" items="${example.getAdditionalPage()}">
20+
<c:if test="${page.getName() == 'create_web_form'}">
21+
<p>${page.getResultsPageText().replaceFirst("\\{0}", "src/main/resources")}</p>
22+
</c:if>
23+
</c:forEach>
24+
<form class="eg" action="" method="post" data-busy="form">
25+
<input type="hidden" name="_csrf" value="${csrfToken}">
26+
<button type="submit" class="btn btn-docu">${launcherTexts.getContinueButton()}</button>
27+
</form>
28+
</c:when>
29+
</c:choose>
30+
31+
32+
<jsp:include page="../../../partials/foot.jsp"/>

0 commit comments

Comments
 (0)