【问题标题】:CQ5 Programmatically Run a WorkflowCQ5 以编程方式运行工作流
【发布时间】:2013-03-05 20:47:24
【问题描述】:

我在 CQ 中创建了一个需要通过 XHR 以编程方式运行的工作流。

你们中的许多人可能都知道,CQ 文档并不是最好的(至少在这一点上)。如何以编程方式运行它?

【问题讨论】:

  • 请注意,由于这会改变服务器状态,使用 POST 会更好。
  • 感谢您指出这一点:)

标签: servlets workflow aem


【解决方案1】:

经过一段时间的探索,我编写了一个运行工作流模型的 servlet。

这是带有 cmets 的代码:

@Component
@Service
@Properties({
    @Property(name = "sling.servlet.paths", value = "/bin/runmodel"),
    @Property(name = "sling.servlet.methods", value = "GET")
})
public class RunWorkflowModel extends SlingSafeMethodsServlet {

    static private final Logger log = LoggerFactory.getLogger(RunWorkflowModel.class);

    @Reference
    private WorkflowService workflowService;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        ResourceResolver resourceResolver = request.getResourceResolver();
        Session session = resourceResolver.adaptTo(Session.class);

        /* Get Parameters
         * @param path = path you want to run the workflow on
         * @param model = workflow model name you want to run.  Typically found in /etc/workflow/models
         */
        RequestParameterMap params = request.getRequestParameterMap();
        String path = params.getValue("path").getString();
        String model = params.getValue("model").getString();

        // Create a workflow session 
        WorkflowSession wfSession = workflowService.getWorkflowSession(session);
        try {
            // Get the workflow model
            WorkflowModel wfModel = wfSession.getModel(model);                
            // Get the workflow data
            // The first param in the newWorkflowData method is the payloadType.  Just a fancy name to let it know what type of workflow it is working with.
            WorkflowData wfData = wfSession.newWorkflowData("JCR_PATH", path);
            // Run the Workflow.
            wfSession.startWorkflow(wfModel, wfData);
        } catch (WorkflowException ex) {
            response.getWriter().write("failed");
            log.error("Error starting workflow.", ex);
        }

        response.getWriter().write("success");
    }
}

这里是 Ajax 调用

CQ.Ext.Ajax.request({
    url: "/bin/runmodel",
    method: "GET",
    params : { 
        "path"  : "/content/path to item you want the workflow run on",
        "model" : "/etc/workflow/models/name of model/jcr:content/model"
    },
    success: function() {
        console.log("success");
    },
    failure: function(response) {
        CQ.Notification.notifyFromResponse(response);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    相关资源
    最近更新 更多