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.kpme.tklm.time.rules.lunch.sys.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
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.joda.time.LocalDate;
27  import org.kuali.kpme.core.util.OjbSubQueryUtil;
28  import org.kuali.kpme.tklm.time.rules.lunch.sys.SystemLunchRule;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class SystemLunchRuleDaoOjbImpl extends PlatformAwareDaoBaseOjb implements SystemLunchRuleDao {
32  
33  	@Override
34  	public SystemLunchRule getSystemLunchRule(LocalDate asOfDate) {
35  		Criteria root = new Criteria();
36  
37          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(SystemLunchRule.class, asOfDate, Collections.EMPTY_LIST, false));
38          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemLunchRule.class, Collections.EMPTY_LIST, false));
39  
40  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
41  		activeFilter.addEqualTo("active", true);
42  		root.addAndCriteria(activeFilter);
43  
44  		Query query = QueryFactory.newQuery(SystemLunchRule.class, root);
45  		return (SystemLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
46  	}
47  
48  	@Override
49  	public SystemLunchRule getSystemLunchRule(String tkSystemLunchRuleId) {
50  		Criteria crit = new Criteria();
51  		crit.addEqualTo("tkSystemLunchRuleId", tkSystemLunchRuleId);
52  		
53  		Query query = QueryFactory.newQuery(SystemLunchRule.class, crit);
54  		
55  		return (SystemLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
56  	}
57  
58  	@Override
59      @SuppressWarnings("unchecked")
60      public List<SystemLunchRule> getSystemLunchRules(LocalDate fromEffdt, LocalDate toEffdt, String active, String showHistory) {
61          List<SystemLunchRule> results = new ArrayList<SystemLunchRule>();
62          
63          Criteria root = new Criteria();
64  
65          Criteria effectiveDateFilter = new Criteria();
66          if (fromEffdt != null) {
67              effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
68          }
69          if (toEffdt != null) {
70              effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
71          }
72          if (fromEffdt == null && toEffdt == null) {
73              effectiveDateFilter.addLessOrEqualThan("effectiveDate", LocalDate.now().toDate());
74          }
75          root.addAndCriteria(effectiveDateFilter);
76          
77          if (StringUtils.isNotBlank(active)) {
78          	Criteria activeFilter = new Criteria();
79              if (StringUtils.equals(active, "Y")) {
80                  activeFilter.addEqualTo("active", true);
81              } else if (StringUtils.equals(active, "N")) {
82                  activeFilter.addEqualTo("active", false);
83              }
84              root.addAndCriteria(activeFilter);
85          }
86  
87          if (StringUtils.equals(showHistory, "N")) {
88              root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(SystemLunchRule.class, effectiveDateFilter, Collections.EMPTY_LIST, false));
89              root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemLunchRule.class, Collections.EMPTY_LIST, false));
90          }
91          
92          Query query = QueryFactory.newQuery(SystemLunchRule.class, root);
93          results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
94  
95          return results;
96      }
97  
98  }