1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.timecollection.rule.dao;
17
18 import com.google.common.collect.ImmutableList;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.ojb.broker.query.Criteria;
21 import org.apache.ojb.broker.query.Query;
22 import org.apache.ojb.broker.query.QueryFactory;
23 import org.kuali.hr.core.util.OjbSubQueryUtil;
24 import org.kuali.hr.time.collection.rule.TimeCollectionRule;
25 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
26
27 import java.sql.Date;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
32 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
33 .add("workArea")
34 .add("dept")
35 .add("payType")
36 .build();
37
38
39
40
41
42
43
44
45 @Override
46 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) {
47
48
49 TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
50
51
52 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate);
53 if (timeCollectionRule != null) {
54 return timeCollectionRule;
55 }
56
57 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate);
58 if (timeCollectionRule != null) {
59 return timeCollectionRule;
60 }
61
62
63 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate);
64 if (timeCollectionRule != null) {
65 return timeCollectionRule;
66 }
67
68
69 return getTimeCollectionRuleWildCarded("%", -1L, asOfDate);
70 }
71
72 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, Date asOfDate) {
73 Criteria root = new Criteria();
74 ImmutableList<String> fields = new ImmutableList.Builder<String>()
75 .add("workArea")
76 .add("dept")
77 .build();
78
79 root.addEqualTo("dept", dept);
80 root.addEqualTo("workArea", workArea);
81 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeCollectionRule.class, asOfDate, fields, false));
82 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, fields, false));
83
84
85 Criteria activeFilter = new Criteria();
86 activeFilter.addEqualTo("active", true);
87 root.addAndCriteria(activeFilter);
88
89
90 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
91 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
92
93 }
94
95 @Override
96 public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
97 Criteria crit = new Criteria();
98 crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
99
100 Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit);
101 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
102
103 }
104
105
106
107
108
109 @Override
110 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, Date asOfDate) {
111
112
113 TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
114
115
116 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, asOfDate);
117 if (timeCollectionRule != null) {
118 return timeCollectionRule;
119 }
120
121 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, asOfDate);
122 if (timeCollectionRule != null) {
123 return timeCollectionRule;
124 }
125
126
127 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, asOfDate);
128 if (timeCollectionRule != null) {
129 return timeCollectionRule;
130 }
131
132
133 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", asOfDate);
134 if (timeCollectionRule != null) {
135 return timeCollectionRule;
136 }
137
138
139 timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, asOfDate);
140 if (timeCollectionRule != null) {
141 return timeCollectionRule;
142 }
143
144
145 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", asOfDate);
146 if (timeCollectionRule != null) {
147 return timeCollectionRule;
148 }
149
150
151 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", asOfDate);
152 if (timeCollectionRule != null) {
153 return timeCollectionRule;
154 }
155
156
157 return getTimeCollectionRuleWildCarded("%", -1L, "%", asOfDate);
158 }
159
160 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, Date asOfDate) {
161 Criteria root = new Criteria();
162
163 root.addEqualTo("dept", dept);
164 root.addEqualTo("workArea", workArea);
165 root.addEqualTo("payType", payType);
166 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeCollectionRule.class, asOfDate, EQUAL_TO_FIELDS, false));
167 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, EQUAL_TO_FIELDS, false));
168
169
170 Criteria activeFilter = new Criteria();
171 activeFilter.addEqualTo("active", true);
172 root.addAndCriteria(activeFilter);
173
174
175 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
176 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
177
178 }
179
180 @Override
181 @SuppressWarnings("unchecked")
182 public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String active, String showHistory) {
183 List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>();
184
185 Criteria root = new Criteria();
186
187 if (StringUtils.isNotBlank(dept)) {
188 root.addLike("dept", dept);
189 }
190
191 if (workArea != null) {
192 root.addLike("workArea", workArea);
193 }
194
195 if (StringUtils.isNotBlank(payType)) {
196 root.addLike("payType", payType);
197 }
198
199 if (StringUtils.isNotBlank(active)) {
200 Criteria activeFilter = new Criteria();
201 if (StringUtils.equals(active, "Y")) {
202 activeFilter.addEqualTo("active", true);
203 } else if (StringUtils.equals(active, "N")) {
204 activeFilter.addEqualTo("active", false);
205 }
206 root.addAndCriteria(activeFilter);
207 }
208
209 if (StringUtils.equals(showHistory, "N")) {
210 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(TimeCollectionRule.class, EQUAL_TO_FIELDS, false));
211 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, EQUAL_TO_FIELDS, false));
212 }
213
214 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
215 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
216
217 return results;
218 }
219 }