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.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 com.google.common.collect.ImmutableList;
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.ojb.broker.query.Criteria;
026    import org.apache.ojb.broker.query.Query;
027    import org.apache.ojb.broker.query.QueryFactory;
028    import org.apache.ojb.broker.query.ReportQueryByCriteria;
029    import org.kuali.hr.core.util.OjbSubQueryUtil;
030    import org.kuali.hr.time.department.Department;
031    import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
032    
033    public class DepartmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao {
034        private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
035                .add("dept")
036                .build();
037    
038            @Override
039            public void saveOrUpdate(Department dept) {
040                    this.getPersistenceBrokerTemplate().store(dept);
041            }
042    
043            @Override
044            public Department getDepartment(String department, Date asOfDate) {
045                    Criteria root = new Criteria();
046    
047                    root.addEqualTo("dept", department);
048            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
049            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
050    
051                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
052                    activeFilter.addEqualTo("active", true);
053                    root.addAndCriteria(activeFilter);
054    
055                    Query query = QueryFactory.newQuery(Department.class, root);
056    
057                    Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
058    
059                    return d;
060            }
061    
062        @Override
063        public List<Department> getDepartments(String location, Date asOfDate) {
064                    Criteria root = new Criteria();
065    
066                    root.addEqualTo("location", location);
067            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
068            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
069    
070                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
071                    activeFilter.addEqualTo("active", true);
072                    root.addAndCriteria(activeFilter);
073    
074    
075                    Query query = QueryFactory.newQuery(Department.class, root);
076    
077            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
078                    List<Department> d = new ArrayList<Department>(c.size());
079            d.addAll(c);
080    
081                    return d;
082        }
083    
084            @Override
085            @SuppressWarnings("unchecked")
086        public List<Department> getDepartments(String dept, String location, String departmentDescr, String active) {
087            List<Department> results = new ArrayList<Department>();
088            
089            Criteria root = new Criteria();
090    
091            if (StringUtils.isNotBlank(dept)) {
092                root.addLike("dept", dept);
093            }
094            
095            if (StringUtils.isNotBlank(location)) {
096                root.addLike("location", location);
097            }
098            
099            if (StringUtils.isNotBlank(departmentDescr)) {
100                root.addLike("description", departmentDescr);
101            }
102            
103            if (StringUtils.isNotBlank(active)) {
104                    Criteria activeFilter = new Criteria();
105                if (StringUtils.equals(active, "Y")) {
106                    activeFilter.addEqualTo("active", true);
107                } else if (StringUtils.equals(active, "N")) {
108                    activeFilter.addEqualTo("active", false);
109                }
110                root.addAndCriteria(activeFilter);
111            }
112            
113            Query query = QueryFactory.newQuery(Department.class, root);
114            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
115            
116            return results;
117        }
118    
119            @Override
120            public Department getDepartment(String hrDeptId) {
121                    Criteria crit = new Criteria();
122                    crit.addEqualTo("hrDeptId", hrDeptId);
123                    
124                    Query query = QueryFactory.newQuery(Department.class, crit);
125                    return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
126            }
127            
128            @Override
129            public List<Department> getDepartmentByLocation(String location) {
130                    Criteria crit = new Criteria();
131                    crit.addEqualTo("location", location);
132                    
133                    Query query = QueryFactory.newQuery(Department.class, crit);
134    
135            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
136                    List<Department> d = new ArrayList<Department>(c.size());
137            d.addAll(c);
138    
139                    return d;               
140            }
141            
142            @Override
143            public int getDepartmentCount(String department) {
144                    Criteria crit = new Criteria();
145                    crit.addEqualTo("dept", department);
146                    Query query = QueryFactory.newQuery(Department.class, crit);
147                    return this.getPersistenceBrokerTemplate().getCount(query);
148            }
149    }