01: package com.technoetic.xplanner.db;
02:
03: import java.util.Collection;
04: import java.util.List;
05: import java.util.Date;
06:
07: import org.springframework.orm.hibernate.support.HibernateDaoSupport;
08: import org.apache.commons.collections.Predicate;
09: import org.apache.commons.collections.CollectionUtils;
10:
11: import com.technoetic.xplanner.domain.Task;
12:
13: import net.sf.hibernate.type.Type;
14: import net.sf.hibernate.Hibernate;
15:
16: /**
17: * User: Mateusz Prokopowicz
18: * Date: Aug 24, 2005
19: * Time: 6:22:01 PM
20: */
21: public class TaskQueryDao extends HibernateDaoSupport implements
22: TaskQuery {
23:
24: public Collection query(Collection cachedTasks, int personId,
25: Boolean completed, Boolean active) {
26: return CollectionUtils.select(getCurrentTasks(cachedTasks,
27: personId), new TaskStatusFilter(completed, active));
28: }
29:
30: private Collection getCurrentTasks(Collection cache, int personId) {
31: if (cache == null) {
32: cache = queryTasks("tasks.current.accepted", personId);
33: cache.addAll(queryTasks("tasks.current.worked", personId));
34: }
35: return cache;
36: }
37:
38: public List queryTasks(String queryName, int personId) {
39: return getHibernateTemplate().findByNamedQueryAndNamedParam(
40: queryName, new String[] { "now", "personId" },
41: new Object[] { new Date(), new Integer(personId) },
42: new Type[] { Hibernate.DATE, Hibernate.INTEGER });
43: }
44:
45: public List queryTasks(String queryName, Date date) {
46: return getHibernateTemplate().findByNamedQuery(queryName, date);
47: }
48:
49: private class TaskStatusFilter implements Predicate {
50: Boolean isCompleted;
51: Boolean isActive;
52:
53: public TaskStatusFilter(Boolean isCompleted, Boolean isActive) {
54: this .isCompleted = isCompleted;
55: this .isActive = isActive;
56: }
57:
58: public boolean evaluate(Object o) {
59: final Task task = ((Task) o);
60: return (isCompleted == null || (isCompleted.booleanValue() == task
61: .isCompleted()))
62: && (isActive == null || (isActive.booleanValue() == task
63: .getTimeEntries().size() > 0));
64: }
65: }
66: }
|