Search in sources :

Example 1 with WorkflowException

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

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

the class DamMetadataPropertyResetProcess method execute.

@Override
public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
    ResourceResolver resourceResolver = null;
    String wfPayload = null;
    try {
        resourceResolver = this.getResourceResolver(workflowSession.getSession());
        wfPayload = (String) workItem.getWorkflowData().getPayload();
        final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, wfPayload);
        final Map<String, String> srcDestMap = this.getProcessArgsMap(metaDataMap);
        for (final String payload : payloads) {
            final Asset asset = DamUtil.resolveToAsset(resourceResolver.getResource(payload));
            if (asset == null) {
                log.debug("Payload path [ {} ] does not resolve to an asset", payload);
                continue;
            }
            String metadataPath = String.format("%s/%s/%s", asset.getPath(), JcrConstants.JCR_CONTENT, DamConstants.METADATA_FOLDER);
            Resource metadataResource = resourceResolver.getResource(metadataPath);
            if (metadataResource == null) {
                log.error("Could not find the metadata node for Asset [ " + asset.getPath() + " ]");
                throw new WorkflowException("Could not find the metadata node for Asset [ " + asset.getPath() + " ]");
            }
            final ModifiableValueMap mvm = metadataResource.adaptTo(ModifiableValueMap.class);
            for (final Map.Entry<String, String> entry : srcDestMap.entrySet()) {
                final String srcProperty = entry.getValue();
                final String destProperty = entry.getKey();
                if (mvm.get(srcProperty) != null) {
                    // Remove dest property first in case Types differ
                    mvm.remove(destProperty);
                    // If the src value is NOT null, update the dest property
                    mvm.put(destProperty, mvm.get(srcProperty));
                } else if (mvm.containsKey(srcProperty)) {
                    // Else if the src value IS null, AND the src property exists on the node, remove the dest property
                    mvm.remove(destProperty);
                }
                // Else leave the dest property alone since there is no source defined to overwrite it with
                // Remove the source
                mvm.remove(srcProperty);
            }
        }
    } catch (LoginException e) {
        throw new WorkflowException("Could not get a ResourceResolver object from the WorkflowSession", e);
    } catch (RepositoryException e) {
        throw new WorkflowException(String.format("Could not find the payload for '%s'", wfPayload), e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : WorkflowException(com.day.cq.workflow.WorkflowException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Asset(com.day.cq.dam.api.Asset) LoginException(org.apache.sling.api.resource.LoginException) RepositoryException(javax.jcr.RepositoryException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MetaDataMap(com.day.cq.workflow.metadata.MetaDataMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 3 with WorkflowException

use of com.day.cq.workflow.WorkflowException 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 4 with WorkflowException

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

the class ReplicatePackageProcess method execute.

/*
     * (non-Javadoc)
     *
     * @see
     * com.day.cq.workflow.exec.WorkflowProcess#execute(com.day.cq.workflow.exec
     * .WorkItem, com.day.cq.workflow.WorkflowSession,
     * com.day.cq.workflow.metadata.MetaDataMap)
     */
@Override
public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException {
    log.trace("execute");
    String packagePath = args.get(WorkflowHelper.PROCESS_ARGS, String.class);
    if (StringUtils.isNotEmpty(packagePath)) {
        log.debug("Executing Automatic Package Replicator Job for package {}", packagePath);
        AutomaticPackageReplicatorJob aprJob = new AutomaticPackageReplicatorJob(resourceResolverFactory, replicator, eventAdmin, packagePath);
        try {
            aprJob.excute();
        } catch (Exception e) {
            log.error("Exception executing Automatic Package Replicator Job for package " + packagePath, e);
            throw new WorkflowException("Exception executing Automatic Package Replicator Job for package " + packagePath, e);
        }
    } else {
        log.warn("No package path specified");
        throw new WorkflowException("No package path specified for Automatic Package Replicator Job");
    }
}
Also used : WorkflowException(com.day.cq.workflow.WorkflowException) WorkflowException(com.day.cq.workflow.WorkflowException)

Example 5 with WorkflowException

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

Aggregations

WorkflowException (com.day.cq.workflow.WorkflowException)10 WorkflowSession (com.day.cq.workflow.WorkflowSession)5 Session (javax.jcr.Session)5 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 RepositoryException (javax.jcr.RepositoryException)3 Resource (org.apache.sling.api.resource.Resource)3 ContentVisitor (com.adobe.acs.commons.util.visitors.ContentVisitor)2 ResourceRunnable (com.adobe.acs.commons.util.visitors.ResourceRunnable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 PersistenceException (org.apache.sling.api.resource.PersistenceException)2 WorkflowHelper (com.adobe.acs.commons.util.WorkflowHelper)1 SyntheticWorkflowModel (com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowModel)1 SyntheticWorkflowRunner (com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowRunner)1 SyntheticWorkflowStep (com.adobe.acs.commons.workflow.synthetic.SyntheticWorkflowStep)1 SyntheticWorkflow (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflow)1 SyntheticWorkflowSession (com.adobe.acs.commons.workflow.synthetic.impl.cq.SyntheticWorkflowSession)1 SyntheticCompleteWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticCompleteWorkflowException)1 SyntheticRestartWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticRestartWorkflowException)1 SyntheticTerminateWorkflowException (com.adobe.acs.commons.workflow.synthetic.impl.cq.exceptions.SyntheticTerminateWorkflowException)1 Asset (com.day.cq.dam.api.Asset)1