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 com.google.common.collect.ImmutableList;
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.salgroup.SalGroup;
030    import org.kuali.hr.time.util.TKUtils;
031    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032    
033    public class SalGroupDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements SalGroupDao {
034        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
035                .add("hrSalGroup")
036                .build();
037    
038            @Override
039            public void saveOrUpdate(SalGroup salGroup) {
040                    this.getPersistenceBrokerTemplate().store(salGroup);            
041            }
042    
043            @Override
044            public SalGroup getSalGroup(String salGroup, Date asOfDate) {
045                    Criteria root = new Criteria();
046    
047                    root.addEqualTo("hrSalGroup", salGroup);
048            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(SalGroup.class, asOfDate, EQUAL_TO_FIELDS, false));
049            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SalGroup.class, EQUAL_TO_FIELDS, false));
050                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
051                    activeFilter.addEqualTo("active", true);
052                    root.addAndCriteria(activeFilter);
053                    
054                    Query query = QueryFactory.newQuery(SalGroup.class, root);
055                    SalGroup s = (SalGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
056                    
057                    return s;
058            }
059    
060            @Override
061            public SalGroup getSalGroup(String hrSalGroupId) {
062                    Criteria crit = new Criteria();
063                    crit.addEqualTo("hrSalGroupId", hrSalGroupId);
064                    
065                    Query query = QueryFactory.newQuery(SalGroup.class, crit);
066                    return (SalGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
067            }
068            @Override
069            public int getSalGroupCount(String salGroup) {
070                    Criteria crit = new Criteria();
071                    crit.addEqualTo("hrSalGroup", salGroup);
072                    Query query = QueryFactory.newQuery(SalGroup.class, crit);
073                    return this.getPersistenceBrokerTemplate().getCount(query);
074            }
075    
076            @Override
077        @SuppressWarnings("unchecked")
078        public List<SalGroup> getSalGroups(String hrSalGroup, String descr, Date fromEffdt, Date toEffdt, String active, String showHistory) {
079            List<SalGroup> results = new ArrayList<SalGroup>();
080            
081            Criteria root = new Criteria();
082    
083            if (StringUtils.isNotBlank(hrSalGroup)) {
084                root.addLike("hrSalGroup", hrSalGroup);
085            }
086            
087            if (StringUtils.isNotBlank(descr)) {
088                root.addLike("descr", descr);
089            }
090            
091            Criteria effectiveDateFilter = new Criteria();
092            if (fromEffdt != null) {
093                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
094            }
095            if (toEffdt != null) {
096                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
097            }
098            if (fromEffdt == null && toEffdt == null) {
099                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
100            }
101            root.addAndCriteria(effectiveDateFilter);
102    
103            if (StringUtils.equals(showHistory, "N")) {
104                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(SalGroup.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
105                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SalGroup.class, EQUAL_TO_FIELDS, false));
106            }
107    
108            Query query = QueryFactory.newQuery(SalGroup.class, root);
109            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
110            
111            return results;
112        }
113            
114    }