View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.department.dao;
17  
18  import java.sql.Date;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.ojb.broker.query.Criteria;
25  import org.apache.ojb.broker.query.Query;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.hr.time.department.Department;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  public class DepartmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao {
32  
33  	@Override
34  	public void saveOrUpdate(Department dept) {
35  		this.getPersistenceBrokerTemplate().store(dept);
36  	}
37  
38  	@Override
39  	public Department getDepartment(String department, Date asOfDate) {
40  		Criteria root = new Criteria();
41  		Criteria effdt = new Criteria();
42  		Criteria timestamp = new Criteria();
43  
44  		effdt.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
45  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
46  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Department.class, effdt);
47  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
48  
49  		timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
50  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
51  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Department.class, timestamp);
52  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
53  
54  		root.addEqualTo("dept", department);
55  		root.addEqualTo("effectiveDate", effdtSubQuery);
56  		root.addEqualTo("timestamp", timestampSubQuery);
57  
58  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
59  		activeFilter.addEqualTo("active", true);
60  		root.addAndCriteria(activeFilter);
61  
62  		Query query = QueryFactory.newQuery(Department.class, root);
63  
64  		Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
65  
66  		return d;
67  	}
68  
69      @Override
70      public List<Department> getDepartments(String location, Date asOfDate) {
71  		Criteria root = new Criteria();
72  		Criteria effdt = new Criteria();
73  		Criteria timestamp = new Criteria();
74  
75  		effdt.addEqualToField("location", Criteria.PARENT_QUERY_PREFIX + "location");
76  		effdt.addLessOrEqualThan("effectiveDate", asOfDate);
77  		ReportQueryByCriteria effdtSubQuery = QueryFactory.newReportQuery(Department.class, effdt);
78  		effdtSubQuery.setAttributes(new String[] { "max(effdt)" });
79  
80  		timestamp.addEqualToField("dept", Criteria.PARENT_QUERY_PREFIX + "dept");
81  		timestamp.addEqualToField("effectiveDate", Criteria.PARENT_QUERY_PREFIX + "effectiveDate");
82  		ReportQueryByCriteria timestampSubQuery = QueryFactory.newReportQuery(Department.class, timestamp);
83  		timestampSubQuery.setAttributes(new String[] { "max(timestamp)" });
84  
85  		root.addEqualTo("location", location);
86  		root.addEqualTo("effectiveDate", effdtSubQuery);
87  		root.addEqualTo("timestamp", timestampSubQuery);
88  
89  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
90  		activeFilter.addEqualTo("active", true);
91  		root.addAndCriteria(activeFilter);
92  
93  
94  		Query query = QueryFactory.newQuery(Department.class, root);
95  
96          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
97  		List<Department> d = new ArrayList<Department>(c.size());
98          d.addAll(c);
99  
100 		return d;
101     }
102 
103 	@Override
104 	@SuppressWarnings("unchecked")
105     public List<Department> getDepartments(String dept, String location, String departmentDescr, String active) {
106         List<Department> results = new ArrayList<Department>();
107         
108         Criteria root = new Criteria();
109 
110         if (StringUtils.isNotBlank(dept)) {
111             root.addLike("dept", dept);
112         }
113         
114         if (StringUtils.isNotBlank(location)) {
115             root.addLike("location", location);
116         }
117         
118         if (StringUtils.isNotBlank(departmentDescr)) {
119             root.addLike("description", departmentDescr);
120         }
121         
122         if (StringUtils.isNotBlank(active)) {
123         	Criteria activeFilter = new Criteria();
124             if (StringUtils.equals(active, "Y")) {
125                 activeFilter.addEqualTo("active", true);
126             } else if (StringUtils.equals(active, "N")) {
127                 activeFilter.addEqualTo("active", false);
128             }
129             root.addAndCriteria(activeFilter);
130         }
131         
132         Query query = QueryFactory.newQuery(Department.class, root);
133         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
134         
135         return results;
136     }
137 
138 	@Override
139 	public Department getDepartment(String hrDeptId) {
140 		Criteria crit = new Criteria();
141 		crit.addEqualTo("hrDeptId", hrDeptId);
142 		
143 		Query query = QueryFactory.newQuery(Department.class, crit);
144 		return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
145 	}
146 	
147 	@Override
148 	public List<Department> getDepartmentByLocation(String location) {
149 		Criteria crit = new Criteria();
150 		crit.addEqualTo("location", location);
151 		
152 		Query query = QueryFactory.newQuery(Department.class, crit);
153 
154         Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
155 		List<Department> d = new ArrayList<Department>(c.size());
156         d.addAll(c);
157 
158 		return d;		
159 	}
160 	
161 	@Override
162 	public int getDepartmentCount(String department) {
163 		Criteria crit = new Criteria();
164 		crit.addEqualTo("dept", department);
165 		Query query = QueryFactory.newQuery(Department.class, crit);
166 		return this.getPersistenceBrokerTemplate().getCount(query);
167 	}
168 }