001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.db;
023:
024: import java.io.Serializable;
025: import java.util.ArrayList;
026: import java.util.List;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.hibernate.Query;
031: import org.hibernate.Session;
032: import org.jbpm.JbpmException;
033: import org.jbpm.graph.exe.ProcessInstance;
034: import org.jbpm.taskmgmt.exe.TaskInstance;
035:
036: public class TaskMgmtSession implements Serializable {
037:
038: private static final long serialVersionUID = 1L;
039:
040: JbpmSession jbpmSession = null;
041: Session session = null;
042:
043: public TaskMgmtSession(JbpmSession jbpmSession) {
044: this .jbpmSession = jbpmSession;
045: this .session = jbpmSession.getSession();
046: }
047:
048: public TaskMgmtSession(Session session) {
049: this .session = session;
050: this .jbpmSession = new JbpmSession(session);
051: }
052:
053: /**
054: * get the tasllist for a given actor.
055: */
056: public List findTaskInstances(String actorId) {
057: List result = null;
058: try {
059: Query query = session
060: .getNamedQuery("TaskMgmtSession.findTaskInstancesByActorId");
061: query.setString("actorId", actorId);
062: result = query.list();
063: } catch (Exception e) {
064: log.error(e);
065: jbpmSession.handleException();
066: throw new JbpmException(
067: "couldn't get task instances list for actor '"
068: + actorId + "'", e);
069: }
070: return result;
071: }
072:
073: /**
074: * get all the task instances for all the given actorIds.
075: * @return a list of task instances. An empty list is returned in case no task instances are found.
076: */
077: public List findTaskInstances(List actorIds) {
078: if (actorIds == null)
079: return new ArrayList(0);
080: return findTaskInstances((String[]) actorIds
081: .toArray(new String[actorIds.size()]));
082: }
083:
084: /**
085: * get all the task instances for all the given actorIds.
086: */
087: public List findTaskInstances(String[] actorIds) {
088: List result = null;
089: try {
090: Query query = session
091: .getNamedQuery("TaskMgmtSession.findTaskInstancesByActorIds");
092: query.setParameterList("actorIds", actorIds);
093: result = query.list();
094: } catch (Exception e) {
095: log.error(e);
096: jbpmSession.handleException();
097: throw new JbpmException(
098: "couldn't get task instances list for actors '"
099: + actorIds + "'", e);
100: }
101: return result;
102: }
103:
104: /**
105: * get the taskinstances for which the given actor is in the pool.
106: */
107: public List findPooledTaskInstances(String actorId) {
108: List result = null;
109: try {
110: Query query = session
111: .getNamedQuery("TaskMgmtSession.findPooledTaskInstancesByActorId");
112: query.setString("swimlaneActorId", actorId);
113: result = query.list();
114: } catch (Exception e) {
115: log.error(e);
116: jbpmSession.handleException();
117: throw new JbpmException(
118: "couldn't get pooled task instances list for actor '"
119: + actorId + "'", e);
120: }
121: return result;
122: }
123:
124: /**
125: * get the taskinstances for which the given actor is in the pool.
126: */
127: public List findPooledTaskInstances(List actorIds) {
128: List result = null;
129: try {
130: Query query = session
131: .getNamedQuery("TaskMgmtSession.findPooledTaskInstancesByActorIds");
132: query.setParameterList("actorIds", actorIds);
133: result = query.list();
134: } catch (Exception e) {
135: log.error(e);
136: jbpmSession.handleException();
137: throw new JbpmException(
138: "couldn't get pooled task instances list for actors '"
139: + actorIds + "'", e);
140: }
141: return result;
142: }
143:
144: /**
145: * get active taskinstances for a given token.
146: */
147: public List findTaskInstancesByToken(long tokenId) {
148: List result = null;
149: try {
150: Query query = session
151: .getNamedQuery("TaskMgmtSession.findTaskInstancesByTokenId");
152: query.setLong("tokenId", tokenId);
153: result = query.list();
154: } catch (Exception e) {
155: log.error(e);
156: jbpmSession.handleException();
157: throw new JbpmException(
158: "couldn't get task instances by token '" + tokenId
159: + "'", e);
160: }
161: return result;
162: }
163:
164: /**
165: * get active taskinstances for a given token.
166: */
167: public List findTaskInstancesByProcessInstance(
168: ProcessInstance processInstance) {
169: List result = null;
170: try {
171: Query query = session
172: .getNamedQuery("TaskMgmtSession.findTaskInstancesByProcessInstance");
173: query.setEntity("processInstance", processInstance);
174: result = query.list();
175: } catch (Exception e) {
176: log.error(e);
177: jbpmSession.handleException();
178: throw new JbpmException(
179: "couldn't get task instances by process instance '"
180: + processInstance + "'", e);
181: }
182: return result;
183: }
184:
185: /**
186: * get the task instance for a given task instance-id.
187: */
188: public TaskInstance loadTaskInstance(long taskInstanceId) {
189: TaskInstance taskInstance = null;
190: try {
191: taskInstance = (TaskInstance) session.load(
192: TaskInstance.class, new Long(taskInstanceId));
193: } catch (Exception e) {
194: log.error(e);
195: jbpmSession.handleException();
196: throw new JbpmException("couldn't get task instance '"
197: + taskInstanceId + "'", e);
198: }
199: return taskInstance;
200: }
201:
202: /**
203: * get the task instance for a given task instance-id.
204: */
205: public TaskInstance getTaskInstance(long taskInstanceId) {
206: TaskInstance taskInstance = null;
207: try {
208: taskInstance = (TaskInstance) session.get(
209: TaskInstance.class, new Long(taskInstanceId));
210: } catch (Exception e) {
211: log.error(e);
212: jbpmSession.handleException();
213: throw new JbpmException("couldn't get task instance '"
214: + taskInstanceId + "'", e);
215: }
216: return taskInstance;
217: }
218:
219: public List findTaskInstancesByIds(List taskInstanceIds) {
220: List result = null;
221: try {
222: Query query = session
223: .getNamedQuery("TaskMgmtSession.findTaskInstancesByIds");
224: query.setParameterList("taskInstanceIds", taskInstanceIds);
225: result = query.list();
226: } catch (Exception e) {
227: log.error(e);
228: jbpmSession.handleException();
229: throw new JbpmException(
230: "couldn't get task instances by ids '"
231: + taskInstanceIds + "'", e);
232: }
233: return result;
234: }
235:
236: private static final Log log = LogFactory
237: .getLog(TaskMgmtSession.class);
238: }
|