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.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  	@SuppressWarnings("unchecked")
83      public List<Department> getDepartments(String dept, String location, String departmentDescr, String active, String showHistory) {
84          List<Department> results = new ArrayList<Department>();
85  
86          Criteria root = new Criteria();
87  
88          if (StringUtils.isNotBlank(dept)) {
89          	root.addLike("dept", dept);
90          }
91  
92          if (StringUtils.isNotBlank(location)) {
93              root.addLike("UPPER(`location`)", location.toUpperCase()); // KPME-2695
94          }
95  
96          if (StringUtils.isNotBlank(departmentDescr)) {
97              root.addLike("UPPER(`description`)", departmentDescr.toUpperCase()); // KPME-2695
98          }
99  
100         if (StringUtils.isNotBlank(active)) {
101             Criteria activeFilter = new Criteria();
102             if (StringUtils.equals(active, "Y")) {
103                 activeFilter.addEqualTo("active", true);
104             } else if (StringUtils.equals(active, "N")) {
105                 activeFilter.addEqualTo("active", false);
106             }
107             root.addAndCriteria(activeFilter);
108         }
109 
110         if (StringUtils.equals(showHistory, "N")) {
111             root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Department.class, Department.EQUAL_TO_FIELDS, false));
112             root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, Department.EQUAL_TO_FIELDS, false));
113         }
114 
115         Query query = QueryFactory.newQuery(Department.class, root);
116         results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
117 
118         return results;
119     }
120 
121 	@Override
122 	public Department getDepartment(String hrDeptId) {
123 		Criteria crit = new Criteria();
124 		crit.addEqualTo("hrDeptId", hrDeptId);
125 		
126 		Query query = QueryFactory.newQuery(Department.class, crit);
127 		return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
128 	}
129 	
130 	@Override
131 	public int getDepartmentCount(String department) {
132 		Criteria crit = new Criteria();
133 		crit.addEqualTo("dept", department);
134 		Query query = QueryFactory.newQuery(Department.class, crit);
135 		return this.getPersistenceBrokerTemplate().getCount(query);
136 	}
137 	
138 	@Override
139 	public List<Department> getDepartments(String department) {
140 		List<Department> results = new ArrayList<Department>();
141 		Criteria crit = new Criteria();
142 		crit.addEqualTo("dept", department);
143 		crit.addEqualTo("active", true);
144 		Query query = QueryFactory.newQuery(Department.class, crit);
145 		results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
146 		return results;
147 	}
148 }