<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Re: How to Complete a WorkItem using REST API
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/analamala">analamala</a> in <i>jBPM</i> - <a href="http://community.jboss.org/message/631358#631358">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>Hello Chris,</p><p><span>Thanks a lot for your time. Let me step back and thank you sincerely for the information that you posted at </span><a class="jive-link-external-small" href="http://community.jboss.org/thread/166755?start=0&tstart=0" target="_blank">http://community.jboss.org/thread/166755?start=0&tstart=0</a><span>. Really helped in many ways.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Coming back to this discussion, I made some progress on how to complete a workitem by extending the REST API. To answer your question, yes, the code that I modified, is running in the context of a jbpm engine. Here are my changes to "gwt-console-server\WEB-INF\classes\org\jboss\bpm\console\server\ProcessMgmtFacade.java"</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #0000ff;">@POST</span></p><p><span style="color: #0000ff;">    @Path("definition/{id}/ctr_component_complete")</span></p><p><span style="color: #0000ff;">    @Produces("application/json")</span></p><p><span style="color: #0000ff;">    public Response ctrComponentComplete(@PathParam("id") String definitionId,</span></p><p><span style="color: #0000ff;">            @FormParam("sessionID") String sessionID,</span></p><p><span style="color: #0000ff;">            @FormParam("workitemID") String workItemID) {</span></p><p><span style="color: #0000ff;">        try {</span></p><p><span style="color: #0000ff;">            EntityManagerFactory emf = Persistence</span></p><p><span style="color: #0000ff;">                    .createEntityManagerFactory("org.jbpm.persistence.jpa");</span></p><p><span style="color: #0000ff;">            Environment env = KnowledgeBaseFactory.newEnvironment();</span></p><p><span style="color: #0000ff;">            env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);</span></p><p><span style="color: #0000ff;">            env.set(EnvironmentName.TRANSACTION_MANAGER,</span></p><p><span style="color: #0000ff;">                    TransactionManagerServices.getTransactionManager());</span></p><p><span style="color: #0000ff;">            UserTransaction ut = (UserTransaction) new InitialContext()</span></p><p><span style="color: #0000ff;">                    .lookup("java:comp/UserTransaction");</span></p><p><span style="color: #0000ff;">            env.set(EnvironmentName.TRANSACTION, ut);</span></p><p><span style="color: #0000ff;">            StatefulKnowledgeSession ksession = JPAKnowledgeService</span></p><p><span style="color: #0000ff;">                    .loadStatefulKnowledgeSession(</span></p><p><span style="color: #0000ff;">                            new Integer(sessionID).intValue(),</span></p><p><span style="color: #0000ff;">                            readKnowledgeBase(), null, env);</span></p><p><span style="color: #0000ff;">            ksession.getWorkItemManager().completeWorkItem(</span></p><p><span style="color: #0000ff;">                    new Long(workItemID).longValue(), null);</span></p><p>            </p><p><span style="color: #0000ff;">            ksession.dispose();</span></p><p>            </p><p><span style="color: #0000ff;">            return createJsonResponse("SUCCESS");</span></p><p><span style="color: #0000ff;">        } catch (Throwable t) {</span></p><p><span style="color: #0000ff;">            throw new WebApplicationException(t, 500);</span></p><p><span style="color: #0000ff;">        }</span></p><p><span style="color: #0000ff;">    }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #0000ff;">private KnowledgeBase readKnowledgeBase() throws Exception {</span></p><p><span style="color: #0000ff;">        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory</span></p><p><span style="color: #0000ff;">                .newKnowledgeBuilder();</span></p><p><span style="color: #0000ff;">        kbuilder.add(ResourceFactory.newClassPathResource("CTRWorkflow.bpmn"),</span></p><p><span style="color: #0000ff;">                ResourceType.BPMN2);</span></p><p><span style="color: #0000ff;">        KnowledgeBase kbase = kbuilder.newKnowledgeBase();</span></p><p><span style="color: #0000ff;">        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());</span></p><p><span style="color: #0000ff;">        return kbase;</span></p><p><span style="color: #0000ff;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #000000;">As you can see, I am creating a new KnowledgeBase and using the parameters, 'sessionID' and 'workitemID' to accomplish this. Everything seems to be working as per the process design. However, in the jbpm-console web application's 'Process Overview' tab, the process instance is still showing up as 'RUNNING', eventhough I can see from the logs that the process has reached the end state. To confirm that the state is not changing, I clicked on the 'Diagram' button to bring up the process image. The image is coming up but not updating with the latest state. <br/></span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #000000;">I have a feeling that this issue may be related to creating a new KnowledgeBase rather than using the existing KnowledgeBase that JBPM engine and the jbpm-console web application share. To mitigate this problem, I tried to get a reference to the KnowledgeBase by trying several alternate approaches. But by these alternate approaches, I am not able to even complete the workitem. Here is one of the approaches that I tried.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span style="color: #ff0000;">private KnowledgeBase readKnowledgeBase() throws Exception {</span></p><p><span style="color: #ff0000;">        KnowledgeBase kbase = null;</span></p><p><span style="color: #ff0000;">        try {</span></p><p><span style="color: #ff0000;">            ResourceChangeScannerConfiguration sconf = ResourceFactory.getResourceChangeScannerService().newResourceChangeScannerConfiguration();</span></p><p><span style="color: #ff0000;">            sconf.setProperty( "drools.resource.scanner.interval", "10" );</span></p><p><span style="color: #ff0000;">            ResourceFactory.getResourceChangeScannerService().configure( sconf );</span></p><p><span style="color: #ff0000;">            ResourceFactory.getResourceChangeScannerService().start();</span></p><p><span style="color: #ff0000;">            ResourceFactory.getResourceChangeNotifierService().start();</span></p><p><span style="color: #ff0000;">            KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();</span></p><p><span style="color: #ff0000;">            aconf.setProperty("drools.agent.newInstance", "false");</span></p><p><span style="color: #ff0000;">            KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("Guvnor default", aconf);</span></p><p><span style="color: #ff0000;">            kagent.applyChangeSet(ResourceFactory.newClassPathResource("ChangeSet.xml"));</span></p><p><span style="color: #ff0000;">            kbase = kagent.getKnowledgeBase();</span></p><p><span style="color: #ff0000;">            for (org.drools.definition.process.Process process: kbase.getProcesses()) {</span></p><p><span style="color: #ff0000;">                log.debug("Loading process from Guvnor: " + process.getId());</span></p><p><span style="color: #ff0000;">            }</span></p><p><span style="color: #ff0000;">        } catch (Throwable t) {</span></p><p><span style="color: #ff0000;">            if (t instanceof RuntimeException</span></p><p><span style="color: #ff0000;">                    && "KnowledgeAgent exception while trying to deserialize".equals(t.getMessage())) {</span></p><p><span style="color: #ff0000;">                log.error("Could not connect to guvnor");</span></p><p><span style="color: #ff0000;">                if (t.getCause() != null) {</span></p><p><span style="color: #ff0000;">                    System.out.println(t.getCause().getMessage());</span></p><p><span style="color: #ff0000;">                }</span></p><p><span style="color: #ff0000;">            }</span></p><p><span style="color: #ff0000;">            log.error("Could not load processes from guvnor: " + t.getMessage());</span></p><p><span style="color: #ff0000;">            t.printStackTrace();</span></p><p><span style="color: #ff0000;">        }</span></p><p><span style="color: #ff0000;">        if (kbase == null) {</span></p><p><span style="color: #ff0000;">            System.out.println("kbase is NULL");</span></p><p><span style="color: #ff0000;">            kbase = KnowledgeBaseFactory.newKnowledgeBase();</span></p><p><span style="color: #ff0000;">        }</span></p><p><span style="color: #ff0000;">        return kbase;</span></p><p><span style="color: #ff0000;">    }</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Here is my process image. Note that both the domain specific service tasks, 'Extraction' & 'Validation' operate in asynchronous mode, by EXCLUDING the "manager.completeWorkItem(workItem.getId(), null);" call.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><a href="http://community.jboss.org/servlet/JiveServlet/showImage/2-631358-17153/CTRworkflow.png"><span> http://community.jboss.org/servlet/JiveServlet/downloadImage/2-631358-17153/450-144/CTRworkflow.png </span></a></p><p>I really apreciate your time on this and hope this discussion helps few more community members out there.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Thank you.</p><p>-Ajay</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/631358#631358">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in jBPM at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2034">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>