11 Replies Latest reply on Dec 21, 2007 2:14 AM by dleerob

    removeTaskInstance doesn't work!

    mindthegap

      Hi,

      I need in my code to remove some TaskInstances.
      I have:

      static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
      
      public void removeTI(TaskInstance SomeTaskInstance) {
       JbpmContext jc = jbpmConfiguration.createJbpmContext();
       TaskMgmtSession tms = jc.getTaskMgmtSession();
       try {
       GraphSession gs = jc.getGraphSession();
       ProcessDefinition pd = gs.findLatestProcessDefinition("PROCESS");
       ProcessInstance pi = new ProcessInstance(pd);
      pi.getTaskMgmtInstance().removeTaskInstance(SomeTaskInstance);
      
       jc.save(pi);
       } finally {
      
       jc.close();
       }
       }
      


      it doesn't throw any error but my jBPM database remains exactly the same.
      What im i forgot?

      Thanks in advance

      RQ_

        • 1. Re: removeTaskInstance doesn't work!
          kukeltje

          uhhmmm... you pass a taskinstancereference to a method wgich creates a new process (so no taskinstances) and want to remove "it" ..... that will never work. I'd even expect an exception, but that might be a runtime exception....

          • 2. Re: removeTaskInstance doesn't work!
            mindthegap

            Sorry, the real code is this:

            public void removeStateProcessInstance(Integer keyRef, String processNname, String state) {
             JbpmContext jc = jbpmConfiguration.createJbpmContext();
             TaskMgmtSession tms = jc.getTaskMgmtSession();
             try {
             GraphSession gs = jc.getGraphSession();
             ProcessDefinition pd = gs.findLatestProcessDefinition(processName);
            
             List processInstances = gs.findProcessInstances(pd.getId());
             if (processInstances.isEmpty() == false) {
             ListIterator listProcessInstances = processInstances.listIterator();
             while (listProcessInstances.hasNext() ) {
             ProcessInstance pi = (ProcessInstance)listProcessInstances.next();
             ContextInstance contextInstance = pi.getContextInstance();
             if(contextInstance.getVariable("key")!=null) {
             if(keyRef.intValue() == Integer.parseInt(contextInstance.getVariable("key").toString())) {
            
             //Obtém o TaskInstanceId da última tarefa da lista actual
             Iterator taskInstances = pi.getTaskMgmtInstance().getTaskInstances().iterator();
             TaskInstance ti = null;
            
             long tid = 0;
             while (taskInstances.hasNext()) {
             ti = (TaskInstance) taskInstances.next();
             if(ti.getName().equals(state)) tid = ti.getId();
             }
            
             //Put 'End' field to null and get token node
             Node node = null;
             ArrayList<Long> x = new ArrayList<Long>();
             for(Iterator i=pi.getTaskMgmtInstance().getTaskInstances().iterator(); i.hasNext();) {
             ti = (TaskInstance) i.next();
             if(ti.getId()==tid) {
             ti.setEnd(null);
             node = ti.getToken().getNode();
             }
            
             if(ti.getId()>tid){
             x.add(ti.getId());
             }
             }
            
            
             Token token = pi.getRootToken();
             token.setNode(node);
            
             int i;
            //remove taskInstances
             for(i=0;i<x.size();i++) {
             TaskInstance tit = tms.loadTaskInstance(Long.parseLong(x.get(i).toString()));
             pi.getTaskMgmtInstance().removeTaskInstance(tit);
            
             }
            
             }
             }
             jc.save(pi);
             break;
             }
             }
             }catch (Exception e) {
             System.out.println(e.getMessage());
             } finally {
             jc.close();
             }
             }
            


            It doesn't save the changes in the db!
            Is there any restriction to remove taskInstances?

            Thanks in advance!

            • 3. Re: removeTaskInstance doesn't work!
              kukeltje

              don't see anything wrong with that at first site. Are you sure anthing 'should' be removed.....? Do you see in the debug of jbpm that anything is being tried to be removed?

              And btw, why do you want to remove taskinstances from the db?

              • 4. Re: removeTaskInstance doesn't work!
                mindthegap

                I Ronald,

                If i delete the specific row in jbpm_logs then i can delete the taskInstance.

                Id like to give to the user the possibility of making undo.
                Can Process logs be used to implement the undo?


                Thanks!

                • 5. Re: removeTaskInstance doesn't work!
                  kukeltje

                  Aren't there any exceptions then? Like fk contraint violations???

                  And yes, using the logs is one of the things to use when implementing undo. See jbpm 2.0 on how that was done and look at the jira for some undo info

                  • 6. Re: removeTaskInstance doesn't work!
                    mindthegap

                    No exceptions :(
                    The code i show to you runs perfectly and in debug i inspect the return of getTaskInstances method and the taskInstances are removed...but after closing the context my db remains the same!! :)


                    Ok, im going to read the documentation.
                    But btw the best way to implementing undo is through process_logs?

                    Thanks Ronald

                    • 7. Re: removeTaskInstance doesn't work!
                      kukeltje

                      using the processlogs plays an important role for sure, but there is more to it (details unknown to me)

                      If you turn on hibernate sql showing, do you see statements that they are removed? Other things I'm thinking of are: transaction not committed, hibernate config to fail silently if there are fk constraints violated??? turn on hibernate debugging..

                      Ronald

                      • 8. Re: removeTaskInstance doesn't work!
                        mindthegap

                        Hi!

                        After turning on the show sql parameter i saw that no DELETE statement appears in my console.

                        So i do this test case that is OK and continues to not show any DELETE jbpm.TASKINSTANCE...:

                        public void testToken() {
                         JbpmContext jc = jbpmConfiguration.createJbpmContext();
                         TaskMgmtSession tms = jc.getTaskMgmtSession();
                         try {
                         GraphSession gs = jc.getGraphSession();
                         ProcessDefinition pd = gs.findLatestProcessDefinition("XXX");
                         ProcessInstance pi = new ProcessInstance(pd);
                        
                         Token token = pi.getRootToken();
                         assertEquals("INIT", token.getNode().getName());
                         pi.signal();
                         assertEquals("REG", token.getNode().getName());
                         pi.signal();
                         assertEquals("PRO", token.getNode().getName());
                        
                         //remove logs ( a despaired try :)))
                         LoggingInstance li = pi.getLoggingInstance();
                         List x= li.getLogs();
                         x.removeAll(x);
                        
                         TaskInstance tit = (TaskInstance) tms.findTaskInstancesByToken(token.getId()).get(0);
                         assertEquals("REG", tit.getName());
                         pi.getTaskMgmtInstance().removeTaskInstance(tit);
                        
                         jc.save(pi);
                         }catch (Exception e) {
                         System.out.println("no catch enter :(");
                         } finally {
                         jc.close();
                         }
                         }
                        


                        Any idea?

                        Thanks in advance

                        • 9. Re: removeTaskInstance doesn't work!
                          kukeltje

                          do you see sql statements at all?

                          • 10. Re: removeTaskInstance doesn't work!
                            mindthegap

                            Yep

                            Hibernate: select processdef0_.ID_ ...
                            Hibernate: insert into JBPM_TOKEN ...
                            Hibernate: insert into JBPM_PROCESSINSTANCE ...
                            Hibernate: select definition0_.PROCESSDEFINITION_ ...
                            Hibernate: select events0_.PROCESSDEFINITION_ ...
                            Hibernate: select startstate0_.ID_ ...
                            Hibernate: select leavingtra0_.FROM_ ...
                            Hibernate: select events0_.NODE_ ...
                            Hibernate: select events0_.TRANSITION_ ...
                            Hibernate: select node0_.ID_ ...
                            Hibernate: select events0_.NODE_ ...
                            Hibernate: select tasks0_.TASKNODE_ ...
                            Hibernate: insert into JBPM_TASKINSTANCE ...
                            Hibernate: select events0_.TASK_ ...
                            Hibernate: select leavingtra0_.FROM_ ...
                            Hibernate: select events0_.TRANSITION_ ...
                            Hibernate: select node0_.ID_ ...
                            Hibernate: select events0_.NODE_ ...
                            Hibernate: select tasks0_.TASKNODE_ ...
                            Hibernate: insert into JBPM_TASKINSTANCE ...
                            Hibernate: select events0_.TASK_ ...
                            Hibernate: insert into JBPM_MODULEINSTANCE ...
                            Hibernate: insert into JBPM_MODULEINSTANCE ...
                            Hibernate: update JBPM_TOKEN ...
                            Hibernate: update JBPM_PROCESSINSTANCE ...
                            Hibernate: update JBPM_TASKINSTANCE ...
                            Hibernate: update JBPM_TASKINSTANCE ...
                            Hibernate: update JBPM_MODULEINSTANCE ...
                            Hibernate: update JBPM_MODULEINSTANCE ...
                            Hibernate: select taskinstan0_.ID_ ...
                            


                            Strange...isnt it?

                            Thanks

                            RQ_

                            • 11. Re: removeTaskInstance doesn't work!
                              dleerob

                              I have the same problem where my removeTaskInstance isnt actually saving the changes to the db. My task instance still exists after calling that method.

                              Method removeTaskInstance fail becausa a constraint violation.
                              But if i delete processLogs it seems to work.

                              RQ_

                              How did you delete the processLogs?