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.accrual.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.log4j.Logger;
027    import org.apache.ojb.broker.query.Criteria;
028    import org.apache.ojb.broker.query.Query;
029    import org.apache.ojb.broker.query.QueryFactory;
030    import org.apache.ojb.broker.query.ReportQueryByCriteria;
031    import org.kuali.hr.core.util.OjbSubQueryUtil;
032    import org.kuali.hr.time.accrual.TimeOffAccrual;
033    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
034    
035    public class TimeOffAccrualDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TimeOffAccrualDao {
036        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
037                .add("principalId")
038                .add("accrualCategory")
039                .build();
040    
041            @SuppressWarnings("unused")
042            private static final Logger LOG = Logger.getLogger(TimeOffAccrualDaoSpringOjbImpl.class);
043    
044            public void saveOrUpdate(TimeOffAccrual timeOffAccrual) {
045                    this.getPersistenceBrokerTemplate().store(timeOffAccrual);
046            }
047    
048            public void saveOrUpdate(List<TimeOffAccrual> timeOffAccrualList) {
049                    if (timeOffAccrualList != null) {
050                            for (TimeOffAccrual timeOffAccrual : timeOffAccrualList) {
051                                    this.getPersistenceBrokerTemplate().store(timeOffAccrual);
052                            }
053                    }
054            }
055            
056            public List<TimeOffAccrual> getTimeOffAccruals (String principalId) {
057                    List<TimeOffAccrual> timeOffAccruals = new LinkedList<TimeOffAccrual>();
058                    
059                    Criteria root = new Criteria();
060                    root.addEqualTo("principalId", principalId);
061    
062            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(TimeOffAccrual.class, EQUAL_TO_FIELDS, false));
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            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TimeOffAccrual.class, asOfDate, EQUAL_TO_FIELDS, false));
090    
091            root.addEqualTo("principalId", principalId);
092    
093            Query query = QueryFactory.newQuery(TimeOffAccrual.class, root);
094            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
095    
096            if (c != null) {
097                timeOffAccruals.addAll(c);
098            }
099    
100            return timeOffAccruals;
101        }
102            
103            @Override
104            public int getTimeOffAccrualCount(String accrualCategory, Date effectiveDate, String principalId, String lmAccrualId) {
105                    Criteria crit = new Criteria();
106                    crit.addEqualTo("accrualCategory", accrualCategory);
107                    crit.addEqualTo("effectiveDate", effectiveDate);
108                    crit.addEqualTo("principalId", principalId);
109                    if(lmAccrualId != null) {
110                            crit.addEqualTo("lmAccrualId", lmAccrualId);
111                    }
112                    Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
113                    return this.getPersistenceBrokerTemplate().getCount(query);
114            }
115    
116        @Override
117        public List<TimeOffAccrual> getTimeOffAccruals(String principalId, String accrualCategory) {
118            Criteria crit = new Criteria();
119    
120            List<TimeOffAccrual> results = new ArrayList<TimeOffAccrual>();
121    
122            if(StringUtils.isNotBlank(principalId) && StringUtils.isNotEmpty(principalId)){
123                crit.addLike("principalId", principalId);
124            }
125            if(StringUtils.isNotBlank(accrualCategory)){
126                crit.addLike("accrualCategory", accrualCategory);
127            }
128    
129            Query query = QueryFactory.newQuery(TimeOffAccrual.class, crit);
130            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
131            results.addAll(c);
132    
133            return results;
134        }
135    }