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.earncode.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 com.google.common.collect.ImmutableList;
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.log4j.Logger;
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.kuali.hr.core.util.OjbSubQueryUtil;
031    import org.kuali.hr.time.earncode.EarnCode;
032    import org.kuali.hr.time.util.TKUtils;
033    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
034    
035    public class EarnCodeDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeDao {
036        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
037                .add("earnCode")
038                //.add("leavePlan")
039                .build();
040    
041            private static final Logger LOG = Logger.getLogger(EarnCodeDaoSpringOjbImpl.class);
042    
043            public void saveOrUpdate(EarnCode earnCode) {
044                    this.getPersistenceBrokerTemplate().store(earnCode);
045            }
046    
047            public void saveOrUpdate(List<EarnCode> earnCodeList) {
048                    if (earnCodeList != null) {
049                            for (EarnCode earnCode : earnCodeList) {
050                                    this.getPersistenceBrokerTemplate().store(earnCode);
051                            }
052                    }
053            }
054    
055            public EarnCode getEarnCodeById(String earnCodeId) {
056                    Criteria crit = new Criteria();
057                    crit.addEqualTo("hrEarnCodeId", earnCodeId);
058                    return (EarnCode) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(EarnCode.class, crit));
059            }
060    
061            @Override
062            public EarnCode getEarnCode(String earnCode, Date asOfDate) {
063                    EarnCode ec = null;
064    
065                    Criteria root = new Criteria();
066    
067                    root.addEqualTo("earnCode", earnCode);
068            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCode.class, asOfDate, EQUAL_TO_FIELDS, false));
069            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, EQUAL_TO_FIELDS, false));
070    
071                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
072                    activeFilter.addEqualTo("active", true);
073                    root.addAndCriteria(activeFilter);
074                    
075                    
076                    Query query = QueryFactory.newQuery(EarnCode.class, root);
077                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
078    
079                    if (obj != null) {
080                            ec = (EarnCode) obj;
081                    }
082    
083                    return ec;
084            }
085    
086            @Override
087            public List<EarnCode> getOvertimeEarnCodes(Date asOfDate) {
088                    Criteria root = new Criteria();
089    
090                    root.addEqualTo("ovtEarnCode", "Y");
091            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCode.class, asOfDate, EQUAL_TO_FIELDS, false));
092            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, EQUAL_TO_FIELDS, false));
093    //              root.addEqualTo("active", true);
094                    
095                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
096                    activeFilter.addEqualTo("active", true);
097                    root.addAndCriteria(activeFilter);
098                    
099                    
100                    Query query = QueryFactory.newQuery(EarnCode.class, root);
101                    List<EarnCode> ovtEarnCodes = (List<EarnCode>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
102                    return ovtEarnCodes;
103            }
104            
105            @Override
106            public int getEarnCodeCount(String earnCode) {
107                    Criteria crit = new Criteria();
108                    crit.addEqualTo("earnCode", earnCode);
109                    Query query = QueryFactory.newQuery(EarnCode.class, crit);
110                    return this.getPersistenceBrokerTemplate().getCount(query);
111            }
112            
113            @Override
114            public int getNewerEarnCodeCount(String earnCode, Date effdt) {
115                    Criteria crit = new Criteria();
116                    crit.addEqualTo("earnCode", earnCode);
117                    crit.addEqualTo("active", "Y");
118                    crit.addGreaterThan("effectiveDate", effdt);
119                    Query query = QueryFactory.newQuery(EarnCode.class, crit);
120            return this.getPersistenceBrokerTemplate().getCount(query);
121            }
122    
123            @Override
124            public List<EarnCode> getEarnCodes(String leavePlan, Date asOfDate) {
125                    List<EarnCode> earnCodes = new ArrayList<EarnCode>();
126                    Criteria root = new Criteria();
127    
128            List<String> fields = new ArrayList<String>();
129            fields.add("earnCode");
130            fields.add("leavePlan");
131                    root.addEqualTo("leavePlan", leavePlan);
132            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(EarnCode.class, asOfDate, fields, false));
133            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, fields, false));
134                    
135                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
136                    activeFilter.addEqualTo("active", true);
137                    root.addAndCriteria(activeFilter);
138                    
139                    
140                    Query query = QueryFactory.newQuery(EarnCode.class, root);
141                    Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
142    
143                    if (c != null) {
144                            earnCodes.addAll(c);
145                    }       
146                    return earnCodes;
147            }
148    
149            @Override
150        @SuppressWarnings("unchecked")
151        public List<EarnCode> getEarnCodes(String earnCode, String ovtEarnCode, String descr, String leavePlan, String accrualCategory, Date fromEffdt, Date toEffdt, String active, String showHistory) {
152            List<EarnCode> results = new ArrayList<EarnCode>();
153            
154            Criteria root = new Criteria();
155            
156            if (StringUtils.isNotBlank(earnCode)) {
157                root.addLike("earnCode", earnCode);
158            }
159            
160            if (StringUtils.isNotBlank(ovtEarnCode)) {
161                root.addEqualTo("ovtEarnCode", ovtEarnCode);
162            }
163            
164            if (StringUtils.isNotBlank(descr)) {
165                root.addLike("description", descr);
166            }
167    
168            if (StringUtils.isNotBlank(leavePlan)) {
169                    root.addLike("leavePlan", leavePlan);
170            }
171            
172            if (StringUtils.isNotBlank(accrualCategory)) {
173                    root.addLike("accrualCategory", accrualCategory);
174            }
175            
176            Criteria effectiveDateFilter = new Criteria();
177            if (fromEffdt != null) {
178                effectiveDateFilter.addGreaterOrEqualThan("effectiveDate", fromEffdt);
179            }
180            if (toEffdt != null) {
181                effectiveDateFilter.addLessOrEqualThan("effectiveDate", toEffdt);
182            }
183            if (fromEffdt == null && toEffdt == null) {
184                effectiveDateFilter.addLessOrEqualThan("effectiveDate", TKUtils.getCurrentDate());
185            }
186            root.addAndCriteria(effectiveDateFilter);
187            
188            if (StringUtils.isNotBlank(active)) {
189                    Criteria activeFilter = new Criteria();
190                if (StringUtils.equals(active, "Y")) {
191                    activeFilter.addEqualTo("active", true);
192                } else if (StringUtils.equals(active, "N")) {
193                    activeFilter.addEqualTo("active", false);
194                }
195                root.addAndCriteria(activeFilter);
196            }
197    
198            if (StringUtils.equals(showHistory, "N")) {
199                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithFilter(EarnCode.class, effectiveDateFilter, EQUAL_TO_FIELDS, false));
200                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(EarnCode.class, EQUAL_TO_FIELDS, false));
201            }
202            
203            Query query = QueryFactory.newQuery(EarnCode.class, root);
204            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
205            
206            return results;
207        }
208            
209    }