View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.timecollection.rule.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.collection.rule.TimeCollectionRule;
29  import org.kuali.hr.time.util.TKUtils;
30  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31  
32  public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
33  
34      /*
35        * Returns valid TimeCollectionRule based on dept,workArea, and asOfDate
36        * dept and work area are wildcardable values
37        * @see org.kuali.hr.time.timecollection.rule.dao.TimeCollectionRuleDaoService#getTimeCollectionRule(java.lang.String dept,
38        * java.lang.Long workArea, java.sql.Date asOfDate)
39        */
40  
41      @Override
42      public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) {
43  
44  
45          TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
46  
47          //First call confirm no exact match
48          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate);
49          if (timeCollectionRule != null) {
50              return timeCollectionRule;
51          }
52          //Try with dept wildcarded *
53          timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate);
54          if (timeCollectionRule != null) {
55              return timeCollectionRule;
56          }
57  
58          //Try with work area wildcarded
59          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate);
60          if (timeCollectionRule != null) {
61              return timeCollectionRule;
62          }
63  
64          //Try with everything wildcarded
65          timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, asOfDate);
66          if (timeCollectionRule != null) {
67              return timeCollectionRule;
68          }
69  
70          //Return default time collection rule
71          timeCollectionRule = new TimeCollectionRule();
72          timeCollectionRule.setActive(true);
73          timeCollectionRule.setClockUserFl(true);
74          timeCollectionRule.setDept(dept);
75          timeCollectionRule.setWorkArea(workArea);
76  
77          return timeCollectionRule;
78      }
79  
80      private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, Date asOfDate) {
81          Criteria root = new Criteria();
82          Criteria effdt = new Criteria();
83          Criteria timestamp = new Criteria();
84  
85          effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
86          effdt.addLessOrEqualThan("effectiveDate", asOfDate);
87  //		effdt.addEqualTo("active", true);
88          effdt.addEqualTo("dept", dept);
89          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
90          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
91  
92          timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
93          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
94  //		timestamp.addEqualTo("active", true);
95          timestamp.addEqualTo("dept", dept);
96          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
97          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
98  
99          root.addEqualTo("dept", dept);
100         root.addEqualTo("workArea", workArea);
101         root.addEqualTo("effectiveDate", effdtSubQuery);
102         root.addEqualTo("timestamp", timestampSubQuery);
103 //		root.addEqualTo("active", true);
104 
105         Criteria activeFilter = new Criteria(); // Inner Join For Activity
106         activeFilter.addEqualTo("active", true);
107         root.addAndCriteria(activeFilter);
108 
109 
110         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
111         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
112 
113     }
114 
115     @Override
116     public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
117         Criteria crit = new Criteria();
118         crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
119 
120         Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit);
121         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
122 
123     }
124 
125     /* Jira 1152
126       * Returns valid TimeCollectionRule based on dept, workArea, payType, and asOfDate
127       * dept, work area, and payType can be wildcardable values
128       */
129     @Override
130     public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, Date asOfDate) {
131 
132 
133         TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
134 
135         //First call confirm no exact match
136         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, asOfDate);
137         if (timeCollectionRule != null) {
138             return timeCollectionRule;
139         }
140         //Try with dept wildcarded *
141         timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, asOfDate);
142         if (timeCollectionRule != null) {
143             return timeCollectionRule;
144         }
145 
146         //Try with work area wildcarded
147         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, asOfDate);
148         if (timeCollectionRule != null) {
149             return timeCollectionRule;
150         }
151 
152         //Try with payType wildcarded
153         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", asOfDate);
154         if (timeCollectionRule != null) {
155             return timeCollectionRule;
156         }
157 
158         //Try with dept and workArea wildcarded
159         timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, asOfDate);
160         if (timeCollectionRule != null) {
161             return timeCollectionRule;
162         }
163 
164         //Try with dept and payType wildcarded
165         timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", asOfDate);
166         if (timeCollectionRule != null) {
167             return timeCollectionRule;
168         }
169 
170         //Try with workArea and payType wildcarded
171         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", asOfDate);
172         if (timeCollectionRule != null) {
173             return timeCollectionRule;
174         }
175 
176         //Try with everything wildcarded
177         timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, "%", asOfDate);
178         if (timeCollectionRule != null) {
179             return timeCollectionRule;
180         }
181 
182         //Return default time collection rule
183         timeCollectionRule = new TimeCollectionRule();
184         timeCollectionRule.setActive(true);
185         timeCollectionRule.setClockUserFl(true);
186         timeCollectionRule.setDept(dept);
187         timeCollectionRule.setWorkArea(workArea);
188 
189         return timeCollectionRule;
190     }
191 
192     private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, Date asOfDate) {
193         Criteria root = new Criteria();
194         Criteria effdt = new Criteria();
195         Criteria timestamp = new Criteria();
196 
197         effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
198         effdt.addLessOrEqualThan("effectiveDate", asOfDate);
199 //		effdt.addEqualTo("active", true);
200         effdt.addEqualTo("dept", dept);
201         effdt.addEqualTo("payType", payType);
202         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
203         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
204 
205         timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
206         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
207 //		timestamp.addEqualTo("active", true);
208         timestamp.addEqualTo("dept", dept);
209         timestamp.addEqualTo("payType", payType);
210         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
211         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
212 
213         root.addEqualTo("dept", dept);
214         root.addEqualTo("workArea", workArea);
215         root.addEqualTo("payType", payType);
216         root.addEqualTo("effectiveDate", effdtSubQuery);
217         root.addEqualTo("timestamp", timestampSubQuery);
218 //		root.addEqualTo("active", true);
219 
220         Criteria activeFilter = new Criteria(); // Inner Join For Activity
221         activeFilter.addEqualTo("active", true);
222         root.addAndCriteria(activeFilter);
223 
224 
225         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
226         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
227 
228     }
229 
230     @Override
231     public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String active, String showHistory) {
232 
233         List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>();
234 
235         Criteria root = new Criteria();
236         Criteria effdt = new Criteria();
237         Criteria timestamp = new Criteria();
238         if (workArea != null) {
239             effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
240         } else {
241             effdt.addIsNull(Criteria.PARENT_QUERY_PREFIX + "workArea");
242         }
243         effdt.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
244         if (StringUtils.isNotBlank(dept)) {
245             effdt.addEqualTo("dept", dept);
246         }
247         if (StringUtils.isNotBlank(payType)) {
248             effdt.addEqualTo("payType", payType);
249         }
250         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
251         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
252         if (workArea != null) {
253             timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
254         } else {
255             timestamp.addIsNull(Criteria.PARENT_QUERY_PREFIX + "workArea");
256         }
257         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
258         if (StringUtils.isNotBlank(dept)) {
259             timestamp.addEqualTo("dept", dept);
260         }
261         if (StringUtils.isNotBlank(payType)) {
262             timestamp.addEqualTo("payType", payType);
263         }
264         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
265         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
266 
267         if (StringUtils.isNotBlank(dept)) {
268             root.addLike("dept", dept);
269         }
270 
271         if (workArea != null) {
272             root.addLike("workArea", workArea);
273         }
274         if (StringUtils.isNotBlank(payType)) {
275             root.addLike("payType", payType);
276         }
277         //root.addEqualTo("effectiveDate", effdtSubQuery);
278         //root.addEqualTo("timestamp", timestampSubQuery);
279 
280         if (StringUtils.equals(active, "Y")) {
281             Criteria activeFilter = new Criteria(); // Inner Join For Activity
282             activeFilter.addEqualTo("active", true);
283             root.addAndCriteria(activeFilter);
284         } else if (StringUtils.equals(active, "N")) {
285             Criteria activeFilter = new Criteria(); // Inner Join For Activity
286             activeFilter.addEqualTo("active", false);
287             root.addAndCriteria(activeFilter);
288         }
289 
290         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
291         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
292         results.addAll(c);
293 
294         return results;
295     }
296 }