Search in sources :

Example 1 with WorkflowSession

use of com.day.cq.workflow.WorkflowSession in project acs-aem-commons by Adobe-Consulting-Services.

the class ReplicateWithOptionsWorkflowProcess method execute.

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    ResourceResolver resourceResolver = null;
    final long start = System.currentTimeMillis();
    try {
        resourceResolver = workflowHelper.getResourceResolver(workflowSession);
        final String originalPayload = (String) workItem.getWorkflowData().getPayload();
        final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, originalPayload);
        final ProcessArgs processArgs = new ProcessArgs(metaDataMap);
        final AtomicInteger count = new AtomicInteger(0);
        // Anonymous inner class to facilitate counting of processed payloads
        final ResourceRunnable replicatorRunnable = new ResourceRunnable() {

            @Override
            public void run(final Resource resource) throws Exception {
                if (processArgs.isThrottle()) {
                    throttledTaskRunner.waitForLowCpuAndLowMemory();
                }
                replicator.replicate(resource.getResourceResolver().adaptTo(Session.class), processArgs.getReplicationActionType(), resource.getPath(), processArgs.getReplicationOptions(resource));
                count.incrementAndGet();
            }
        };
        final ContentVisitor visitor = new ContentVisitor(replicatorRunnable);
        for (final String payload : payloads) {
            final Resource resource = resourceResolver.getResource(payload);
            if (processArgs.isTraverseTree()) {
                // Traverse the tree
                visitor.accept(resource);
            } else {
                // Only execute on the provided payload
                replicatorRunnable.run(resource);
            }
        }
        log.info("Replicate with Options processed [ {} ] total payloads in {} ms", count.get(), System.currentTimeMillis() - start);
    } catch (Exception e) {
        throw new WorkflowException(e);
    }
}
Also used : ContentVisitor(com.adobe.acs.commons.util.visitors.ContentVisitor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WorkflowException(com.day.cq.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) WorkflowException(com.day.cq.workflow.WorkflowException) ResourceRunnable(com.adobe.acs.commons.util.visitors.ResourceRunnable) WorkflowSession(com.day.cq.workflow.WorkflowSession) Session(javax.jcr.Session)

Example 2 with WorkflowSession

use of com.day.cq.workflow.WorkflowSession 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)

Example 3 with WorkflowSession

use of com.day.cq.workflow.WorkflowSession in project acs-aem-commons by Adobe-Consulting-Services.

the class InitFormServlet method doGet.

@Override
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    final JsonObject json = new JsonObject();
    // Runners
    accumulate(json, KEY_RUNNER_TYPES, withLabelValue("AEM Workflow", AEMWorkflowRunnerImpl.class.getName()));
    accumulate(json, KEY_RUNNER_TYPES, withLabelValue("Synthetic Workflow (Single-threaded)", SyntheticWorkflowRunnerImpl.class.getName()));
    accumulate(json, KEY_RUNNER_TYPES, withLabelValue("Synthetic Workflow (Multi-threaded)", FastActionManagerRunnerImpl.class.getName()));
    // Query Types
    accumulate(json, KEY_QUERY_TYPES, withLabelValue("QueryBuilder", "queryBuilder"));
    accumulate(json, KEY_QUERY_TYPES, withLabelValue("List", "list"));
    accumulate(json, KEY_QUERY_TYPES, withLabelValue("xPath", "xpath"));
    accumulate(json, KEY_QUERY_TYPES, withLabelValue("JCR-SQL2", "JCR-SQL2"));
    accumulate(json, KEY_QUERY_TYPES, withLabelValue("JCR-SQL", "JCR-SQL"));
    // User Event Data
    accumulate(json, KEY_USER_EVENT_DATA, withLabelValue("Custom user-event-data", ""));
    accumulate(json, KEY_USER_EVENT_DATA, withLabelValue("changedByWorkflowProcess", "changedByWorkflowProcess"));
    accumulate(json, KEY_USER_EVENT_DATA, withLabelValue("acs-aem-commons.bulk-workflow-manager", "acs-aem-commons.bulk-workflow-manager"));
    // Workflow Models
    final WorkflowSession workflowSession = workflowService.getWorkflowSession(request.getResourceResolver().adaptTo(Session.class));
    try {
        final WorkflowModel[] workflowModels = workflowSession.getModels();
        for (final WorkflowModel workflowModel : workflowModels) {
            boolean transientWorkflow = isTransient(request.getResourceResolver(), workflowModel.getId());
            String workflowLabel = workflowModel.getTitle();
            if (transientWorkflow) {
                workflowLabel += " ( Transient )";
            }
            JsonObject jsonWorkflow = withLabelValue(workflowLabel, workflowModel.getId());
            jsonWorkflow.addProperty("transient", transientWorkflow);
            accumulate(json, "workflowModels", jsonWorkflow);
        }
        response.getWriter().write(json.toString());
    } catch (WorkflowException e) {
        log.error("Could not create workflow model drop-down.", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not collect workflows", e.getMessage());
    }
}
Also used : WorkflowSession(com.day.cq.workflow.WorkflowSession) WorkflowException(com.day.cq.workflow.WorkflowException) JsonObject(com.google.gson.JsonObject) WorkflowModel(com.day.cq.workflow.model.WorkflowModel) Session(javax.jcr.Session) WorkflowSession(com.day.cq.workflow.WorkflowSession)

Example 4 with WorkflowSession

use of com.day.cq.workflow.WorkflowSession in project acs-aem-commons by Adobe-Consulting-Services.

the class AEMWorkflowRunnerImpl method forceTerminate.

@SuppressWarnings("squid:S00112")
@Override
public void forceTerminate(Workspace workspace, Payload payload) throws Exception {
    final WorkflowSession workflowSession = workflowService.getWorkflowSession(payload.getResourceResolver().adaptTo(Session.class));
    Workflow workflow = null;
    fail(workspace, payload);
    try {
        workflow = payload.getWorkflow();
        if (workflow != null) {
            if (workflow.isActive()) {
                workflowSession.terminateWorkflow(workflow);
                log.info("Force Terminated workflow [ {} ]", workflow.getId());
                payload.setStatus(Status.FORCE_TERMINATED);
                if (workspace.getConfig().isPurgeWorkflow()) {
                    purge(payload);
                }
            } else {
                log.warn("Trying to force terminate an inactive workflow [ {} ]", workflow.getId());
            }
        } else {
            payload.setStatus(Status.FORCE_TERMINATED);
        }
    } catch (WorkflowException e) {
        throw new Exception(e);
    }
}
Also used : WorkflowSession(com.day.cq.workflow.WorkflowSession) WorkflowException(com.day.cq.workflow.WorkflowException) Workflow(com.day.cq.workflow.exec.Workflow) WorkflowException(com.day.cq.workflow.WorkflowException) RepositoryException(javax.jcr.RepositoryException) PersistenceException(org.apache.sling.api.resource.PersistenceException) WorkflowSession(com.day.cq.workflow.WorkflowSession) Session(javax.jcr.Session)

Example 5 with WorkflowSession

use of com.day.cq.workflow.WorkflowSession in project acs-aem-commons by Adobe-Consulting-Services.

the class Payload method getWorkflow.

public Workflow getWorkflow() throws WorkflowException {
    final WorkflowSession workflowSession = workflowService.getWorkflowSession(resource.getResourceResolver().adaptTo(Session.class));
    String tmp = getWorkflowInstanceId();
    try {
        if (resource.getResourceResolver().getResource(tmp) != null) {
            return workflowSession.getWorkflow(tmp);
        }
    } catch (Exception e) {
        log.error(String.format("Could not get workflow with id [ %s ] for payload [ %s ~> %s ]", tmp, getPath(), getPayloadPath()), e);
    }
    return null;
}
Also used : WorkflowSession(com.day.cq.workflow.WorkflowSession) WorkflowException(com.day.cq.workflow.WorkflowException) JSONException(org.apache.sling.commons.json.JSONException) PersistenceException(org.apache.sling.api.resource.PersistenceException) Session(javax.jcr.Session) WorkflowSession(com.day.cq.workflow.WorkflowSession)

Aggregations

WorkflowSession (com.day.cq.workflow.WorkflowSession)6 Session (javax.jcr.Session)5 WorkflowException (com.day.cq.workflow.WorkflowException)4 WorkflowModel (com.day.cq.workflow.model.WorkflowModel)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 ContentVisitor (com.adobe.acs.commons.util.visitors.ContentVisitor)1 ResourceRunnable (com.adobe.acs.commons.util.visitors.ResourceRunnable)1 SyntheticWorkItem (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkItem)1 SyntheticWorkflowSession (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflowSession)1 SyntheticCompleteWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticCompleteWorkflowException)1 SyntheticTerminateWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException)1 Workflow (com.day.cq.workflow.exec.Workflow)1 JsonObject (com.google.gson.JsonObject)1 Date (java.util.Date)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 RepositoryException (javax.jcr.RepositoryException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 JSONArray (org.apache.sling.commons.json.JSONArray)1 JSONException (org.apache.sling.commons.json.JSONException)1