KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.db;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.hibernate.HibernateException;
11 import org.hibernate.Query;
12 import org.hibernate.Session;
13 import org.jbpm.graph.exe.ProcessInstance;
14 import org.jbpm.graph.exe.Token;
15 import org.jbpm.logging.exe.LoggingInstance;
16 import org.jbpm.logging.log.ProcessLog;
17
18 public class LoggingSession {
19
20   JbpmSession jbpmSession;
21   Session session;
22   
23   public LoggingSession(JbpmSession jbpmSession) {
24     this.jbpmSession = jbpmSession;
25     this.session = jbpmSession.getSession();
26   }
27   
28   /**
29    * returns a map that maps {@link Token}s to {@link List}s. The lists contain the ordered
30    * logs for the given token. The lists are retrieved with {@link #findLogsByToken(long)}.
31    */

32   public Map JavaDoc findLogsByProcessInstance(long processInstanceId) {
33     Map JavaDoc tokenLogs = new HashMap JavaDoc();
34     try {
35       ProcessInstance processInstance = (ProcessInstance) session.load(ProcessInstance.class, new Long JavaDoc(processInstanceId));
36       collectTokenLogs(tokenLogs, processInstance.getRootToken());
37     } catch (Exception JavaDoc e) {
38       log.error(e);
39       jbpmSession.handleException();
40       throw new RuntimeException JavaDoc("couldn't get logs for process instance '"+processInstanceId+"'", e);
41     }
42     return tokenLogs;
43   }
44
45   private void collectTokenLogs(Map JavaDoc tokenLogs, Token token) {
46     tokenLogs.put(token, findLogsByToken(token.getId()));
47     Map JavaDoc children = token.getChildren();
48     if ( (children!=null)
49          && (!children.isEmpty())
50        ) {
51       Iterator JavaDoc iter = children.values().iterator();
52       while (iter.hasNext()) {
53         Token child = (Token) iter.next();
54         collectTokenLogs(tokenLogs, child);
55       }
56     }
57   }
58   
59   private static final String JavaDoc findLogsByToken =
60     "select pl " +
61     "from org.jbpm.logging.log.ProcessLog as pl " +
62     "where pl.token = :token " +
63     "order by pl.index";
64   /**
65    * collects the logs for a given token, ordered by creation time.
66    */

67   public List JavaDoc findLogsByToken(long tokenId) {
68     List JavaDoc result = null;
69     try {
70       Token token = (Token) session.load(Token.class, new Long JavaDoc(tokenId));
71       Query query = session.createQuery(findLogsByToken);
72       query.setEntity("token", token);
73       result = query.list();
74     } catch (Exception JavaDoc e) {
75       log.error(e);
76       jbpmSession.handleException();
77       throw new RuntimeException JavaDoc("couldn't get logs for token '"+tokenId+"'", e);
78     }
79     return result;
80   }
81   
82   void saveLogs(ProcessInstance processInstance) {
83     LoggingInstance loggingInstance = processInstance.getLoggingInstance();
84     if (loggingInstance!=null) {
85       Iterator JavaDoc iter = loggingInstance.getLogs().iterator();
86       while (iter.hasNext()) {
87         ProcessLog processLog = (ProcessLog) iter.next();
88         saveProcessLog(processLog);
89       }
90     }
91   }
92
93   /**
94    * saves the given process log to the database.
95    */

96   public void saveProcessLog(ProcessLog processLog) {
97     try {
98       session.save(processLog);
99     } catch (Exception JavaDoc e) {
100       log.error(e);
101       jbpmSession.handleException();
102       throw new RuntimeException JavaDoc("couldn't save process log '"+processLog+"'", e);
103     }
104   }
105   
106   /**
107    * get the process log for a given id.
108    */

109   public ProcessLog loadProcessLog(long processLogId) {
110     ProcessLog processLog = null;
111     try {
112       processLog = (ProcessLog) session.load(ProcessLog.class, new Long JavaDoc(processLogId));
113     } catch (Exception JavaDoc e) {
114       log.error(e);
115       jbpmSession.handleException();
116       throw new RuntimeException JavaDoc("couldn't get process log '"+processLogId+"'", e);
117     }
118     return processLog;
119   }
120   
121   private static final Log log = LogFactory.getLog(LoggingSession.class);
122 }
123
Popular Tags