001    /**
002     * Copyright 2004-2012 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.accrual.dao;
017    
018    import java.sql.Date;
019    import java.util.Collection;
020    import java.util.LinkedList;
021    import java.util.List;
022    
023    import org.apache.log4j.Logger;
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.accrual.TimeOffAccrual;
029    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030    
031    public class TimeOffAccrualkDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimeOffAccrualDao {
032    
033            @SuppressWarnings("unused")
034            private static final Logger LOG = Logger.getLogger(TimeOffAccrualkDaoSpringOjbImpl.class);
035    
036            public void saveOrUpdate(TimeOffAccrual timeOffAccrual) {
037                    this.getPersistenceBrokerTemplate().store(timeOffAccrual);
038            }
039    
040            public void saveOrUpdate(List<TimeOffAccrual> timeOffAccrualList) {
041                    if (timeOffAccrualList != null) {
042                            for (TimeOffAccrual timeOffAccrual : timeOffAccrualList) {
043                                    this.getPersistenceBrokerTemplate().store(timeOffAccrual);
044                            }
045                    }
046            }
047            
048            public List<TimeOffAccrual> getTimeOffAccruals (String principalId) {
049                    
050                    List<TimeOffAccrual> timeOffAccruals = new LinkedList<TimeOffAccrual>();
051                    
052                    Criteria root = new Criteria();
053                    Criteria effdt = new Criteria();
054    
055                    // OJB's awesome sub query setup part 1
056                    effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
057                    effdt.addEqualToField("accrualCategory", Criteria.PARENT_QUERY_PREFIX + "accrualCategory");
058                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeOffAccrual.class, effdt);
059                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
060    
061                    root.addEqualTo("principalId", principalId);
062                    root.addEqualTo("effectiveDate", effdtSubQuery);
063    
064                    Query query = QueryFactory.newQuery(TimeOffAccrual.class, root);
065                    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
066                    
067                    if (c != null) {
068                            timeOffAccruals.addAll(c);
069                    }
070    
071                    return timeOffAccruals;
072            }
073    
074            @Override
075            public TimeOffAccrual getTimeOffAccrual(Long laTimeOffAccrualId) {
076                    Criteria crit = new Criteria();
077                    crit.addEqualTo("lmAccrualId", laTimeOffAccrualId);
078                    
079                    Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
080                    return (TimeOffAccrual)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
081    
082            }
083            
084            // KPME-1011
085            public List<TimeOffAccrual> getActiveTimeOffAccruals (String principalId, Date asOfDate) {
086                            List<TimeOffAccrual> timeOffAccruals = new LinkedList<TimeOffAccrual>();
087                            
088                            Criteria root = new Criteria();
089                            Criteria effdt = new Criteria();
090    
091                            // OJB's awesome sub query setup part 1
092                            effdt.addEqualToField("principalId", Criteria.PARENT_QUERY_PREFIX + "principalId");
093                            effdt.addEqualToField("accrualCategory", Criteria.PARENT_QUERY_PREFIX + "accrualCategory");
094                            effdt.addLessOrEqualThan("effectiveDate", asOfDate);
095                            ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(TimeOffAccrual.class, effdt);
096                            effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
097    
098                            root.addEqualTo("principalId", principalId);
099                            root.addEqualTo("effectiveDate", effdtSubQuery);
100                            
101                            Query query = QueryFactory.newQuery(TimeOffAccrual.class, root);
102                            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
103                            
104                            if (c != null) {
105                                    timeOffAccruals.addAll(c);
106                            }
107                            
108                            return timeOffAccruals;
109                    }
110            
111            @Override
112            public int getTimeOffAccrualCount(String accrualCategory, Date effectiveDate, String principalId, String lmAccrualId) {
113                    Criteria crit = new Criteria();
114                    crit.addEqualTo("accrualCategory", accrualCategory);
115                    crit.addEqualTo("effectiveDate", effectiveDate);
116                    crit.addEqualTo("principalId", principalId);
117                    if(lmAccrualId != null) {
118                            crit.addEqualTo("lmAccrualId", lmAccrualId);
119                    }
120                    Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
121                    return this.getPersistenceBrokerTemplate().getCount(query);
122            }
123    }