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.syslunch.dao;
017
018 import java.sql.Date;
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.apache.ojb.broker.query.Criteria;
024 import org.apache.ojb.broker.query.Query;
025 import org.apache.ojb.broker.query.QueryFactory;
026 import org.apache.ojb.broker.query.ReportQueryByCriteria;
027 import org.kuali.hr.time.syslunch.rule.SystemLunchRule;
028 import org.kuali.hr.time.util.TKUtils;
029 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030
031 public class SystemLunchRuleDaoImpl extends PlatformAwareDaoBaseOjb implements SystemLunchRuleDao {
032
033 @Override
034 public SystemLunchRule getSystemLunchRule(Date asOfDate) {
035 Criteria root = new Criteria();
036 Criteria effdt = new Criteria();
037 Criteria timestamp = new Criteria();
038
039 //effdt.addEqualToField("tkSystemLunchRuleId", Criteria.PARENT_QUERY_PREFIX + "tkSystemLunchRuleId");
040 effdt.addLessOrEqualThan("effectiveDate", asOfDate);
041 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, effdt);
042 effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
043
044 //timestamp.addEqualToField("tkSystemLunchRuleId", Criteria.PARENT_QUERY_PREFIX + "tkSystemLunchRuleId");
045 timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
046 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, timestamp);
047 timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
048
049 root.addEqualTo("effectiveDate", effdtSubQuery);
050 root.addEqualTo("timestamp", timestampSubQuery);
051
052 Criteria activeFilter = new Criteria(); // Inner Join For Activity
053 activeFilter.addEqualTo("active", true);
054 root.addAndCriteria(activeFilter);
055
056 Query query = QueryFactory.newQuery(SystemLunchRule.class, root);
057 return (SystemLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
058 }
059
060 @Override
061 public SystemLunchRule getSystemLunchRule(String tkSystemLunchRuleId) {
062 Criteria crit = new Criteria();
063 crit.addEqualTo("tkSystemLunchRuleId", tkSystemLunchRuleId);
064
065 Query query = QueryFactory.newQuery(SystemLunchRule.class, crit);
066
067 return (SystemLunchRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
068 }
069
070 @Override
071 @SuppressWarnings("unchecked")
072 public List<SystemLunchRule> getSystemLunchRules(Date fromEffdt, Date toEffdt, String active, String showHistory) {
073 List<SystemLunchRule> results = new ArrayList<SystemLunchRule>();
074
075 Criteria root = new Criteria();
076
077 Criteria effectiveDateFilter = new Criteria();
078 if (fromEffdt != null) {
079 effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
080 }
081 if (toEffdt != null) {
082 effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
083 }
084 if (fromEffdt == null && toEffdt == null) {
085 effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
086 }
087 root.addAndCriteria(effectiveDateFilter);
088
089 if (StringUtils.isNotBlank(active)) {
090 Criteria activeFilter = new Criteria();
091 if (StringUtils.equals(active, "Y")) {
092 activeFilter.addEqualTo("active", true);
093 } else if (StringUtils.equals(active, "N")) {
094 activeFilter.addEqualTo("active", false);
095 }
096 root.addAndCriteria(activeFilter);
097 }
098
099 if (StringUtils.equals(showHistory, "N")) {
100 Criteria effdt = new Criteria();
101 effdt.addAndCriteria(effectiveDateFilter);
102 ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, effdt);
103 effdtSubQuery.setAttributes(new String[]{"max(effdt)"});
104 root.addEqualTo("effectiveDate", effdtSubQuery);
105
106 Criteria timestamp = new Criteria();
107 timestamp.addAndCriteria(effectiveDateFilter);
108 ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(SystemLunchRule.class, timestamp);
109 timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
110 root.addEqualTo("timestamp", timestampSubQuery);
111 }
112
113 Query query = QueryFactory.newQuery(SystemLunchRule.class, root);
114 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
115
116 return results;
117 }
118
119 }