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.paygrade.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.paygrade.PayGrade;
030    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
031    
032    public class PayGradeDaoSpringObjImpl  extends PlatformAwareDaoBaseOjb implements PayGradeDao {
033        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
034                .add("payGrade")
035                .add("salGroup")
036                .build();
037    
038            @Override
039            public PayGrade getPayGrade(String payGrade, String salGroup, Date asOfDate) {
040                    Criteria root = new Criteria();
041    
042                    root.addEqualTo("payGrade", payGrade);
043                    root.addEqualTo("salGroup", salGroup);
044            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(PayGrade.class, new java.sql.Date(asOfDate.getTime()), EQUAL_TO_FIELDS, false));
045            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PayGrade.class, EQUAL_TO_FIELDS, false));
046    
047                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
048                    activeFilter.addEqualTo("active", true);
049                    root.addAndCriteria(activeFilter);
050    
051                    
052                    Query query = QueryFactory.newQuery(PayGrade.class, root);
053                    
054                    PayGrade pg = (PayGrade)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
055                    
056                    return pg;
057            }
058    
059            @Override
060            public PayGrade getPayGrade(String hrPayGradeId) {
061                    Criteria crit = new Criteria();
062                    crit.addEqualTo("hrPayGradeId", hrPayGradeId);
063                    
064                    Query query = QueryFactory.newQuery(PayGrade.class, crit);
065                    
066                    return (PayGrade)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
067            }
068            
069            public int getPayGradeCount(String payGrade) {
070                    Criteria crit = new Criteria();
071                    crit.addEqualTo("payGrade", payGrade);
072                    Query query = QueryFactory.newQuery(PayGrade.class, crit);
073                    return this.getPersistenceBrokerTemplate().getCount(query);
074            }
075    
076            @Override
077        @SuppressWarnings("unchecked")
078        public List<PayGrade> getPayGrades(String payGrade, String payGradeDescr, String active, String showHistory) {
079            List<PayGrade> results = new ArrayList<PayGrade>();
080            
081            Criteria root = new Criteria();
082    
083            if (StringUtils.isNotBlank(payGrade)) {
084                root.addLike("payGrade", payGrade);
085            }
086            
087            if (StringUtils.isNotBlank(payGradeDescr)) {
088                root.addLike("description", payGradeDescr);
089            }
090            
091            if (StringUtils.isNotBlank(active)) {
092                    Criteria activeFilter = new Criteria();
093                if (StringUtils.equals(active, "Y")) {
094                    activeFilter.addEqualTo("active", true);
095                } else if (StringUtils.equals(active, "N")) {
096                    activeFilter.addEqualTo("active", false);
097                }
098                root.addAndCriteria(activeFilter);
099            }
100    
101            if (StringUtils.equals(showHistory, "N")) {
102                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(PayGrade.class, EQUAL_TO_FIELDS, false));
103                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(PayGrade.class, EQUAL_TO_FIELDS, false));
104            }
105            
106            Query query = QueryFactory.newQuery(PayGrade.class, root);
107            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
108    
109            return results;
110        }
111    
112    }