001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.timecollection.rule.dao;
017
018 import com.google.common.collect.ImmutableList;
019 import org.apache.commons.lang.StringUtils;
020 import org.apache.ojb.broker.query.Criteria;
021 import org.apache.ojb.broker.query.Query;
022 import org.apache.ojb.broker.query.QueryFactory;
023 import org.kuali.hr.core.util.OjbSubQueryUtil;
024 import org.kuali.hr.time.collection.rule.TimeCollectionRule;
025 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
026
027 import java.sql.Date;
028 import java.util.ArrayList;
029 import java.util.List;
030
031 public class TimeCollectionRuleDaoServiceImpl extends PlatformAwareDaoBaseOjb implements TimeCollectionRuleDaoService {
032 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
033 .add("workArea")
034 .add("dept")
035 .add("payType")
036 .build();
037
038 /*
039 * Returns valid TimeCollectionRule based on dept,workArea, and asOfDate
040 * dept and work area are wildcardable values
041 * @see org.kuali.hr.time.timecollection.rule.dao.TimeCollectionRuleDaoService#getTimeCollectionRule(java.lang.String dept,
042 * java.lang.Long workArea, java.sql.Date asOfDate)
043 */
044
045 @Override
046 public TimeCollectionRule getTimeCollectionRule(String dept, Long workArea, Date asOfDate) {
047
048
049 TimeCollectionRule timeCollectionRule = new TimeCollectionRule();
050
051 //First call confirm no exact match
052 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, workArea, asOfDate);
053 if (timeCollectionRule != null) {
054 return timeCollectionRule;
055 }
056 //Try with dept wildcarded *
057 timeCollectionRule = getTimeCollectionRuleWildCarded("%", workArea, asOfDate);
058 if (timeCollectionRule != null) {
059 return timeCollectionRule;
060 }
061
062 //Try with work area wildcarded
063 timeCollectionRule = getTimeCollectionRuleWildCarded(dept, -1L, asOfDate);
064 if (timeCollectionRule != null) {
065 return timeCollectionRule;
066 }
067
068 //Try with everything wildcarded
069 return getTimeCollectionRuleWildCarded("%", -1L, asOfDate);
070 }
071
072 private TimeCollectionRule getTimeCollectionRuleWildCarded(String dept, Long workArea, Date asOfDate) {
073 Criteria root = new Criteria();
074 ImmutableList<String> fields = new ImmutableList.Builder<String>()
075 .add("workArea")
076 .add("dept")
077 .build();
078
079 root.addEqualTo("dept", dept);
080 root.addEqualTo("workArea", workArea);
081 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeCollectionRule.class, asOfDate, fields, false));
082 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TimeCollectionRule.class, fields, false));
083 // root.addEqualTo("active", true);
084
085 Criteria activeFilter = new Criteria(); // Inner Join For Activity
086 activeFilter.addEqualTo("active", true);
087 root.addAndCriteria(activeFilter);
088
089
090 Query query = QueryFactory.newQuery(TimeCollectionRule.class, root);
091 return (TimeCollectionRule) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
092
093 }
094
095 @Override
096 public TimeCollectionRule getTimeCollectionRule(String tkTimeCollectionRuleId) {
097 Criteria crit = new Criteria();
098 crit.addEqualTo("tkTimeCollectionRuleId", tkTimeCollectionRuleId);
099
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 }