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