Search in sources :

Example 1 with WorkflowException

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

the class WorkflowInstanceRemoverImpl method getStatus.

private String getStatus(Resource workflowInstanceResource) {
    String instanceStatus = workflowInstanceResource.getValueMap().get(PN_STATUS, "UNKNOWN");
    if (!STATUS_RUNNING.equalsIgnoreCase(instanceStatus)) {
        log.debug("Status of [ {} ] is not RUNNING, so we can take it at face value", instanceStatus);
        return instanceStatus;
    }
    // Else check if its RUNNING or STALE
    log.debug("Status is [ {} ] so we have to determine if its RUNNING or STALE", instanceStatus);
    Resource metadataResource = workflowInstanceResource.getChild("data/metaData");
    if (metadataResource == null) {
        log.debug("Workflow instance data/metaData does not exist for [ {} ]", workflowInstanceResource.getPath());
        return instanceStatus;
    }
    final ValueMap properties = metadataResource.getValueMap();
    final String[] jobIds = StringUtils.splitByWholeSeparator(properties.get("currentJobs", ""), JOB_SEPARATOR);
    if (jobIds.length == 0) {
        log.debug("No jobs found for [ {} ] so assuming status as [ {} ]", workflowInstanceResource.getPath(), instanceStatus);
    }
    // Make sure there are no JOBS that match to this jobs name
    for (final String jobId : jobIds) {
        if (jobManager.getJobById(jobId) != null) {
            // Found a job for this jobID; so this is a RUNNING job
            log.debug("JobManager found a job for jobId [ {} ] so marking workflow instances [ {} ] as RUNNING", jobId, workflowInstanceResource.getPath());
            return STATUS_RUNNING;
        }
    }
    log.debug("JobManager could not find any jobs for jobIds [ {} ] so  workflow instance [ {} ] is potentially STALE", StringUtils.join(jobIds, ", "), workflowInstanceResource.getPath());
    final WorkflowSession workflowSession = workflowInstanceResource.getResourceResolver().adaptTo(WorkflowSession.class);
    Workflow workflow = null;
    try {
        workflow = workflowSession.getWorkflow(workflowInstanceResource.getPath());
        if (workflow == null) {
            throw new WorkflowException(String.format("Workflow instance object is null for [ %s]", workflowInstanceResource.getPath()));
        }
    } catch (WorkflowException e) {
        log.warn("Unable to locate Workflow Instance for [ {} ] So it cannot be RUNNING and must be STALE. ", workflowInstanceResource.getPath(), e);
        return "STALE";
    }
    final List<WorkItem> workItems = workflow.getWorkItems(new WorkItemFilter() {

        public boolean doInclude(WorkItem workItem) {
            // Only include active Workflow instances (ones without an End Time) in this list
            return workItem.getTimeEnded() == null;
        }
    });
    if (!workItems.isEmpty()) {
        // If at least 1 work item exists that does not have an end time (meaning its still active), then its RUNNING
        return STATUS_RUNNING;
    }
    return "STALE";
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) WorkflowSession(com.adobe.granite.workflow.WorkflowSession) WorkflowException(com.adobe.granite.workflow.WorkflowException) Resource(org.apache.sling.api.resource.Resource) Workflow(com.adobe.granite.workflow.exec.Workflow) WorkItemFilter(com.adobe.granite.workflow.exec.filter.WorkItemFilter) WorkItem(com.adobe.granite.workflow.exec.WorkItem)

Example 2 with WorkflowException

use of com.adobe.granite.workflow.WorkflowException 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 3 with WorkflowException

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

the class WorkflowModelDataSourceServlet method doGet.

@Override
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull 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) ArrayList(java.util.ArrayList) Resource(org.apache.sling.api.resource.Resource) WorkflowModel(com.adobe.granite.workflow.model.WorkflowModel) SimpleDataSource(com.adobe.granite.ui.components.ds.SimpleDataSource) DataSource(com.adobe.granite.ui.components.ds.DataSource)

Example 4 with WorkflowException

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

the class BrandPortalSyncProcess method execute.

public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final List<String> assetPaths = new ArrayList<String>();
    final ReplicationActionType replicationActionType = getReplicationActionType(metaDataMap);
    try {
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, (String) workItem.getWorkflowData().getPayload());
        for (final String payload : payloads) {
            // Convert the payloads to Assets, in preparation for Brand Portal publication
            // Note that this only supports Assets as payloads and NOT Asset Folders
            final Asset asset = DamUtil.resolveToAsset(resourceResolver.getResource(payload));
            if (asset == null) {
                log.debug("Payload path [ {} ] does not resolve to an asset", payload);
            } else {
                assetPaths.add(asset.getPath());
            }
        }
        // Based on the WF Process activation/deactivation directive; leverage the DamSyncService to publish the the Asset
        if (ReplicationActionType.ACTIVATE.equals(replicationActionType)) {
            damSyncService.publishResourcesToMP(assetPaths, resourceResolver);
        } else if (ReplicationActionType.DEACTIVATE.equals(replicationActionType)) {
            damSyncService.unpublishResourcesFromMP(assetPaths, resourceResolver);
        } else {
            log.warn("Unknown replication action type [ {} ] for AEM Assets Brand Portal Sync", replicationActionType);
        }
    } catch (RepositoryException e) {
        log.error("Could not find the payload", e);
        throw new WorkflowException("Could not find the payload");
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : WorkflowException(com.adobe.granite.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) Asset(com.day.cq.dam.api.Asset) RepositoryException(javax.jcr.RepositoryException) ReplicationActionType(com.day.cq.replication.ReplicationActionType)

Example 5 with WorkflowException

use of com.adobe.granite.workflow.WorkflowException 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)

Aggregations

WorkflowException (com.adobe.granite.workflow.WorkflowException)6 Resource (org.apache.sling.api.resource.Resource)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 WorkflowModel (com.adobe.granite.workflow.model.WorkflowModel)3 WorkflowSession (com.adobe.granite.workflow.WorkflowSession)2 Asset (com.day.cq.dam.api.Asset)2 ArrayList (java.util.ArrayList)2 AudioException (com.adobe.acs.commons.dam.audio.impl.AudioException)1 DataSource (com.adobe.granite.ui.components.ds.DataSource)1 SimpleDataSource (com.adobe.granite.ui.components.ds.SimpleDataSource)1 WorkItem (com.adobe.granite.workflow.exec.WorkItem)1 Workflow (com.adobe.granite.workflow.exec.Workflow)1 WorkflowData (com.adobe.granite.workflow.exec.WorkflowData)1 WorkItemFilter (com.adobe.granite.workflow.exec.filter.WorkItemFilter)1 HierarchyNodeInheritanceValueMap (com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap)1 InheritanceValueMap (com.day.cq.commons.inherit.InheritanceValueMap)1 ReplicationActionType (com.day.cq.replication.ReplicationActionType)1 Serializable (java.io.Serializable)1 RepositoryException (javax.jcr.RepositoryException)1 ServletException (javax.servlet.ServletException)1