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 java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.List;
022    
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.time.shiftdiff.rule.ShiftDifferentialRule;
029    import org.kuali.hr.time.util.TKUtils;
030    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
031    
032    public class ShiftDifferentialRuleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements ShiftDifferentialRuleDao {
033            
034            @Override
035            public ShiftDifferentialRule findShiftDifferentialRule(String id) {
036                    Object o = this.getPersistenceBrokerTemplate().getObjectById(ShiftDifferentialRule.class, id);
037                    
038                    return (ShiftDifferentialRule)o;
039            }
040    
041            @SuppressWarnings("unchecked")
042            @Override
043            public List<ShiftDifferentialRule> findShiftDifferentialRules(String location, String hrSalGroup, String payGrade, String pyCalendarGroup, Date asOfDate) {
044                    List<ShiftDifferentialRule> list = new ArrayList<ShiftDifferentialRule>();
045    
046                    Criteria root = new Criteria();
047                    Criteria effdt = new Criteria();
048                    Criteria timestamp = new Criteria();
049    
050                    effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
051                    effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
052                    effdt.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
053                    effdt.addEqualToField("pyCalendarGroup", Criteria.PARENT_QUERY_PREFIX + "pyCalendarGroup");
054                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
055                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, effdt);
056                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
057    
058                    timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
059                    timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
060                    timestamp.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
061                    timestamp.addEqualToField("pyCalendarGroup", Criteria.PARENT_QUERY_PREFIX + "pyCalendarGroup");
062                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
063                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, timestamp);
064                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
065    
066                    root.addEqualTo("location", location);
067                    root.addEqualTo("hrSalGroup", hrSalGroup);
068                    root.addEqualTo("payGrade", payGrade);
069                    root.addEqualTo("pyCalendarGroup", pyCalendarGroup);
070                    root.addEqualTo("effectiveDate", effdtSubQuery);
071                    root.addEqualTo("timestamp", timestampSubQuery);
072    
073                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
074                    activeFilter.addEqualTo("active", true);
075                    root.addAndCriteria(activeFilter);
076                    
077                    Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
078                    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
079    
080                    if (c != null) {
081                            list.addAll(c);
082                    }
083                    
084                    return list;
085            }
086    
087            public void saveOrUpdate(ShiftDifferentialRule shiftDifferentialRule) {
088                    this.getPersistenceBrokerTemplate().store(shiftDifferentialRule);
089            }
090    
091            public void saveOrUpdate(List<ShiftDifferentialRule> shiftDifferentialRules) {
092                    for (ShiftDifferentialRule sdr : shiftDifferentialRules) {
093                            saveOrUpdate(sdr);
094                    }
095            }
096    
097            @Override
098        @SuppressWarnings("unchecked")
099        public List<ShiftDifferentialRule> getShiftDifferentialRules(String location, String hrSalGroup, String payGrade, Date fromEffdt, Date toEffdt, 
100                                                                                                                             String active, String showHistory) {
101            
102                    List<ShiftDifferentialRule> results = new ArrayList<ShiftDifferentialRule>();
103            
104            Criteria root = new Criteria();
105    
106            if (StringUtils.isNotBlank(location)) {
107                root.addLike("location", location);
108            }
109            
110            if (StringUtils.isNotBlank(hrSalGroup)) {
111                root.addLike("hrSalGroup", hrSalGroup);
112            }
113            
114            if (StringUtils.isNotBlank(payGrade)) {
115                root.addLike("payGrade", payGrade);
116            }
117    
118            Criteria effectiveDateFilter = new Criteria();
119            if (fromEffdt != null) {
120                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
121            }
122            if (toEffdt != null) {
123                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
124            }
125            if (fromEffdt == null && toEffdt == null) {
126                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
127            }
128            root.addAndCriteria(effectiveDateFilter);
129            
130            if (StringUtils.isNotBlank(active)) {
131                    Criteria activeFilter = new Criteria();
132                if (StringUtils.equals(active, "Y")) {
133                    activeFilter.addEqualTo("active", true);
134                } else if (StringUtils.equals(active, "N")) {
135                    activeFilter.addEqualTo("active", false);
136                }
137                root.addAndCriteria(activeFilter);
138            }
139    
140            if (StringUtils.equals(showHistory, "N")) {
141                    Criteria effdt = new Criteria();
142                    effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
143                    effdt.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
144                    effdt.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
145                    effdt.addAndCriteria(effectiveDateFilter);
146                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, effdt);
147                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
148    
149                    Criteria timestamp = new Criteria();
150                    timestamp.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
151                    timestamp.addEqualToField("hrSalGroup", Criteria.PARENT_QUERY_PREFIX + "hrSalGroup");
152                    timestamp.addEqualToField("payGrade", Criteria.PARENT_QUERY_PREFIX + "payGrade");
153                    timestamp.addAndCriteria(effectiveDateFilter);
154                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(ShiftDifferentialRule.class, timestamp);
155                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
156            }
157            
158            Query query = QueryFactory.newQuery(ShiftDifferentialRule.class, root);
159            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
160    
161            return results;
162        }
163    
164    }