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.department.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.ojb.broker.query.Criteria;
024    import org.apache.ojb.broker.query.Query;
025    import org.apache.ojb.broker.query.QueryFactory;
026    import org.apache.ojb.broker.query.ReportQueryByCriteria;
027    import org.kuali.hr.time.department.Department;
028    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
029    
030    public class DepartmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao {
031    
032            @Override
033            public void saveOrUpdate(Department dept) {
034                    this.getPersistenceBrokerTemplate().store(dept);
035            }
036    
037            @Override
038            public Department getDepartment(String department, Date asOfDate) {
039                    Criteria root = new Criteria();
040                    Criteria effdt = new Criteria();
041                    Criteria timestamp = new Criteria();
042    
043                    effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
044                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
045                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Department.class, effdt);
046                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
047    
048                    timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
049                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
050                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Department.class, timestamp);
051                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
052    
053                    root.addEqualTo("dept", department);
054                    root.addEqualTo("effectiveDate", effdtSubQuery);
055                    root.addEqualTo("timestamp", timestampSubQuery);
056    
057                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
058                    activeFilter.addEqualTo("active", true);
059                    root.addAndCriteria(activeFilter);
060    
061                    Query query = QueryFactory.newQuery(Department.class, root);
062    
063                    Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
064    
065                    return d;
066            }
067    
068        @Override
069        public List<Department> getDepartments(String location, Date asOfDate) {
070                    Criteria root = new Criteria();
071                    Criteria effdt = new Criteria();
072                    Criteria timestamp = new Criteria();
073    
074                    effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
075                    effdt.addLessOrEqualThan("effectiveDate", asOfDate);
076                    ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Department.class, effdt);
077                    effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
078    
079                    timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
080                    timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
081                    ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Department.class, timestamp);
082                    timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
083    
084                    root.addEqualTo("location", location);
085                    root.addEqualTo("effectiveDate", effdtSubQuery);
086                    root.addEqualTo("timestamp", timestampSubQuery);
087    
088                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
089                    activeFilter.addEqualTo("active", true);
090                    root.addAndCriteria(activeFilter);
091    
092    
093                    Query query = QueryFactory.newQuery(Department.class, root);
094    
095            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
096                    List<Department> d = new ArrayList<Department>(c.size());
097            d.addAll(c);
098    
099                    return d;
100        }
101    
102            @Override
103            public Department getDepartment(String hrDeptId) {
104                    Criteria crit = new Criteria();
105                    crit.addEqualTo("hrDeptId", hrDeptId);
106                    
107                    Query query = QueryFactory.newQuery(Department.class, crit);
108                    return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
109            }
110            
111            @Override
112            public List<Department> getDepartmentByLocation(String location) {
113                    Criteria crit = new Criteria();
114                    crit.addEqualTo("location", location);
115                    
116                    Query query = QueryFactory.newQuery(Department.class, crit);
117    
118            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
119                    List<Department> d = new ArrayList<Department>(c.size());
120            d.addAll(c);
121    
122                    return d;               
123            }
124            
125            @Override
126            public int getDepartmentCount(String department) {
127                    Criteria crit = new Criteria();
128                    crit.addEqualTo("dept", department);
129                    Query query = QueryFactory.newQuery(Department.class, crit);
130                    return this.getPersistenceBrokerTemplate().getCount(query);
131            }
132    }