001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.jbpm.db;
022:
023: import com.liferay.jbpm.util.WorkflowUtil;
024: import com.liferay.portal.kernel.log.Log;
025: import com.liferay.portal.kernel.log.LogFactoryUtil;
026: import com.liferay.portal.kernel.util.DateUtil;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.StringPool;
029: import com.liferay.portal.kernel.util.StringUtil;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.util.dao.hibernate.QueryPos;
032: import com.liferay.util.dao.hibernate.QueryUtil;
033:
034: import java.sql.Timestamp;
035:
036: import java.text.DateFormat;
037:
038: import java.util.ArrayList;
039: import java.util.Calendar;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.TimeZone;
043:
044: import org.hibernate.Hibernate;
045: import org.hibernate.Query;
046: import org.hibernate.SQLQuery;
047: import org.hibernate.Session;
048: import org.hibernate.dialect.Dialect;
049: import org.hibernate.engine.SessionFactoryImplementor;
050:
051: import org.jbpm.JbpmContext;
052: import org.jbpm.graph.exe.ProcessInstance;
053: import org.jbpm.taskmgmt.exe.TaskInstance;
054:
055: /**
056: * <a href="GraphSession.java.html"><b><i>View Source</i></b></a>
057: *
058: * @author Charles May
059: *
060: */
061: public class GraphSession extends org.jbpm.db.GraphSession {
062:
063: public static String COUNT_PROCESS_DEFINITIONS_BY_NAME = GraphSession.class
064: .getName()
065: + ".countProcessDefinitionsByName";
066:
067: public static String COUNT_PROCESS_INSTANCES_BY_SEARCH_TERMS = GraphSession.class
068: .getName()
069: + ".countProcessInstancesBySearchTerms";
070:
071: public static String COUNT_TASK_INSTANCES_BY_ACTOR = GraphSession.class
072: .getName()
073: + ".countTaskInstancesByActor";
074:
075: public static String COUNT_TASK_INSTANCES_BY_POOL = GraphSession.class
076: .getName()
077: + ".countTaskInstancesByPool";
078:
079: public static String FIND_PROCESS_DEFINITIONS_BY_NAME = GraphSession.class
080: .getName()
081: + ".findProcessDefinitionsByName";
082:
083: public static String FIND_PROCESS_INSTANCES_BY_SEARCH_TERMS = GraphSession.class
084: .getName()
085: + ".findProcessInstancesBySearchTerms";
086:
087: public static String FIND_TASK_INSTANCES_BY_ACTOR = GraphSession.class
088: .getName()
089: + ".findTaskInstancesByActor";
090:
091: public static String FIND_TASK_INSTANCES_BY_POOL = GraphSession.class
092: .getName()
093: + ".findTaskInstancesByPool";
094:
095: public GraphSession(String userId, String timeZoneId,
096: JbpmContext jbpmContext) {
097:
098: super (jbpmContext.getSession());
099:
100: _userId = userId;
101: _timeZoneId = timeZoneId;
102: _jbpmContext = jbpmContext;
103: _session = jbpmContext.getSession();
104:
105: if (_session != null) {
106: SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) _session
107: .getSessionFactory();
108:
109: _dialect = sessionFactory.getDialect();
110: }
111: }
112:
113: public void close() {
114: if (_session != null) {
115: _session.close();
116: }
117: }
118:
119: public int countProcessDefinitionsByName(String name) {
120: try {
121: String sql = CustomSQLUtil
122: .get(COUNT_PROCESS_DEFINITIONS_BY_NAME);
123:
124: Query q = _session.createQuery(sql);
125:
126: q.setString("name", name);
127:
128: Iterator itr = q.list().iterator();
129:
130: if (itr.hasNext()) {
131: Number count = (Number) itr.next();
132:
133: if (count != null) {
134: return count.intValue();
135: }
136: }
137:
138: return 0;
139: } catch (Exception e) {
140: _log.error(e, e);
141:
142: throw new RuntimeException(e.getMessage());
143: }
144: }
145:
146: public int countProcessInstancesBySearchTerms(
147: String definitionName, String definitionVersion,
148: String startDateGT, String startDateLT, String endDateGT,
149: String endDateLT, boolean hideEndedTasks,
150: String assignedUserId, boolean andOperator) {
151:
152: try {
153: int definitionVersionInt = 0;
154:
155: if (!Validator.isNumber(definitionVersion)) {
156: definitionVersion = null;
157: } else {
158: definitionVersionInt = GetterUtil
159: .getInteger(definitionVersion);
160: }
161:
162: String assignedUserIdInnerJoin = StringPool.BLANK;
163: String assignedUserIdCheck = StringPool.BLANK;
164:
165: if (Validator.isNotNull(assignedUserId)) {
166: assignedUserIdInnerJoin = "INNER JOIN JBPM_TOKEN ON JBPM_TOKEN.PROCESSINSTANCE_ = "
167: + "JBPM_PROCESSINSTANCE.ID_ INNER JOIN "
168: + "JBPM_TASKINSTANCE ON JBPM_TASKINSTANCE.TOKEN_ = "
169: + "JBPM_TOKEN.ID_ ";
170: assignedUserIdCheck = "(JBPM_TASKINSTANCE.ACTORID_ = ?) AND ";
171: }
172:
173: String endDateCheck = "(JBPM_PROCESSINSTANCE.END_ IS NULL) ";
174:
175: if (!hideEndedTasks) {
176: endDateCheck = "((JBPM_PROCESSINSTANCE.END_ >= ? [$AND_OR_NULL_CHECK$]) "
177: + "AND (JBPM_PROCESSINSTANCE.END_ <= ? "
178: + "[$AND_OR_NULL_CHECK$])) ";
179: }
180:
181: String sql = CustomSQLUtil
182: .get(COUNT_PROCESS_INSTANCES_BY_SEARCH_TERMS);
183:
184: sql = StringUtil.replace(sql,
185: "[$ASSIGNED_USER_ID_INNER_JOIN$]",
186: assignedUserIdInnerJoin);
187: sql = StringUtil.replace(sql, "[$ASSIGNED_USER_ID_CHECK$]",
188: assignedUserIdCheck);
189: sql = StringUtil.replace(sql, "[$END_DATE_CHECK$]",
190: endDateCheck);
191: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
192:
193: SQLQuery q = _session.createSQLQuery(sql);
194:
195: QueryPos qPos = QueryPos.getInstance(q);
196:
197: qPos.add(definitionName);
198: qPos.add(definitionName);
199: qPos.add(definitionVersionInt);
200: qPos.add(definitionVersion);
201: qPos.add(_getDate(startDateGT, true));
202: qPos.add(_getDate(startDateGT, true));
203: qPos.add(_getDate(startDateLT, false));
204: qPos.add(_getDate(startDateLT, false));
205:
206: if (assignedUserId != null) {
207: qPos.add(assignedUserId);
208: }
209:
210: if (!hideEndedTasks) {
211: qPos.add(_getDate(endDateGT, true));
212: qPos.add(_getDate(endDateGT, true));
213: qPos.add(_getDate(endDateLT, false));
214: qPos.add(_getDate(endDateLT, false));
215: }
216:
217: Iterator itr = q.list().iterator();
218:
219: if (itr.hasNext()) {
220: Number count = (Number) itr.next();
221:
222: if (count != null) {
223: return count.intValue();
224: }
225: }
226:
227: return 0;
228: } catch (Exception e) {
229: _log.error(e, e);
230:
231: throw new RuntimeException(e.getMessage());
232: }
233: }
234:
235: public int countTaskInstancesBySearchTerms(String taskName,
236: String definitionName, String assignedTo,
237: String createDateGT, String createDateLT,
238: String startDateGT, String startDateLT, String endDateGT,
239: String endDateLT, boolean hideEndedTasks,
240: boolean andOperator) {
241:
242: try {
243: String sql = "";
244:
245: int index = 0;
246:
247: if (Validator.isNull(assignedTo)
248: || assignedTo.equals("all")) {
249: sql += "(";
250: sql += CustomSQLUtil.get(COUNT_TASK_INSTANCES_BY_ACTOR);
251: sql += ") ";
252: sql += "UNION ";
253: sql += "(";
254: sql += CustomSQLUtil.get(COUNT_TASK_INSTANCES_BY_POOL);
255: sql += ") ";
256:
257: index = 2;
258: } else if (assignedTo.equals("me")) {
259: sql += CustomSQLUtil.get(COUNT_TASK_INSTANCES_BY_ACTOR);
260:
261: index = 1;
262: } else if (assignedTo.equals("pool")) {
263: sql += CustomSQLUtil.get(COUNT_TASK_INSTANCES_BY_POOL);
264:
265: index = 1;
266: }
267:
268: String endDateCheck = "(JBPM_TASKINSTANCE.END_ IS NULL) ";
269:
270: if (!hideEndedTasks) {
271: endDateCheck = "((JBPM_TASKINSTANCE.END_ >= ? "
272: + "[$AND_OR_NULL_CHECK$]) AND "
273: + "(JBPM_TASKINSTANCE.END_ <= ? [$AND_OR_NULL_CHECK$])) ";
274: }
275:
276: sql = StringUtil.replace(sql, "[$END_DATE_CHECK$]",
277: endDateCheck);
278: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
279:
280: SQLQuery q = _session.createSQLQuery(sql);
281:
282: QueryPos qPos = QueryPos.getInstance(q);
283:
284: for (int i = 0; i < index; i++) {
285: qPos.add(taskName);
286: qPos.add(taskName);
287: qPos.add(definitionName);
288: qPos.add(definitionName);
289: qPos.add(_getDate(createDateGT, true));
290: qPos.add(_getDate(createDateGT, true));
291: qPos.add(_getDate(createDateLT, false));
292: qPos.add(_getDate(createDateLT, false));
293: qPos.add(_getDate(startDateGT, true));
294: qPos.add(_getDate(startDateGT, true));
295: qPos.add(_getDate(startDateLT, false));
296: qPos.add(_getDate(startDateLT, false));
297:
298: if (!hideEndedTasks) {
299: qPos.add(_getDate(endDateGT, true));
300: qPos.add(_getDate(endDateGT, true));
301: qPos.add(_getDate(endDateLT, false));
302: qPos.add(_getDate(endDateLT, false));
303: }
304:
305: qPos.add(_userId);
306: }
307:
308: int count = 0;
309:
310: Iterator itr = q.list().iterator();
311:
312: while (itr.hasNext()) {
313: Number i = (Number) itr.next();
314:
315: if (i != null) {
316: count += i.intValue();
317: }
318: }
319:
320: return count;
321: } catch (Exception e) {
322: _log.error(e, e);
323:
324: throw new RuntimeException(e.getMessage());
325: }
326: }
327:
328: public List findProcessDefinitionsByName(String name, int begin,
329: int end) {
330: try {
331: String sql = CustomSQLUtil
332: .get(FIND_PROCESS_DEFINITIONS_BY_NAME);
333:
334: Query q = _session.createQuery(sql);
335:
336: q.setString("name", name);
337:
338: return QueryUtil.list(q, _dialect, begin, end);
339: } catch (Exception e) {
340: _log.error(e, e);
341:
342: throw new RuntimeException(e.getMessage());
343: }
344: }
345:
346: public List findProcessInstancesBySearchTerms(
347: String definitionName, String definitionVersion,
348: String startDateGT, String startDateLT, String endDateGT,
349: String endDateLT, boolean hideEndedTasks,
350: String assignedUserId, boolean andOperator, int begin,
351: int end) {
352:
353: List list = new ArrayList();
354:
355: try {
356: int definitionVersionInt = 0;
357:
358: if (!Validator.isNumber(definitionVersion)) {
359: definitionVersion = null;
360: } else {
361: definitionVersionInt = GetterUtil
362: .getInteger(definitionVersion);
363: }
364:
365: String assignedUserIdInnerJoin = StringPool.BLANK;
366: String assignedUserIdCheck = StringPool.BLANK;
367:
368: if (Validator.isNotNull(assignedUserId)) {
369: assignedUserIdInnerJoin = "INNER JOIN JBPM_TOKEN ON JBPM_TOKEN.PROCESSINSTANCE_ = "
370: + "JBPM_PROCESSINSTANCE.ID_ INNER JOIN "
371: + "JBPM_TASKINSTANCE ON JBPM_TASKINSTANCE.TOKEN_ = "
372: + "JBPM_TOKEN.ID_ ";
373: assignedUserIdCheck = "(JBPM_TASKINSTANCE.ACTORID_ = ?) AND ";
374: }
375:
376: String endDateCheck = "(JBPM_PROCESSINSTANCE.END_ IS NULL) ";
377:
378: if (!hideEndedTasks) {
379: endDateCheck = "((JBPM_PROCESSINSTANCE.END_ >= ? [$AND_OR_NULL_CHECK$]) "
380: + "AND (JBPM_PROCESSINSTANCE.END_ <= ? "
381: + "[$AND_OR_NULL_CHECK$])) ";
382: }
383:
384: String sql = CustomSQLUtil
385: .get(FIND_PROCESS_INSTANCES_BY_SEARCH_TERMS);
386:
387: sql = StringUtil.replace(sql,
388: "[$ASSIGNED_USER_ID_INNER_JOIN$]",
389: assignedUserIdInnerJoin);
390: sql = StringUtil.replace(sql, "[$ASSIGNED_USER_ID_CHECK$]",
391: assignedUserIdCheck);
392: sql = StringUtil.replace(sql, "[$END_DATE_CHECK$]",
393: endDateCheck);
394: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
395:
396: SQLQuery q = _session.createSQLQuery(sql);
397:
398: q.addScalar("instanceId", Hibernate.LONG);
399:
400: QueryPos qPos = QueryPos.getInstance(q);
401:
402: qPos.add(definitionName);
403: qPos.add(definitionName);
404: qPos.add(definitionVersionInt);
405: qPos.add(definitionVersion);
406: qPos.add(_getDate(startDateGT, true));
407: qPos.add(_getDate(startDateGT, true));
408: qPos.add(_getDate(startDateLT, false));
409: qPos.add(_getDate(startDateLT, false));
410:
411: if (Validator.isNotNull(assignedUserId)) {
412: qPos.add(assignedUserId);
413: }
414:
415: if (!hideEndedTasks) {
416: qPos.add(_getDate(endDateGT, true));
417: qPos.add(_getDate(endDateGT, true));
418: qPos.add(_getDate(endDateLT, false));
419: qPos.add(_getDate(endDateLT, false));
420: }
421:
422: Iterator itr = QueryUtil.iterate(q, _dialect, begin, end);
423:
424: while (itr.hasNext()) {
425: Long instanceId = (Long) itr.next();
426:
427: ProcessInstance processInstance = _jbpmContext
428: .loadProcessInstance(instanceId.longValue());
429:
430: WorkflowUtil.initInstance(processInstance);
431:
432: list.add(processInstance);
433: }
434:
435: } catch (Exception e) {
436: _log.error(e, e);
437:
438: throw new RuntimeException(e.getMessage());
439: }
440:
441: return list;
442: }
443:
444: public List findTaskInstancesBySearchTerms(String taskName,
445: String definitionName, String assignedTo,
446: String createDateGT, String createDateLT,
447: String startDateGT, String startDateLT, String endDateGT,
448: String endDateLT, boolean hideEndedTasks,
449: boolean andOperator, int begin, int end) {
450:
451: List list = new ArrayList();
452:
453: try {
454: String sql = "";
455:
456: int index = 0;
457:
458: if (Validator.isNull(assignedTo)
459: || assignedTo.equals("all")) {
460: sql += "(";
461: sql += CustomSQLUtil.get(FIND_TASK_INSTANCES_BY_ACTOR);
462: sql += ") ";
463: sql += "UNION ";
464: sql += "(";
465: sql += CustomSQLUtil.get(FIND_TASK_INSTANCES_BY_POOL);
466: sql += ") ";
467:
468: index = 2;
469: } else if (assignedTo.equals("me")) {
470: sql += CustomSQLUtil.get(FIND_TASK_INSTANCES_BY_ACTOR);
471:
472: index = 1;
473: } else if (assignedTo.equals("pool")) {
474: sql += CustomSQLUtil.get(FIND_TASK_INSTANCES_BY_POOL);
475:
476: index = 1;
477: }
478:
479: sql += "ORDER BY taskActorId DESC, taskCreate ASC";
480:
481: String endDateCheck = "(JBPM_TASKINSTANCE.END_ IS NULL) ";
482:
483: if (!hideEndedTasks) {
484: endDateCheck = "((JBPM_TASKINSTANCE.END_ >= ? "
485: + "[$AND_OR_NULL_CHECK$]) AND "
486: + "(JBPM_TASKINSTANCE.END_ <= ? [$AND_OR_NULL_CHECK$])) ";
487: }
488:
489: sql = StringUtil.replace(sql, "[$END_DATE_CHECK$]",
490: endDateCheck);
491: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
492:
493: SQLQuery q = _session.createSQLQuery(sql);
494:
495: q.addScalar("taskId", Hibernate.LONG);
496:
497: QueryPos qPos = QueryPos.getInstance(q);
498:
499: for (int i = 0; i < index; i++) {
500: qPos.add(taskName);
501: qPos.add(taskName);
502: qPos.add(definitionName);
503: qPos.add(definitionName);
504: qPos.add(_getDate(createDateGT, true));
505: qPos.add(_getDate(createDateGT, true));
506: qPos.add(_getDate(createDateLT, false));
507: qPos.add(_getDate(createDateLT, false));
508: qPos.add(_getDate(startDateGT, true));
509: qPos.add(_getDate(startDateGT, true));
510: qPos.add(_getDate(startDateLT, false));
511: qPos.add(_getDate(startDateLT, false));
512:
513: if (!hideEndedTasks) {
514: qPos.add(_getDate(endDateGT, true));
515: qPos.add(_getDate(endDateGT, true));
516: qPos.add(_getDate(endDateLT, false));
517: qPos.add(_getDate(endDateLT, false));
518: }
519:
520: qPos.add(_userId);
521: }
522:
523: Iterator itr = QueryUtil.iterate(q, _dialect, begin, end);
524:
525: while (itr.hasNext()) {
526: Long taskId = (Long) itr.next();
527:
528: TaskInstance taskInstance = _jbpmContext
529: .loadTaskInstance(taskId.longValue());
530:
531: WorkflowUtil.initTask(taskInstance);
532:
533: list.add(taskInstance);
534: }
535: } catch (Exception e) {
536: _log.error(e, e);
537:
538: throw new RuntimeException(e.getMessage());
539: }
540:
541: return list;
542: }
543:
544: private Timestamp _getDate(String date, boolean greaterThan) {
545: if (Validator.isNull(date)) {
546: return null;
547: } else {
548: Calendar calendar = Calendar.getInstance();
549:
550: DateFormat dateFormat = DateUtil.getISOFormat();
551:
552: calendar.setTime(GetterUtil.getDate(date, dateFormat));
553:
554: if (greaterThan) {
555: calendar.set(Calendar.HOUR_OF_DAY, 0);
556: calendar.set(Calendar.MINUTE, 0);
557: calendar.set(Calendar.SECOND, 0);
558: calendar.set(Calendar.MILLISECOND, 0);
559: } else {
560: calendar.set(Calendar.HOUR_OF_DAY, 23);
561: calendar.set(Calendar.MINUTE, 59);
562: calendar.set(Calendar.SECOND, 59);
563: calendar.set(Calendar.MILLISECOND, 999);
564: }
565:
566: TimeZone timeZone = TimeZone.getTimeZone(_timeZoneId);
567:
568: int offset = timeZone.getOffset(calendar.getTimeInMillis());
569:
570: return new Timestamp(calendar.getTimeInMillis() - offset);
571: }
572: }
573:
574: private static Log _log = LogFactoryUtil.getLog(GraphSession.class);
575:
576: private String _userId;
577: private String _timeZoneId;
578: private JbpmContext _jbpmContext;
579: private Session _session;
580: private Dialect _dialect;
581:
582: }
|