1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.roles.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.collections.CollectionUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.ojb.broker.query.Criteria;
27 import org.apache.ojb.broker.query.Query;
28 import org.apache.ojb.broker.query.QueryFactory;
29 import org.apache.ojb.broker.query.ReportQueryByCriteria;
30 import org.kuali.hr.core.util.OjbSubQueryUtil;
31 import org.kuali.hr.job.Job;
32 import org.kuali.hr.time.roles.TkRole;
33 import org.kuali.hr.time.service.base.TkServiceLocator;
34 import org.kuali.hr.time.util.TKUtils;
35 import org.kuali.hr.time.util.TkConstants;
36 import org.kuali.hr.time.workarea.WorkArea;
37 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
38 import org.kuali.rice.krad.service.KRADServiceLocator;
39
40 public class TkRoleDaoSpringOjbImpl extends PlatformAwareDaoBaseOjb implements TkRoleDao {
41 private static final ImmutableList<String> EQUAL_TO_FIELDS = new ImmutableList.Builder<String>()
42 .add("roleName")
43 .add("positionNumber")
44 .add("principalId")
45 .add("workArea")
46 .add("department")
47 .add("chart")
48 .build();
49
50
51 public List<TkRole> findAllRoles(String principalId, Date asOfDate) {
52 return findRoles(principalId, asOfDate, null, null, null, null);
53 }
54
55 @SuppressWarnings("unchecked")
56 @Override
57 public List<TkRole> findPositionRoles(String positionNumber, Date asOfDate, String roleName, Long workArea, String department, String chart) {
58 List<TkRole> roles = new ArrayList<TkRole>();
59
60 Criteria root = new Criteria();
61 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
62 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
63
64
65 if (workArea != null)
66 root.addEqualTo("workArea", workArea);
67 if (StringUtils.isNotEmpty(department))
68 root.addEqualTo("department", department);
69 if (chart != null)
70 root.addEqualTo("chart", chart);
71 if (roleName != null)
72 root.addEqualTo("roleName", roleName);
73 if (positionNumber != null)
74 root.addEqualTo("positionNumber", positionNumber);
75
76
77 Criteria activeFilter = new Criteria();
78 activeFilter.addEqualTo("active", true);
79 root.addAndCriteria(activeFilter);
80
81 Query query = QueryFactory.newQuery(TkRole.class, root);
82 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
83
84 if (c != null) {
85 roles.addAll(c);
86 }
87
88 return roles;
89 }
90
91 @SuppressWarnings("unchecked")
92 @Override
93 public List<TkRole> findRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department, String chart) {
94 List<TkRole> roles = new ArrayList<TkRole>();
95
96 Criteria root = new Criteria();
97
98 if (StringUtils.isNotEmpty(principalId)) {
99 root.addEqualTo("principalId", principalId);
100 }
101
102 if (asOfDate != null) {
103 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
104 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
105 }
106
107 if (StringUtils.isNotEmpty(roleName)) {
108 root.addEqualTo("roleName", roleName);
109 }
110
111 if (workArea != null) {
112 root.addEqualTo("workArea", workArea);
113 }
114 if (StringUtils.isNotEmpty(department)) {
115 Criteria departmentCriteria = new Criteria();
116 departmentCriteria.addEqualTo("department", department);
117 Collection<WorkArea> collectionWorkAreas = TkServiceLocator.getWorkAreaService().getWorkAreas(department, asOfDate);
118 if (CollectionUtils.isNotEmpty(collectionWorkAreas)) {
119 List<Long> longWorkAreas = new ArrayList<Long>();
120 for(WorkArea cwa : collectionWorkAreas){
121 longWorkAreas.add(cwa.getWorkArea());
122 }
123 Criteria workAreaCriteria = new Criteria();
124 workAreaCriteria.addIn("workArea", longWorkAreas);
125 departmentCriteria.addOrCriteria(workAreaCriteria);
126 }
127 root.addAndCriteria(departmentCriteria);
128 }
129
130 if (StringUtils.isNotEmpty(chart)) {
131 root.addEqualTo("chart", chart);
132 }
133
134 root.addEqualTo("active", true);
135
136 Query query = QueryFactory.newQuery(TkRole.class, root);
137
138
139 query.setStartAtIndex(0);
140 query.setEndAtIndex(299);
141 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
142
143 if (c != null) {
144 roles.addAll(c);
145 }
146
147 if (StringUtils.isNotBlank(principalId)) {
148
149 List<Job> lstActiveJobs = TkServiceLocator.getJobService().getJobs(principalId, asOfDate);
150 for (Job job : lstActiveJobs) {
151 if (job.getPositionNumber() != null) {
152 List<TkRole> lstRoles = findPositionRoles(job.getPositionNumber(),
153 asOfDate, roleName, workArea, department, chart);
154 roles.addAll(lstRoles);
155 }
156 }
157 } else if (workArea != null) {
158 List<TkRole> lstPosRoles = getPositionRolesForWorkArea(workArea, asOfDate);
159 for (TkRole tkRole : lstPosRoles) {
160 if (!roles.contains(tkRole)) {
161 roles.add(tkRole);
162 }
163 }
164 }
165 return roles;
166 }
167
168 @SuppressWarnings("unchecked")
169 @Override
170 public List<TkRole> findInActiveRoles(String principalId, Date asOfDate, String roleName, Long workArea, String department, String chart) {
171 List<TkRole> roles = new ArrayList<TkRole>();
172
173 Criteria root = new Criteria();
174
175 if (StringUtils.isNotEmpty(principalId)) {
176 root.addEqualTo("principalId", principalId);
177 }
178
179 if (asOfDate != null) {
180 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
181 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
182 }
183
184
185 if (StringUtils.isNotEmpty(roleName)) {
186 root.addEqualTo("roleName", roleName);
187 }
188
189 if (workArea != null) {
190 root.addEqualTo("workArea", workArea);
191 }
192
193 if (StringUtils.isNotEmpty(department)) {
194 root.addEqualTo("department", department);
195 }
196
197 if (StringUtils.isNotEmpty(chart)) {
198 root.addEqualTo("chart", chart);
199 }
200
201 root.addEqualTo("active", false);
202
203 Query query = QueryFactory.newQuery(TkRole.class, root);
204 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
205
206 if (c != null) {
207 roles.addAll(c);
208 }
209
210 return roles;
211 }
212
213 @Override
214 public void saveOrUpdateRole(TkRole role) {
215 KRADServiceLocator.getBusinessObjectService().save(role);
216 }
217
218 @Override
219 public void saveOrUpdateRoles(List<TkRole> roles) {
220 if (roles != null) {
221 for (TkRole role : roles) {
222 saveOrUpdateRole(role);
223 }
224 }
225 }
226
227 @Override
228 public TkRole getRole(String tkRoleId) {
229 Criteria currentRecordCriteria = new Criteria();
230 currentRecordCriteria.addEqualTo("hrRolesId", tkRoleId);
231
232 return (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
233 }
234
235 @Override
236 public TkRole getRolesByPosition(String positionNumber) {
237 Criteria currentRecordCriteria = new Criteria();
238 currentRecordCriteria.addEqualTo("positionNumber", positionNumber);
239
240 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false));
241
242
243 Criteria activeFilter = new Criteria();
244 activeFilter.addEqualTo("active", true);
245 currentRecordCriteria.addAndCriteria(activeFilter);
246
247
248 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
249 return tkRole;
250 }
251
252 @Override
253 public TkRole getInactiveRolesByPosition(String positionNumber) {
254 Criteria currentRecordCriteria = new Criteria();
255 currentRecordCriteria.addEqualTo("positionNumber", positionNumber);
256
257 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false));
258
259
260 Criteria activeFilter = new Criteria();
261 activeFilter.addEqualTo("active", false);
262 currentRecordCriteria.addAndCriteria(activeFilter);
263
264
265 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
266 return tkRole;
267 }
268
269
270 @Override
271 public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) {
272 List<TkRole> roles = new ArrayList<TkRole>();
273
274 Criteria root = new Criteria();
275
276 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
277 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
278
279
280 if (workArea != null)
281 root.addEqualTo("workArea", workArea);
282 root.addEqualTo("roleName", TkConstants.ROLE_TK_APPROVER);
283
284
285 Criteria activeFilter = new Criteria();
286 activeFilter.addEqualTo("active", true);
287 root.addAndCriteria(activeFilter);
288
289 Query query = QueryFactory.newQuery(TkRole.class, root);
290 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
291
292 if (c != null) {
293 roles.addAll(c);
294 }
295
296 return roles;
297 }
298 }