1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.task.dao;
17
18 import java.sql.Date;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.ojb.broker.query.Criteria;
25 import org.apache.ojb.broker.query.Query;
26 import org.apache.ojb.broker.query.QueryFactory;
27 import org.apache.ojb.broker.query.ReportQueryByCriteria;
28 import org.kuali.hr.time.task.Task;
29 import org.kuali.hr.time.util.TKUtils;
30 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31
32 public class TaskDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TaskDao {
33
34 @Override
35 public Task getMaxTask() {
36 Criteria root = new Criteria();
37 Criteria crit = new Criteria();
38
39 ReportQueryByCriteria taskNumberSubQuery = QueryFactory.newReportQuery(Task.class, crit);
40 taskNumberSubQuery.setAttributes(new String[]{"max(task)"});
41
42 root.addEqualTo("task", taskNumberSubQuery);
43
44 Query query = QueryFactory.newQuery(Task.class, root);
45 return (Task) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
46 }
47
48 @Override
49 public Task getTask(Long task, Date asOfDate) {
50 Criteria root = new Criteria();
51 Criteria effdt = new Criteria();
52 Criteria timestamp = new Criteria();
53
54 effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
55 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
56 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
57 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
58
59 timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
60 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
61 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
62 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
63
64 root.addEqualTo("task", task);
65 root.addEqualTo("effectiveDate", effdtSubQuery);
66 root.addEqualTo("timestamp", timestampSubQuery);
67
68 Criteria activeFilter = new Criteria();
69 activeFilter.addEqualTo("active", true);
70 root.addAndCriteria(activeFilter);
71
72 Task t = null;
73 Query query = QueryFactory.newQuery(Task.class, root);
74 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
75 if (obj instanceof Task)
76 t = (Task) obj;
77
78 return t;
79 }
80
81 @Override
82 public void saveOrUpdate(Task task) {
83 this.getPersistenceBrokerTemplate().store(task);
84 }
85
86 @Override
87 public void saveOrUpdate(List<Task> tasks) {
88 for (Task task : tasks)
89 this.getPersistenceBrokerTemplate().store(task);
90 }
91
92 @Override
93 public List<Task> getTasks(Long task, String description, Long workArea, String workAreaDesc, Date fromEffdt, Date toEffdt) {
94 Criteria crit = new Criteria();
95 Criteria effdt = new Criteria();
96 Criteria timestamp = new Criteria();
97
98 List<Task> results = new ArrayList<Task>();
99
100 if (task != null) {
101 crit.addLike("task", task);
102 }
103 if (StringUtils.isNotEmpty(description)) {
104 crit.addLike("description", description);
105 }
106 if (workArea != null) {
107 crit.addEqualTo("workArea", workArea);
108 }
109 if (fromEffdt != null) {
110 crit.addGreaterOrEqualThan("effectiveDate", fromEffdt);
111 }
112 if (toEffdt != null) {
113 crit.addLessOrEqualThan("effectiveDate", toEffdt);
114 } else {
115 crit.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
116 }
117
118 effdt.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
119 if (toEffdt != null) {
120 effdt.addLessOrEqualThan("effectiveDate", toEffdt);
121 }
122 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Task.class, effdt);
123 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
124
125 timestamp.addEqualToField("task", Criteria.PARENT_QUERY_PREFIX + "task");
126 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
127 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Task.class, timestamp);
128 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
129
130 crit.addEqualTo("effectiveDate", effdtSubQuery);
131 crit.addEqualTo("timestamp", timestampSubQuery);
132
133 Criteria activeFilter = new Criteria();
134 activeFilter.addEqualTo("active", true);
135 crit.addAndCriteria(activeFilter);
136
137 Query query = QueryFactory.newQuery(Task.class, crit);
138 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
139 results.addAll(c);
140
141 return results;
142 }
143
144 @Override
145 public int getTaskCount(Long task) {
146 Criteria crit = new Criteria();
147 crit.addEqualTo("task",task);
148 Query query = QueryFactory.newQuery(Task.class, crit);
149 return this.getPersistenceBrokerTemplate().getCount(query);
150 }
151 }