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.Collection;
012: import java.util.Collections;
013: import java.util.Date;
014: import java.util.Iterator;
015:
016: public class CurrentTaskQuery {
017: private Logger log = Logger.getLogger(getClass());
018: private static String query;
019: private Collection tasksInProgress;
020: private Collection completedTasks;
021: private Collection tasks;
022: private int personId;
023:
024: private java.util.Collection getTasks() throws QueryException {
025: if (personId == 0) {
026: throw new QueryException("no person specified for query");
027: }
028: Session session = ThreadSession.get();
029: if (tasks == null) {
030: try {
031: if (session == null) {
032: log
033: .error("no Hibernate session provided, ignoring "
034: + this );
035: return Collections.EMPTY_LIST;
036: }
037: try {
038: if (query == null) {
039: query = "select distinct task "
040: + " from task in class com.technoetic.xplanner.domain.Task, "
041: + " time_entry in class com.technoetic.xplanner.domain.TimeEntry, "
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.id = time_entry.taskId and"
046: + " task.story.id = story.id and story.iterationId = iteration.id and"
047: + " (iteration.startDate <= ? and iteration.endDate >= ?) and"
048: + " (time_entry.person1Id = person.id or time_entry.person2Id = person.id)"
049: + " and person.id = ?";
050: }
051: Date now = new Date();
052: tasks = session.find(query, new Object[] { now,
053: now, new Integer(personId) }, new Type[] {
054: Hibernate.DATE, Hibernate.DATE,
055: Hibernate.INTEGER });
056: } catch (Exception ex) {
057: log.error("query error", ex);
058: } finally {
059: session.connection().rollback();
060: }
061: } catch (Exception ex) {
062: log.error("error in CurrentTaskQuery", ex);
063: }
064: }
065: return tasks;
066: }
067:
068: public void setPersonId(int personId) {
069: this .personId = personId;
070: }
071:
072: public int getPersonId() {
073: return personId;
074: }
075:
076: public Collection getCompletedTasks() throws Exception {
077: if (completedTasks == null) {
078: completedTasks = new ArrayList();
079: Iterator taskItr = getTasks().iterator();
080: while (taskItr.hasNext()) {
081: Task task = (Task) taskItr.next();
082: if (task.isCompleted()) {
083: completedTasks.add(task);
084: }
085: }
086: }
087: return completedTasks;
088: }
089:
090: public Collection getTasksInProgress() throws QueryException {
091: if (tasksInProgress == null) {
092: tasksInProgress = new ArrayList();
093: Iterator taskItr = getTasks().iterator();
094: while (taskItr.hasNext()) {
095: Task task = (Task) taskItr.next();
096: if (!task.isCompleted()) {
097: tasksInProgress.add(task);
098: }
099: }
100: }
101: return tasksInProgress;
102: }
103: }
|