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.leaveplan.dao;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.LinkedList;
022    import java.util.List;
023    
024    import com.google.common.collect.ImmutableList;
025    import org.apache.commons.lang.StringUtils;
026    import org.apache.ojb.broker.query.Criteria;
027    import org.apache.ojb.broker.query.Query;
028    import org.apache.ojb.broker.query.QueryFactory;
029    import org.apache.ojb.broker.query.ReportQueryByCriteria;
030    import org.joda.time.DateTime;
031    import org.kuali.hr.core.util.OjbSubQueryUtil;
032    import org.kuali.hr.lm.leaveplan.LeavePlan;
033    import org.kuali.hr.time.calendar.CalendarEntries;
034    import org.kuali.hr.time.util.TKUtils;
035    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
036    
037    public class LeavePlanDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements LeavePlanDao {
038        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
039                .add("leavePlan")
040                .build();
041    
042            @Override
043            public LeavePlan getLeavePlan(String lmLeavePlanId) {
044                    Criteria crit = new Criteria();
045                    crit.addEqualTo("lmLeavePlanId", lmLeavePlanId);
046                    Query query = QueryFactory.newQuery(LeavePlan.class, crit);
047                    return (LeavePlan) this.getPersistenceBrokerTemplate().getObjectByQuery(query);
048            }
049            
050            @Override
051            public LeavePlan getLeavePlan(String leavePlan, Date asOfDate) {
052                    LeavePlan lp = null;
053    
054                    Criteria root = new Criteria();
055    
056            java.sql.Date effDate = asOfDate == null ? null : new java.sql.Date(asOfDate.getTime());
057                    root.addEqualTo("leavePlan", leavePlan);
058            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(LeavePlan.class, effDate, EQUAL_TO_FIELDS, false));
059            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeavePlan.class, EQUAL_TO_FIELDS, false));
060                    
061                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
062                    activeFilter.addEqualTo("active", true);
063                    root.addAndCriteria(activeFilter);
064                    
065                    
066                    Query query = QueryFactory.newQuery(LeavePlan.class, root);
067                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
068    
069                    if (obj != null) {
070                            lp = (LeavePlan) obj;
071                    }
072    
073                    return lp;
074            }
075    
076            @Override
077            public int getNumberLeavePlan(String leavePlan) {
078                    Criteria crit = new Criteria();
079                    crit.addEqualTo("leavePlan", leavePlan);
080                    Query query = QueryFactory.newQuery(LeavePlan.class, crit);
081                    return this.getPersistenceBrokerTemplate().getCount(query);
082            }
083            
084            @Override
085            public List<LeavePlan> getAllActiveLeavePlan(String leavePlan, Date asOfDate) {
086                    Criteria root = new Criteria();
087            root.addEqualTo("leavePlan", leavePlan);
088            root.addEqualTo("active", true);
089            root.addLessOrEqualThan("effectiveDate", asOfDate);
090    
091            Query query = QueryFactory.newQuery(LeavePlan.class, root);
092            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
093            
094            List<LeavePlan> lps = new LinkedList<LeavePlan>();
095            if (c != null) {
096                    lps.addAll(c);
097            }
098            return lps;
099                    
100            }
101            
102            @Override
103            public List<LeavePlan> getAllInActiveLeavePlan(String leavePlan, Date asOfDate) {
104                    Criteria root = new Criteria();
105            root.addEqualTo("leavePlan", leavePlan);
106            root.addEqualTo("active", false);
107            root.addLessOrEqualThan("effectiveDate", asOfDate);
108            
109            Query query = QueryFactory.newQuery(LeavePlan.class, root);
110            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
111            
112            List<LeavePlan> lps = new LinkedList<LeavePlan>();
113            if (c != null) {
114                    lps.addAll(c);
115            }
116            return lps;
117            }
118    
119        @Override
120        @SuppressWarnings("unchecked")
121        public List<LeavePlan> getLeavePlans(String leavePlan, String calendarYearStart, String descr, String planningMonths, Date fromEffdt, Date toEffdt, 
122                                                                             String active, String showHistory) {
123    
124            List<LeavePlan> results = new ArrayList<LeavePlan>();
125            
126            Criteria root = new Criteria();
127    
128            if (StringUtils.isNotBlank(leavePlan)) {
129                    root.addLike("leavePlan", leavePlan);
130            }
131            
132            if (StringUtils.isNotBlank(calendarYearStart)) {
133                    root.addEqualTo("calendarYearStart", calendarYearStart);
134            }
135            
136            if (StringUtils.isNotBlank(descr)) {
137                    root.addLike("descr", descr);
138            }
139            
140            if (StringUtils.isNotBlank(planningMonths)) {
141                    root.addEqualTo("planningMonths", planningMonths);
142            }
143            
144            Criteria effectiveDateFilter = new Criteria();
145            if (fromEffdt != null) {
146                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
147            }
148            if (toEffdt != null) {
149                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
150            }
151            if (fromEffdt == null && toEffdt == null) {
152                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
153            }
154            root.addAndCriteria(effectiveDateFilter);
155    
156            if (StringUtils.isNotBlank(active)) {
157                Criteria activeFilter = new Criteria();
158                if (StringUtils.equals(active, "Y")) {
159                    activeFilter.addEqualTo("active", true);
160                } else if (StringUtils.equals(active, "N")) {
161                    activeFilter.addEqualTo("active", false);
162                }
163                root.addAndCriteria(activeFilter);
164            }
165    
166            if (StringUtils.equals(showHistory, "N")) {
167                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(LeavePlan.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
168                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(LeavePlan.class, EQUAL_TO_FIELDS, false));
169            }
170    
171            Query query = QueryFactory.newQuery(LeavePlan.class, root);
172            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
173    
174            return results;
175        }
176    
177            @Override
178            public List<LeavePlan> getLeavePlansNeedsScheduled(int thresholdDays,
179                            Date asOfDate) {
180    
181            Criteria root = new Criteria();
182    
183            root.addEqualTo("active", true);
184    
185            Query query = QueryFactory.newQuery(LeavePlan.class, root);
186            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
187    
188            List<LeavePlan> leavePlans = new ArrayList<LeavePlan>(c.size());
189            leavePlans.addAll(c);
190    
191            return leavePlans;
192            }
193        
194    }