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