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.salgroup.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.salgroup.SalGroup;
028    import org.kuali.hr.time.util.TKUtils;
029    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030    
031    public class SalGroupDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements SalGroupDao {
032    
033            @Override
034            public void saveOrUpdate(SalGroup salGroup) {
035                    this.getPersistenceBrokerTemplate().store(salGroup);            
036            }
037    
038            @Override
039            public SalGroup getSalGroup(String salGroup, Date asOfDate) {
040                    Criteria root = new Criteria();
041                    Criteria effdt = new Criteria();
042                    Criteria timestamp = new Criteria();
043    
044                    effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
045                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
046                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(SalGroup.class, effdt);
047                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
048    
049                    timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
050                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
051                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(SalGroup.class, timestamp);
052                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
053    
054                    root.addEqualTo("hrSalGroup", salGroup);
055                    root.addEqualTo("effectiveDate", effdtSubQuery);
056                    root.addEqualTo("timestamp", timestampSubQuery);
057                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
058                    activeFilter.addEqualTo("active", true);
059                    root.addAndCriteria(activeFilter);
060                    
061                    Query query = QueryFactory.newQuery(SalGroup.class, root);
062                    SalGroup s = (SalGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
063                    
064                    return s;
065            }
066    
067            @Override
068            public SalGroup getSalGroup(String hrSalGroupId) {
069                    Criteria crit = new Criteria();
070                    crit.addEqualTo("hrSalGroupId", hrSalGroupId);
071                    
072                    Query query = QueryFactory.newQuery(SalGroup.class, crit);
073                    return (SalGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
074            }
075            @Override
076            public int getSalGroupCount(String salGroup) {
077                    Criteria crit = new Criteria();
078                    crit.addEqualTo("hrSalGroup", salGroup);
079                    Query query = QueryFactory.newQuery(SalGroup.class, crit);
080                    return this.getPersistenceBrokerTemplate().getCount(query);
081            }
082    
083            @Override
084        @SuppressWarnings("unchecked")
085        public List<SalGroup> getSalGroups(String hrSalGroup, String descr, Date fromEffdt, Date toEffdt, String active, String showHistory) {
086            List<SalGroup> results = new ArrayList<SalGroup>();
087            
088            Criteria root = new Criteria();
089    
090            if (StringUtils.isNotBlank(hrSalGroup)) {
091                root.addLike("hrSalGroup", hrSalGroup);
092            }
093            
094            if (StringUtils.isNotBlank(descr)) {
095                root.addLike("descr", descr);
096            }
097            
098            Criteria effectiveDateFilter = new Criteria();
099            if (fromEffdt != null) {
100                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
101            }
102            if (toEffdt != null) {
103                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
104            }
105            if (fromEffdt == null && toEffdt == null) {
106                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
107            }
108            root.addAndCriteria(effectiveDateFilter);
109    
110            if (StringUtils.equals(showHistory, "N")) {
111                Criteria effdt = new Criteria();
112                    effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
113                    effdt.addAndCriteria(effectiveDateFilter);
114                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(SalGroup.class, effdt);
115                effdtSubQuery.setAttributes(new String[]{"max(effectiveDate)"});
116                root.addEqualTo("effectiveDate", effdtSubQuery);
117                
118                Criteria timestamp = new Criteria();
119                timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
120                timestamp.addAndCriteria(effectiveDateFilter);
121                ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(SalGroup.class, timestamp);
122                timestampSubQuery.setAttributes(new String[]{"max(timestamp)"});
123                root.addEqualTo("timestamp", timestampSubQuery);
124            }
125    
126            Query query = QueryFactory.newQuery(SalGroup.class, root);
127            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
128            
129            return results;
130        }
131            
132    }