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.graceperiod.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.graceperiod.rule.GracePeriodRule;
028    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
029    
030    public class GracePeriodDaoImpl extends PlatformAwareDaoBaseOjb implements GracePeriodDao {
031            public GracePeriodRule getGracePeriodRule(Date asOfDate){
032                    GracePeriodRule gracePeriodRule = null;
033                    
034                    Criteria root = new Criteria();
035                    Criteria effdt = new Criteria();
036                    Criteria timestamp = new Criteria();
037    
038                    // OJB's awesome sub query setup part 1
039                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
040    //              effdt.addEqualTo("active", true);
041                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(GracePeriodRule.class, effdt);
042                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
043    
044                    // OJB's awesome sub query setup part 2
045                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
046    //              timestamp.addEqualTo("active", true);
047                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(GracePeriodRule.class, timestamp);
048                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
049    
050                    root.addEqualTo("effectiveDate", effdtSubQuery);
051                    root.addEqualTo("timestamp", timestampSubQuery);
052    
053                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
054                    activeFilter.addEqualTo("active", true);
055                    root.addAndCriteria(activeFilter);
056                    
057                    
058                    Query query = QueryFactory.newQuery(GracePeriodRule.class, root);
059                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
060    
061                    if (obj != null) {
062                            gracePeriodRule = (GracePeriodRule) obj;
063                    }
064                    
065                    return gracePeriodRule;
066            }
067    
068            @Override
069            public GracePeriodRule getGracePeriodRule(String tkGracePeriodId) {
070                    Criteria crit = new Criteria();
071                    crit.addEqualTo("tkGracePeriodRuleId", tkGracePeriodId);
072                    
073                    Query query = QueryFactory.newQuery(GracePeriodRule.class, crit);
074                    return (GracePeriodRule)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
075            }
076    
077            @Override
078        @SuppressWarnings("unchecked")
079        public List<GracePeriodRule> getGracePeriodRules(String hourFactor, String active) {
080            List<GracePeriodRule> results = new ArrayList<GracePeriodRule>();
081            
082            Criteria root = new Criteria();
083    
084            if (StringUtils.isNotBlank(hourFactor)) {
085                root.addLike("hourFactor", hourFactor);
086            }
087            
088            if (StringUtils.isNotBlank(active)) {
089                    Criteria activeFilter = new Criteria();
090                if (StringUtils.equals(active, "Y")) {
091                    activeFilter.addEqualTo("active", true);
092                } else if (StringUtils.equals(active, "N")) {
093                    activeFilter.addEqualTo("active", false);
094                }
095                root.addAndCriteria(activeFilter);
096            }
097            
098            Query query = QueryFactory.newQuery(GracePeriodRule.class, root);
099            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
100            
101            return results;
102        }
103            
104    }