Search in sources :

Example 1 with WorkflowModel

use of com.adobe.granite.workflow.model.WorkflowModel in project acs-aem-commons by Adobe-Consulting-Services.

the class WorkflowDelegationStep method execute.

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metadata) throws WorkflowException {
    Map<String, String> args = getProcessArgsMap(metadata);
    String propertyName = args.get(WORKFLOW_PROPERTY_NAME);
    String defaultWorkflowModelId = args.get(DEFAULT_WORKFLOW_MODEL);
    boolean terminateOnDelegation = Boolean.parseBoolean(StringUtils.lowerCase(args.get(TERMINATE_ON_DELEGATION)));
    if (StringUtils.isBlank(propertyName)) {
        throw new WorkflowException("PROCESS_ARG [ " + WORKFLOW_PROPERTY_NAME + " ] not defined");
    }
    log.debug("Provided PROCESS_ARGS: propertyName = [ {} ], Default Workflow Model = [ {} ]", propertyName, defaultWorkflowModelId);
    ResourceResolver resolver = null;
    WorkflowData wfData = workItem.getWorkflowData();
    if (!workflowHelper.isPathTypedPayload(wfData)) {
        log.warn("Could not locate a JCR_PATH payload for this workflow. Skipping delegation.");
        return;
    }
    final String path = wfData.getPayload().toString();
    // Get the resource resolver
    resolver = workflowHelper.getResourceResolver(workflowSession);
    if (resolver == null) {
        throw new WorkflowException("Could not adapt the WorkflowSession to a ResourceResolver. Something has gone terribly wrong!");
    }
    // Derive the Page or Asset resource so we have a normalized entry-point for finding the /jcr:content resource
    final Resource pageOrAssetResource = workflowHelper.getPageOrAssetResource(resolver, path);
    if (pageOrAssetResource == null) {
        log.warn("Could not resolve [ {} ] to an Asset or Page. Skipping delegation.", path);
        return;
    }
    // Get the Asset or Page's jcr:content resource, so we have a common inherited property look-up scheme
    final Resource jcrContentResource = pageOrAssetResource.getChild(JcrConstants.JCR_CONTENT);
    if (jcrContentResource == null) {
        throw new WorkflowException(String.format("Could not find a child jcr:content resource for [ %s ]", pageOrAssetResource.getPath()));
    }
    // Look up the content-hierarchy for the delegate workflow model
    final InheritanceValueMap inheritance = new HierarchyNodeInheritanceValueMap(jcrContentResource);
    final String foundWorkflowModelId = StringUtils.trim(inheritance.getInherited(propertyName, String.class));
    final WorkflowModel delegateWorkflowModel = getDelegateWorkflowModel(workflowSession, foundWorkflowModelId, defaultWorkflowModelId);
    if (delegateWorkflowModel != null) {
        workflowSession.startWorkflow(delegateWorkflowModel, wfData);
        log.info("Delegating payload [ {} ] to Workflow Model [ {} ]", wfData.getPayload(), delegateWorkflowModel.getId());
        if (terminateOnDelegation) {
            log.info("Terminating current workflow due to PROCESS_ARGS[ {} ] = [ {} ]", TERMINATE_ON_DELEGATION, terminateOnDelegation);
            workflowSession.terminateWorkflow(workItem.getWorkflow());
        }
    } else {
        log.warn("No valid delegate Workflow Model could be located. Skipping workflow delegation.");
    }
}
Also used : HierarchyNodeInheritanceValueMap(com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap) InheritanceValueMap(com.day.cq.commons.inherit.InheritanceValueMap) HierarchyNodeInheritanceValueMap(com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap) WorkflowException(com.adobe.granite.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel) WorkflowData(com.adobe.granite.workflow.exec.WorkflowData)

Example 2 with WorkflowModel

use of com.adobe.granite.workflow.model.WorkflowModel in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class WorkflowModelDataSourceServletTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, APPS_ROOT);
    WorkflowSession workflowSessionMock = mock(WorkflowSession.class);
    WorkflowModel workflowModelMock = mock(WorkflowModel.class);
    when(workflowModelMock.getTitle()).thenReturn("Workflow Title");
    when(workflowModelMock.getId()).thenReturn("test/workflow");
    when(workflowSessionMock.getModels()).thenReturn(new WorkflowModel[] { workflowModelMock });
    context.registerAdapter(ResourceResolver.class, WorkflowSession.class, (Function<ResourceResolver, WorkflowSession>) input -> workflowSessionMock);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) AemContextExtension(io.wcm.testing.mock.aem.junit5.AemContextExtension) Mockito.when(org.mockito.Mockito.when) AemContext(io.wcm.testing.mock.aem.junit5.AemContext) Function(java.util.function.Function) Test(org.junit.jupiter.api.Test) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel) DataSource(com.adobe.granite.ui.components.ds.DataSource) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) CoreComponentTestContext(com.adobe.cq.wcm.core.components.context.CoreComponentTestContext) WorkflowSession(com.adobe.granite.workflow.WorkflowSession) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WorkflowSession(com.adobe.granite.workflow.WorkflowSession) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with WorkflowModel

use of com.adobe.granite.workflow.model.WorkflowModel in project acs-aem-commons by Adobe-Consulting-Services.

the class WorkflowDelegationStep method getWorkflowModel.

private WorkflowModel getWorkflowModel(WorkflowSession workflowSession, String workflowModelId) {
    workflowModelId = StringUtils.stripToEmpty(workflowModelId);
    WorkflowModel workflowModel = null;
    if (StringUtils.isNotBlank(workflowModelId)) {
        if (!workflowModelId.endsWith("/jcr:content/model")) {
            ResourceResolver resourceResolver = workflowHelper.getResourceResolver(workflowSession);
            Resource resource = resourceResolver.getResource(workflowModelId + "/jcr:content/model");
            if (resource != null && StringUtils.equals(resource.getValueMap().get(JcrConstants.JCR_PRIMARYTYPE, String.class), "cq:WorkflowModel")) {
                workflowModelId = resource.getPath();
            }
        }
        try {
            workflowModel = workflowSession.getModel(workflowModelId);
        } catch (WorkflowException e) {
            log.warn("Could not find Workflow Model for [ {} ]", workflowModelId);
        }
    }
    return workflowModel;
}
Also used : WorkflowException(com.adobe.granite.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel)

Example 4 with WorkflowModel

use of com.adobe.granite.workflow.model.WorkflowModel in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class WorkflowModelDataSourceServlet method doGet.

@Override
protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) throws ServletException, IOException {
    try {
        WorkflowSession workflowSession = request.getResourceResolver().adaptTo(WorkflowSession.class);
        ArrayList<Resource> resources = new ArrayList<>();
        if (workflowSession != null) {
            WorkflowModel[] models = workflowSession.getModels();
            for (WorkflowModel model : models) {
                resources.add(new WorkflowModelResource(model, request.getResourceResolver()));
            }
        }
        SimpleDataSource dataSource = new SimpleDataSource(resources.iterator());
        request.setAttribute(DataSource.class.getName(), dataSource);
    } catch (WorkflowException e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) WorkflowSession(com.adobe.granite.workflow.WorkflowSession) WorkflowException(com.adobe.granite.workflow.WorkflowException) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Aggregations

WorkflowModel (com.adobe.granite.workflow.model.WorkflowModel)4 Resource (org.apache.sling.api.resource.Resource)4 WorkflowException (com.adobe.granite.workflow.WorkflowException)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3 DataSource (com.adobe.granite.ui.components.ds.DataSource)2 WorkflowSession (com.adobe.granite.workflow.WorkflowSession)2 CoreComponentTestContext (com.adobe.cq.wcm.core.components.context.CoreComponentTestContext)1 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 WorkflowData (com.adobe.granite.workflow.exec.WorkflowData)1 HierarchyNodeInheritanceValueMap (com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap)1 InheritanceValueMap (com.day.cq.commons.inherit.InheritanceValueMap)1 AemContext (io.wcm.testing.mock.aem.junit5.AemContext)1 AemContextExtension (io.wcm.testing.mock.aem.junit5.AemContextExtension)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1 ServletException (javax.servlet.ServletException)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1