KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.db;
2
3 import java.util.Iterator JavaDoc;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.hibernate.HibernateException;
8 import org.hibernate.Query;
9 import org.hibernate.Session;
10 import org.jbpm.graph.exe.ProcessInstance;
11 import org.jbpm.scheduler.exe.SchedulerInstance;
12 import org.jbpm.scheduler.exe.Timer;
13
14 public class SchedulerSession {
15
16   JbpmSession jbpmSession = null;
17   Session session = null;
18   
19   public SchedulerSession(JbpmSession jbpmSession) {
20     this.jbpmSession = jbpmSession;
21     this.session = jbpmSession.getSession();
22   }
23
24   public void saveTimer(Timer timer) {
25     try {
26       session.save(timer);
27     } catch (Exception JavaDoc e) {
28       log.error(e);
29       jbpmSession.handleException();
30       throw new RuntimeException JavaDoc("couldn't save timer '"+timer+"' to the database", e);
31     }
32   }
33
34   public void deleteTimer(Timer timer) {
35     try {
36       session.delete(timer);
37     } catch (Exception JavaDoc e) {
38       log.error(e);
39       jbpmSession.handleException();
40       throw new RuntimeException JavaDoc("couldn't delete timer '"+timer+"' from the database", e);
41     }
42   }
43
44   private static final String JavaDoc findTimersByDueDate =
45     "select ti " +
46     "from org.jbpm.scheduler.exe.Timer as ti " +
47     "where ti.exception is null " +
48     "order by ti.dueDate asc";
49   public Iterator JavaDoc findTimersByDueDate() {
50     try {
51       return session.createQuery(findTimersByDueDate).iterate();
52     } catch (Exception JavaDoc e) {
53       log.error(e);
54       jbpmSession.handleException();
55       throw new RuntimeException JavaDoc("couldn't find timers from the database", e);
56     }
57   }
58
59   private static final String JavaDoc findFailedTimers =
60     "select ti " +
61     "from org.jbpm.scheduler.exe.Timer as ti " +
62     "where ti.exception is not null " +
63     "order by ti.dueDate asc";
64   public Iterator JavaDoc findFailedTimers() {
65     try {
66       return session.createQuery(findFailedTimers).iterate();
67     } catch (Exception JavaDoc e) {
68       log.error(e);
69       jbpmSession.handleException();
70       throw new RuntimeException JavaDoc("couldn't find failed timers from the database", e);
71     }
72   }
73
74   void saveTimers(ProcessInstance processInstance) {
75     SchedulerInstance schedulerInstance = processInstance.getSchedulerInstance();
76     
77     // if the process instance was ended,
78
if (schedulerInstance.isProcessEnded()) {
79       // delete all the timers for this process instance
80
cancelTimersForProcessInstance(processInstance);
81       
82     } else {
83       // save the scheduled timers
84
Iterator JavaDoc iter = schedulerInstance.getScheduledTimers().iterator();
85       while (iter.hasNext()) {
86         Timer timer = (Timer) iter.next();
87         log.debug("saving timer "+timer);
88         session.save(timer);
89       }
90       
91       // delete the cancelled timers
92
iter = schedulerInstance.getCancelledTimerNames().iterator();
93       while (iter.hasNext()) {
94         SchedulerInstance.CancelledTimer cancelledTimer = (SchedulerInstance.CancelledTimer) iter.next();
95         cancelTimers(cancelledTimer);
96       }
97     }
98   }
99   
100   private static final String JavaDoc deleteTimersQuery =
101     "delete from org.jbpm.scheduler.exe.Timer " +
102     "where name = :timerName" +
103     " and token = :token";
104   public void cancelTimers(SchedulerInstance.CancelledTimer cancelledTimer) {
105     try {
106       Query query = session.createQuery(deleteTimersQuery);
107       query.setString("timerName", cancelledTimer.getTimerName());
108       query.setEntity("token", cancelledTimer.getToken());
109       query.executeUpdate();
110       
111     } catch (Exception JavaDoc e) {
112       log.error(e);
113       jbpmSession.handleException();
114       throw new RuntimeException JavaDoc("couldn't delete timers '"+cancelledTimer.getTimerName()+"' on token '"+cancelledTimer.getToken().getId()+"' from the database", e);
115     }
116   }
117
118   private static final String JavaDoc deleteTimersForProcessInstanceQuery =
119     "delete from org.jbpm.scheduler.exe.Timer " +
120     "where processInstance = :processInstance";
121   public void cancelTimersForProcessInstance(ProcessInstance processInstance) {
122     try {
123       Query query = session.createQuery(deleteTimersForProcessInstanceQuery);
124       query.setEntity("processInstance", processInstance);
125       query.executeUpdate();
126       
127     } catch (Exception JavaDoc e) {
128       log.error(e);
129       jbpmSession.handleException();
130       throw new RuntimeException JavaDoc("couldn't delete timers for process instance '"+processInstance+"'", e);
131     }
132   }
133
134   private static final Log log = LogFactory.getLog(SchedulerSession.class);
135 }
136
Popular Tags