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