001: package com.technoetic.xplanner.domain.repository;
002:
003: import java.util.Collection;
004: import java.util.Date;
005: import java.util.List;
006:
007: import net.sf.hibernate.Hibernate;
008: import net.sf.hibernate.HibernateException;
009: import net.sf.hibernate.type.Type;
010: import org.apache.commons.collections.CollectionUtils;
011: import org.apache.commons.collections.Predicate;
012: import org.springframework.orm.hibernate.HibernateTemplate;
013:
014: import com.technoetic.xplanner.domain.Task;
015:
016: /*
017: * A Hibernate implementation of the TaskRepository interface.
018: *
019: * Implementation is based pretty heavily on (and should
020: * eventually supercede) TaskQueryHelper.
021: *
022: * @author James Beard
023: * @see com.technoetic.xplanner.db.TaskQueryHelper
024: */
025: public class TaskRepositoryHibernate extends HibernateObjectRepository
026: implements TaskRepository {
027: public static final String EMAIL_TO_LEADS_QUERY = "com.technoetic.xplanner.domain.TimeEntryEmailNotificationToProjectSpecificLeads";
028: public static final String EMAIL_TO_ACCEPTORS_QUERY = "com.technoetic.xplanner.domain.TimeEntryEmailNotificationToAcceptors";
029:
030: /*
031: * A implementation of the Predicate interface to filter
032: * collections of tasks based on whether they are completed
033: * and/or active.
034: *
035: * @author James Beard
036: */
037: private class TaskStatusFilter implements Predicate {
038: Boolean isCompleted;
039: Boolean isActive;
040:
041: public TaskStatusFilter(Boolean isCompleted, Boolean isActive) {
042: this .isCompleted = isCompleted;
043: this .isActive = isActive;
044: }
045:
046: public boolean evaluate(Object o) {
047: final Task task = (Task) o;
048: return (isCompleted == null || isCompleted.booleanValue() == task
049: .isCompleted())
050: && (isActive == null || isActive.booleanValue() == task
051: .getTimeEntries().size() > 0);
052: }
053: }
054:
055: public TaskRepositoryHibernate() throws HibernateException {
056: super (Task.class);
057: }
058:
059: /*
060: * Returns a collection of tasks in current iterations where personId
061: * is the acceptor, and the task has been started.
062: *
063: * @param personId the id of the acceptor
064: * @return the collection of tasks
065: */
066: public Collection getCurrentActiveTasksForPerson(int personId) {
067: return getCurrentActiveTasks(getCurrentTasksForPerson(personId));
068: }
069:
070: /*
071: * Filters a collection of tasks for those in current iterations
072: * that have been started.
073: *
074: * Can be used to further filter a cached Collection of all the tasks for
075: * a particular person.
076: *
077: * @param tasks the collection of tasks
078: * @return the filtered collection of tasks
079: */
080: public Collection getCurrentActiveTasks(Collection tasks) {
081: return CollectionUtils.select(tasks, new TaskStatusFilter(
082: Boolean.FALSE, Boolean.TRUE));
083: }
084:
085: /*
086: * Returns a collection of tasks in current iterations where personId
087: * is the acceptor, and the task hasn't been started yet.
088: *
089: * @param personId the id of the acceptor
090: * @return the collection of tasks
091: */
092: public Collection getCurrentPendingTasksForPerson(int personId) {
093: return getCurrentPendingTasks(getCurrentTasksForPerson(personId));
094: }
095:
096: /*
097: * Filters a collection of tasks for those in current iterations
098: * that haven't been started yet.
099: *
100: * Can be used to further filter a cached Collection of all the tasks for
101: * a particular person.
102: *
103: * @param tasks the collection of tasks
104: * @return the filtered collection of tasks
105: */
106: public Collection getCurrentPendingTasks(Collection tasks) {
107: return CollectionUtils.select(tasks, new TaskStatusFilter(
108: Boolean.FALSE, Boolean.FALSE));
109: }
110:
111: /*
112: * Returns a collection of tasks in current iterations where personId
113: * is the acceptor, and the task has already been completed.
114: *
115: * @param personId the id of the acceptor
116: * @return the collection of tasks
117: */
118: public Collection getCurrentCompletedTasksForPerson(int personId) {
119: return getCurrentCompletedTasks(getCurrentTasksForPerson(personId));
120: }
121:
122: /*
123: * Filters a collection of tasks for those in current iterations
124: * that have already been completed.
125: *
126: * Can be used to further filter a cached Collection of all the tasks for
127: * a particular person.
128: *
129: * @param tasks the collection of tasks
130: * @return the filtered collection of tasks
131: */
132: public Collection getCurrentCompletedTasks(Collection tasks) {
133: return CollectionUtils.select(tasks, new TaskStatusFilter(
134: Boolean.TRUE, null));
135: }
136:
137: /*
138: * Returns a collection of tasks in future iterations where personId
139: * is the acceptor, and the task has not been completed.
140: *
141: * @param personId the id of the acceptor
142: * @return the collection of tasks
143: */
144: public Collection getFutureTasksForPerson(int personId) {
145: return queryTasks("tasks.planned.future", personId);
146: }
147:
148: public Collection getTaskAcceptorsEmailNotification(Date date) {
149: return getHibernateTemplate().findByNamedQuery(
150: EMAIL_TO_ACCEPTORS_QUERY, date);
151: }
152:
153: public Collection getProjectLeadsEmailNotification(Date date) {
154: return getHibernateTemplate().findByNamedQuery(
155: EMAIL_TO_LEADS_QUERY, date);
156: }
157:
158: /*
159: * Returns a collection of tasks in current iterations where personId
160: * is the acceptor.
161: *
162: * @param personId the id of the acceptor
163: * @return the collection of tasks
164: */
165: public Collection getCurrentTasksForPerson(int personId) {
166: Collection currentTasks = queryTasks("tasks.current.accepted",
167: personId);
168: currentTasks
169: .addAll(queryTasks("tasks.current.worked", personId));
170:
171: return currentTasks;
172: }
173:
174: private List queryTasks(String queryName, int personId) {
175: return getHibernateTemplate().findByNamedQueryAndNamedParam(
176: queryName, new String[] { "now", "personId" },
177: new Object[] { new Date(), new Integer(personId) },
178: new Type[] { Hibernate.DATE, Hibernate.INTEGER });
179: }
180: }
|