View Javadoc

1   /**
2    * Copyright 2004-2013 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.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.apache.ojb.broker.query.ReportQueryByCriteria;
27  import org.kuali.hr.time.collection.rule.TimeCollectionRule;
28  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29  
30  public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
31  
32      /*
33        * Returns valid TimeCollectionRule based on dept,workArea, and asOfDate
34        * dept and work area are wildcardable values
35        * @see org.kuali.hr.time.timecollection.rule.dao.TimeCollectionRuleDaoService#getTimeCollectionRule(java.lang.String dept,
36        * java.lang.Long workArea, java.sql.Date asOfDate)
37        */
38  
39      @Override
40      public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) {
41  
42  
43          TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
44  
45          //First call confirm no exact match
46          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate);
47          if (timeCollectionRule != null) {
48              return timeCollectionRule;
49          }
50          //Try with dept wildcarded *
51          timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate);
52          if (timeCollectionRule != null) {
53              return timeCollectionRule;
54          }
55  
56          //Try with work area wildcarded
57          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate);
58          if (timeCollectionRule != null) {
59              return timeCollectionRule;
60          }
61  
62          //Try with everything wildcarded
63          timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, asOfDate);
64          if (timeCollectionRule != null) {
65              return timeCollectionRule;
66          }
67  
68          //Return default time collection rule
69          timeCollectionRule = new TimeCollectionRule();
70          timeCollectionRule.setActive(true);
71          timeCollectionRule.setClockUserFl(true);
72          timeCollectionRule.setDept(dept);
73          timeCollectionRule.setWorkArea(workArea);
74  
75          return timeCollectionRule;
76      }
77  
78      private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, Date asOfDate) {
79          Criteria root = new Criteria();
80          Criteria effdt = new Criteria();
81          Criteria timestamp = new Criteria();
82  
83          effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
84          effdt.addLessOrEqualThan("effectiveDate", asOfDate);
85  //		effdt.addEqualTo("active", true);
86          effdt.addEqualTo("dept", dept);
87          ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
88          effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
89  
90          timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
91          timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
92  //		timestamp.addEqualTo("active", true);
93          timestamp.addEqualTo("dept", dept);
94          ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
95          timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
96  
97          root.addEqualTo("dept", dept);
98          root.addEqualTo("workArea", workArea);
99          root.addEqualTo("effectiveDate", effdtSubQuery);
100         root.addEqualTo("timestamp", timestampSubQuery);
101 //		root.addEqualTo("active", true);
102 
103         Criteria activeFilter = new Criteria(); // Inner Join For Activity
104         activeFilter.addEqualTo("active", true);
105         root.addAndCriteria(activeFilter);
106 
107 
108         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
109         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
110 
111     }
112 
113     @Override
114     public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
115         Criteria crit = new Criteria();
116         crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
117 
118         Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit);
119         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
120 
121     }
122 
123     /* Jira 1152
124       * Returns valid TimeCollectionRule based on dept, workArea, payType, and asOfDate
125       * dept, work area, and payType can be wildcardable values
126       */
127     @Override
128     public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, Date asOfDate) {
129 
130 
131         TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
132 
133         //First call confirm no exact match
134         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, asOfDate);
135         if (timeCollectionRule != null) {
136             return timeCollectionRule;
137         }
138         //Try with dept wildcarded *
139         timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, asOfDate);
140         if (timeCollectionRule != null) {
141             return timeCollectionRule;
142         }
143 
144         //Try with work area wildcarded
145         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, asOfDate);
146         if (timeCollectionRule != null) {
147             return timeCollectionRule;
148         }
149 
150         //Try with payType wildcarded
151         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", asOfDate);
152         if (timeCollectionRule != null) {
153             return timeCollectionRule;
154         }
155 
156         //Try with dept and workArea wildcarded
157         timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, asOfDate);
158         if (timeCollectionRule != null) {
159             return timeCollectionRule;
160         }
161 
162         //Try with dept and payType wildcarded
163         timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", asOfDate);
164         if (timeCollectionRule != null) {
165             return timeCollectionRule;
166         }
167 
168         //Try with workArea and payType wildcarded
169         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", asOfDate);
170         if (timeCollectionRule != null) {
171             return timeCollectionRule;
172         }
173 
174         //Try with everything wildcarded
175         timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, "%", asOfDate);
176         if (timeCollectionRule != null) {
177             return timeCollectionRule;
178         }
179 
180         //Return default time collection rule
181         timeCollectionRule = new TimeCollectionRule();
182         timeCollectionRule.setActive(true);
183         timeCollectionRule.setClockUserFl(true);
184         timeCollectionRule.setDept(dept);
185         timeCollectionRule.setWorkArea(workArea);
186 
187         return timeCollectionRule;
188     }
189 
190     private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, Date asOfDate) {
191         Criteria root = new Criteria();
192         Criteria effdt = new Criteria();
193         Criteria timestamp = new Criteria();
194 
195         effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
196         effdt.addLessOrEqualThan("effectiveDate", asOfDate);
197 //		effdt.addEqualTo("active", true);
198         effdt.addEqualTo("dept", dept);
199         effdt.addEqualTo("payType", payType);
200         ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
201         effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
202 
203         timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
204         timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
205 //		timestamp.addEqualTo("active", true);
206         timestamp.addEqualTo("dept", dept);
207         timestamp.addEqualTo("payType", payType);
208         ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
209         timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
210 
211         root.addEqualTo("dept", dept);
212         root.addEqualTo("workArea", workArea);
213         root.addEqualTo("payType", payType);
214         root.addEqualTo("effectiveDate", effdtSubQuery);
215         root.addEqualTo("timestamp", timestampSubQuery);
216 //		root.addEqualTo("active", true);
217 
218         Criteria activeFilter = new Criteria(); // Inner Join For Activity
219         activeFilter.addEqualTo("active", true);
220         root.addAndCriteria(activeFilter);
221 
222 
223         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
224         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
225 
226     }
227 
228 	@Override
229     @SuppressWarnings("unchecked")
230     public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String active, String showHistory) {
231         List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>();
232 
233         Criteria root = new Criteria();
234 
235         if (StringUtils.isNotBlank(dept)) {
236             root.addLike("dept", dept);
237         }
238 
239         if (workArea != null) {
240             root.addLike("workArea", workArea);
241         }
242         
243         if (StringUtils.isNotBlank(payType)) {
244             root.addLike("payType", payType);
245         }
246         
247         if (StringUtils.isNotBlank(active)) {
248         	Criteria activeFilter = new Criteria();
249             if (StringUtils.equals(active, "Y")) {
250                 activeFilter.addEqualTo("active", true);
251             } else if (StringUtils.equals(active, "N")) {
252                 activeFilter.addEqualTo("active", false);
253             }
254             root.addAndCriteria(activeFilter);
255         }
256         
257         if (StringUtils.equals(showHistory, "N")) {
258 	        Criteria effdt = new Criteria();
259 	        effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
260 	        effdt.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
261 	        effdt.addEqualToField("payType", Criteria.PARENT_QUERY_PREFIX + "payType");
262 	        ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, effdt);
263 	        effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
264 	        root.addEqualTo("effectiveDate", effdtSubQuery);
265 	        
266 	        Criteria timestamp = new Criteria();
267 	        timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
268 	        timestamp.addEqualToField("workArea", Criteria.PARENT_QUERY_PREFIX + "workArea");
269 	        timestamp.addEqualToField("payType", Criteria.PARENT_QUERY_PREFIX + "payType");
270 	        timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
271 	        ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(TimeCollectionRule.class, timestamp);
272 	        timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
273 	        root.addEqualTo("timestamp", timestampSubQuery);
274         }
275         
276         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
277         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
278 
279         return results;
280     }
281 }