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.shiftdiff.rule.dao;
017    
018    import com.google.common.collect.ImmutableList;
019    import org.apache.commons.lang.StringUtils;
020    import org.apache.ojb.broker.query.Criteria;
021    import org.apache.ojb.broker.query.Query;
022    import org.apache.ojb.broker.query.QueryFactory;
023    import org.kuali.hr.core.util.OjbSubQueryUtil;
024    import org.kuali.hr.time.shiftdiff.rule.ShiftDifferentialRule;
025    import org.kuali.hr.time.util.TKUtils;
026    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
027    
028    import java.sql.Date;
029    import java.util.ArrayList;
030    import java.util.Collection;
031    import java.util.List;
032    
033    public class ShiftDifferentialRuleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ShiftDifferentialRuleDao {
034            
035            @Override
036            public ShiftDifferentialRule findShiftDifferentialRule(String id) {
037                    Object o = this.getPersistenceBrokerTemplate().getObjectById(ShiftDifferentialRule.class, id);
038                    
039                    return (ShiftDifferentialRule)o;
040            }
041    
042            @SuppressWarnings("unchecked")
043            @Override
044            public List<ShiftDifferentialRule> findShiftDifferentialRules(String location, String hrSalGroup, String payGrade, String pyCalendarGroup, Date asOfDate) {
045                    List<ShiftDifferentialRule> list = new ArrayList<ShiftDifferentialRule>();
046    
047                    Criteria root = new Criteria();
048    
049                    root.addEqualTo("location", location);
050                    root.addEqualTo("hrSalGroup", hrSalGroup);
051                    root.addEqualTo("payGrade", payGrade);
052                    root.addEqualTo("pyCalendarGroup", pyCalendarGroup);
053    
054            ImmutableList<String> fields = new ImmutableList.Builder<String>()
055                    .add("location")
056                    .add("hrSalGroup")
057                    .add("payGrade")
058                    .add("pyCalendarGroup")
059                    .build();
060            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(ShiftDifferentialRule.class, asOfDate, fields, false));
061            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ShiftDifferentialRule.class, fields, false));
062    
063    
064                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
065                    activeFilter.addEqualTo("active", true);
066                    root.addAndCriteria(activeFilter);
067                    
068                    Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
069                    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
070    
071                    if (c != null) {
072                            list.addAll(c);
073                    }
074                    
075                    return list;
076            }
077    
078            public void saveOrUpdate(ShiftDifferentialRule shiftDifferentialRule) {
079                    this.getPersistenceBrokerTemplate().store(shiftDifferentialRule);
080            }
081    
082            public void saveOrUpdate(List<ShiftDifferentialRule> shiftDifferentialRules) {
083                    for (ShiftDifferentialRule sdr : shiftDifferentialRules) {
084                            saveOrUpdate(sdr);
085                    }
086            }
087    
088            @Override
089        @SuppressWarnings("unchecked")
090        public List<ShiftDifferentialRule> getShiftDifferentialRules(String location, String hrSalGroup, String payGrade, Date fromEffdt, Date toEffdt, 
091                                                                                                                             String active, String showHistory) {
092            
093                    List<ShiftDifferentialRule> results = new ArrayList<ShiftDifferentialRule>();
094            
095            Criteria root = new Criteria();
096    
097            if (StringUtils.isNotBlank(location)) {
098                root.addLike("location", location);
099            }
100            
101            if (StringUtils.isNotBlank(hrSalGroup)) {
102                root.addLike("hrSalGroup", hrSalGroup);
103            }
104            
105            if (StringUtils.isNotBlank(payGrade)) {
106                root.addLike("payGrade", payGrade);
107            }
108    
109            Criteria effectiveDateFilter = new Criteria();
110            if (fromEffdt != null) {
111                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
112            }
113            if (toEffdt != null) {
114                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
115            }
116            if (fromEffdt == null && toEffdt == null) {
117                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
118            }
119    
120            
121            if (StringUtils.isNotBlank(active)) {
122                    Criteria activeFilter = new Criteria();
123                if (StringUtils.equals(active, "Y")) {
124                    activeFilter.addEqualTo("active", true);
125                } else if (StringUtils.equals(active, "N")) {
126                    activeFilter.addEqualTo("active", false);
127                }
128                root.addAndCriteria(activeFilter);
129            }
130    
131            if (StringUtils.equals(showHistory, "N")) {
132                root.addAndCriteria(effectiveDateFilter);
133            }
134            
135            Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
136            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
137    
138            return results;
139        }
140    
141    }