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 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        * Returns valid TimeCollectionRule based on dept,workArea, and asOfDate
40        * dept and work area are wildcardable values
41        * @see org.kuali.hr.time.timecollection.rule.dao.TimeCollectionRuleDaoService#getTimeCollectionRule(java.lang.String dept,
42        * java.lang.Long workArea, java.sql.Date asOfDate)
43        */
44  
45      @Override
46      public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) {
47  
48  
49          TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
50  
51          //First call confirm no exact match
52          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate);
53          if (timeCollectionRule != null) {
54              return timeCollectionRule;
55          }
56          //Try with dept wildcarded *
57          timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate);
58          if (timeCollectionRule != null) {
59              return timeCollectionRule;
60          }
61  
62          //Try with work area wildcarded
63          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate);
64          if (timeCollectionRule != null) {
65              return timeCollectionRule;
66          }
67  
68          //Try with everything wildcarded
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  //		root.addEqualTo("active", true);
84  
85          Criteria activeFilter = new Criteria(); // Inner Join For Activity
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     /* Jira 1152
106       * Returns valid TimeCollectionRule based on dept, workArea, payType, and asOfDate
107       * dept, work area, and payType can be wildcardable values
108       */
109     @Override
110     public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, Date asOfDate) {
111 
112 
113         TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
114 
115         //First call confirm no exact match
116         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, asOfDate);
117         if (timeCollectionRule != null) {
118             return timeCollectionRule;
119         }
120         //Try with dept wildcarded *
121         timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, asOfDate);
122         if (timeCollectionRule != null) {
123             return timeCollectionRule;
124         }
125 
126         //Try with work area wildcarded
127         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, asOfDate);
128         if (timeCollectionRule != null) {
129             return timeCollectionRule;
130         }
131 
132         //Try with payType wildcarded
133         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", asOfDate);
134         if (timeCollectionRule != null) {
135             return timeCollectionRule;
136         }
137 
138         //Try with dept and workArea wildcarded
139         timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, asOfDate);
140         if (timeCollectionRule != null) {
141             return timeCollectionRule;
142         }
143 
144         //Try with dept and payType wildcarded
145         timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", asOfDate);
146         if (timeCollectionRule != null) {
147             return timeCollectionRule;
148         }
149 
150         //Try with workArea and payType wildcarded
151         timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", asOfDate);
152         if (timeCollectionRule != null) {
153             return timeCollectionRule;
154         }
155 
156         //Try with everything wildcarded
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 //		root.addEqualTo("active", true);
169 
170         Criteria activeFilter = new Criteria(); // Inner Join For Activity
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 }