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.lm.timeoff.dao;
017    
018    
019    import java.util.ArrayList;
020    import java.util.Date;
021    import java.util.List;
022    
023    import com.google.common.collect.ImmutableList;
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.ojb.broker.query.Criteria;
026    import org.apache.ojb.broker.query.Query;
027    import org.apache.ojb.broker.query.QueryFactory;
028    import org.apache.ojb.broker.query.ReportQueryByCriteria;
029    import org.kuali.hr.core.util.OjbSubQueryUtil;
030    import org.kuali.hr.lm.timeoff.SystemScheduledTimeOff;
031    import org.kuali.hr.time.util.TKUtils;
032    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
033    
034    public class SystemScheduledTimeOffDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements SystemScheduledTimeOffDao {
035    
036            @Override
037            public SystemScheduledTimeOff getSystemScheduledTimeOff(String lmSystemScheduledTimeOffId) {
038                    Criteria crit = new Criteria();
039                    crit.addEqualTo("lmSystemScheduledTimeOffId", lmSystemScheduledTimeOffId);
040                    Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, crit);
041                    return (SystemScheduledTimeOff) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
042            }
043    
044            @Override
045            public List<SystemScheduledTimeOff> getSystemScheduledTimeOffForPayPeriod(
046                            String leavePlan, Date startDate, Date endDate) {
047                    Criteria root = new Criteria();
048                    root.addEqualTo("leavePlan", leavePlan);
049                    root.addBetween("accruedDate", startDate, endDate);
050                    return (List<SystemScheduledTimeOff>)this.getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(SystemScheduledTimeOff.class, root));
051            }
052    
053            @Override
054            public SystemScheduledTimeOff getSystemScheduledTimeOffByDate(String leavePlan, Date startDate) {
055                    Criteria root = new Criteria();
056                    root.addEqualTo("leavePlan", leavePlan);
057                    root.addEqualTo("accruedDate", startDate);
058                    return (SystemScheduledTimeOff)this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(SystemScheduledTimeOff.class, root));
059            }
060    
061            @Override
062        @SuppressWarnings("unchecked")
063        public List<SystemScheduledTimeOff> getSystemScheduledTimeOffs(Date fromEffdt, Date toEffdt, String earnCode, Date fromAccruedDate,Date toAccruedDate, 
064                                                                                                                               Date fromSchTimeOffDate, Date toSchTimeOffDate, String active, String showHistory) {
065            
066            List<SystemScheduledTimeOff> results = new ArrayList<SystemScheduledTimeOff>();
067            
068            Criteria root = new Criteria();
069    
070            if (StringUtils.isNotBlank(earnCode)) {
071                root.addLike("earnCode", earnCode);
072            }
073    
074            Criteria effectiveDateFilter = new Criteria();
075            if (fromEffdt != null) {
076                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
077            }
078            if (toEffdt != null) {
079                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
080            }
081            if (fromEffdt == null && toEffdt == null) {
082                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
083            }
084            root.addAndCriteria(effectiveDateFilter);
085    
086            if (fromAccruedDate != null) {
087                root.addGreaterOrEqualThan("accruedDate", fromAccruedDate);
088            }
089            if (toAccruedDate != null) {
090                root.addLessOrEqualThan("accruedDate", toAccruedDate);
091            }
092    
093            if (fromSchTimeOffDate != null) {
094                root.addGreaterOrEqualThan("scheduledTimeOffDate", fromSchTimeOffDate);
095            }
096            if (toSchTimeOffDate != null) {
097                root.addLessOrEqualThan("scheduledTimeOffDate", toSchTimeOffDate);
098            }
099            
100            if (StringUtils.isNotBlank(active)) {
101                    Criteria activeFilter = new Criteria();
102                if (StringUtils.equals(active, "Y")) {
103                    activeFilter.addEqualTo("active", true);
104                } else if (StringUtils.equals(active, "N")) {
105                    activeFilter.addEqualTo("active", false);
106                }
107                root.addAndCriteria(activeFilter);
108            }
109            
110            if (StringUtils.equals(showHistory, "N")) {
111                ImmutableList<String> fields = new ImmutableList.Builder<String>()
112                        .add("earnCode")
113                        .add("accruedDate")
114                        .build();
115                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(SystemScheduledTimeOff.class, effectiveDateFilter, fields, false));
116                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemScheduledTimeOff.class, fields, false));
117            }
118            
119            Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, root);
120            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
121            
122            return results;
123        }
124            
125            @Override
126        @SuppressWarnings("unchecked")
127        public List<SystemScheduledTimeOff> getSystemScheduledTimeOffsForLeavePlan(Date fromAccruedDate,Date toAccruedDate, String leavePlan) {
128            List<SystemScheduledTimeOff> results = new ArrayList<SystemScheduledTimeOff>();
129            Criteria root = new Criteria();
130    
131            if (fromAccruedDate != null) {
132                root.addGreaterOrEqualThan("accruedDate", fromAccruedDate);
133            }
134            if (toAccruedDate != null) {
135                root.addLessOrEqualThan("accruedDate", toAccruedDate);
136            }
137            
138            if(StringUtils.isNotEmpty(leavePlan)) {
139                    root.addEqualTo("leavePlan", leavePlan);
140            }
141            Criteria activeFilter = new Criteria();
142            activeFilter.addEqualTo("active", true);
143            root.addAndCriteria(activeFilter);
144    
145            ImmutableList<String> fields = new ImmutableList.Builder<String>()
146                    .add("leavePlan")
147                    .add("accruedDate")
148                    .build();
149            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(SystemScheduledTimeOff.class, fields, false));
150            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(SystemScheduledTimeOff.class, fields, false));
151            
152            Query query = QueryFactory.newQuery(SystemScheduledTimeOff.class, root);
153            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
154            
155            return results;
156        }
157    
158    }