View Javadoc
1   /**
2    * Copyright 2004-2015 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.kpme.core.department.dao;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.ojb.broker.query.Criteria;
24  import org.apache.ojb.broker.query.Query;
25  import org.apache.ojb.broker.query.QueryFactory;
26  import org.joda.time.LocalDate;
27  import org.kuali.kpme.core.department.Department;
28  import org.kuali.kpme.core.util.OjbSubQueryUtil;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  import com.google.common.collect.ImmutableList;
32  
33  public class DepartmentDaoOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao {
34      
35  	@Override
36  	public void saveOrUpdate(Department dept) {
37  		this.getPersistenceBrokerTemplate().store(dept);
38  	}
39  
40  	@Override
41  	public Department getDepartment(String department, LocalDate asOfDate) {
42  		Criteria root = new Criteria();
43  
44  		root.addEqualTo("dept", department);
45          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, Department.EQUAL_TO_FIELDS, false));
46          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, Department.EQUAL_TO_FIELDS, false));
47  
48  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
49  		activeFilter.addEqualTo("active", true);
50  		root.addAndCriteria(activeFilter);
51  
52  		Query query = QueryFactory.newQuery(Department.class, root);
53  
54  		Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
55  
56  		return d;
57  	}
58  
59      @Override
60      public List<Department> getDepartments(String location, LocalDate asOfDate) {
61  		Criteria root = new Criteria();
62  
63  		root.addEqualTo("location", location);
64          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, Department.EQUAL_TO_FIELDS, false));
65          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, Department.EQUAL_TO_FIELDS, false));
66  
67  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
68  		activeFilter.addEqualTo("active", true);
69  		root.addAndCriteria(activeFilter);
70  
71  
72  		Query query = QueryFactory.newQuery(Department.class, root);
73  
74          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
75  		List<Department> d = new ArrayList<Department>(c.size());
76          d.addAll(c);
77  
78  		return d;
79      }
80  
81      @Override
82      public List<Department> getDepartmentsForLocations(List<String> locations, LocalDate asOfDate) {
83          Criteria root = new Criteria();
84  
85          root.addIn("location", locations);
86          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, Department.EQUAL_TO_FIELDS, false));
87          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, Department.EQUAL_TO_FIELDS, false));
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, String showHistory, String payrollApproval) {
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("UPPER(location)", location.toUpperCase()); // KPME-2695
116         }
117 
118         if (StringUtils.isNotBlank(departmentDescr)) {
119             root.addLike("UPPER(description)", departmentDescr.toUpperCase()); // KPME-2695
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         if (StringUtils.equals(showHistory, "N")) {
133             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Department.class, Department.EQUAL_TO_FIELDS, false));
134             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, Department.EQUAL_TO_FIELDS, false));
135         }
136 
137         if (StringUtils.isNotEmpty(payrollApproval)) {
138             root.addEqualTo("payrollApproval",payrollApproval);
139         }
140 
141         Query query = QueryFactory.newQuery(Department.class, root);
142         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
143 
144         return results;
145     }
146 
147 	@Override
148 	public Department getDepartment(String hrDeptId) {
149 		Criteria crit = new Criteria();
150 		crit.addEqualTo("hrDeptId", hrDeptId);
151 		
152 		Query query = QueryFactory.newQuery(Department.class, crit);
153 		return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
154 	}
155 	
156 	@Override
157 	public int getDepartmentCount(String department) {
158 		Criteria crit = new Criteria();
159 		crit.addEqualTo("dept", department);
160 		Query query = QueryFactory.newQuery(Department.class, crit);
161 		return this.getPersistenceBrokerTemplate().getCount(query);
162 	}
163 	
164 	@Override
165 	public List<Department> getDepartments(String department) {
166 		List<Department> results = new ArrayList<Department>();
167 		Criteria crit = new Criteria();
168 		crit.addEqualTo("dept", department);
169 		crit.addEqualTo("active", true);
170 		Query query = QueryFactory.newQuery(Department.class, crit);
171 		results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
172 		return results;
173 	}
174 }