1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.impl.role;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.ojb.broker.query.Criteria;
21 import org.apache.ojb.broker.query.Query;
22 import org.apache.ojb.broker.query.QueryFactory;
23 import org.apache.ojb.broker.query.ReportQueryByCriteria;
24 import org.joda.time.DateTime;
25 import org.kuali.rice.core.api.criteria.Predicate;
26 import org.kuali.rice.core.api.criteria.PredicateFactory;
27 import org.kuali.rice.core.api.criteria.PredicateUtils;
28 import org.kuali.rice.core.api.criteria.QueryByCriteria;
29 import org.kuali.rice.core.api.membership.MemberType;
30 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
31 import org.kuali.rice.kim.api.KimConstants;
32 import org.kuali.rice.kim.api.group.GroupMember;
33 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
34 import org.kuali.rice.kim.api.identity.principal.Principal;
35 import org.kuali.rice.kim.api.permission.Permission;
36 import org.kuali.rice.kim.api.responsibility.Responsibility;
37 import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults;
38 import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
39 import org.kuali.rice.kim.api.role.Role;
40 import org.kuali.rice.kim.api.role.RoleMember;
41 import org.kuali.rice.kim.api.role.RoleMembership;
42 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
43 import org.kuali.rice.kim.impl.KIMPropertyConstants;
44 import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
45 import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
46
47 import java.sql.Timestamp;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.HashMap;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Map.Entry;
54
55 import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
56
57 public class RoleDaoOjb extends PlatformAwareDaoBaseOjb implements RoleDao {
58
59
60
61
62
63
64 private void addSubCriteriaBasedOnRoleQualification(Criteria c, Map<String, String> qualification) {
65 if (qualification != null && CollectionUtils.isNotEmpty(qualification.keySet())) {
66 for (Map.Entry<String, String> qualifier : qualification.entrySet()) {
67 Criteria subCrit = new Criteria();
68 if (StringUtils.isNotEmpty(qualifier.getValue())) {
69 String value = (qualifier.getValue()).replace('*', '%');
70 subCrit.addLike("attributeValue", value);
71 subCrit.addEqualTo("kimAttributeId", qualifier.getKey());
72 subCrit.addEqualToField("assignedToId", Criteria.PARENT_QUERY_PREFIX + "id");
73 ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(RoleMemberAttributeDataBo.class, subCrit);
74 c.addExists(subQuery);
75 }
76 }
77 }
78 }
79
80 @SuppressWarnings("unchecked")
81 public List<RoleMemberBo> getRolePrincipalsForPrincipalIdAndRoleIds(Collection<String> roleIds, String principalId, Map<String, String> qualification) {
82
83 Criteria c = new Criteria();
84
85 if (roleIds != null) {
86 c.addIn(KIMPropertyConstants.RoleMember.ROLE_ID, roleIds);
87 }
88 if (principalId != null) {
89 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_ID, principalId);
90 }
91 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.PRINCIPAL.getCode());
92 addSubCriteriaBasedOnRoleQualification(c, qualification);
93
94 Query query = QueryFactory.newQuery(RoleMemberBo.class, c);
95 Collection<RoleMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
96 ArrayList<RoleMemberBo> results = new ArrayList<RoleMemberBo>(coll.size());
97 for (RoleMemberBo rm : coll) {
98 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
99 results.add(rm);
100 }
101 }
102 return results;
103 }
104
105 public List<GroupMember> getGroupPrincipalsForPrincipalIdAndGroupIds(Collection<String> groupIds, String principalId) {
106 List<String> groupIdValues = new ArrayList<String>();
107 List<GroupMember> groupPrincipals = new ArrayList<GroupMember>();
108 if (groupIds != null
109 && principalId == null) {
110 groupIdValues = new ArrayList<String>(groupIds);
111 } else if (principalId != null) {
112 groupIdValues = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
113 }
114 if (groupIdValues != null
115 && groupIdValues.size() > 0) {
116 Collection<GroupMember> groupMembers = KimApiServiceLocator.getGroupService().getMembers(groupIdValues);
117 for (GroupMember groupMembershipInfo : groupMembers) {
118 if (principalId != null) {
119 if (MemberType.PRINCIPAL.equals(groupMembershipInfo.getType())
120 && StringUtils.equals(principalId, groupMembershipInfo.getMemberId())
121 && groupMembershipInfo.isActive(new DateTime())) {
122 groupPrincipals.add(groupMembershipInfo);
123 }
124 } else {
125 groupPrincipals.add(groupMembershipInfo);
126 }
127 }
128 }
129 return groupPrincipals;
130 }
131
132 public List<GroupMember> getGroupMembers(Collection<String> groupIds) {
133 List<GroupMember> groupMembers = new ArrayList<GroupMember>();
134 if (groupIds != null) {
135 List<String> groupIdValues = new ArrayList<String>(groupIds);
136
137 if (groupIdValues.size() > 0) {
138
139 Collection<GroupMember> groupMemberships = KimApiServiceLocator.getGroupService().getMembers(groupIdValues);
140
141 if (!CollectionUtils.isEmpty(groupMemberships)) {
142 for (GroupMember groupMembershipInfo : groupMemberships) {
143 if (MemberType.GROUP.equals(groupMembershipInfo.getType())
144 && groupMembershipInfo.isActive(new DateTime())) {
145 groupMembers.add(groupMembershipInfo);
146 }
147 }
148 }
149 }
150 }
151 return groupMembers;
152 }
153
154 @SuppressWarnings("unchecked")
155 public List<RoleMemberBo> getRoleGroupsForGroupIdsAndRoleIds(Collection<String> roleIds, Collection<String> groupIds, Map<String, String> qualification) {
156 Criteria c = new Criteria();
157 if (roleIds != null && !roleIds.isEmpty()) {
158 c.addIn(KIMPropertyConstants.RoleMember.ROLE_ID, roleIds);
159 }
160 if (groupIds != null && !groupIds.isEmpty()) {
161 c.addIn(KIMPropertyConstants.RoleMember.MEMBER_ID, groupIds);
162 }
163 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.GROUP.getCode());
164 addSubCriteriaBasedOnRoleQualification(c, qualification);
165
166 Query query = QueryFactory.newQuery(RoleMemberBo.class, c);
167 Collection<RoleMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
168 ArrayList<RoleMemberBo> results = new ArrayList<RoleMemberBo>(coll.size());
169 for (RoleMemberBo rm : coll) {
170 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
171 results.add(rm);
172 }
173 }
174 return results;
175 }
176
177 @SuppressWarnings("unchecked")
178 public Map<String, DelegateTypeBo> getDelegationImplMapFromRoleIds(Collection<String> roleIds) {
179 Map<String, DelegateTypeBo> results = new HashMap<String, DelegateTypeBo>();
180 if (CollectionUtils.isNotEmpty(roleIds)) {
181 Criteria c = new Criteria();
182 c.addIn(KIMPropertyConstants.Delegation.ROLE_ID, roleIds);
183 c.addEqualTo(KIMPropertyConstants.Delegation.ACTIVE, Boolean.TRUE);
184 Query query = QueryFactory.newQuery(DelegateTypeBo.class, c);
185 Collection<DelegateTypeBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
186 for (DelegateTypeBo delegateBo : coll) {
187 results.put(delegateBo.getDelegationId(), delegateBo);
188 }
189 }
190 return results;
191 }
192
193 @SuppressWarnings("unchecked")
194 public List<DelegateTypeBo> getDelegationBosForRoleIds(Collection<String> roleIds) {
195 List<DelegateTypeBo> results = new ArrayList<DelegateTypeBo>();
196 if (roleIds != null && !roleIds.isEmpty()) {
197 Criteria c = new Criteria();
198 c.addIn(KIMPropertyConstants.Delegation.ROLE_ID, roleIds);
199 c.addEqualTo(KIMPropertyConstants.Delegation.ACTIVE, Boolean.TRUE);
200 Query query = QueryFactory.newQuery(DelegateTypeBo.class, c);
201 Collection<DelegateTypeBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
202 for (DelegateTypeBo delegateBo : coll) {
203 results.add(delegateBo);
204 }
205 }
206 return results;
207 }
208
209 @SuppressWarnings("unchecked")
210 public List<DelegateMemberBo> getDelegationPrincipalsForPrincipalIdAndDelegationIds(
211 Collection<String> delegationIds, String principalId) {
212 Criteria c = new Criteria();
213
214 if (principalId != null) {
215 c.addEqualTo(KIMPropertyConstants.DelegationMember.MEMBER_ID, principalId);
216 }
217 c.addEqualTo(KIMPropertyConstants.DelegationMember.MEMBER_TYPE_CODE, MemberType.PRINCIPAL.getCode());
218 if (delegationIds != null && !delegationIds.isEmpty()) {
219 c.addIn(KIMPropertyConstants.DelegationMember.DELEGATION_ID, delegationIds);
220 }
221 Query query = QueryFactory.newQuery(DelegateMemberBo.class, c);
222 Collection<DelegateMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
223 ArrayList<DelegateMemberBo> results = new ArrayList<DelegateMemberBo>(coll.size());
224 for (DelegateMemberBo rm : coll) {
225 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
226 results.add(rm);
227 }
228 }
229 return results;
230 }
231
232 @SuppressWarnings("unchecked")
233 public List<DelegateMemberBo> getDelegationGroupsForGroupIdsAndDelegationIds(
234 Collection<String> delegationIds, List<String> groupIds) {
235 Criteria c = new Criteria();
236 if (delegationIds != null && !delegationIds.isEmpty()) {
237 c.addIn(KIMPropertyConstants.DelegationMember.DELEGATION_ID, delegationIds);
238 }
239 if (groupIds != null && !groupIds.isEmpty()) {
240 c.addIn(KIMPropertyConstants.DelegationMember.MEMBER_ID, groupIds);
241 }
242 c.addEqualTo(KIMPropertyConstants.DelegationMember.MEMBER_TYPE_CODE, MemberType.GROUP.getCode());
243 Query query = QueryFactory.newQuery(DelegateMemberBo.class, c);
244 Collection<DelegateMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
245 ArrayList<DelegateMemberBo> results = new ArrayList<DelegateMemberBo>(coll.size());
246 for (DelegateMemberBo rm : coll) {
247 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
248 results.add(rm);
249 }
250 }
251 return results;
252 }
253
254 @SuppressWarnings("unchecked")
255 public List<RoleMemberBo> getRoleMembersForRoleIds(Collection<String> roleIds, String memberTypeCode, Map<String, String> qualification) {
256 Criteria c = new Criteria();
257
258 if (roleIds != null && !roleIds.isEmpty()) {
259 c.addIn(KIMPropertyConstants.RoleMember.ROLE_ID, roleIds);
260 }
261 if (memberTypeCode != null) {
262 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, memberTypeCode);
263 }
264 addSubCriteriaBasedOnRoleQualification(c, qualification);
265
266 Query query = QueryFactory.newQuery(RoleMemberBo.class, c);
267 Collection<RoleMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
268 ArrayList<RoleMemberBo> results = new ArrayList<RoleMemberBo>(coll.size());
269 for (RoleMemberBo rm : coll) {
270 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
271 results.add(rm);
272 }
273 }
274 return results;
275 }
276
277 @SuppressWarnings("unchecked")
278 public List<RoleMemberBo> getRoleMembershipsForRoleIdsAsMembers(Collection<String> roleIds, Map<String, String> qualification) {
279 Criteria c = new Criteria();
280
281 if (roleIds != null && !roleIds.isEmpty()) {
282 c.addIn(KIMPropertyConstants.RoleMember.MEMBER_ID, roleIds);
283 }
284 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.ROLE.getCode());
285 addSubCriteriaBasedOnRoleQualification(c, qualification);
286
287 Query query = QueryFactory.newQuery(RoleMemberBo.class, c);
288 Collection<RoleMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
289 ArrayList<RoleMemberBo> results = new ArrayList<RoleMemberBo>(coll.size());
290 for (RoleMemberBo rm : coll) {
291 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
292 results.add(rm);
293 }
294 }
295 return results;
296 }
297
298 @SuppressWarnings("unchecked")
299 public List<RoleMemberBo> getRoleMembershipsForMemberId(String memberType, String memberId, Map<String, String> qualification) {
300 Criteria c = new Criteria();
301 List<RoleMemberBo> parentRoleMembers = new ArrayList<RoleMemberBo>();
302
303 if (StringUtils.isEmpty(memberId)
304 || StringUtils.isEmpty(memberType)) {
305 return parentRoleMembers;
306 }
307
308 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_ID, memberId);
309 c.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, memberType);
310 addSubCriteriaBasedOnRoleQualification(c, qualification);
311
312 Query query = QueryFactory.newQuery(RoleMemberBo.class, c);
313 Collection<RoleMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
314 ArrayList<RoleMemberBo> results = new ArrayList<RoleMemberBo>(coll.size());
315 for (RoleMemberBo rm : coll) {
316 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
317 results.add(rm);
318 }
319 }
320 return results;
321 }
322
323 @SuppressWarnings("unchecked")
324 public List<RoleMemberBo> getRoleMembersForRoleIdsWithFilters(Collection<String> roleIds, String principalId, Collection<String> groupIds, Map<String, String> qualification) {
325 Criteria c = new Criteria();
326
327 if (roleIds != null && !roleIds.isEmpty()) {
328 c.addIn(KIMPropertyConstants.RoleMember.ROLE_ID, roleIds);
329 }
330 Criteria orSet = new Criteria();
331 orSet.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.ROLE.getCode());
332 Criteria principalCheck = new Criteria();
333 if (principalId != null) {
334 principalCheck.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_ID, principalId);
335 }
336 principalCheck.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.PRINCIPAL.getCode());
337 orSet.addOrCriteria(principalCheck);
338 Criteria groupCheck = new Criteria();
339 if (groupIds != null && !groupIds.isEmpty()) {
340 groupCheck.addIn(KIMPropertyConstants.RoleMember.MEMBER_ID, groupIds);
341 }
342 groupCheck.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.GROUP.getCode());
343 orSet.addOrCriteria(groupCheck);
344 c.addAndCriteria(orSet);
345 addSubCriteriaBasedOnRoleQualification(c, qualification);
346
347 Query query = QueryFactory.newQuery(RoleMemberBo.class, c);
348 Collection<RoleMemberBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
349 ArrayList<RoleMemberBo> results = new ArrayList<RoleMemberBo>(coll.size());
350 for (RoleMemberBo rm : coll) {
351 if (rm.isActive(new Timestamp(System.currentTimeMillis()))) {
352 results.add(rm);
353 }
354 }
355 return results;
356 }
357
358 public List<RoleBo> getRoles(Map<String, String> fieldValues) {
359 Criteria criteria = new Criteria();
360 Map<String, Map<String, String>> criteriaMap = setupCritMaps(fieldValues);
361
362
363 Map<String, String> lookupNames = criteriaMap.get("lookupNames");
364 for (Map.Entry<String, String> entry : lookupNames.entrySet()) {
365 if (StringUtils.isNotBlank(entry.getValue())) {
366 if (!entry.getKey().equals(KIMPropertyConstants.Principal.PRINCIPAL_NAME)) {
367 addLikeToCriteria(criteria, entry.getKey(), entry.getValue());
368 } else {
369 List<String> roleIds = getRoleIdsForPrincipalName(entry.getValue());
370 if (roleIds != null && !roleIds.isEmpty()) {
371 criteria.addIn(KimConstants.PrimaryKeyConstants.ID, roleIds);
372 } else {
373
374 roleIds.add("NOTFOUND");
375 criteria.addIn(KimConstants.PrimaryKeyConstants.ID, roleIds);
376 }
377 }
378 }
379 }
380 if (!criteriaMap.get("attr").isEmpty()) {
381 String kimTypeId = null;
382 for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
383 if (entry.getKey().equals("kimTypeId")) {
384 kimTypeId = entry.getValue();
385 break;
386 }
387 }
388 setupAttrCriteria(criteria, criteriaMap.get("attr"), kimTypeId);
389 }
390 if (!criteriaMap.get("perm").isEmpty()) {
391 criteria.addExists(setupPermCriteria(criteriaMap.get("perm")));
392 }
393 if (!criteriaMap.get("resp").isEmpty()) {
394 criteria.addExists(setupRespCriteria(criteriaMap.get("resp")));
395 }
396 if (!criteriaMap.get("group").isEmpty()) {
397 criteria.addExists(setupGroupCriteria(criteriaMap.get("group")));
398 }
399
400 Query q = QueryFactory.newQuery(RoleBo.class, criteria);
401
402 return (List) getPersistenceBrokerTemplate().getCollectionByQuery(q);
403 }
404
405
406 private List<String> getPrincipalIdsForPrincipalName(String principalName) {
407 QueryByCriteria.Builder qb = QueryByCriteria.Builder.create();
408 qb.setPredicates(equal("principals.principalName", principalName));
409 List<EntityDefault> entities = KimApiServiceLocator.getIdentityService().findEntityDefaults(qb.build()).getResults();
410
411 List<String> principalIds = new ArrayList<String>();
412 for (EntityDefault entity : entities) {
413 for (Principal principal : entity.getPrincipals()) {
414 principalIds.add(principal.getPrincipalId());
415 }
416 }
417
418 return principalIds;
419
420 }
421
422 private List<String> getRoleIdsForPrincipalName(String value) {
423 String principalName = value.replace('*', '%');
424 List<String> roleIds = new ArrayList<String>();
425 Criteria memberSubCrit = new Criteria();
426 QueryByCriteria.Builder qb = QueryByCriteria.Builder.create();
427 qb.setPredicates(like("principals.principalName", principalName));
428 List<EntityDefault> entities = KimApiServiceLocator.getIdentityService().findEntityDefaults(qb.build()).getResults();
429 if (entities == null
430 || entities.size() == 0) {
431 return roleIds;
432 }
433
434 List<String> principalIds = new ArrayList<String>();
435 for (EntityDefault entity : entities) {
436 for (Principal principal : entity.getPrincipals()) {
437 principalIds.add(principal.getPrincipalId());
438 }
439 }
440 if (principalIds != null && !principalIds.isEmpty()) {
441 memberSubCrit.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.PRINCIPAL.getCode());
442 memberSubCrit.addIn(KIMPropertyConstants.RoleMember.MEMBER_ID, principalIds);
443
444 ReportQueryByCriteria memberSubQuery = QueryFactory.newReportQuery(RoleMemberBo.class, memberSubCrit);
445 for (RoleMemberBo roleMbr : (List<RoleMemberBo>) getPersistenceBrokerTemplate().getCollectionByQuery(memberSubQuery)) {
446 if (roleMbr.isActive(new Timestamp(System.currentTimeMillis())) && !roleIds.contains(roleMbr.getRoleId())) {
447 roleIds.add(roleMbr.getRoleId());
448 }
449 }
450 }
451
452 List<String> groupIds = new ArrayList<String>();
453 for (String principalId : principalIds) {
454 List<String> principalGroupIds = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(
455 principalId);
456 for (String groupId : principalGroupIds) {
457 if (!groupIds.contains(groupId)) {
458 groupIds.add(groupId);
459 }
460 }
461 }
462
463 if (groupIds != null && !groupIds.isEmpty()) {
464 Criteria grpRoleCrit = new Criteria();
465 grpRoleCrit.addEqualTo(KIMPropertyConstants.RoleMember.MEMBER_TYPE_CODE, MemberType.GROUP.getCode());
466 grpRoleCrit.addIn(KIMPropertyConstants.RoleMember.MEMBER_ID, groupIds);
467
468 ReportQueryByCriteria memberSubQuery = QueryFactory.newReportQuery(RoleMemberBo.class, grpRoleCrit);
469
470 for (RoleMemberBo roleMbr : (List<RoleMemberBo>) getPersistenceBrokerTemplate().getCollectionByQuery(memberSubQuery)) {
471 if (roleMbr.isActive(new Timestamp(System.currentTimeMillis())) && !roleIds.contains(roleMbr.getRoleId())) {
472 roleIds.add(roleMbr.getRoleId());
473 }
474 }
475 }
476
477 return roleIds;
478 }
479
480 private Map<String, Map<String, String>> setupCritMaps(Map<String, String> fieldValues) {
481
482 Map<String, Map<String, String>> critMap = new HashMap<String, Map<String, String>>();
483 List<String> permFieldName = new ArrayList<String>();
484 permFieldName.add("permName");
485 permFieldName.add("permNamespaceCode");
486 permFieldName.add("permTmplName");
487 permFieldName.add("permTmplNamespaceCode");
488 List<String> respFieldName = new ArrayList<String>();
489 respFieldName.add("respName");
490 respFieldName.add("respNamespaceCode");
491 respFieldName.add("respTmplName");
492 respFieldName.add("respTmplNamespaceCode");
493 Map<String, String> permFieldMap = new HashMap<String, String>();
494 Map<String, String> respFieldMap = new HashMap<String, String>();
495 Map<String, String> attrFieldMap = new HashMap<String, String>();
496 Map<String, String> groupFieldMap = new HashMap<String, String>();
497 Map<String, String> lookupNamesMap = new HashMap<String, String>();
498
499 for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
500 if (StringUtils.isNotBlank(entry.getValue())) {
501 String nameValue = entry.getValue();
502 if (permFieldName.contains(entry.getKey())) {
503 permFieldMap.put(entry.getKey(), nameValue);
504 } else if (respFieldName.contains(entry.getKey())) {
505 respFieldMap.put(entry.getKey(), nameValue);
506 } else if (entry.getKey().startsWith(KimConstants.AttributeConstants.GROUP_NAME)) {
507 groupFieldMap.put(entry.getKey(), nameValue);
508 } else if (entry.getKey().contains(".")) {
509 attrFieldMap.put(entry.getKey(), nameValue).replace('*', '%');
510 } else {
511 lookupNamesMap.put(entry.getKey(), nameValue);
512 }
513 }
514 }
515
516 critMap.put("perm", permFieldMap);
517 critMap.put("resp", respFieldMap);
518 critMap.put("group", groupFieldMap);
519 critMap.put("attr", attrFieldMap);
520 critMap.put("lookupNames", lookupNamesMap);
521 return critMap;
522 }
523
524
525 private void setupAttrCriteria(Criteria crit, Map<String, String> attrCrit, String kimTypeId) {
526 for (Map.Entry<String, String> entry : attrCrit.entrySet()) {
527 Criteria subCrit = new Criteria();
528 addLikeToCriteria(subCrit, "attributes.attributeValue", entry.getValue());
529 addEqualToCriteria(subCrit, "attributes.kimAttributeId", entry.getKey().substring(entry.getKey().indexOf(".") + 1, entry.getKey().length()));
530 addEqualToCriteria(subCrit, "attributes.kimTypeId", kimTypeId);
531 subCrit.addEqualToField("roleId", Criteria.PARENT_QUERY_PREFIX + "id");
532 crit.addExists(QueryFactory.newReportQuery(RoleMemberBo.class, subCrit));
533 }
534 }
535
536 private ReportQueryByCriteria setupPermCriteria(Map<String, String> permCrit) {
537
538 Map<String, String> actualCriteriaMap = new HashMap<String, String>();
539 for (Map.Entry<String, String> entry : permCrit.entrySet()) {
540 if (entry.getKey().equals("permTmplName") || entry.getKey().equals("permTmplNamespaceCode")) {
541 if (entry.getKey().equals("permTmplName")) {
542 actualCriteriaMap.put("template." + KimConstants.UniqueKeyConstants.PERMISSION_TEMPLATE_NAME, entry.getValue());
543 } else {
544 actualCriteriaMap.put("template." + KimConstants.UniqueKeyConstants.NAMESPACE_CODE, entry.getValue());
545 }
546 }
547
548 if (entry.getKey().equals("permName") || entry.getKey().equals("permNamespaceCode")) {
549 if (entry.getKey().equals("permName")) {
550 actualCriteriaMap.put(KimConstants.UniqueKeyConstants.PERMISSION_NAME, entry.getValue());
551 } else {
552 actualCriteriaMap.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, entry.getValue());
553 }
554 }
555 }
556
557 Predicate predicate = PredicateUtils.convertMapToPredicate(actualCriteriaMap);
558 List<Permission> permList = KimApiServiceLocator.getPermissionService().findPermissions(
559 QueryByCriteria.Builder.fromPredicates(predicate)).getResults();
560 List<String> roleIds = null;
561
562 if (permList != null && !permList.isEmpty()) {
563 roleIds = getRoleIdsForPermissions(permList);
564 }
565
566 if (roleIds == null || roleIds.isEmpty()) {
567 roleIds = new ArrayList<String>();
568 roleIds.add("-1");
569 }
570
571 Criteria memberSubCrit = new Criteria();
572 memberSubCrit.addIn("id", roleIds);
573 memberSubCrit.addEqualToField("id", Criteria.PARENT_QUERY_PREFIX + "id");
574 return QueryFactory.newReportQuery(RoleBo.class, memberSubCrit);
575
576 }
577
578 private List<String> getRoleIdsForPermissions(Collection<Permission> permissions) {
579 if ( permissions.isEmpty() ) {
580 return new ArrayList<String>(0);
581 }
582 List<String> permissionIds = new ArrayList<String>( permissions.size() );
583 for ( Permission permission : permissions ) {
584 permissionIds.add( permission.getId() );
585 }
586 Criteria c = new Criteria();
587 c.addIn( "permissionId", permissionIds );
588 c.addEqualTo( "active", true );
589
590 Query query = QueryFactory.newQuery( RolePermissionBo.class, c, true );
591 Collection<RolePermissionBo> coll = getPersistenceBrokerTemplate().getCollectionByQuery(query);
592 List<String> roleIds = new ArrayList<String>( coll.size() );
593 for ( RolePermissionBo rp : coll ) {
594 roleIds.add( rp.getRoleId() );
595 }
596 return roleIds;
597 }
598
599 private ReportQueryByCriteria setupRespCriteria(Map<String, String> respCrit) {
600 QueryByCriteria.Builder queryByCriteriaBuilder = QueryByCriteria.Builder.create();
601 Map<String, String> actualCriteriaMap = new HashMap<String, String>();
602 for (Map.Entry<String, String> entry : respCrit.entrySet()) {
603 if (entry.getKey().equals("respTmplName") || entry.getKey().equals("respTmplNamespaceCode")) {
604 if (entry.getKey().equals("respTmplName")) {
605 actualCriteriaMap.put("template." + KimConstants.UniqueKeyConstants.RESPONSIBILITY_TEMPLATE_NAME, entry.getValue());
606 } else {
607 actualCriteriaMap.put("template." + KimConstants.UniqueKeyConstants.NAMESPACE_CODE, entry.getValue());
608 }
609 }
610 if (entry.getKey().equals("respName") || entry.getKey().equals("respNamespaceCode")) {
611 if (entry.getKey().equals("respName")) {
612 actualCriteriaMap.put(KimConstants.UniqueKeyConstants.RESPONSIBILITY_NAME, entry.getValue());
613 } else {
614 actualCriteriaMap.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, entry.getValue());
615 }
616 }
617 }
618 Predicate predicate = PredicateUtils.convertMapToPredicate(actualCriteriaMap);
619 queryByCriteriaBuilder.setPredicates(predicate);
620
621 ResponsibilityService responsibilityService = KimApiServiceLocator.getResponsibilityService();
622 ResponsibilityQueryResults results = responsibilityService.findResponsibilities(queryByCriteriaBuilder.build());
623 List<Responsibility> responsibilities = results.getResults();
624
625
626 List<String> roleIds = new ArrayList<String>();
627 for (Responsibility responsibility : responsibilities) {
628 roleIds.addAll(responsibilityService.getRoleIdsForResponsibility(responsibility.getId()));
629 }
630
631 if (roleIds.isEmpty()) {
632 roleIds.add("-1");
633 }
634
635 Criteria memberSubCrit = new Criteria();
636 memberSubCrit.addIn("id", roleIds);
637 memberSubCrit.addEqualToField("id", Criteria.PARENT_QUERY_PREFIX + "id");
638 return QueryFactory.newReportQuery(RoleBo.class, memberSubCrit);
639
640 }
641
642 private ReportQueryByCriteria setupGroupCriteria(Map<String,String> groupCrit) {
643
644
645 final QueryByCriteria.Builder searchCrit = QueryByCriteria.Builder.create();
646 Map<String, String> actualCrit = new HashMap<String, String>();
647 for (Entry<String, String> entry : groupCrit.entrySet()) {
648 if (entry.getKey().equals(KimConstants.AttributeConstants.GROUP_NAME)) {
649 actualCrit.put(KimConstants.AttributeConstants.NAME, entry.getValue());
650 } else {
651 actualCrit.put(KimConstants.AttributeConstants.NAMESPACE_CODE, entry.getValue());
652 }
653 }
654
655 Criteria crit = new Criteria();
656 Predicate predicate = PredicateUtils.convertMapToPredicate(actualCrit);
657 searchCrit.setPredicates(predicate);
658 List<String> groupIds = KimApiServiceLocator.getGroupService().findGroupIds(searchCrit.build());
659
660 if(groupIds == null || groupIds.isEmpty()){
661 groupIds = new ArrayList<String>();
662 groupIds.add("-1");
663 }
664 crit.addIn("memberId", groupIds);
665 crit.addEqualToField("roleId", Criteria.PARENT_QUERY_PREFIX + "id");
666
667 return QueryFactory.newReportQuery(RoleMemberBo.class, crit);
668
669 }
670
671 private void addLikeToCriteria(Criteria criteria, String propertyName, String propertyValue) {
672 String[] keyValues = getCaseInsensitiveValues(propertyName, propertyValue);
673 criteria.addLike(keyValues[0], keyValues[1]);
674 }
675
676 private void addEqualToCriteria(Criteria criteria, String propertyName, String propertyValue) {
677 String[] keyValues = getCaseInsensitiveValues(propertyName, propertyValue);
678 criteria.addEqualTo(keyValues[0], keyValues[1]);
679 }
680
681 private String[] getCaseInsensitiveValues(String propertyName, String propertyValue) {
682 String[] keyValues = new String[2];
683 keyValues[0] = propertyName == null ? "" : getDbPlatform().getUpperCaseFunction() + "(" + propertyName + ")";
684 keyValues[1] = propertyValue == null ? "" : propertyValue.toUpperCase();
685 return keyValues;
686 }
687
688 private boolean hasCoreRoleMemberCriteria(Map<String, String> fieldValues) {
689 return StringUtils.isNotEmpty(fieldValues.get(KimConstants.PrimaryKeyConstants.ID)) ||
690 StringUtils.isNotEmpty(fieldValues.get(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID)) ||
691 StringUtils.isNotEmpty(fieldValues.get(KimConstants.PrimaryKeyConstants.MEMBER_ID)) ||
692 StringUtils.isNotEmpty(fieldValues.get(KIMPropertyConstants.KimMember.MEMBER_TYPE_CODE)) ||
693 StringUtils.isNotEmpty(fieldValues.get(KIMPropertyConstants.KimMember.ACTIVE_FROM_DATE)) ||
694 StringUtils.isNotEmpty(fieldValues.get(KIMPropertyConstants.KimMember.ACTIVE_TO_DATE));
695 }
696
697 private boolean hasExtraRoleMemberCriteria(Map<String, String> fieldValues) {
698 return StringUtils.isNotEmpty(fieldValues.get(KimConstants.KimUIConstants.MEMBER_NAME)) ||
699 StringUtils.isNotEmpty(fieldValues.get(KimConstants.KimUIConstants.MEMBER_NAMESPACE_CODE));
700 }
701
702 @SuppressWarnings("unchecked")
703 private List<RoleBo> getRoleMembersRoles(String memberNamespaceCode, String memberName) {
704 Criteria queryCriteria = new Criteria();
705 addEqualToCriteria(queryCriteria, KimConstants.UniqueKeyConstants.NAMESPACE_CODE, memberNamespaceCode);
706 addEqualToCriteria(queryCriteria, KimConstants.UniqueKeyConstants.NAME, memberName);
707 Query q = QueryFactory.newQuery(RoleBo.class, queryCriteria);
708 return (List<RoleBo>) getPersistenceBrokerTemplate().getCollectionByQuery(q);
709 }
710 }