[jboss-user] [JBoss jBPM] - Re: Problems with deleting a process instance of a complex p

dleerob do-not-reply at jboss.com
Mon Nov 17 09:13:20 EST 2008


Thanks Jitendra for the helpful post. In the end, I managed to get my processes (with sub process) to delete successfully. I initially had problems with your exact method, but in the end it seemed to work. Here is how I got my implementation to work.

1) I copied the method deleteProcessInstance(ProcessInstance processInstance, boolean includeTasks, boolean includeJobs) from jbpm-jpdl-3.2.3\src\jpdl\org\jbpm\db\GraphSession.java, and put it into my own manager class, and modified as mentioned in point 3).

2) I copied the method deleteSubProcesses(Token token) from jbpm-jpdl-3.2.3\src\jpdl\org\jbpm\db\GraphSession.java, and put it into my own manager class and modified as mentioned in point 4).

3) 
Modified deleteProcessInstance method to look as follows:

  | public void deleteProcessInstance(ProcessInstance processInstance, boolean includeTasks, boolean includeJobs) {
  |     	log.info("Deleting process instance: " + processInstance.getProcessDefinition().getName()+" ("+processInstance.getId()+")");
  |     	if (processInstance==null) throw new JbpmException("processInstance is null in JbpmSession.deleteProcessInstance()");
  |     	try {
  |     		// find the tokens
  |     		Query query = hibernateSession.getNamedQuery("GraphSession.findTokensForProcessInstance");
  |     		query.setEntity("processInstance", processInstance);
  |     		List tokens = query.list();
  | 
  |     		// deleteSubProcesses
  |     		Iterator iter = tokens.iterator();
  |     		while (iter.hasNext()) {
  |     			Token token = (Token) iter.next();
  |     			deleteSubProcesses(token);
  |     		}
  | 
  |     		// jobs
  |     		if (includeJobs) {
  |     			query = hibernateSession.getNamedQuery("GraphSession.deleteJobsForProcessInstance");
  |     			query.setEntity("processInstance", processInstance);
  |     			query.executeUpdate();
  |     		}
  | 
  |     		// tasks
  |     		if (includeTasks) {
  |     			query = hibernateSession.getNamedQuery("GraphSession.findTaskInstanceIdsForProcessInstance");
  |     			query.setEntity("processInstance", processInstance);
  |     			List taskInstances = query.list();
  |     			//This is where the code differs from GraphSession.deleteProcessInstance---------
  |     			if(taskInstances.size() > 0 ) {
  |     				for (int x = 0; x < taskInstances.size(); x++) {
  |     					TaskInstance ti = (TaskInstance)taskInstances.get(x);
  |     					hibernateSession.delete(ti);
  |     				}
  |     			}
  |     			//---------------------------------------------------------------------------------
  |     		}
  | 
  |     		// delete the logs for all the process instance's tokens
  |     		query = hibernateSession.getNamedQuery("GraphSession.selectLogsForTokens");
  |     		query.setParameterList("tokens", tokens);
  |     		List logs = query.list();
  |     		iter = logs.iterator();
  |     		while (iter.hasNext()) {
  |     			hibernateSession.delete(iter.next());
  |     		}
  |     		
  |     		//This code is in addition to the code found in GraphSession.deleteProcessInstance--
  |     		//Delete tokens
  |     		iter = tokens.iterator();
  |     		while(iter.hasNext()) {
  |     			Token token = (Token)iter.next();
  |     			if(processInstance.getRootToken().getId()!= token.getId()) {
  |     				hibernateSession.delete(token);
  |     			}
  |     		}
  |     		//------------------------------------------------------------------------------------
  | 
  |     		// then delete the process instance
  |     		hibernateSession.delete(processInstance);
  | 
  |     	} 
  |     	catch (Exception e) {
  |     		throw new JbpmException("couldn't delete process instance '" + processInstance.getId() + "'", e);
  |     	} 
  | 
  |     }
  | 

4) Modified deleteSubProcesses method to look as follows:

  | private void deleteSubProcesses(Token token) {
  |     	//This is where the code different from GraphSession.deleteSubProcesses(Token)--------------------
  |     	//Commented out the following 2 lines-----------------------------------------------------
  |     	//Query query = hibernateSession.getNamedQuery("GraphSession.findSubProcessInstances");
  |     	//query.setEntity("processInstance", token.getProcessInstance());
  |     	//----------------------------------------------------------------------------------------
  |     	//Added the following 2 lines instead-----------------------------------------------------
  |     	Query query = hibernateSession.createQuery("select pi from org.jbpm.graph.exe.ProcessInstance as pi where pi.superProcessToken = :token");
  |     	query.setParameter("token", token);
  |     	//----------------------------------------------------------------------------------------
  |     	//------------------------------------------------------------------------------------------------
  |     	List processInstances = query.list();
  | 
  |     	if (processInstances == null || processInstances.isEmpty()) {
  |     		return; 
  |     	}
  |     	
  |     	Iterator iter = processInstances.iterator();
  |     	while (iter.hasNext()) {
  |     		ProcessInstance subProcessInstance = (ProcessInstance) iter.next();
  |     		subProcessInstance.setSuperProcessToken(null);
  |     		token.setSubProcessInstance(null);
  |     		deleteProcessInstance(subProcessInstance, true, true);
  |     	}
  |     	if (token.getChildren()!=null) {
  |     		iter = token.getChildren().values().iterator();
  |     		while (iter.hasNext()) {
  |     			Token child = (Token) iter.next();
  |     			deleteSubProcesses(child);
  |     		}
  |     	}
  |     }
  | 

When I wanted to delete a process instance, I use my modified methods instead. It seemed to work.

Can anyone see anything wrong with doing it this way?
Hope it helps someone else.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4189806#4189806

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4189806



More information about the jboss-user mailing list