View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.time.rules.timecollection.dao;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.ojb.broker.query.Criteria;
23  import org.apache.ojb.broker.query.Query;
24  import org.apache.ojb.broker.query.QueryFactory;
25  import org.joda.time.LocalDate;
26  import org.kuali.kpme.core.util.OjbSubQueryUtil;
27  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
28  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29  
30  public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
31  
32      @Override
33      public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
34          Criteria crit = new Criteria();
35          crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
36  
37          Query query = QueryFactory.newQuery(TimeCollectionRule.class, crit);
38          return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
39  
40      }
41  
42      /* Jira 1152
43        * Returns valid TimeCollectionRule based on dept, workArea, payType, groupKeyCode, and asOfDate
44        * dept, work area, and payType can be wildcardable values
45        */
46      @Override
47      public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, String payType, String groupKeyCode, LocalDate asOfDate) {
48  
49  
50          TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
51  
52          //First call confirm no exact match
53          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, payType, groupKeyCode, asOfDate);
54          if (timeCollectionRule != null) {
55              return timeCollectionRule;
56          }
57          //Try with dept wildcarded *
58          timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, payType, groupKeyCode, asOfDate);
59          if (timeCollectionRule != null) {
60              return timeCollectionRule;
61          }
62  
63          //Try with work area wildcarded
64          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, payType, groupKeyCode, asOfDate);
65          if (timeCollectionRule != null) {
66              return timeCollectionRule;
67          }
68  
69          //Try with payType wildcarded
70          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, "%", groupKeyCode, asOfDate);
71          if (timeCollectionRule != null) {
72              return timeCollectionRule;
73          }
74  
75          //Try with dept and workArea wildcarded
76          timeCollectionRule = getTimeCollectionRuleWildCarded("%", -1L, payType, groupKeyCode, asOfDate);
77          if (timeCollectionRule != null) {
78              return timeCollectionRule;
79          }
80  
81          //Try with dept and payType wildcarded
82          timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, "%", groupKeyCode, asOfDate);
83          if (timeCollectionRule != null) {
84              return timeCollectionRule;
85          }
86  
87          //Try with workArea and payType wildcarded
88          timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, "%", groupKeyCode, asOfDate);
89          if (timeCollectionRule != null) {
90              return timeCollectionRule;
91          }
92  
93          //Try with everything wildcarded
94          return getTimeCollectionRuleWildCarded("%", -1L, "%", groupKeyCode, asOfDate);
95      }
96  
97      private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, String payType, String groupKeyCode, LocalDate asOfDate) {
98          Criteria root = new Criteria();
99  
100         root.addEqualTo("dept", dept);
101         root.addEqualTo("workArea", workArea);
102         root.addEqualTo("payType", payType);
103         root.addEqualTo("groupKeyCode", groupKeyCode);
104         root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeCollectionRule.class, asOfDate, TimeCollectionRule.BUSINESS_KEYS, false));
105         root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, TimeCollectionRule.BUSINESS_KEYS, false));
106 //		root.addEqualTo("active", true);
107 
108         Criteria activeFilter = new Criteria(); // Inner Join For Activity
109         activeFilter.addEqualTo("active", true);
110         root.addAndCriteria(activeFilter);
111 
112 
113         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
114         return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
115 
116     }
117 
118 	@Override
119     @SuppressWarnings("unchecked")
120     public List<TimeCollectionRule> getTimeCollectionRules(String dept, Long workArea, String payType, String groupKeyCode, String active, String showHistory) {
121         List<TimeCollectionRule> results = new ArrayList<TimeCollectionRule>();
122 
123         Criteria root = new Criteria();
124 
125         if (StringUtils.isNotBlank(dept)) {
126             root.addLike("dept", dept);
127         }
128 
129         if (workArea != null) {
130             OjbSubQueryUtil.addNumericCriteria(root, "workArea", workArea.toString());
131         }
132         
133         if (StringUtils.isNotBlank(payType)) {
134             root.addLike("payType", payType);
135         }
136         
137         if (StringUtils.isNotBlank(groupKeyCode)) {
138         	root.addLike("groupKeyCode", groupKeyCode);
139         }
140         
141         if (StringUtils.isNotBlank(active)) {
142         	Criteria activeFilter = new Criteria();
143             if (StringUtils.equals(active, "Y")) {
144                 activeFilter.addEqualTo("active", true);
145             } else if (StringUtils.equals(active, "N")) {
146                 activeFilter.addEqualTo("active", false);
147             }
148             root.addAndCriteria(activeFilter);
149         }
150         
151         if (StringUtils.equals(showHistory, "N")) {
152             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(TimeCollectionRule.class, TimeCollectionRule.BUSINESS_KEYS, false));
153             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, TimeCollectionRule.BUSINESS_KEYS, false));
154         }
155         
156         Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
157         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
158 
159         return results;
160     }
161 }