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 ImmutableList<String> fields = new ImmutableList.Builder<String>()
241 .add("positionNumber")
242 .build();
243 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false));
244
245
246
247 Criteria activeFilter = new Criteria();
248 activeFilter.addEqualTo("active", true);
249 currentRecordCriteria.addAndCriteria(activeFilter);
250
251
252 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
253 return tkRole;
254 }
255
256 @Override
257 public TkRole getInactiveRolesByPosition(String positionNumber) {
258 Criteria currentRecordCriteria = new Criteria();
259 currentRecordCriteria.addEqualTo("positionNumber", positionNumber);
260
261 ImmutableList<String> fields = new ImmutableList.Builder<String>()
262 .add("positionNumber")
263 .build();
264 currentRecordCriteria.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, TKUtils.getCurrentDate(), EQUAL_TO_FIELDS, false));
265
266
267 Criteria activeFilter = new Criteria();
268 activeFilter.addEqualTo("active", false);
269 currentRecordCriteria.addAndCriteria(activeFilter);
270
271
272 TkRole tkRole = (TkRole) this.getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(TkRole.class, currentRecordCriteria));
273 return tkRole;
274 }
275
276
277 @Override
278 public List<TkRole> getPositionRolesForWorkArea(Long workArea, Date asOfDate) {
279 List<TkRole> roles = new ArrayList<TkRole>();
280
281 Criteria root = new Criteria();
282
283 root.addEqualTo("effectiveDate", OjbSubQueryUtil.getEffectiveDateSubQuery(TkRole.class, asOfDate, EQUAL_TO_FIELDS, false));
284 root.addEqualTo("timestamp", OjbSubQueryUtil.getTimestampSubQuery(TkRole.class, EQUAL_TO_FIELDS, false));
285
286
287 if (workArea != null)
288 root.addEqualTo("workArea", workArea);
289 root.addEqualTo("roleName", TkConstants.ROLE_TK_APPROVER);
290
291
292 Criteria activeFilter = new Criteria();
293 activeFilter.addEqualTo("active", true);
294 root.addAndCriteria(activeFilter);
295
296 Query query = QueryFactory.newQuery(TkRole.class, root);
297 Collection c = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
298
299 if (c != null) {
300 roles.addAll(c);
301 }
302
303 return roles;
304 }
305 }