001: package com.technoetic.xplanner.db;
002:
003: import com.technoetic.xplanner.db.hibernate.ThreadSession;
004: import com.technoetic.xplanner.domain.Task;
005: import net.sf.hibernate.Hibernate;
006: import net.sf.hibernate.Session;
007: import net.sf.hibernate.type.Type;
008: import org.apache.log4j.Logger;
009:
010: import java.util.ArrayList;
011: import java.util.Collections;
012: import java.util.Date;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: public class PlannedTaskQuery {
017: private Logger log = Logger.getLogger(getClass());
018: private static String query;
019: private static String futureQuery;
020: private java.util.Collection tasks = new ArrayList();
021: private int personId;
022:
023: public boolean hasCurrentTasks() throws QueryException {
024: return getTasks().size() > 0;
025: }
026:
027: public java.util.Collection getTasks() throws QueryException {
028: if (personId == 0) {
029: throw new QueryException("no person specified for query");
030: }
031: Session session = ThreadSession.get();
032: try {
033: if (session == null) {
034: log.error("no Hibernate session provided, ignoring "
035: + this );
036: return Collections.EMPTY_LIST;
037: }
038: try {
039: if (query == null) {
040: query = "select distinct task "
041: + " from task in class com.technoetic.xplanner.domain.Task, "
042: + " iteration in class com.technoetic.xplanner.domain.Iteration, "
043: + " story in class com.technoetic.xplanner.domain.UserStory,"
044: + " person in class com.technoetic.xplanner.domain.Person "
045: + " where task.story.id = story.id and story.iterationId = iteration.id and"
046: + " (iteration.startDate <= ? and iteration.endDate >= ?) and"
047: + " task.acceptorId = person.id and person.id = ? and task.completed = 'false'";
048: }
049: Date now = new Date();
050: List acceptedTasks = session.find(query, new Object[] {
051: now, now, new Integer(personId) }, new Type[] {
052: Hibernate.DATE, Hibernate.DATE,
053: Hibernate.INTEGER });
054: Iterator itr = acceptedTasks.iterator();
055: tasks.clear();
056: while (itr.hasNext()) {
057: Task task = (Task) itr.next();
058: if (task.getTimeEntries().size() == 0) {
059: tasks.add(task);
060: }
061: }
062: } catch (Exception ex) {
063: log.error("query error", ex);
064: } finally {
065: session.connection().rollback();
066: }
067: } catch (Exception ex) {
068: log.error("error in PlannedTaskQuery", ex);
069: }
070: return tasks;
071: }
072:
073: public void setPersonId(int personId) {
074: this .personId = personId;
075: }
076:
077: public int getPersonId() {
078: return personId;
079: }
080:
081: public boolean hasFutureTasks() throws QueryException {
082: return getFutureTasks().size() > 0;
083: }
084:
085: public java.util.Collection getFutureTasks() throws QueryException {
086: if (personId == 0) {
087: throw new QueryException("no person specified for query");
088: }
089: Session session = ThreadSession.get();
090: try {
091: if (session == null) {
092: log.error("no Hibernate session provided, ignoring "
093: + this );
094: return Collections.EMPTY_LIST;
095: }
096: try {
097: if (futureQuery == null) {
098: futureQuery = "select distinct task "
099: + " from task in class com.technoetic.xplanner.domain.Task, "
100: + " iteration in class com.technoetic.xplanner.domain.Iteration, "
101: + " story in class com.technoetic.xplanner.domain.UserStory,"
102: + " person in class com.technoetic.xplanner.domain.Person "
103: + " where task.story.id = story.id and story.iterationId = iteration.id and"
104: + " iteration.startDate > ? and"
105: + " task.acceptorId = person.id and"
106: + " person.id = ? and task.completed = 'false'";
107: }
108: Date now = new Date();
109: return session.find(futureQuery, new Object[] { now,
110: new Integer(personId) }, new Type[] {
111: Hibernate.DATE, Hibernate.INTEGER });
112: } catch (Exception ex) {
113: log.error("query error", ex);
114: } finally {
115: session.connection().rollback();
116: }
117: } catch (Exception ex) {
118: log.error("error in PlannedTaskQuery", ex);
119: }
120: return Collections.EMPTY_LIST;
121: }
122: }
|