1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 .build();
37
38 @Override
39 public void saveOrUpdate(Department dept) {
40 this.getPersistenceBrokerTemplate().store(dept);
41 }
42
43 @Override
44 public Department getDepartment(String department, Date asOfDate) {
45 Criteria root = new Criteria();
46
47 root.addEqualTo("dept", department);
48 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
49 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
50
51 Criteria activeFilter = new Criteria();
52 activeFilter.addEqualTo("active", true);
53 root.addAndCriteria(activeFilter);
54
55 Query query = QueryFactory.newQuery(Department.class, root);
56
57 Department d = (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
58
59 return d;
60 }
61
62 @Override
63 public List<Department> getDepartments(String location, Date asOfDate) {
64 Criteria root = new Criteria();
65
66 root.addEqualTo("location", location);
67 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(Department.class, asOfDate, EQUAL_TO_FIELDS, false));
68 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
69
70 Criteria activeFilter = new Criteria();
71 activeFilter.addEqualTo("active", true);
72 root.addAndCriteria(activeFilter);
73
74
75 Query query = QueryFactory.newQuery(Department.class, root);
76
77 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
78 List<Department> d = new ArrayList<Department>(c.size());
79 d.addAll(c);
80
81 return d;
82 }
83
84 @Override
85 @SuppressWarnings("unchecked")
86 public List<Department> getDepartments(String dept, String location, String departmentDescr, String active, String showHistory) {
87 List<Department> results = new ArrayList<Department>();
88
89 Criteria root = new Criteria();
90
91 if (StringUtils.isNotBlank(dept)) {
92 root.addLike("dept", dept);
93 }
94
95 if (StringUtils.isNotBlank(location)) {
96 root.addLike("location", location);
97 }
98
99 if (StringUtils.isNotBlank(departmentDescr)) {
100 root.addLike("description", departmentDescr);
101 }
102
103 if (StringUtils.isNotBlank(active)) {
104 Criteria activeFilter = new Criteria();
105 if (StringUtils.equals(active, "Y")) {
106 activeFilter.addEqualTo("active", true);
107 } else if (StringUtils.equals(active, "N")) {
108 activeFilter.addEqualTo("active", false);
109 }
110 root.addAndCriteria(activeFilter);
111 }
112
113 if (StringUtils.equals(showHistory, "N")) {
114 ImmutableList<String> fields = new ImmutableList.Builder<String>()
115 .add("dept")
116 .add("location")
117 .add("description")
118 .build();
119 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQueryWithoutFilter(Department.class, EQUAL_TO_FIELDS, false));
120 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(Department.class, EQUAL_TO_FIELDS, false));
121 }
122
123 Query query = QueryFactory.newQuery(Department.class, root);
124 results.addAll(getPersistenceBrokerTemplate().getCollectionByQuery(query));
125
126 return results;
127 }
128
129 @Override
130 public Department getDepartment(String hrDeptId) {
131 Criteria crit = new Criteria();
132 crit.addEqualTo("hrDeptId", hrDeptId);
133
134 Query query = QueryFactory.newQuery(Department.class, crit);
135 return (Department)this.getPersistenceBrokerTemplate().getObjectByQuery(query);
136 }
137
138 @Override
139 public List<Department> getDepartmentByLocation(String location) {
140 Criteria crit = new Criteria();
141 crit.addEqualTo("location", location);
142
143 Query query = QueryFactory.newQuery(Department.class, crit);
144
145 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
146 List<Department> d = new ArrayList<Department>(c.size());
147 d.addAll(c);
148
149 return d;
150 }
151
152 @Override
153 public int getDepartmentCount(String department) {
154 Criteria crit = new Criteria();
155 crit.addEqualTo("dept", department);
156 Query query = QueryFactory.newQuery(Department.class, crit);
157 return this.getPersistenceBrokerTemplate().getCount(query);
158 }
159 }