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    
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                .add("location")
037                .build();
038    
039            @Override
040            public void saveOrUpdate(Department dept) {
041                    this.getPersistenceBrokerTemplate().store(dept);
042            }
043    
044            @Override
045            public Department getDepartment(String department, Date asOfDate) {
046                    Criteria root = new Criteria();
047    
048                    root.addEqualTo("dept", department);
049            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
050            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
051    
052                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
053                    activeFilter.addEqualTo("active", true);
054                    root.addAndCriteria(activeFilter);
055    
056                    Query query = QueryFactory.newQuery(Department.class, root);
057    
058                    Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
059    
060                    return d;
061            }
062    
063        @Override
064        public List<Department> getDepartments(String location, Date asOfDate) {
065                    Criteria root = new Criteria();
066    
067                    root.addEqualTo("location", location);
068            root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
069            root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
070    
071                    Criteria activeFilter = new Criteria(); // Inner Join For Activity
072                    activeFilter.addEqualTo("active", true);
073                    root.addAndCriteria(activeFilter);
074    
075    
076                    Query query = QueryFactory.newQuery(Department.class, root);
077    
078            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
079                    List<Department> d = new ArrayList<Department>(c.size());
080            d.addAll(c);
081    
082                    return d;
083        }
084    
085            @Override
086            @SuppressWarnings("unchecked")
087        public List<Department> getDepartments(String dept, String location, String departmentDescr, String active, String showHistory) {
088            List<Department> results = new ArrayList<Department>();
089    
090            Criteria root = new Criteria();
091    
092            if (StringUtils.isNotBlank(dept)) {
093                root.addLike("dept", dept);
094            }
095    
096            if (StringUtils.isNotBlank(location)) {
097                root.addLike("location", location);
098            }
099    
100            if (StringUtils.isNotBlank(departmentDescr)) {
101                root.addLike("description", departmentDescr);
102            }
103    
104            if (StringUtils.isNotBlank(active)) {
105                Criteria activeFilter = new Criteria();
106                if (StringUtils.equals(active, "Y")) {
107                    activeFilter.addEqualTo("active", true);
108                } else if (StringUtils.equals(active, "N")) {
109                    activeFilter.addEqualTo("active", false);
110                }
111                root.addAndCriteria(activeFilter);
112            }
113    
114            if (StringUtils.equals(showHistory, "N")) {
115                root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Department.class, EQUAL_TO_FIELDS, false));
116                root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
117            }
118    
119            Query query = QueryFactory.newQuery(Department.class, root);
120            results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
121    
122            return results;
123        }
124    
125            @Override
126            public Department getDepartment(String hrDeptId) {
127                    Criteria crit = new Criteria();
128                    crit.addEqualTo("hrDeptId", hrDeptId);
129                    
130                    Query query = QueryFactory.newQuery(Department.class, crit);
131                    return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
132            }
133            
134            @Override
135            public List<Department> getDepartmentByLocation(String location) {
136                    Criteria crit = new Criteria();
137                    crit.addEqualTo("location", location);
138                    
139                    Query query = QueryFactory.newQuery(Department.class, crit);
140    
141            Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
142                    List<Department> d = new ArrayList<Department>(c.size());
143            d.addAll(c);
144    
145                    return d;               
146            }
147            
148            @Override
149            public int getDepartmentCount(String department) {
150                    Criteria crit = new Criteria();
151                    crit.addEqualTo("dept", department);
152                    Query query = QueryFactory.newQuery(Department.class, crit);
153                    return this.getPersistenceBrokerTemplate().getCount(query);
154            }
155    }