Skip to content

Commit

Permalink
Merge pull request #133 from docusign/feature/add-maestro-examples
Browse files Browse the repository at this point in the history
Prepared Maestro examples
  • Loading branch information
paigesrossi authored Apr 11, 2024
2 parents 6a3ba8a + 8e5acd3 commit 53c30b3
Show file tree
Hide file tree
Showing 22 changed files with 1,410 additions and 9 deletions.
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
<oauth2.version>2.5.6</oauth2.version>
<commonsio.version>2.11.0</commonsio.version>

<esignature.version>4.4.0</esignature.version>
<esignature.version>4.5.0</esignature.version>
<rooms.version>1.4.3</rooms.version>
<click.version> 1.4.0</click.version>
<monitor.version>1.3.0</monitor.version>
<admin.version>1.3.0</admin.version>
<webforms.version>1.0.2-RC12</webforms.version>
<maestro.version>1.0.0-RC1</maestro.version>
<swagger-core-version>2.2.8</swagger-core-version>
<jackson-version>2.14.2</jackson-version>
<jersey2.version>3.0.9</jersey2.version>
Expand Down Expand Up @@ -105,7 +106,7 @@
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.1.0</version>
<version>2.1.6</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -168,6 +169,12 @@
<classifier>shaded</classifier>
</dependency>

<dependency>
<groupId>com.docusign</groupId>
<artifactId>docusign-maestro-java</artifactId>
<version>${maestro.version}</version>
</dependency>

<dependency>
<groupId>com.docusign</groupId>
<artifactId>docusign-monitor-java</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/docusign/DSConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public class DSConfiguration {
@Value("${DS_WEBFORMS_BASE_PATH}")
private String webFormsBasePath;

@Value("${DS_MAESTRO_BASE_PATH}")
private String maestroBasePath;

@Value("${CodeExamplesManifest}")
private String codeExamplesManifest;

Expand All @@ -126,6 +129,8 @@ public String getBaseUrl(ApiIndex apiIndex, OAuth.Account oauthAccount) {
return monitorBasePath;
} else if (ApiIndex.ADMIN.equals(apiIndex)) {
return adminBasePath;
} else if (ApiIndex.MAESTRO.equals(apiIndex)) {
return maestroBasePath;
}

return oauthAccount.getBaseUri();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/docusign/common/ApiIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ public enum ApiIndex {
ESIGNATURE("/pages/esignature/index", "/restapi", "/eg001", "/eg"),
ROOMS("/pages/rooms/index", "/restapi", "/r001", "/r"),
CLICK("/pages/click/index", "/clickapi", "/c001", "/c"),
MAESTRO("/pages/maestro/index", "", "/mae001", "/mae"),
MONITOR("/pages/monitor/index", "", "/m001", "/m"),
ADMIN("/pages/admin/index", "/management", "/a001", "/a"),
CONNECT("/pages/connect/index", "", "/con001", "/con"),
WEBFORMS("/pages/webforms/index", "/restapi", "/web001", "/web");

private final String indexPath;

private final String baseUrlSuffix;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/docusign/common/WorkArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,6 @@ public class WorkArguments {
private String hmacSecret;

private String jsonPayload;

private String instanceName;
}
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;
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit 53c30b3

Please sign in to comment.