2013-03-05 30 views
13

Tôi đã tạo một quy trình làm việc, trong CQ, cần thiết để chạy chương trình qua XHR.CQ5 Lập trình Chạy quy trình làm việc

Như nhiều bạn có thể biết, Tài liệu CQ không phải là lớn nhất (ít nhất là cho thời điểm này). Làm thế nào tôi có thể chạy nó theo chương trình?

+1

Lưu ý rằng, vì điều này thay đổi trạng thái máy chủ, sử dụng POST sẽ tốt hơn. –

+0

Cảm ơn bạn đã chỉ ra điều đó :) – Woodifer

Trả lời

16

Sau khi poking xung quanh trong một thời gian tôi đã viết một servlet chạy một mô hình quy trình làm việc.

Đây là mã với ý kiến:

@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"); 
    } 
} 

Đây là cuộc gọi 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); 
    } 
}); 
Các vấn đề liên quan