|
| 1 | +package com.docusign.controller.webForms.examples; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import javax.servlet.http.HttpServletResponse; |
| 6 | + |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.stereotype.Controller; |
| 9 | +import org.springframework.ui.ModelMap; |
| 10 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 11 | +import org.springframework.web.servlet.view.RedirectView; |
| 12 | + |
| 13 | +import com.docusign.DSConfiguration; |
| 14 | +import com.docusign.common.WorkArguments; |
| 15 | +import com.docusign.controller.eSignature.services.CreateTemplateService; |
| 16 | +import com.docusign.controller.webForms.services.CreateAndEmbedFormService; |
| 17 | +import com.docusign.core.model.Session; |
| 18 | +import com.docusign.core.model.User; |
| 19 | +import com.docusign.esign.client.ApiClient; |
| 20 | +import com.docusign.esign.client.ApiException; |
| 21 | +import com.docusign.esign.model.EnvelopeTemplate; |
| 22 | +import com.docusign.esign.model.EnvelopeTemplateResults; |
| 23 | +import com.docusign.esign.model.TemplateSummary; |
| 24 | +import com.docusign.webforms.model.WebFormInstance; |
| 25 | +import com.docusign.webforms.model.WebFormSummaryList; |
| 26 | + |
| 27 | +@Controller |
| 28 | +@RequestMapping("/web001") |
| 29 | +public class WEB001ControllerCreateAndEmbedForm extends AbstractWebFormsController { |
| 30 | + |
| 31 | + private static final String TEMPLATE_ID = "templateId"; |
| 32 | + |
| 33 | + private static final String TEMPLATE_NAME = "Web Form Example Template"; |
| 34 | + |
| 35 | + private static final String DOCUMENT_FILE_NAME = "World_Wide_Corp_Web_Form.pdf"; |
| 36 | + |
| 37 | + private static final String WEB_FORM_CONFIG = "web-form-config.json"; |
| 38 | + |
| 39 | + public static final String EMBED = "pages/webforms/examples/embed"; |
| 40 | + |
| 41 | + public static final String INSTANCE_TOKEN = "instanceToken"; |
| 42 | + |
| 43 | + public static final String URL = "url"; |
| 44 | + |
| 45 | + public static final String INTEGRATION_KEY = "integrationKey"; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + public WEB001ControllerCreateAndEmbedForm(DSConfiguration config, Session session, User user) { |
| 49 | + super(config, "web001", session, user); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void onInitModel(WorkArguments args, ModelMap model) throws Exception { |
| 54 | + super.onInitModel(args, model); |
| 55 | + model.addAttribute(TEMPLATE_ID, session.getWebformTemplateId()); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Object doWork( |
| 60 | + WorkArguments args, |
| 61 | + ModelMap model, |
| 62 | + HttpServletResponse response |
| 63 | + ) throws ApiException, IOException, com.docusign.webforms.client.ApiException { |
| 64 | + if (session.getWebformTemplateId() == null) { |
| 65 | + ApiClient eSignApiClient = createESignApiClient(session.getBasePath(), user.getAccessToken()); |
| 66 | + String accountId = session.getAccountId(); |
| 67 | + |
| 68 | + EnvelopeTemplateResults envelopeTemplateResults = CreateTemplateService.searchTemplatesByName( |
| 69 | + eSignApiClient, |
| 70 | + accountId, |
| 71 | + TEMPLATE_NAME); |
| 72 | + |
| 73 | + if (Integer.parseInt(envelopeTemplateResults.getResultSetSize()) > 0) { |
| 74 | + EnvelopeTemplate template = envelopeTemplateResults.getEnvelopeTemplates().get(0); |
| 75 | + session.setWebformTemplateId(template.getTemplateId()); |
| 76 | + } else { |
| 77 | + TemplateSummary template = CreateTemplateService.createTemplate( |
| 78 | + eSignApiClient, |
| 79 | + accountId, |
| 80 | + CreateAndEmbedFormService.prepareEnvelopeTemplate(TEMPLATE_NAME, DOCUMENT_FILE_NAME) |
| 81 | + ); |
| 82 | + |
| 83 | + session.setWebformTemplateId(template.getTemplateId()); |
| 84 | + } |
| 85 | + |
| 86 | + model.addAttribute(TEMPLATE_ID, session.getWebformTemplateId()); |
| 87 | + CreateAndEmbedFormService.addTemplateIdToForm(WEB_FORM_CONFIG, session.getWebformTemplateId()); |
| 88 | + |
| 89 | + return new RedirectView("web001"); |
| 90 | + } |
| 91 | + |
| 92 | + var webFormsApiClient = createWebFormsApiClient( |
| 93 | + config.getWebFormsBasePath(), |
| 94 | + user.getAccessToken() |
| 95 | + ); |
| 96 | + |
| 97 | + WebFormSummaryList forms = CreateAndEmbedFormService.getForms( |
| 98 | + webFormsApiClient, |
| 99 | + session.getAccountId(), |
| 100 | + TEMPLATE_NAME |
| 101 | + ); |
| 102 | + |
| 103 | + if (forms.getItems() == null || forms.getItems().size() == 0) { |
| 104 | + return new RedirectView("web001"); |
| 105 | + } |
| 106 | + |
| 107 | + String formId = forms.getItems().get(0).getId(); |
| 108 | + |
| 109 | + WebFormInstance form = CreateAndEmbedFormService.createInstance( |
| 110 | + webFormsApiClient, |
| 111 | + session.getAccountId(), |
| 112 | + formId |
| 113 | + ); |
| 114 | + |
| 115 | + session.setWebformTemplateId(null); |
| 116 | + model.addAttribute(LAUNCHER_TEXTS, config.getCodeExamplesText().SupportingTexts); |
| 117 | + model.addAttribute(INSTANCE_TOKEN, form.getInstanceToken()); |
| 118 | + model.addAttribute(URL, form.getFormUrl()); |
| 119 | + model.addAttribute(INTEGRATION_KEY, config.getUserId()); |
| 120 | + |
| 121 | + return EMBED; |
| 122 | + } |
| 123 | +} |
0 commit comments