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.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 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.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030    
031    public class EarnCodeDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements EarnCodeDao {
032    
033            @SuppressWarnings("unused")
034            private static final Logger LOG = Logger.getLogger(EarnCodeDaoSpringOjbImpl.class);
035    
036            public void saveOrUpdate(EarnCode earnCode) {
037                    this.getPersistenceBrokerTemplate().store(earnCode);
038            }
039    
040            public void saveOrUpdate(List<EarnCode> earnCodeList) {
041                    if (earnCodeList != null) {
042                            for (EarnCode earnCode : earnCodeList) {
043                                    this.getPersistenceBrokerTemplate().store(earnCode);
044                            }
045                    }
046            }
047    
048            public EarnCode getEarnCodeById(String earnCodeId) {
049                    Criteria crit = new Criteria();
050                    crit.addEqualTo("hrEarnCodeId", earnCodeId);
051                    return (EarnCode) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(EarnCode.class, crit));
052            }
053    
054            @Override
055            public EarnCode getEarnCode(String earnCode, Date asOfDate) {
056                    EarnCode ec = null;
057    
058                    Criteria root = new Criteria();
059                    Criteria effdt = new Criteria();
060                    Criteria timestamp = new Criteria();
061    
062                    // OJB's awesome sub query setup part 1
063                    effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
064                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
065    //              effdt.addEqualTo("active", true);
066                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
067                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
068    
069                    // OJB's awesome sub query setup part 2
070                    timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
071                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
072    //              timestamp.addEqualTo("active", true);
073                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
074                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
075    
076                    root.addEqualTo("earnCode", earnCode);
077                    root.addEqualTo("effectiveDate", effdtSubQuery);
078                    root.addEqualTo("timestamp", timestampSubQuery);
079    //              root.addEqualTo("active", true);
080                    
081                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
082                    activeFilter.addEqualTo("active", true);
083                    root.addAndCriteria(activeFilter);
084                    
085                    
086                    Query query = QueryFactory.newQuery(EarnCode.class, root);
087                    Object obj = this.getPersistenceBrokerTemplate().getObjectByQuery(query);
088    
089                    if (obj != null) {
090                            ec = (EarnCode) obj;
091                    }
092    
093                    return ec;
094            }
095    
096            @Override
097            public List<EarnCode> getOvertimeEarnCodes(Date asOfDate) {
098                    Criteria root = new Criteria();
099                    Criteria effdt = new Criteria();
100                    Criteria timestamp = new Criteria();
101    
102                    // OJB's awesome sub query setup part 1
103                    effdt.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
104                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
105    //              effdt.addEqualTo("active", true);
106                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnCode.class, effdt);
107                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
108    
109                    // OJB's awesome sub query setup part 2
110                    timestamp.addEqualToField("earnCode", Criteria.PARENT_QUERY_PREFIX + "earnCode");
111                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
112    //              timestamp.addEqualTo("active", true);
113                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnCode.class, timestamp);
114                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
115    
116                    root.addEqualTo("ovtEarnCode", "Y");
117                    root.addEqualTo("effectiveDate", effdtSubQuery);
118                    root.addEqualTo("timestamp", timestampSubQuery);
119    //              root.addEqualTo("active", true);
120                    
121                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
122                    activeFilter.addEqualTo("active", true);
123                    root.addAndCriteria(activeFilter);
124                    
125                    
126                    Query query = QueryFactory.newQuery(EarnCode.class, root);
127                    List<EarnCode> ovtEarnCodes = (List<EarnCode>)this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
128                    return ovtEarnCodes;
129            }
130            
131            @Override
132            public int getEarnCodeCount(String earnCode) {
133                    Criteria crit = new Criteria();
134                    crit.addEqualTo("earnCode", earnCode);
135                    Query query = QueryFactory.newQuery(EarnCode.class, crit);
136                    return this.getPersistenceBrokerTemplate().getCount(query);
137            }
138            
139            @Override
140            public int getNewerEarnCodeCount(String earnCode, Date effdt) {
141                    Criteria crit = new Criteria();
142                    crit.addEqualTo("earnCode", earnCode);
143                    crit.addEqualTo("active", "Y");
144                    crit.addGreaterThan("effectiveDate", effdt);
145                    Query query = QueryFactory.newQuery(EarnCode.class, crit);
146            return this.getPersistenceBrokerTemplate().getCount(query);
147            }
148    }