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