001    /**
002     * Copyright 2004-2012 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.shiftdiff.rule.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
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.shiftdiff.rule.ShiftDifferentialRule;
028    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
029    
030    public class ShiftDifferentialRuleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ShiftDifferentialRuleDao {
031            
032            @Override
033            public ShiftDifferentialRule findShiftDifferentialRule(String id) {
034                    Object o = this.getPersistenceBrokerTemplate().getObjectById(ShiftDifferentialRule.class, id);
035                    
036                    return (ShiftDifferentialRule)o;
037            }
038    
039            @SuppressWarnings("unchecked")
040            @Override
041            public List<ShiftDifferentialRule> findShiftDifferentialRules(String location, String hrSalGroup, String payGrade, String pyCalendarGroup, Date asOfDate) {
042                    List<ShiftDifferentialRule> list = new ArrayList<ShiftDifferentialRule>();
043    
044                    Criteria root = new Criteria();
045                    Criteria effdt = new Criteria();
046                    Criteria timestamp = new Criteria();
047    
048                    effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
049                    effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
050                    effdt.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
051                    effdt.addEqualToField("pyCalendarGroup", Criteria.PARENT_QUERY_PREFIX + "pyCalendarGroup");
052                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
053                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, effdt);
054                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
055    
056                    timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
057                    timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
058                    timestamp.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
059                    timestamp.addEqualToField("pyCalendarGroup", Criteria.PARENT_QUERY_PREFIX + "pyCalendarGroup");
060                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
061                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, timestamp);
062                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
063    
064                    root.addEqualTo("location", location);
065                    root.addEqualTo("hrSalGroup", hrSalGroup);
066                    root.addEqualTo("payGrade", payGrade);
067                    root.addEqualTo("pyCalendarGroup", pyCalendarGroup);
068                    root.addEqualTo("effectiveDate", effdtSubQuery);
069                    root.addEqualTo("timestamp", timestampSubQuery);
070    
071                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
072                    activeFilter.addEqualTo("active", true);
073                    root.addAndCriteria(activeFilter);
074                    
075                    Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
076                    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
077    
078                    if (c != null) {
079                            list.addAll(c);
080                    }
081                    
082                    return list;
083            }
084    
085            public void saveOrUpdate(ShiftDifferentialRule shiftDifferentialRule) {
086                    this.getPersistenceBrokerTemplate().store(shiftDifferentialRule);
087            }
088    
089            public void saveOrUpdate(List<ShiftDifferentialRule> shiftDifferentialRules) {
090                    for (ShiftDifferentialRule sdr : shiftDifferentialRules) {
091                            saveOrUpdate(sdr);
092                    }
093            }
094    
095    }