001/**
002 * Copyright 2004-2014 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 */
016package org.kuali.kpme.tklm.time.rules.shiftdifferential.dao;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.List;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.ojb.broker.query.Criteria;
024import org.apache.ojb.broker.query.Query;
025import org.apache.ojb.broker.query.QueryFactory;
026import org.joda.time.DateTime;
027import org.joda.time.LocalDate;
028import org.kuali.kpme.core.assignment.AssignmentBo;
029import org.kuali.kpme.core.util.OjbSubQueryUtil;
030import org.kuali.kpme.tklm.time.rules.shiftdifferential.ShiftDifferentialRule;
031import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032
033public class ShiftDifferentialRuleDaoOjbImpl 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, LocalDate 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//                .add("earnCode")
060//                .build();
061        root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(ShiftDifferentialRule.class, asOfDate, ShiftDifferentialRule.BUSINESS_KEYS, false));
062        root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ShiftDifferentialRule.class, ShiftDifferentialRule.BUSINESS_KEYS, false));
063
064
065                Criteria activeFilter = new Criteria(); // Inner Join For Activity
066                activeFilter.addEqualTo("active", true);
067                root.addAndCriteria(activeFilter);
068                
069                Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
070                Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
071
072                if (c != null) {
073                        list.addAll(c);
074                }
075                
076                return list;
077        }
078
079        public void saveOrUpdate(ShiftDifferentialRule shiftDifferentialRule) {
080                this.getPersistenceBrokerTemplate().store(shiftDifferentialRule);
081        }
082
083        public void saveOrUpdate(List<ShiftDifferentialRule> shiftDifferentialRules) {
084                for (ShiftDifferentialRule sdr : shiftDifferentialRules) {
085                        saveOrUpdate(sdr);
086                }
087        }
088
089        @Override
090    @SuppressWarnings("unchecked")
091    public List<ShiftDifferentialRule> getShiftDifferentialRules(String location, String hrSalGroup, String payGrade, LocalDate fromEffdt, LocalDate toEffdt, 
092                                                                                                                         String active, String showHistory) {
093        
094                List<ShiftDifferentialRule> results = new ArrayList<ShiftDifferentialRule>();
095        
096        Criteria root = new Criteria();
097
098        if (StringUtils.isNotBlank(location)) {
099                root.addLike("UPPER(location)", location.toUpperCase()); // KPME-2635 ignore the case
100        }        
101        
102        if (StringUtils.isNotBlank(hrSalGroup)) {
103                root.addLike("UPPER(hrSalGroup)", hrSalGroup.toUpperCase());  // KPME-2635 ignore the case
104        }
105        
106        if (StringUtils.isNotBlank(payGrade)) {
107                root.addLike("UPPER(payGrade)", payGrade.toUpperCase()); // KPME-2635 ignore the case
108        }
109
110        Criteria effectiveDateFilter = new Criteria();
111        if (fromEffdt != null) {
112            effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt.toDate());
113        }
114        if (toEffdt != null) {
115            effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt.toDate());
116        }
117        if (fromEffdt == null && toEffdt == null) {
118            effectiveDateFilter.addLessOrEqualThan("effectiveDate", DateTime.now().toDate());
119        }
120
121        
122        if (StringUtils.isNotBlank(active)) {
123                Criteria activeFilter = new Criteria();
124            if (StringUtils.equals(active, "Y")) {
125                activeFilter.addEqualTo("active", true);
126            } else if (StringUtils.equals(active, "N")) {
127                activeFilter.addEqualTo("active", false);
128            }
129            root.addAndCriteria(activeFilter);
130        }
131
132        if (StringUtils.equals(showHistory, "N")) {
133            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(ShiftDifferentialRule.class, effectiveDateFilter, ShiftDifferentialRule.BUSINESS_KEYS, false));
134            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(ShiftDifferentialRule.class, ShiftDifferentialRule.BUSINESS_KEYS, false));
135        }
136        
137        Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
138        results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
139
140        return results;
141    }
142
143}