|
| 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 | +} |
0 commit comments