KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > db > GraphSession


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 /**
11  * are the graph related database operations.
12  */

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   // process definitions //////////////////////////////////////////////////////
24

25   /**
26    * saves the process definitions. this method does not assign a version
27    * number. that is the responsibility of the {@link org.jbpm.jpdl.par.ProcessArchiveDeployer}.
28    */

29   public void saveProcessDefinition( ProcessDefinition processDefinition ) {
30     try {
31       session.save(processDefinition);
32     } catch (Exception JavaDoc e) {
33       log.error(e);
34       jbpmSession.handleException();
35       throw new RuntimeException JavaDoc("couldn't save process definition '" + processDefinition + "'", e);
36     }
37   }
38   
39   /**
40    * loads a process definition from the database by the identifier.
41    */

42   public ProcessDefinition loadProcessDefinition(long processDefinitionId) {
43     try {
44       return (ProcessDefinition) session.load( ProcessDefinition.class, new Long JavaDoc(processDefinitionId) );
45     } catch (Exception JavaDoc e) {
46       log.error(e);
47       jbpmSession.handleException();
48       throw new RuntimeException JavaDoc("couldn't load process definition '" + processDefinitionId + "'", e);
49     }
50   }
51   
52   private static final String JavaDoc findProcessDefinitionByNameAndVersionQuery =
53     "select pd " +
54     "from org.jbpm.graph.def.ProcessDefinition as pd " +
55     "where pd.name = :name " +
56     " and pd.version = :version";
57   /**
58    * queries the database for a process definition with the given name and version.
59    */

60   public ProcessDefinition findProcessDefinition(String JavaDoc 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 JavaDoc e) {
71       log.error(e);
72       jbpmSession.handleException();
73       throw new RuntimeException JavaDoc("couldn't get process definition with name '"+name+"' and version '"+version+"'", e);
74     }
75     return processDefinition;
76   }
77   
78   private static final String JavaDoc 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   /**
84    * queries the database for the latest version of a process definition with the given name.
85    */

86   public ProcessDefinition findLatestProcessDefinition(String JavaDoc 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 JavaDoc e) {
96       log.error(e);
97       jbpmSession.handleException();
98       throw new RuntimeException JavaDoc("couldn't find process definition '" + name + "'", e);
99     }
100     return processDefinition;
101   }
102
103   /**
104    * queries the database for the latest version of each process definition.
105    * Process definitions are distinct by name.
106    */

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 JavaDoc 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 JavaDoc e) {
125       log.error(e);
126       jbpmSession.handleException();
127       throw new RuntimeException JavaDoc("couldn't find latest versions of process definitions", e);
128     }
129     return processDefinitions;
130   }
131
132   private static final String JavaDoc findAllProcessDefinitionsQuery =
133     "select pd " +
134     "from org.jbpm.graph.def.ProcessDefinition as pd " +
135     "order by pd.name, pd.version desc";
136   /**
137    * queries the database for all process definitions, ordered by name (ascending), then by version (descending).
138    */

139   public List findAllProcessDefinitions() {
140     try {
141       Query query = session.createQuery(findAllProcessDefinitionsQuery);
142       return query.list();
143     } catch (Exception JavaDoc e) {
144       log.error(e);
145       jbpmSession.handleException();
146       throw new RuntimeException JavaDoc("couldn't find all process definitions", e);
147     }
148   }
149
150   private static final String JavaDoc 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   /**
156    * queries the database for all versions of process definitions with the given name, ordered by version (descending).
157    */

158   public List findAllProcessDefinitionVersions(String JavaDoc 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 JavaDoc("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 JavaDoc("processDefinition is null in JbpmSession.deleteProcessDefinition()");
175     try {
176       // delete all the process instances of this definition
177
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       // then delete the process definition
186
session.delete(processDefinition);
187
188     } catch (Exception JavaDoc e) {
189       log.error(e);
190       jbpmSession.handleException();
191       throw new RuntimeException JavaDoc("couldn't delete process definition '" + processDefinition.getId() + "'", e);
192     }
193   }
194
195   // process instances ////////////////////////////////////////////////////////
196

197   /**
198    * save a new process instance.
199    */

200   public void saveProcessInstance(ProcessInstance processInstance) {
201     try {
202       // saveProcessInstanceLogs(processInstance);
203
session.saveOrUpdate(processInstance);
204       jbpmSession.getLoggingSession().saveLogs(processInstance);
205       jbpmSession.getSchedulerSession().saveTimers(processInstance);
206     } catch (Exception JavaDoc e) {
207       log.error(e);
208       jbpmSession.handleException();
209       throw new RuntimeException JavaDoc("couldn't save process instance '" + processInstance + "'", e);
210     }
211   }
212
213   /**
214    * loads a process instance from the database by the identifier.
215    */

216   public ProcessInstance loadProcessInstance(long processInstanceId) {
217     try {
218       ProcessInstance processInstance = (ProcessInstance) session.load( ProcessInstance.class, new Long JavaDoc(processInstanceId) );
219       return processInstance;
220     } catch (Exception JavaDoc e) {
221       log.error(e);
222       jbpmSession.handleException();
223       throw new RuntimeException JavaDoc("couldn't load process instance '" + processInstanceId + "'", e);
224     }
225   }
226
227   /**
228    * loads a token from the database by the identifier.
229    */

230   public Token loadToken(long tokenId) {
231     try {
232       Token token = (Token) session.load(Token.class, new Long JavaDoc(tokenId));
233       return token;
234     } catch (Exception JavaDoc e) {
235       log.error(e);
236       jbpmSession.handleException();
237       throw new RuntimeException JavaDoc("couldn't load token '" + tokenId + "'", e);
238     }
239   }
240
241   /**
242    * locks a process instance in the database.
243    */

244   public void lockProcessInstance(long processInstanceId) {
245     lockProcessInstance(loadProcessInstance(processInstanceId));
246   }
247
248   /**
249    * locks a process instance in the database.
250    */

251   public void lockProcessInstance(ProcessInstance processInstance) {
252     try {
253       session.lock( processInstance, LockMode.UPGRADE );
254     } catch (Exception JavaDoc e) {
255       log.error(e);
256       jbpmSession.handleException();
257       throw new RuntimeException JavaDoc("couldn't lock process instance '" + processInstance.getId() + "'", e);
258     }
259   }
260
261   private static final String JavaDoc 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   /**
267    * fetches all processInstances for the given process definition from the database.
268    * The returned list of process instances is sorted start date, youngest first.
269    */

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 JavaDoc e) {
278       log.error(e);
279       jbpmSession.handleException();
280       throw new RuntimeException JavaDoc("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 JavaDoc findTokensForProcessInstance =
290     "select token " +
291     "from org.jbpm.graph.exe.Token token " +
292     "where token.processInstance = :processInstance ";
293   private static final String JavaDoc 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 JavaDoc("processInstance is null in JbpmSession.deleteProcessInstance()");
298     try {
299       // find the tokens
300
Query query = session.createQuery(findTokensForProcessInstance);
301       query.setEntity("processInstance", processInstance);
302       List tokens = query.list();
303       
304       // delete the logs for all the process instance's tokens
305
query = session.createQuery(deleteLogsForTokens);
306       query.setParameterList("tokens", tokens);
307       query.executeUpdate();
308
309       // then delete the process instance
310
session.delete(processInstance);
311       
312     } catch (Exception JavaDoc e) {
313       log.error(e);
314       jbpmSession.handleException();
315       throw new RuntimeException JavaDoc("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