Search in sources :

Example 1 with SyntheticTerminateWorkflowException

use of com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException in project acs-aem-commons by Adobe-Consulting-Services.

the class SyntheticWorkflowRunnerImpl method run.

@SuppressWarnings({ "squid:S3776", "squid:S1163", "squid:S1143" })
private void run(final ResourceResolver resourceResolver, final String payloadPath, final List<SyntheticWorkflowStep> workflowSteps, final boolean autoSaveAfterEachWorkflowProcess, final boolean autoSaveAtEnd) throws WorkflowException {
    final Session session = resourceResolver.adaptTo(Session.class);
    // Create the WorkflowData obj; This will persist through all WF Process Steps
    // This must be remained defined once to it can be shared by reference across CQ and Granite SyntheticWorkflow isntances
    final SyntheticWorkflowData workflowData = new SyntheticWorkflowData("JCR_PATH", payloadPath);
    // Create the Workflow obj; This will persist through all WF Process Steps
    // The Workflow MetadataMap will leverage the WorkflowData's MetadataMap as these two maps should be in sync
    final SyntheticWorkflow cqWorkflow = new SyntheticWorkflow("Synthetic Workflow ( " + payloadPath + " )", workflowData);
    final com.adobe.acs.commons.workflow.synthetic.impl.granite.SyntheticWorkflow graniteWorkflow = new com.adobe.acs.commons.workflow.synthetic.impl.granite.SyntheticWorkflow("Synthetic Workflow ( " + payloadPath + " )", workflowData);
    boolean terminated = false;
    for (final SyntheticWorkflowStep workflowStep : workflowSteps) {
        SyntheticWorkflowProcess workflowProcess;
        if (WorkflowProcessIdType.PROCESS_LABEL.equals(workflowStep.getIdType())) {
            workflowProcess = this.workflowProcessesByLabel.get(workflowStep.getId());
        } else {
            workflowProcess = this.workflowProcessesByProcessName.get(workflowStep.getId());
        }
        if (workflowProcess != null) {
            final long start = System.currentTimeMillis();
            try {
                final SyntheticMetaDataMap workflowProcessMetaDataMap = new SyntheticMetaDataMap(workflowStep.getMetadataMap());
                if (SyntheticWorkflowProcess.Type.GRANITE.equals(workflowProcess.getWorkflowType())) {
                    runGraniteWorkflowProcess(session, graniteWorkflow, workflowProcessMetaDataMap, workflowProcess);
                } else if (SyntheticWorkflowProcess.Type.CQ.equals(workflowProcess.getWorkflowType())) {
                    runCqWorkflowProcess(session, cqWorkflow, workflowProcessMetaDataMap, workflowProcess);
                } else {
                    log.warn("Workflow process step is of an unknown type [ {} ]. Skipping.", workflowProcess.getWorkflowType());
                }
            } catch (SyntheticTerminateWorkflowException ex) {
                // Terminate entire Workflow execution for this payload
                terminated = true;
                log.info("Synthetic CQ workflow execution stopped via terminate for [ {} ]", payloadPath);
                break;
            } catch (com.adobe.acs.commons.workflow.synthetic.impl.granite.exceptions.SyntheticTerminateWorkflowException ex) {
                // Terminate entire Workflow execution for this payload
                terminated = true;
                log.info("Synthetic Granite workflow execution stopped via terminate for [ {} ]", payloadPath);
                break;
            } catch (SyntheticRestartWorkflowException ex) {
                // Handle CQ Restart Workflow; catch/throw for clarity in whats happening
                throw ex;
            } catch (com.adobe.acs.commons.workflow.synthetic.impl.granite.exceptions.SyntheticRestartWorkflowException ex) {
                // Handle Granite Restart Exceptions by transforming them into CQ Worlflow Restart Exceptions which the rest of this API leverages
                throw new SyntheticRestartWorkflowException(ex.getMessage());
            } catch (WorkflowException ex) {
                // Handle CQ Workflow Exception; catch/throw for clarity in whats happening
                throw ex;
            } catch (com.adobe.granite.workflow.WorkflowException ex) {
                // Handle Granite Workflow Exceptions by transforming them into CQ Workflow Exceptions which the rest of this API leverages
                throw new WorkflowException(ex);
            } finally {
                try {
                    if (!terminated && autoSaveAfterEachWorkflowProcess && session.hasPendingChanges()) {
                        session.save();
                    }
                    log.debug("Executed synthetic workflow process [ {} ] on [ {} ] in [ " + String.valueOf(System.currentTimeMillis() - start) + " ] ms", workflowStep.getId(), payloadPath);
                } catch (RepositoryException e) {
                    log.error("Could not save at end of synthetic workflow process execution" + " [ {} ] for payload path [ {} ]", workflowStep.getId(), payloadPath);
                    log.error("Synthetic workflow process save failed.", e);
                    throw new WorkflowException(e);
                }
            }
        } else {
            log.error("Synthetic workflow runner retrieved a null Workflow Process for process.label [ {} ]", workflowStep.getId());
        }
    }
    try {
        if (autoSaveAtEnd && session.hasPendingChanges()) {
            session.save();
        }
    } catch (RepositoryException e) {
        log.error("Could not complete save at end of synthetic workflow execution process" + " [ {} ]", payloadPath, e);
        throw new WorkflowException(e);
    }
}
Also used : SyntheticWorkflow(com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflow) SyntheticRestartWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticRestartWorkflowException) SyntheticWorkflowStep(com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowStep) WorkflowException(com.day.cq.workflow.WorkflowException) SyntheticTerminateWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException) SyntheticRestartWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticRestartWorkflowException) SyntheticCompleteWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticCompleteWorkflowException) RepositoryException(javax.jcr.RepositoryException) SyntheticTerminateWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException) SyntheticWorkflowSession(com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflowSession) WorkflowSession(com.day.cq.workflow.WorkflowSession) Session(javax.jcr.Session)

Example 2 with SyntheticTerminateWorkflowException

use of com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException in project acs-aem-commons by Adobe-Consulting-Services.

the class SyntheticWorkflowRunnerImpl method runCqWorkflowProcess.

private void runCqWorkflowProcess(Session session, SyntheticWorkflow workflow, SyntheticMetaDataMap workflowProcessMetaDataMap, SyntheticWorkflowProcess workflowProcess) throws WorkflowException {
    final WorkflowSession workflowSession = this.getCqWorkflowSession(session);
    // Each Workflow Process Step gets its own workItem whose life starts and ends w the WF Process
    final SyntheticWorkItem workItem = new SyntheticWorkItem(workflow.getWorkflowData());
    workItem.setWorkflow(workflow);
    log.trace("Executing CQ synthetic workflow process [ {} ] on [ {} ]", workflowProcess.getProcessId(), workflow.getWorkflowData().getPayload());
    // Execute the Workflow Process
    try {
        workflowProcess.getCqWorkflowProcess().execute(workItem, workflowSession, workflowProcessMetaDataMap);
        workItem.setTimeEnded(new Date());
    } catch (SyntheticCompleteWorkflowException ex) {
        // Workitem force-completed via a call to workflowSession.complete(..)
        workItem.setTimeEnded(new Date());
        log.trace(ex.getMessage());
    } catch (SyntheticTerminateWorkflowException ex) {
        workItem.setTimeEnded(new Date());
        log.trace(ex.getMessage());
        throw ex;
    }
}
Also used : SyntheticWorkItem(com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkItem) SyntheticTerminateWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException) SyntheticCompleteWorkflowException(com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticCompleteWorkflowException) SyntheticWorkflowSession(com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflowSession) WorkflowSession(com.day.cq.workflow.WorkflowSession) Date(java.util.Date)

Aggregations

SyntheticWorkflowSession (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflowSession)2 SyntheticCompleteWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticCompleteWorkflowException)2 SyntheticTerminateWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException)2 WorkflowSession (com.day.cq.workflow.WorkflowSession)2 SyntheticWorkflowStep (com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowStep)1 SyntheticWorkItem (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkItem)1 SyntheticWorkflow (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflow)1 SyntheticRestartWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticRestartWorkflowException)1 WorkflowException (com.day.cq.workflow.WorkflowException)1 Date (java.util.Date)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1