|
| 1 | +package com.docusign.controller.maestro.examples; |
| 2 | + |
| 3 | +import com.docusign.DSConfiguration; |
| 4 | +import com.docusign.common.WorkArguments; |
| 5 | +import com.docusign.controller.maestro.services.CreateWorkflowService; |
| 6 | +import com.docusign.controller.maestro.services.TriggerWorkflowService; |
| 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.maestro.client.ApiException; |
| 11 | +import com.docusign.maestro.api.WorkflowManagementApi; |
| 12 | +import com.docusign.maestro.client.ApiClient; |
| 13 | +import com.docusign.maestro.model.NewOrUpdatedWorkflowDefinitionResponse; |
| 14 | +import com.docusign.maestro.model.WorkflowDefinitionList; |
| 15 | +import com.docusign.maestro.model.WorkflowDefinitionMetadata; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.stereotype.Controller; |
| 18 | +import org.springframework.ui.ModelMap; |
| 19 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 20 | + |
| 21 | +import javax.servlet.http.HttpServletResponse; |
| 22 | +import java.io.IOException; |
| 23 | +import java.net.URISyntaxException; |
| 24 | +import java.security.InvalidKeyException; |
| 25 | +import java.security.NoSuchAlgorithmException; |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +@Controller |
| 29 | +@RequestMapping("/mae001") |
| 30 | +public class EG001ControllerTriggerWorkflow extends AbstractMaestroController { |
| 31 | + |
| 32 | + private static final String MODEL_TEMPLATE_ID = "templateId"; |
| 33 | + |
| 34 | + private static final String MODEL_WORKFLOW_ID = "workflowId"; |
| 35 | + |
| 36 | + private static final String MODEL_PUBLISH_LINK_ID = "publishLink"; |
| 37 | + |
| 38 | + public static final String WORKFLOW_NAME = "Example workflow - send invite to signer"; |
| 39 | + |
| 40 | + @Autowired |
| 41 | + public EG001ControllerTriggerWorkflow(DSConfiguration config, Session session, User user) { |
| 42 | + super(config, "mae001", session, user); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void onInitModel(WorkArguments args, ModelMap model) throws Exception { |
| 47 | + super.onInitModel(args, model); |
| 48 | + ApiClient apiClient = createApiClient(config.getMaestroBasePath(), user.getAccessToken()); |
| 49 | + String accountId = session.getAccountId(); |
| 50 | + String templateId = session.getTemplateId(); |
| 51 | + |
| 52 | + try { |
| 53 | + WorkflowDefinitionList workflowDefinition = TriggerWorkflowService.getWorkflowDefinitions( |
| 54 | + apiClient, |
| 55 | + accountId); |
| 56 | + |
| 57 | + if (workflowDefinition.getValue() != null && !workflowDefinition.getValue().isEmpty()) { |
| 58 | + Optional<WorkflowDefinitionMetadata> workflow = workflowDefinition |
| 59 | + .getValue() |
| 60 | + .stream() |
| 61 | + .filter(x -> x.getName().equals(WORKFLOW_NAME)) |
| 62 | + .min((x, y) -> y.getLastUpdatedDate().compareTo(x.getLastUpdatedDate())); |
| 63 | + |
| 64 | + workflow.ifPresent(workflowDefinitionMetadata -> session.setWorkflowId(workflowDefinitionMetadata.getId())); |
| 65 | + } |
| 66 | + |
| 67 | + if (session.getIsWorkflowPublished()) { |
| 68 | + String publishLink = TriggerWorkflowService.publishWorkFlow( |
| 69 | + apiClient, |
| 70 | + accountId, |
| 71 | + session.getWorkflowId()); |
| 72 | + |
| 73 | + if (!publishLink.isEmpty()) { |
| 74 | + model.addAttribute(MODEL_PUBLISH_LINK_ID, getTextForCodeExampleByApiType().getAdditionalPage() |
| 75 | + .get(0).getResultsPageText().replaceFirst("\\{0}", publishLink)); |
| 76 | + } else { |
| 77 | + session.setIsWorkflowPublished(false); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (session.getWorkflowId() == null && templateId != null) { |
| 82 | + WorkflowManagementApi managementApi = new WorkflowManagementApi(apiClient); |
| 83 | + |
| 84 | + NewOrUpdatedWorkflowDefinitionResponse createWorkflow = CreateWorkflowService.createWorkflowDefinition( |
| 85 | + managementApi, |
| 86 | + accountId, |
| 87 | + templateId); |
| 88 | + |
| 89 | + session.setWorkflowId(createWorkflow.getWorkflowDefinitionId()); |
| 90 | + |
| 91 | + String publishLink = TriggerWorkflowService.publishWorkFlow( |
| 92 | + apiClient, |
| 93 | + accountId, |
| 94 | + session.getWorkflowId()); |
| 95 | + |
| 96 | + session.setIsWorkflowPublished(true); |
| 97 | + model.addAttribute(MODEL_PUBLISH_LINK_ID, getTextForCodeExampleByApiType().getAdditionalPage().get(0) |
| 98 | + .getResultsPageText().replaceFirst("\\{0}", publishLink)); |
| 99 | + } |
| 100 | + } catch(ApiException exception) { |
| 101 | + if(exception.getCode() == 403) { |
| 102 | + model.addAttribute(MODEL_PUBLISH_LINK_ID, config.getCodeExamplesText().SupportingTexts.ContactSupportToEnableFeature |
| 103 | + .replaceFirst("\\{0}", "Maestro")); |
| 104 | + } else { |
| 105 | + throw exception; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + model.addAttribute(MODEL_TEMPLATE_ID, templateId); |
| 110 | + model.addAttribute(MODEL_WORKFLOW_ID, session.getWorkflowId()); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected Object doWork( |
| 115 | + WorkArguments args, |
| 116 | + ModelMap model, |
| 117 | + HttpServletResponse response |
| 118 | + ) throws ApiException, IOException, NoSuchAlgorithmException, InvalidKeyException, URISyntaxException { |
| 119 | + //ds-snippet-start:Maestro1Step2 |
| 120 | + ApiClient apiClient = createApiClient(config.getMaestroBasePath(), user.getAccessToken()); |
| 121 | + //ds-snippet-end:Maestro1Step2 |
| 122 | + String accountId = session.getAccountId(); |
| 123 | + |
| 124 | + //ds-snippet-start:Maestro1Step3 |
| 125 | + var workflow = TriggerWorkflowService.getWorkflowDefinition( |
| 126 | + apiClient, |
| 127 | + accountId, |
| 128 | + session.getWorkflowId()); |
| 129 | + //ds-snippet-end:Maestro1Step3 |
| 130 | + |
| 131 | + var result = TriggerWorkflowService.triggerWorkflow( |
| 132 | + apiClient, |
| 133 | + accountId, |
| 134 | + workflow.getTriggerUrl(), |
| 135 | + args.getSignerEmail(), |
| 136 | + args.getSignerName(), |
| 137 | + args.getCcEmail(), |
| 138 | + args.getCcName(), |
| 139 | + args.getInstanceName()); |
| 140 | + |
| 141 | + session.setInstanceId(result.getInstanceId()); |
| 142 | + |
| 143 | + DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName) |
| 144 | + .withMessage(getTextForCodeExampleByApiType().ResultsPageText) |
| 145 | + .withJsonObject(result) |
| 146 | + .addToModel(model, config); |
| 147 | + return DONE_EXAMPLE_PAGE; |
| 148 | + } |
| 149 | +} |
0 commit comments