-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from docusign/feature/add-maestro-examples
Prepared Maestro examples
- Loading branch information
Showing
22 changed files
with
1,410 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/docusign/controller/maestro/examples/AbstractMaestroController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.docusign.controller.maestro.examples; | ||
|
||
import com.docusign.DSConfiguration; | ||
import com.docusign.maestro.client.ApiClient; | ||
import com.docusign.core.controller.AbstractController; | ||
import com.docusign.core.model.Session; | ||
import com.docusign.core.model.User; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Controller; | ||
|
||
/** | ||
* Abstract base class for all Maestro controllers. | ||
*/ | ||
@Controller | ||
public abstract class AbstractMaestroController extends AbstractController { | ||
|
||
private static final String EXAMPLE_PAGES_PATH = "pages/maestro/examples/"; | ||
|
||
protected final Session session; | ||
|
||
protected final User user; | ||
|
||
public AbstractMaestroController(DSConfiguration config, String exampleName, Session session, User user) { | ||
super(config, exampleName); | ||
this.session = session; | ||
this.user = user; | ||
} | ||
|
||
/** | ||
* Creates new instance of the Maestro API client. | ||
* | ||
* @param basePath URL to Maestro REST API | ||
* @param userAccessToken user's access token | ||
* @return an instance of the {@link ApiClient} | ||
*/ | ||
protected static ApiClient createApiClient(String basePath, String userAccessToken) { | ||
ApiClient apiClient = new ApiClient(basePath); | ||
apiClient.addDefaultHeader(HttpHeaders.AUTHORIZATION, BEARER_AUTHENTICATION + userAccessToken); | ||
return apiClient; | ||
} | ||
|
||
protected String getExamplePagesPath() { | ||
return AbstractMaestroController.EXAMPLE_PAGES_PATH; | ||
} | ||
} |
149 changes: 149 additions & 0 deletions
149
src/main/java/com/docusign/controller/maestro/examples/EG001ControllerTriggerWorkflow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package com.docusign.controller.maestro.examples; | ||
|
||
import com.docusign.DSConfiguration; | ||
import com.docusign.common.WorkArguments; | ||
import com.docusign.controller.maestro.services.CreateWorkflowService; | ||
import com.docusign.controller.maestro.services.TriggerWorkflowService; | ||
import com.docusign.core.model.DoneExample; | ||
import com.docusign.core.model.Session; | ||
import com.docusign.core.model.User; | ||
import com.docusign.maestro.client.ApiException; | ||
import com.docusign.maestro.api.WorkflowManagementApi; | ||
import com.docusign.maestro.client.ApiClient; | ||
import com.docusign.maestro.model.NewOrUpdatedWorkflowDefinitionResponse; | ||
import com.docusign.maestro.model.WorkflowDefinitionList; | ||
import com.docusign.maestro.model.WorkflowDefinitionMetadata; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.ModelMap; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Optional; | ||
|
||
@Controller | ||
@RequestMapping("/mae001") | ||
public class EG001ControllerTriggerWorkflow extends AbstractMaestroController { | ||
|
||
private static final String MODEL_TEMPLATE_ID = "templateId"; | ||
|
||
private static final String MODEL_WORKFLOW_ID = "workflowId"; | ||
|
||
private static final String MODEL_PUBLISH_LINK_ID = "publishLink"; | ||
|
||
public static final String WORKFLOW_NAME = "Example workflow - send invite to signer"; | ||
|
||
@Autowired | ||
public EG001ControllerTriggerWorkflow(DSConfiguration config, Session session, User user) { | ||
super(config, "mae001", session, user); | ||
} | ||
|
||
@Override | ||
protected void onInitModel(WorkArguments args, ModelMap model) throws Exception { | ||
super.onInitModel(args, model); | ||
ApiClient apiClient = createApiClient(config.getMaestroBasePath(), user.getAccessToken()); | ||
String accountId = session.getAccountId(); | ||
String templateId = session.getTemplateId(); | ||
|
||
try { | ||
WorkflowDefinitionList workflowDefinition = TriggerWorkflowService.getWorkflowDefinitions( | ||
apiClient, | ||
accountId); | ||
|
||
if (workflowDefinition.getValue() != null && !workflowDefinition.getValue().isEmpty()) { | ||
Optional<WorkflowDefinitionMetadata> workflow = workflowDefinition | ||
.getValue() | ||
.stream() | ||
.filter(x -> x.getName().equals(WORKFLOW_NAME)) | ||
.min((x, y) -> y.getLastUpdatedDate().compareTo(x.getLastUpdatedDate())); | ||
|
||
workflow.ifPresent(workflowDefinitionMetadata -> session.setWorkflowId(workflowDefinitionMetadata.getId())); | ||
} | ||
|
||
if (session.getIsWorkflowPublished()) { | ||
String publishLink = TriggerWorkflowService.publishWorkFlow( | ||
apiClient, | ||
accountId, | ||
session.getWorkflowId()); | ||
|
||
if (!publishLink.isEmpty()) { | ||
model.addAttribute(MODEL_PUBLISH_LINK_ID, getTextForCodeExampleByApiType().getAdditionalPage() | ||
.get(0).getResultsPageText().replaceFirst("\\{0}", publishLink)); | ||
} else { | ||
session.setIsWorkflowPublished(false); | ||
} | ||
} | ||
|
||
if (session.getWorkflowId() == null && templateId != null) { | ||
WorkflowManagementApi managementApi = new WorkflowManagementApi(apiClient); | ||
|
||
NewOrUpdatedWorkflowDefinitionResponse createWorkflow = CreateWorkflowService.createWorkflowDefinition( | ||
managementApi, | ||
accountId, | ||
templateId); | ||
|
||
session.setWorkflowId(createWorkflow.getWorkflowDefinitionId()); | ||
|
||
String publishLink = TriggerWorkflowService.publishWorkFlow( | ||
apiClient, | ||
accountId, | ||
session.getWorkflowId()); | ||
|
||
session.setIsWorkflowPublished(true); | ||
model.addAttribute(MODEL_PUBLISH_LINK_ID, getTextForCodeExampleByApiType().getAdditionalPage().get(0) | ||
.getResultsPageText().replaceFirst("\\{0}", publishLink)); | ||
} | ||
} catch(ApiException exception) { | ||
if(exception.getCode() == 403) { | ||
model.addAttribute(MODEL_PUBLISH_LINK_ID, config.getCodeExamplesText().SupportingTexts.ContactSupportToEnableFeature | ||
.replaceFirst("\\{0}", "Maestro")); | ||
} else { | ||
throw exception; | ||
} | ||
} | ||
|
||
model.addAttribute(MODEL_TEMPLATE_ID, templateId); | ||
model.addAttribute(MODEL_WORKFLOW_ID, session.getWorkflowId()); | ||
} | ||
|
||
@Override | ||
protected Object doWork( | ||
WorkArguments args, | ||
ModelMap model, | ||
HttpServletResponse response | ||
) throws ApiException, IOException, NoSuchAlgorithmException, InvalidKeyException, URISyntaxException { | ||
//ds-snippet-start:Maestro1Step2 | ||
ApiClient apiClient = createApiClient(config.getMaestroBasePath(), user.getAccessToken()); | ||
//ds-snippet-end:Maestro1Step2 | ||
String accountId = session.getAccountId(); | ||
|
||
//ds-snippet-start:Maestro1Step3 | ||
var workflow = TriggerWorkflowService.getWorkflowDefinition( | ||
apiClient, | ||
accountId, | ||
session.getWorkflowId()); | ||
//ds-snippet-end:Maestro1Step3 | ||
|
||
var result = TriggerWorkflowService.triggerWorkflow( | ||
apiClient, | ||
accountId, | ||
workflow.getTriggerUrl(), | ||
args.getSignerEmail(), | ||
args.getSignerName(), | ||
args.getCcEmail(), | ||
args.getCcName(), | ||
args.getInstanceName()); | ||
|
||
session.setInstanceId(result.getInstanceId()); | ||
|
||
DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName) | ||
.withMessage(getTextForCodeExampleByApiType().ResultsPageText) | ||
.withJsonObject(result) | ||
.addToModel(model, config); | ||
return DONE_EXAMPLE_PAGE; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/docusign/controller/maestro/examples/EG002ControllerCancelWorkflow.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.docusign.controller.maestro.examples; | ||
|
||
import com.docusign.DSConfiguration; | ||
import com.docusign.common.WorkArguments; | ||
import com.docusign.controller.maestro.services.CancelWorkflowInstanceService; | ||
import com.docusign.core.model.DoneExample; | ||
import com.docusign.core.model.Session; | ||
import com.docusign.core.model.User; | ||
import com.docusign.esign.client.ApiException; | ||
import com.docusign.maestro.client.ApiClient; | ||
import com.docusign.maestro.model.CancelResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.ModelMap; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
@Controller | ||
@RequestMapping("/mae002") | ||
public class EG002ControllerCancelWorkflow extends AbstractMaestroController { | ||
|
||
private static final String MODEL_WORKFLOW_ID = "workflowId"; | ||
|
||
private static final String MODEL_INSTANCE_ID = "instanceId"; | ||
|
||
@Autowired | ||
public EG002ControllerCancelWorkflow(DSConfiguration config, Session session, User user) { | ||
super(config, "mae002", session, user); | ||
} | ||
|
||
@Override | ||
protected void onInitModel(WorkArguments args, ModelMap model) throws Exception { | ||
super.onInitModel(args, model); | ||
model.addAttribute(MODEL_WORKFLOW_ID, session.getWorkflowId()); | ||
model.addAttribute(MODEL_INSTANCE_ID, session.getInstanceId()); | ||
} | ||
|
||
@Override | ||
protected Object doWork( | ||
WorkArguments args, | ||
ModelMap model, | ||
HttpServletResponse response | ||
) throws ApiException, IOException, NoSuchAlgorithmException, InvalidKeyException, com.docusign.maestro.client.ApiException { | ||
//ds-snippet-start:Maestro2Step2 | ||
ApiClient apiClient = createApiClient(config.getMaestroBasePath(), user.getAccessToken()); | ||
//ds-snippet-end:Maestro2Step2 | ||
|
||
CancelResponse cancelResponse = CancelWorkflowInstanceService.cancelWorkflowInstance( | ||
apiClient, | ||
session.getAccountId(), | ||
session.getInstanceId()); | ||
|
||
DoneExample.createDefault(getTextForCodeExampleByApiType().ExampleName) | ||
.withMessage(getTextForCodeExampleByApiType().ResultsPageText | ||
.replaceFirst("\\{0}", session.getInstanceId())) | ||
.withJsonObject(cancelResponse) | ||
.addToModel(model, config); | ||
return DONE_EXAMPLE_PAGE; | ||
} | ||
} |
Oops, something went wrong.