1 package org.jbpm.db; 2 3 import java.util.*; 4 5 import org.apache.commons.logging.*; 6 import org.hibernate.*; 7 import org.jbpm.graph.def.*; 8 import org.jbpm.graph.exe.*; 9 10 13 public class GraphSession { 14 15 JbpmSession jbpmSession = null; 16 Session session = null; 17 18 public GraphSession(JbpmSession jbpmSession) { 19 this.jbpmSession = jbpmSession; 20 this.session = jbpmSession.getSession(); 21 } 22 23 25 29 public void saveProcessDefinition( ProcessDefinition processDefinition ) { 30 try { 31 session.save(processDefinition); 32 } catch (Exception e) { 33 log.error(e); 34 jbpmSession.handleException(); 35 throw new RuntimeException ("couldn't save process definition '" + processDefinition + "'", e); 36 } 37 } 38 39 42 public ProcessDefinition loadProcessDefinition(long processDefinitionId) { 43 try { 44 return (ProcessDefinition) session.load( ProcessDefinition.class, new Long (processDefinitionId) ); 45 } catch (Exception e) { 46 log.error(e); 47 jbpmSession.handleException(); 48 throw new RuntimeException ("couldn't load process definition '" + processDefinitionId + "'", e); 49 } 50 } 51 52 private static final String findProcessDefinitionByNameAndVersionQuery = 53 "select pd " + 54 "from org.jbpm.graph.def.ProcessDefinition as pd " + 55 "where pd.name = :name " + 56 " and pd.version = :version"; 57 60 public ProcessDefinition findProcessDefinition(String name, int version) { 61 ProcessDefinition processDefinition = null; 62 try { 63 Query query = session.createQuery(findProcessDefinitionByNameAndVersionQuery); 64 query.setString("name", name); 65 query.setInteger("version", version); 66 Iterator result = query.iterate(); 67 if ( result.hasNext() ) { 68 processDefinition = (ProcessDefinition) result.next(); 69 } 70 } catch (Exception e) { 71 log.error(e); 72 jbpmSession.handleException(); 73 throw new RuntimeException ("couldn't get process definition with name '"+name+"' and version '"+version+"'", e); 74 } 75 return processDefinition; 76 } 77 78 private static final String findLatestProcessDefinitionQuery = 79 "select pd " + 80 "from org.jbpm.graph.def.ProcessDefinition as pd " + 81 "where pd.name = :name " + 82 "order by pd.version desc"; 83 86 public ProcessDefinition findLatestProcessDefinition(String name) { 87 ProcessDefinition processDefinition = null; 88 try { 89 Query query = session.createQuery(findLatestProcessDefinitionQuery); 90 query.setString("name", name); 91 Iterator result = query.iterate(); 92 if ( result.hasNext() ) { 93 processDefinition = (ProcessDefinition) result.next(); 94 } 95 } catch (Exception e) { 96 log.error(e); 97 jbpmSession.handleException(); 98 throw new RuntimeException ("couldn't find process definition '" + name + "'", e); 99 } 100 return processDefinition; 101 } 102 103 107 public List findLatestProcessDefinitions() { 108 List processDefinitions = new ArrayList(); 109 Map processDefinitionsByName = new HashMap(); 110 try { 111 Query query = session.createQuery(findAllProcessDefinitionsQuery); 112 Iterator iter = query.list().iterator(); 113 while (iter.hasNext()) { 114 ProcessDefinition processDefinition = (ProcessDefinition) iter.next(); 115 String processDefinitionName = processDefinition.getName(); 116 ProcessDefinition previous = (ProcessDefinition) processDefinitionsByName.get(processDefinitionName); 117 if ( (previous==null) 118 || (previous.getVersion()<processDefinition.getVersion()) 119 ){ 120 processDefinitionsByName.put(processDefinitionName, processDefinition); 121 } 122 } 123 processDefinitions = new ArrayList(processDefinitionsByName.values()); 124 } catch (Exception e) { 125 log.error(e); 126 jbpmSession.handleException(); 127 throw new RuntimeException ("couldn't find latest versions of process definitions", e); 128 } 129 return processDefinitions; 130 } 131 132 private static final String findAllProcessDefinitionsQuery = 133 "select pd " + 134 "from org.jbpm.graph.def.ProcessDefinition as pd " + 135 "order by pd.name, pd.version desc"; 136 139 public List findAllProcessDefinitions() { 140 try { 141 Query query = session.createQuery(findAllProcessDefinitionsQuery); 142 return query.list(); 143 } catch (Exception e) { 144 log.error(e); 145 jbpmSession.handleException(); 146 throw new RuntimeException ("couldn't find all process definitions", e); 147 } 148 } 149 150 private static final String findAllProcessDefinitionVersionsQuery = 151 "select pd " + 152 "from org.jbpm.graph.def.ProcessDefinition as pd " + 153 "where pd.name = :name " + 154 "order by pd.version desc"; 155 158 public List findAllProcessDefinitionVersions(String name) { 159 try { 160 Query query = session.createQuery(findAllProcessDefinitionVersionsQuery); 161 query.setString("name", name); 162 return query.list(); 163 } catch (HibernateException e) { 164 log.error(e); 165 throw new RuntimeException ("couldn't find all versions of process definition '"+name+"'", e); 166 } 167 } 168 169 public void deleteProcessDefinition(long processDefinitionId) { 170 deleteProcessDefinition(loadProcessDefinition(processDefinitionId)); 171 } 172 173 public void deleteProcessDefinition(ProcessDefinition processDefinition) { 174 if (processDefinition==null) throw new NullPointerException ("processDefinition is null in JbpmSession.deleteProcessDefinition()"); 175 try { 176 List processInstances = findProcessInstances(processDefinition.getId()); 178 if (processInstances!=null) { 179 Iterator iter = processInstances.iterator(); 180 while (iter.hasNext()) { 181 deleteProcessInstance((ProcessInstance) iter.next()); 182 } 183 } 184 185 session.delete(processDefinition); 187 188 } catch (Exception e) { 189 log.error(e); 190 jbpmSession.handleException(); 191 throw new RuntimeException ("couldn't delete process definition '" + processDefinition.getId() + "'", e); 192 } 193 } 194 195 197 200 public void saveProcessInstance(ProcessInstance processInstance) { 201 try { 202 session.saveOrUpdate(processInstance); 204 jbpmSession.getLoggingSession().saveLogs(processInstance); 205 jbpmSession.getSchedulerSession().saveTimers(processInstance); 206 } catch (Exception e) { 207 log.error(e); 208 jbpmSession.handleException(); 209 throw new RuntimeException ("couldn't save process instance '" + processInstance + "'", e); 210 } 211 } 212 213 216 public ProcessInstance loadProcessInstance(long processInstanceId) { 217 try { 218 ProcessInstance processInstance = (ProcessInstance) session.load( ProcessInstance.class, new Long (processInstanceId) ); 219 return processInstance; 220 } catch (Exception e) { 221 log.error(e); 222 jbpmSession.handleException(); 223 throw new RuntimeException ("couldn't load process instance '" + processInstanceId + "'", e); 224 } 225 } 226 227 230 public Token loadToken(long tokenId) { 231 try { 232 Token token = (Token) session.load(Token.class, new Long (tokenId)); 233 return token; 234 } catch (Exception e) { 235 log.error(e); 236 jbpmSession.handleException(); 237 throw new RuntimeException ("couldn't load token '" + tokenId + "'", e); 238 } 239 } 240 241 244 public void lockProcessInstance(long processInstanceId) { 245 lockProcessInstance(loadProcessInstance(processInstanceId)); 246 } 247 248 251 public void lockProcessInstance(ProcessInstance processInstance) { 252 try { 253 session.lock( processInstance, LockMode.UPGRADE ); 254 } catch (Exception e) { 255 log.error(e); 256 jbpmSession.handleException(); 257 throw new RuntimeException ("couldn't lock process instance '" + processInstance.getId() + "'", e); 258 } 259 } 260 261 private static final String findAllProcessInstancesForADefinitionQuery = 262 "select pi " + 263 "from org.jbpm.graph.exe.ProcessInstance as pi " + 264 "where pi.processDefinition.id = :processDefinitionId " + 265 "order by pi.start desc"; 266 270 public List findProcessInstances(long processDefinitionId) { 271 List processInstances = null; 272 try { 273 Query query = session.createQuery(findAllProcessInstancesForADefinitionQuery); 274 query.setLong("processDefinitionId", processDefinitionId); 275 processInstances = query.list(); 276 277 } catch (Exception e) { 278 log.error(e); 279 jbpmSession.handleException(); 280 throw new RuntimeException ("couldn't load process instances for process definition '" + processDefinitionId + "'", e); 281 } 282 return processInstances; 283 } 284 285 public void deleteProcessInstance(long processInstanceId) { 286 deleteProcessInstance(loadProcessInstance(processInstanceId)); 287 } 288 289 private static final String findTokensForProcessInstance = 290 "select token " + 291 "from org.jbpm.graph.exe.Token token " + 292 "where token.processInstance = :processInstance "; 293 private static final String deleteLogsForTokens = 294 "delete org.jbpm.logging.log.ProcessLog " + 295 "where token in (:tokens)"; 296 public void deleteProcessInstance(ProcessInstance processInstance) { 297 if (processInstance==null) throw new NullPointerException ("processInstance is null in JbpmSession.deleteProcessInstance()"); 298 try { 299 Query query = session.createQuery(findTokensForProcessInstance); 301 query.setEntity("processInstance", processInstance); 302 List tokens = query.list(); 303 304 query = session.createQuery(deleteLogsForTokens); 306 query.setParameterList("tokens", tokens); 307 query.executeUpdate(); 308 309 session.delete(processInstance); 311 312 } catch (Exception e) { 313 log.error(e); 314 jbpmSession.handleException(); 315 throw new RuntimeException ("couldn't delete process instance '" + processInstance.getId() + "'", e); 316 } 317 } 318 319 private static final Log log = LogFactory.getLog(GraphSession.class); 320 } 321
| Popular Tags
|