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.earngroup.dao;
017    
018    import org.apache.ojb.broker.query.Criteria;
019    import org.apache.ojb.broker.query.Query;
020    import org.apache.ojb.broker.query.QueryFactory;
021    import org.apache.ojb.broker.query.ReportQueryByCriteria;
022    import org.kuali.hr.time.earngroup.EarnGroup;
023    import org.kuali.hr.time.earngroup.EarnGroupDefinition;
024    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
025    import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
026    
027    import java.sql.Date;
028    
029    public class EarnGroupDaoServiceImpl extends PlatformAwareDaoBaseOjb implements EarnGroupDaoService {
030    
031            @Override
032            public EarnGroup getEarnGroup(String earnGroup, Date asOfDate) {
033                    Criteria root = new Criteria();
034                    Criteria effdt = new Criteria();
035                    Criteria timestamp = new Criteria();
036    
037                    effdt.addEqualToField("earnGroup", Criteria.PARENT_QUERY_PREFIX + "earnGroup");
038                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
039    //              effdt.addEqualTo("active", true);
040                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnGroup.class, effdt);
041                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
042    
043                    timestamp.addEqualToField("earnGroup", Criteria.PARENT_QUERY_PREFIX + "earnGroup");
044                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
045    //              timestamp.addEqualTo("active", true);
046                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnGroup.class, timestamp);
047                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
048    
049                    root.addEqualTo("earnGroup", earnGroup);
050                    root.addEqualTo("effectiveDate", effdtSubQuery);
051                    root.addEqualTo("timestamp", timestampSubQuery);
052    //              root.addEqualTo("active", true);
053                    //do not include the summary setup earn groups
054    
055                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
056                    activeFilter.addEqualTo("active", true);
057                    root.addAndCriteria(activeFilter);
058                                    
059                    Query query = QueryFactory.newQuery(EarnGroup.class, root);
060                    EarnGroup earnGroupObj  = (EarnGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
061                    return earnGroupObj;
062            }
063    
064            @Override
065            public EarnGroup getEarnGroupSummaryForEarnCode(String earnCode, Date asOfDate) {
066                    Criteria root = new Criteria();
067                    Criteria effdt = new Criteria();
068                    Criteria timestamp = new Criteria();
069                    Criteria earnCodeJoin = new Criteria();
070                    
071                    effdt.addEqualToField("earnGroup", Criteria.PARENT_QUERY_PREFIX + "earnGroup");
072                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
073    //              effdt.addEqualTo("active", true);
074                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnGroup.class, effdt);
075                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
076    
077                    timestamp.addEqualToField("earnGroup", Criteria.PARENT_QUERY_PREFIX + "earnGroup");
078                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
079    //              timestamp.addEqualTo("active", true);
080                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnGroup.class, timestamp);
081                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
082                    
083                    earnCodeJoin.addEqualToField("hrEarnGroupId", Criteria.PARENT_QUERY_PREFIX + "hrEarnGroupId");
084                    earnCodeJoin.addEqualTo("earnCode", earnCode);
085                    ReportQueryByCriteria earnCodeSubQuery = QueryFactory.newReportQuery(EarnGroupDefinition.class, earnCodeJoin);
086                    earnCodeSubQuery.setAttributes(new String[]{"hr_earn_group_id"});
087                    
088                    root.addEqualTo("hrEarnGroupId",earnCodeSubQuery);
089                    root.addEqualTo("effectiveDate", effdtSubQuery);
090                    root.addEqualTo("timestamp", timestampSubQuery);
091    //              root.addEqualTo("active", true);
092                    root.addEqualTo("showSummary", true);
093                    
094                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
095                    activeFilter.addEqualTo("active", true);
096                    root.addAndCriteria(activeFilter);
097    
098                    Query query = QueryFactory.newQuery(EarnGroup.class, root);
099                    EarnGroup earnGroupObj  = (EarnGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
100                    return earnGroupObj;
101            }
102    
103            @Override
104            public EarnGroup getEarnGroupForEarnCode(String earnCode, Date asOfDate) {
105                    Criteria root = new Criteria();
106                    Criteria effdt = new Criteria();
107                    Criteria timestamp = new Criteria();
108                    Criteria earnCodeJoin = new Criteria();
109                    
110                    effdt.addEqualToField("earnGroup", Criteria.PARENT_QUERY_PREFIX + "earnGroup");
111                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
112    //              effdt.addEqualTo("active", true);
113                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(EarnGroup.class, effdt);
114                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
115    
116                    timestamp.addEqualToField("earnGroup", Criteria.PARENT_QUERY_PREFIX + "earnGroup");
117                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
118    //              timestamp.addEqualTo("active", true);
119                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(EarnGroup.class, timestamp);
120                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
121                    
122                    earnCodeJoin.addEqualToField("hrEarnGroupId", Criteria.PARENT_QUERY_PREFIX + "hrEarnGroupId");
123                    earnCodeJoin.addEqualTo("earnCode", earnCode);
124                    ReportQueryByCriteria earnCodeSubQuery = QueryFactory.newReportQuery(EarnGroupDefinition.class, earnCodeJoin);
125                    earnCodeSubQuery.setAttributes(new String[]{"hr_earn_group_id"});
126                    
127                    root.addEqualTo("hrEarnGroupId",earnCodeSubQuery);
128                    root.addEqualTo("effectiveDate", effdtSubQuery);
129                    root.addEqualTo("timestamp", timestampSubQuery);
130    //              root.addEqualTo("active", true);
131    
132                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
133                    activeFilter.addEqualTo("active", true);
134                    root.addAndCriteria(activeFilter);
135                    
136                    Query query = QueryFactory.newQuery(EarnGroup.class, root);
137                    EarnGroup earnGroupObj  = (EarnGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
138                    return earnGroupObj;
139            }
140    
141            @Override
142            public EarnGroup getEarnGroup(String hrEarnGroupId) {
143                    Criteria crit = new Criteria();
144                    crit.addEqualTo("hrEarnGroupId", hrEarnGroupId);
145                    
146                    Query query = QueryFactory.newQuery(EarnGroup.class, crit);
147                    return (EarnGroup)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
148            }
149            
150            @Override
151            public int getEarnGroupCount(String earnGroup) {
152                    Criteria crit = new Criteria();
153                crit.addEqualTo("earnGroup", earnGroup);
154                Query query = QueryFactory.newQuery(EarnGroup.class, crit);
155                return this.getPersistenceBrokerTemplate().getCount(query);
156            }
157            @Override
158            public int getNewerEarnGroupCount(String earnGroup, Date effdt) {
159                    Criteria crit = new Criteria();
160                    crit.addEqualTo("earnGroup", earnGroup);
161                    crit.addEqualTo("active", "Y");
162                    crit.addGreaterThan("effectiveDate", effdt);
163                    Query query = QueryFactory.newQuery(EarnGroup.class, crit);
164            return this.getPersistenceBrokerTemplate().getCount(query);
165            }
166    }