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.List;
21
22 import com.google.common.collect.ImmutableList;
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.core.util.OjbSubQueryUtil;
29 import org.kuali.hr.time.task.Task;
30 import org.kuali.hr.time.util.TKUtils;
31 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32
33 public class TaskDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TaskDao {
34 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
35 .add("task")
36 .build();
37
38 @Override
39 public Task getTask(String tkTaskId) {
40 Criteria crit = new Criteria();
41 crit.addEqualTo("tkTaskId", tkTaskId);
42
43 Query query = QueryFactory.newQuery(Task.class, crit);
44 return (Task)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
45 }
46
47 @Override
48 public Task getMaxTask() {
49 Criteria root = new Criteria();
50 Criteria crit = new Criteria();
51
52 ReportQueryByCriteria taskNumberSubQuery = QueryFactory.newReportQuery(Task.class, crit);
53 taskNumberSubQuery.setAttributes(new String[]{"max(task)"});
54
55 root.addEqualTo("task", taskNumberSubQuery);
56
57 Query query = QueryFactory.newQuery(Task.class, root);
58 return (Task) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
59 }
60
61 @Override
62 public Task getTask(Long task, Date asOfDate) {
63 Criteria root = new Criteria();
64
65 root.addEqualTo("task", task);
66 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Task.class, asOfDate, EQUAL_TO_FIELDS, false));
67 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Task.class, EQUAL_TO_FIELDS, false));
68
69 Criteria activeFilter = new Criteria();
70 activeFilter.addEqualTo("active", true);
71 root.addAndCriteria(activeFilter);
72
73 Task t = null;
74 Query query = QueryFactory.newQuery(Task.class, root);
75 Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
76 if (obj instanceof Task)
77 t = (Task) obj;
78
79 return t;
80 }
81
82 @Override
83 public void saveOrUpdate(Task task) {
84 this.getPersistenceBrokerTemplate().store(task);
85 }
86
87 @Override
88 public void saveOrUpdate(List<Task> tasks) {
89 for (Task task : tasks)
90 this.getPersistenceBrokerTemplate().store(task);
91 }
92
93 @SuppressWarnings("unchecked")
94 @Override
95 public List<Task> getTasks(Long task, String description, Long workArea, Date fromEffdt, Date toEffdt) {
96 Criteria root = new Criteria();
97
98 List<Task> results = new ArrayList<Task>();
99
100 if (task != null) {
101 root.addLike("task", task);
102 }
103
104 if (StringUtils.isNotBlank(description)) {
105 root.addLike("description", description);
106 }
107
108 if (workArea != null) {
109 root.addEqualTo("workArea", workArea);
110 }
111
112 Criteria effectiveDateFilter = new Criteria();
113 if (fromEffdt != null) {
114 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
115 }
116 if (toEffdt != null) {
117 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
118 }
119 if (fromEffdt == null && toEffdt == null) {
120 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
121 }
122 root.addAndCriteria(effectiveDateFilter);
123
124 Criteria activeFilter = new Criteria();
125 activeFilter.addEqualTo("active", true);
126 root.addAndCriteria(activeFilter);
127
128 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(Task.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
129 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Task.class, EQUAL_TO_FIELDS, false));
130
131 Query query = QueryFactory.newQuery(Task.class, root);
132 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
133
134 return results;
135 }
136
137 @Override
138 public int getTaskCount(Long task) {
139 Criteria crit = new Criteria();
140 crit.addEqualTo("task",task);
141 Query query = QueryFactory.newQuery(Task.class, crit);
142 return this.getPersistenceBrokerTemplate().getCount(query);
143 }
144 }