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