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 com.google.common.collect.ImmutableList;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.Query;
27  import org.apache.ojb.broker.query.QueryFactory;
28  
29  import org.kuali.hr.core.util.OjbSubQueryUtil;
30  import org.kuali.hr.time.department.Department;
31  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
32  
33  public class DepartmentDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements DepartmentDao {
34      private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
35              .add("dept")
36              .add("location")
37              .build();
38  
39  	@Override
40  	public void saveOrUpdate(Department dept) {
41  		this.getPersistenceBrokerTemplate().store(dept);
42  	}
43  
44  	@Override
45  	public Department getDepartment(String department, Date asOfDate) {
46  		Criteria root = new Criteria();
47  
48  		root.addEqualTo("dept", department);
49          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
50          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
51  
52  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
53  		activeFilter.addEqualTo("active", true);
54  		root.addAndCriteria(activeFilter);
55  
56  		Query query = QueryFactory.newQuery(Department.class, root);
57  
58  		Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
59  
60  		return d;
61  	}
62  
63      @Override
64      public List<Department> getDepartments(String location, Date asOfDate) {
65  		Criteria root = new Criteria();
66  
67  		root.addEqualTo("location", location);
68          root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
69          root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
70  
71  		Criteria activeFilter = new Criteria(); // Inner Join For Activity
72  		activeFilter.addEqualTo("active", true);
73  		root.addAndCriteria(activeFilter);
74  
75  
76  		Query query = QueryFactory.newQuery(Department.class, root);
77  
78          Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
79  		List<Department> d = new ArrayList<Department>(c.size());
80          d.addAll(c);
81  
82  		return d;
83      }
84  
85  	@Override
86  	@SuppressWarnings("unchecked")
87      public List<Department> getDepartments(String dept, String location, String departmentDescr, String active, String showHistory) {
88          List<Department> results = new ArrayList<Department>();
89  
90          Criteria root = new Criteria();
91  
92          if (StringUtils.isNotBlank(dept)) {
93              root.addLike("dept", dept);
94          }
95  
96          if (StringUtils.isNotBlank(location)) {
97              root.addLike("location", location);
98          }
99  
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 }