1 package org.jbpm.db; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 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 32 public Map findLogsByProcessInstance(long processInstanceId) { 33 Map tokenLogs = new HashMap (); 34 try { 35 ProcessInstance processInstance = (ProcessInstance) session.load(ProcessInstance.class, new Long (processInstanceId)); 36 collectTokenLogs(tokenLogs, processInstance.getRootToken()); 37 } catch (Exception e) { 38 log.error(e); 39 jbpmSession.handleException(); 40 throw new RuntimeException ("couldn't get logs for process instance '"+processInstanceId+"'", e); 41 } 42 return tokenLogs; 43 } 44 45 private void collectTokenLogs(Map tokenLogs, Token token) { 46 tokenLogs.put(token, findLogsByToken(token.getId())); 47 Map children = token.getChildren(); 48 if ( (children!=null) 49 && (!children.isEmpty()) 50 ) { 51 Iterator 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 findLogsByToken = 60 "select pl " + 61 "from org.jbpm.logging.log.ProcessLog as pl " + 62 "where pl.token = :token " + 63 "order by pl.index"; 64 67 public List findLogsByToken(long tokenId) { 68 List result = null; 69 try { 70 Token token = (Token) session.load(Token.class, new Long (tokenId)); 71 Query query = session.createQuery(findLogsByToken); 72 query.setEntity("token", token); 73 result = query.list(); 74 } catch (Exception e) { 75 log.error(e); 76 jbpmSession.handleException(); 77 throw new RuntimeException ("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 iter = loggingInstance.getLogs().iterator(); 86 while (iter.hasNext()) { 87 ProcessLog processLog = (ProcessLog) iter.next(); 88 saveProcessLog(processLog); 89 } 90 } 91 } 92 93 96 public void saveProcessLog(ProcessLog processLog) { 97 try { 98 session.save(processLog); 99 } catch (Exception e) { 100 log.error(e); 101 jbpmSession.handleException(); 102 throw new RuntimeException ("couldn't save process log '"+processLog+"'", e); 103 } 104 } 105 106 109 public ProcessLog loadProcessLog(long processLogId) { 110 ProcessLog processLog = null; 111 try { 112 processLog = (ProcessLog) session.load(ProcessLog.class, new Long (processLogId)); 113 } catch (Exception e) { 114 log.error(e); 115 jbpmSession.handleException(); 116 throw new RuntimeException ("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 |