Coverage Report - org.kuali.rice.kim.service.impl.PermissionServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PermissionServiceImpl
0%
0/268
0%
0/130
2.976
PermissionServiceImpl$1
0%
0/7
0%
0/2
2.976
 
 1  
 /*
 2  
  * Copyright 2007-2008 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.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.Collections;
 21  
 import java.util.Comparator;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import javax.jws.WebService;
 27  
 
 28  
 import org.apache.commons.lang.StringUtils;
 29  
 import org.apache.log4j.Logger;
 30  
 import org.kuali.rice.kim.bo.Role;
 31  
 import org.kuali.rice.kim.bo.impl.PermissionImpl;
 32  
 import org.kuali.rice.kim.bo.role.dto.KimPermissionInfo;
 33  
 import org.kuali.rice.kim.bo.role.dto.KimPermissionTemplateInfo;
 34  
 import org.kuali.rice.kim.bo.role.dto.PermissionAssigneeInfo;
 35  
 import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo;
 36  
 import org.kuali.rice.kim.bo.role.impl.KimPermissionImpl;
 37  
 import org.kuali.rice.kim.bo.role.impl.KimPermissionTemplateImpl;
 38  
 import org.kuali.rice.kim.bo.types.dto.AttributeDefinitionMap;
 39  
 import org.kuali.rice.kim.bo.types.dto.AttributeSet;
 40  
 import org.kuali.rice.kim.bo.types.dto.KimTypeInfo;
 41  
 import org.kuali.rice.kim.dao.KimPermissionDao;
 42  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 43  
 import org.kuali.rice.kim.service.PermissionService;
 44  
 import org.kuali.rice.kim.service.RoleService;
 45  
 import org.kuali.rice.kim.service.support.KimPermissionTypeService;
 46  
 import org.kuali.rice.kim.util.KIMWebServiceConstants;
 47  
 import org.kuali.rice.kim.util.KimConstants;
 48  
 import org.kuali.rice.kns.datadictionary.AttributeDefinition;
 49  
 import org.kuali.rice.kns.lookup.CollectionIncomplete;
 50  
 import org.kuali.rice.kns.lookup.Lookupable;
 51  
 import org.kuali.rice.kns.service.DataDictionaryService;
 52  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 53  
 import org.kuali.rice.kns.util.KNSPropertyConstants;
 54  
 
 55  
 /**
 56  
  * This is a description of what this class does - jonathan don't forget to fill this in. 
 57  
  * 
 58  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 59  
  *
 60  
  */
 61  
 @WebService(endpointInterface = KIMWebServiceConstants.PermissionService.INTERFACE_CLASS, serviceName = KIMWebServiceConstants.PermissionService.WEB_SERVICE_NAME, portName = KIMWebServiceConstants.PermissionService.WEB_SERVICE_PORT, targetNamespace = KIMWebServiceConstants.MODULE_TARGET_NAMESPACE)
 62  0
 public class PermissionServiceImpl extends PermissionServiceBase implements PermissionService {
 63  0
         private static final Logger LOG = Logger.getLogger( PermissionServiceImpl.class );
 64  
 
 65  
         private RoleService roleService;
 66  
         private KimPermissionDao permissionDao;
 67  
     private KimPermissionTypeService defaultPermissionTypeService;
 68  
         
 69  
         private List<KimPermissionTemplateInfo> allTemplates;
 70  
         
 71  
     // --------------------
 72  
     // Authorization Checks
 73  
     // --------------------
 74  
     
 75  
         protected KimPermissionTypeService getPermissionTypeService( String namespaceCode, String permissionTemplateName, String permissionName, String permissionId ) {
 76  0
                 StringBuffer cacheKey = new StringBuffer();
 77  0
                 if ( namespaceCode != null ) {
 78  0
                         cacheKey.append( namespaceCode );
 79  
                 }
 80  0
                 cacheKey.append( '|' );
 81  0
                 if ( permissionTemplateName != null ) {
 82  0
                         cacheKey.append( permissionTemplateName );
 83  
                 }
 84  0
                 cacheKey.append( '|' );
 85  0
                 if ( permissionName != null ) {
 86  0
                         cacheKey.append( permissionName );
 87  
                 }
 88  0
                 cacheKey.append( '|' );
 89  0
                 if ( permissionId != null ) {
 90  0
                         cacheKey.append( permissionId );
 91  
                 }
 92  0
                 String key = cacheKey.toString();
 93  0
                 KimPermissionTypeService service = getPermissionTypeServiceByNameCache().get(key);
 94  0
                 if ( service == null ) {
 95  0
                         KimPermissionTemplateImpl permTemplate = null;
 96  0
                         if ( permissionTemplateName != null ) {
 97  0
                                 List<KimPermissionImpl> perms = getPermissionImplsByTemplateName(namespaceCode, permissionTemplateName);
 98  0
                                 if ( !perms.isEmpty() ) {
 99  0
                                         permTemplate = perms.get(0).getTemplate();
 100  
                                 }
 101  0
                         } else if ( permissionName != null ) {
 102  0
                                 List<KimPermissionImpl> perms = getPermissionImplsByName(namespaceCode, permissionName); 
 103  0
                                 if ( !perms.isEmpty() ) {
 104  0
                                         permTemplate = perms.get(0).getTemplate();
 105  
                                 }
 106  0
                         } else if ( permissionId != null ) {
 107  0
                                 KimPermissionImpl perm = getPermissionImpl(permissionId);
 108  0
                                 if ( perm != null ) {
 109  0
                                         permTemplate = perm.getTemplate();
 110  
                                 }
 111  
                         }
 112  0
                         service = getPermissionTypeService( permTemplate );
 113  0
                         getPermissionTypeServiceByNameCache().put(key, service);
 114  
                 }
 115  0
                 return service;
 116  
         }
 117  
 
 118  
     protected KimPermissionTypeService getPermissionTypeService( KimPermissionTemplateImpl permissionTemplate ) {
 119  0
             if ( permissionTemplate == null ) {
 120  0
                     throw new IllegalArgumentException( "permissionTemplate may not be null" );
 121  
             }
 122  0
             KimTypeInfo kimType = KIMServiceLocator.getTypeInfoService().getKimType( permissionTemplate.getKimTypeId() );
 123  0
             String serviceName = kimType.getKimTypeServiceName();
 124  
             // if no service specified, return a default implementation
 125  0
             if ( StringUtils.isBlank( serviceName ) ) {
 126  0
                     return getDefaultPermissionTypeService();
 127  
             }
 128  
             try {
 129  0
                     Object service = KIMServiceLocator.getService( serviceName );
 130  
                     // if we have a service name, it must exist
 131  0
                     if ( service == null ) {
 132  0
                                 throw new RuntimeException("null returned for permission type service for service name: " + serviceName);
 133  
                     }
 134  
                     // whatever we retrieved must be of the correct type
 135  0
                     if ( !(service instanceof KimPermissionTypeService)  ) {
 136  0
                             throw new RuntimeException( "Service " + serviceName + " was not a KimPermissionTypeService.  Was: " + service.getClass().getName() );
 137  
                     }
 138  0
                     return (KimPermissionTypeService)service;
 139  0
             } catch( Exception ex ) {
 140  
                     // sometimes service locators throw exceptions rather than returning null, handle that
 141  0
                     throw new RuntimeException( "Error retrieving service: " + serviceName + " from the KIMServiceLocator.", ex );
 142  
             }
 143  
     }
 144  
     
 145  
     protected KimPermissionTypeService getDefaultPermissionTypeService() {
 146  0
             if ( defaultPermissionTypeService == null ) {
 147  0
                     defaultPermissionTypeService = (KimPermissionTypeService)KIMServiceLocator.getBean(DEFAULT_PERMISSION_TYPE_SERVICE);
 148  
             }
 149  0
                 return defaultPermissionTypeService;
 150  
         }
 151  
         
 152  
     /**
 153  
      * @see org.kuali.rice.kim.service.PermissionService#hasPermission(java.lang.String, String, java.lang.String, AttributeSet)
 154  
      */
 155  
     public boolean hasPermission(String principalId, String namespaceCode, String permissionName, AttributeSet permissionDetails) {
 156  0
             return isAuthorized( principalId, namespaceCode, permissionName, permissionDetails, null );
 157  
     }
 158  
 
 159  
     /**
 160  
      * @see org.kuali.rice.kim.service.PermissionService#isAuthorized( java.lang.String, String, java.lang.String, AttributeSet, AttributeSet)
 161  
      */
 162  
     public boolean isAuthorized(String principalId, String namespaceCode, String permissionName, AttributeSet permissionDetails, AttributeSet qualification ) {
 163  0
             List<String> roleIds = getRoleIdsForPermission( namespaceCode, permissionName, permissionDetails );
 164  0
             if ( roleIds.isEmpty() ) {
 165  0
                     return false;
 166  
             }
 167  0
                 return getRoleService().principalHasRole( principalId, roleIds, qualification );
 168  
     }
 169  
 
 170  
     /**
 171  
      * @see org.kuali.rice.kim.service.PermissionService#hasPermission(String, String, String, AttributeSet)
 172  
      */
 173  
     public boolean hasPermissionByTemplateName(String principalId, String namespaceCode, String permissionTemplateName, AttributeSet permissionDetails) {
 174  0
             return isAuthorizedByTemplateName( principalId, namespaceCode, permissionTemplateName, permissionDetails, null );
 175  
     }
 176  
 
 177  
     /**
 178  
      * @see org.kuali.rice.kim.service.PermissionService#isAuthorized( java.lang.String, String, java.lang.String, AttributeSet, AttributeSet)
 179  
      */
 180  
     public boolean isAuthorizedByTemplateName(String principalId, String namespaceCode, String permissionTemplateName, AttributeSet permissionDetails, AttributeSet qualification ) {
 181  0
             List<String> roleIds = getRoleIdsForPermissionTemplate( namespaceCode, permissionTemplateName, permissionDetails );
 182  0
             if ( roleIds.isEmpty() ) {
 183  0
                     return false;
 184  
             }
 185  0
             return getRoleService().principalHasRole( principalId, roleIds, qualification );
 186  
     }
 187  
 
 188  
     /**
 189  
      * @see org.kuali.rice.kim.service.PermissionService#getAuthorizedPermissions(String, String, String, AttributeSet, AttributeSet)
 190  
      */
 191  
     public List<KimPermissionInfo> getAuthorizedPermissions( String principalId, String namespaceCode, String permissionName, AttributeSet permissionDetails, AttributeSet qualification ) {
 192  
             // get all the permission objects whose name match that requested
 193  0
             List<KimPermissionImpl> permissions = getPermissionImplsByName( namespaceCode, permissionName );
 194  
             // now, filter the full list by the detail passed
 195  0
             List<KimPermissionInfo> applicablePermissions = getMatchingPermissions( permissions, permissionDetails );  
 196  0
             return getPermissionsForUser(principalId, applicablePermissions, qualification);
 197  
     }
 198  
 
 199  
     /**
 200  
      * @see org.kuali.rice.kim.service.PermissionService#getAuthorizedPermissionsByTemplateName(String, String, String, AttributeSet, AttributeSet)
 201  
      */
 202  
     public List<KimPermissionInfo> getAuthorizedPermissionsByTemplateName( String principalId, String namespaceCode, String permissionTemplateName, AttributeSet permissionDetails, AttributeSet qualification ) {
 203  
             // get all the permission objects whose name match that requested
 204  0
             List<KimPermissionImpl> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 205  
             // now, filter the full list by the detail passed
 206  0
             List<KimPermissionInfo> applicablePermissions = getMatchingPermissions( permissions, permissionDetails );  
 207  0
             return getPermissionsForUser(principalId, applicablePermissions, qualification);
 208  
     }
 209  
     
 210  
     /**
 211  
      * Checks the list of permissions against the principal's roles and returns a subset of the list which match.
 212  
      */
 213  
     protected List<KimPermissionInfo> getPermissionsForUser( String principalId, List<KimPermissionInfo> permissions, AttributeSet qualification ) {
 214  0
             ArrayList<KimPermissionInfo> results = new ArrayList<KimPermissionInfo>();
 215  0
             List<KimPermissionInfo> tempList = new ArrayList<KimPermissionInfo>(1);
 216  0
             for ( KimPermissionInfo perm : permissions ) {
 217  0
                     tempList.clear();
 218  0
                     tempList.add( perm );
 219  0
                     List<String> roleIds = permissionDao.getRoleIdsForPermissions( tempList );
 220  
                     // TODO: This could be made a little better by collecting the role IDs into
 221  
                     // a set and then processing the distinct list rather than a check
 222  
                     // for every permission
 223  0
                     if ( roleIds != null && !roleIds.isEmpty() ) {
 224  0
                             if ( getRoleService().principalHasRole( principalId, roleIds, qualification ) ) {
 225  0
                                     results.add( perm );
 226  
                             }
 227  
                     }
 228  0
             }
 229  
             
 230  0
             return results;            
 231  
     }
 232  
 
 233  
     protected Map<String,KimPermissionTypeService> getPermissionTypeServicesByTemplateId( Collection<KimPermissionImpl> permissions ) {
 234  0
             Map<String,KimPermissionTypeService> permissionTypeServices = new HashMap<String, KimPermissionTypeService>( permissions.size() );
 235  0
             for ( KimPermissionImpl perm : permissions ) {
 236  0
                     permissionTypeServices.put(perm.getTemplateId(), getPermissionTypeService( perm.getTemplate() ) );                                    
 237  
             }
 238  0
             return permissionTypeServices;
 239  
     }
 240  
     
 241  
     protected Map<String,List<KimPermissionInfo>> groupPermissionsByTemplate( Collection<KimPermissionImpl> permissions ) {
 242  0
             Map<String,List<KimPermissionInfo>> results = new HashMap<String,List<KimPermissionInfo>>();
 243  0
             for ( KimPermissionImpl perm : permissions ) {
 244  0
                     List<KimPermissionInfo> perms = results.get( perm.getTemplateId() );
 245  0
                     if ( perms == null ) {
 246  0
                             perms = new ArrayList<KimPermissionInfo>();
 247  0
                             results.put( perm.getTemplateId(), perms );
 248  
                     }
 249  0
                     perms.add( perm.toSimpleInfo() );
 250  0
             }
 251  0
             return results;
 252  
     }
 253  
     
 254  
         /**
 255  
      * Compare each of the passed in permissions with the given permissionDetails.  Those that
 256  
      * match are added to the result list.
 257  
      */
 258  
     protected List<KimPermissionInfo> getMatchingPermissions( List<KimPermissionImpl> permissions, AttributeSet permissionDetails ) {
 259  0
             List<KimPermissionInfo> applicablePermissions = new ArrayList<KimPermissionInfo>();            
 260  0
             if ( permissionDetails == null || permissionDetails.isEmpty() ) {
 261  
                     // if no details passed, assume that all match
 262  0
                     for ( KimPermissionImpl perm : permissions ) {
 263  0
                             applicablePermissions.add( perm.toSimpleInfo() );
 264  
                     }
 265  
             } else {
 266  
                     // otherwise, attempt to match the permission details
 267  
                     // build a map of the template IDs to the type services
 268  0
                     Map<String,KimPermissionTypeService> permissionTypeServices = getPermissionTypeServicesByTemplateId( permissions );
 269  
                     // build a map of permissions by template ID
 270  0
                     Map<String,List<KimPermissionInfo>> permissionMap = groupPermissionsByTemplate( permissions );
 271  
                     // loop over the different templates, matching all of the same template against the type
 272  
                     // service at once
 273  0
                     for ( String templateId : permissionMap.keySet() ) {
 274  0
                             KimPermissionTypeService permissionTypeService = permissionTypeServices.get( templateId );
 275  0
                             List<KimPermissionInfo> permissionList = permissionMap.get( templateId );
 276  0
                                 applicablePermissions.addAll( permissionTypeService.getMatchingPermissions( permissionDetails, permissionList ) );                                    
 277  0
                     }
 278  
             }
 279  0
             return applicablePermissions;
 280  
     }
 281  
 
 282  
     /**
 283  
      * @see org.kuali.rice.kim.service.PermissionService#getPermissionAssignees(String, String, AttributeSet, AttributeSet)
 284  
      */
 285  
     public List<PermissionAssigneeInfo> getPermissionAssignees( String namespaceCode, String permissionName, AttributeSet permissionDetails, AttributeSet qualification ) {
 286  0
             List<PermissionAssigneeInfo> results = new ArrayList<PermissionAssigneeInfo>();
 287  0
             List<String> roleIds = getRoleIdsForPermission( namespaceCode, permissionName, permissionDetails);
 288  0
             if ( roleIds.isEmpty() ) {
 289  0
                     return results;
 290  
             }
 291  0
             Collection<RoleMembershipInfo> roleMembers = getRoleService().getRoleMembers( roleIds, qualification );
 292  0
             for ( RoleMembershipInfo rm : roleMembers ) {
 293  0
                     if ( rm.getMemberTypeCode().equals( Role.PRINCIPAL_MEMBER_TYPE ) ) {
 294  0
                             results.add( new PermissionAssigneeInfo( rm.getMemberId(), null, rm.getDelegates() ) );
 295  0
                     } else if ( rm.getMemberTypeCode().equals( Role.GROUP_MEMBER_TYPE ) ) {
 296  0
                             results.add( new PermissionAssigneeInfo( null, rm.getMemberId(), rm.getDelegates() ) );
 297  
                     }
 298  
             }
 299  0
             return results;
 300  
     }
 301  
     
 302  
     public List<PermissionAssigneeInfo> getPermissionAssigneesForTemplateName( String namespaceCode, String permissionTemplateName, AttributeSet permissionDetails, AttributeSet qualification ) {
 303  0
             List<PermissionAssigneeInfo> results = new ArrayList<PermissionAssigneeInfo>();
 304  0
             List<String> roleIds = getRoleIdsForPermissionTemplate( namespaceCode, permissionTemplateName, permissionDetails);
 305  0
             if ( roleIds.isEmpty() ) {
 306  0
                     return results;
 307  
             }
 308  0
             Collection<RoleMembershipInfo> roleMembers = getRoleService().getRoleMembers( roleIds, qualification );
 309  0
             for ( RoleMembershipInfo rm : roleMembers ) {
 310  0
                     if ( rm.getMemberTypeCode().equals( Role.PRINCIPAL_MEMBER_TYPE ) ) {
 311  0
                             results.add( new PermissionAssigneeInfo( rm.getMemberId(), null, rm.getDelegates() ) );
 312  
                     } else { // a group membership
 313  0
                             results.add( new PermissionAssigneeInfo( null, rm.getMemberId(), rm.getDelegates() ) );
 314  
                     }
 315  
             }
 316  0
             return results;
 317  
     }
 318  
     
 319  
     public boolean isPermissionAssigned( String namespaceCode, String permissionName, AttributeSet permissionDetails ) {
 320  0
             return !getRoleIdsForPermission(namespaceCode, permissionName, permissionDetails).isEmpty();
 321  
     }
 322  
     
 323  
     public boolean isPermissionDefined( String namespaceCode, String permissionName, AttributeSet permissionDetails ) {
 324  
             // get all the permission objects whose name match that requested
 325  0
             List<KimPermissionImpl> permissions = getPermissionImplsByName( namespaceCode, permissionName );
 326  
             // now, filter the full list by the detail passed
 327  0
             return !getMatchingPermissions( permissions, permissionDetails ).isEmpty();   
 328  
     }
 329  
     
 330  
     public boolean isPermissionDefinedForTemplateName( String namespaceCode, String permissionTemplateName, AttributeSet permissionDetails ) {
 331  
             // get all the permission objects whose name match that requested
 332  0
             List<KimPermissionImpl> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 333  
             // now, filter the full list by the detail passed
 334  0
             return !getMatchingPermissions( permissions, permissionDetails ).isEmpty();   
 335  
     }
 336  
  
 337  
     public List<String> getRoleIdsForPermission( String namespaceCode, String permissionName, AttributeSet permissionDetails) {
 338  
             // get all the permission objects whose name match that requested
 339  0
             List<KimPermissionImpl> permissions = getPermissionImplsByName( namespaceCode, permissionName );
 340  
             // now, filter the full list by the detail passed
 341  0
             List<KimPermissionInfo> applicablePermissions = getMatchingPermissions( permissions, permissionDetails );            
 342  0
             List<String> roleIds = getRolesForPermissionsFromCache( applicablePermissions );
 343  0
             if ( roleIds == null ) {
 344  0
                     roleIds = permissionDao.getRoleIdsForPermissions( applicablePermissions );
 345  0
                     addRolesForPermissionsToCache( applicablePermissions, roleIds );
 346  
             }
 347  0
             return roleIds;            
 348  
     }
 349  
 
 350  
     protected List<String> getRoleIdsForPermissionTemplate( String namespaceCode, String permissionTemplateName, AttributeSet permissionDetails ) {
 351  
             // get all the permission objects whose name match that requested
 352  0
             List<KimPermissionImpl> permissions = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 353  
             // now, filter the full list by the detail passed
 354  0
             List<KimPermissionInfo> applicablePermissions = getMatchingPermissions( permissions, permissionDetails );
 355  0
             List<String> roleIds = getRolesForPermissionsFromCache( applicablePermissions );
 356  0
             if ( roleIds == null ) {
 357  0
                     roleIds = permissionDao.getRoleIdsForPermissions( applicablePermissions );
 358  0
                     addRolesForPermissionsToCache( applicablePermissions, roleIds );
 359  
             }
 360  0
             return roleIds;
 361  
     }
 362  
     
 363  
     public List<String> getRoleIdsForPermissions( List<KimPermissionInfo> permissions ) {
 364  0
             List<String> roleIds = getRolesForPermissionsFromCache( permissions );
 365  0
             if ( roleIds == null ) {
 366  0
                     roleIds = permissionDao.getRoleIdsForPermissions( permissions );
 367  0
                     addRolesForPermissionsToCache( permissions, roleIds );
 368  
             }
 369  0
             return roleIds;
 370  
     }
 371  
 
 372  
     // --------------------
 373  
     // Permission Data
 374  
     // --------------------
 375  
     
 376  
     /**
 377  
      * @see org.kuali.rice.kim.service.PermissionService#getPermission(java.lang.String)
 378  
      */
 379  
     public KimPermissionInfo getPermission(String permissionId) {
 380  0
             KimPermissionImpl impl = getPermissionImpl( permissionId );
 381  0
             if ( impl != null ) {
 382  0
                     return impl.toSimpleInfo();
 383  
             }
 384  0
             return null;
 385  
     }
 386  
     
 387  
     /**
 388  
      * @see org.kuali.rice.kim.service.PermissionService#getPermissionsByTemplateName(String, String)
 389  
      */
 390  
     public List<KimPermissionInfo> getPermissionsByTemplateName(String namespaceCode, String permissionTemplateName) {
 391  0
             List<KimPermissionImpl> impls = getPermissionImplsByTemplateName( namespaceCode, permissionTemplateName );
 392  0
             List<KimPermissionInfo> results = new ArrayList<KimPermissionInfo>( impls.size() );
 393  0
             for ( KimPermissionImpl impl : impls ) {
 394  0
                     results.add( impl.toSimpleInfo() );
 395  
             }
 396  0
             return results;
 397  
     }
 398  
 
 399  
         /**
 400  
      * @see org.kuali.rice.kim.service.PermissionService#getPermissionsByName(String, String)
 401  
      */
 402  
     public List<KimPermissionInfo> getPermissionsByName(String namespaceCode, String permissionName) {
 403  0
             List<KimPermissionImpl> impls = getPermissionImplsByName( namespaceCode, permissionName );
 404  0
             List<KimPermissionInfo> results = new ArrayList<KimPermissionInfo>( impls.size() );
 405  0
             for ( KimPermissionImpl impl : impls ) {
 406  0
                     results.add( impl.toSimpleInfo() );
 407  
             }
 408  0
             return results;
 409  
     }
 410  
     
 411  
     @SuppressWarnings("unchecked")
 412  
         protected KimPermissionImpl getPermissionImpl(String permissionId) {
 413  0
             if ( StringUtils.isBlank( permissionId ) ) {
 414  0
                     return null;
 415  
             }
 416  0
             String cacheKey = getPermissionImplByIdCacheKey(permissionId);
 417  0
             List<KimPermissionImpl> permissions = (List<KimPermissionImpl>)getCacheAdministrator().getFromCache(cacheKey);
 418  0
             if ( permissions == null ) {
 419  0
                     HashMap<String,Object> pk = new HashMap<String,Object>( 1 );
 420  0
                     pk.put( KimConstants.PrimaryKeyConstants.PERMISSION_ID, permissionId );
 421  0
                     permissions = Collections.singletonList( (KimPermissionImpl)getBusinessObjectService().findByPrimaryKey( KimPermissionImpl.class, pk ) );
 422  0
                     getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 423  
             }
 424  0
             return permissions.get( 0 );
 425  
     }
 426  
     
 427  
     @SuppressWarnings("unchecked")
 428  
         protected List<KimPermissionImpl> getPermissionImplsByTemplateName( String namespaceCode, String permissionTemplateName ) {
 429  0
             String cacheKey = getPermissionImplByTemplateNameCacheKey(namespaceCode, permissionTemplateName);
 430  0
             List<KimPermissionImpl> permissions = (List<KimPermissionImpl>)getCacheAdministrator().getFromCache(cacheKey);
 431  0
             if ( permissions == null ) {            
 432  0
                     HashMap<String,Object> pk = new HashMap<String,Object>( 3 );
 433  0
                     pk.put( "template.namespaceCode", namespaceCode );
 434  0
                     pk.put( "template.name", permissionTemplateName );
 435  0
                         pk.put( KNSPropertyConstants.ACTIVE, "Y" );
 436  0
                     permissions = (List<KimPermissionImpl>)getBusinessObjectService().findMatching( KimPermissionImpl.class, pk );
 437  0
                     getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 438  
             }
 439  0
             return permissions;
 440  
     }
 441  
 
 442  
     @SuppressWarnings("unchecked")
 443  
         protected List<KimPermissionImpl> getPermissionImplsByName( String namespaceCode, String permissionName ) {
 444  0
             String cacheKey = getPermissionImplByNameCacheKey(namespaceCode, permissionName);
 445  0
             List<KimPermissionImpl> permissions = (List<KimPermissionImpl>)getCacheAdministrator().getFromCache(cacheKey);
 446  0
             if ( permissions == null ) {
 447  0
                     HashMap<String,Object> pk = new HashMap<String,Object>( 3 );
 448  0
                     pk.put( KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode );
 449  0
                     pk.put( KimConstants.UniqueKeyConstants.PERMISSION_NAME, permissionName );
 450  0
                         pk.put( KNSPropertyConstants.ACTIVE, "Y" );
 451  0
                     permissions = (List<KimPermissionImpl>)getBusinessObjectService().findMatching( KimPermissionImpl.class, pk );
 452  0
                     getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 453  
             }
 454  0
             return permissions;
 455  
     }
 456  
 
 457  
     
 458  
     
 459  
     // --------------------
 460  
     // Support Methods
 461  
     // --------------------
 462  
         
 463  
         
 464  
         protected RoleService getRoleService() {
 465  0
                 if ( roleService == null ) {
 466  0
                         roleService = KIMServiceLocator.getRoleManagementService();                
 467  
                 }
 468  
 
 469  0
                 return roleService;
 470  
         }
 471  
 
 472  
         public void setRoleService(RoleService roleService) {
 473  0
                 this.roleService = roleService;
 474  0
         }
 475  
 
 476  
         public KimPermissionDao getPermissionDao() {
 477  0
                 return this.permissionDao;
 478  
         }
 479  
 
 480  
         public void setPermissionDao(KimPermissionDao permissionDao) {
 481  0
                 this.permissionDao = permissionDao;
 482  0
         }
 483  
 
 484  
         @SuppressWarnings("unchecked")
 485  
         public List<KimPermissionInfo> lookupPermissions(Map<String, String> searchCriteria, boolean unbounded ){
 486  0
                 Collection baseResults = null;
 487  0
                 Lookupable permissionLookupable = KNSServiceLocator.getLookupable(
 488  
                                 KNSServiceLocator.getBusinessObjectDictionaryService().getLookupableID(PermissionImpl.class)
 489  
                                 );
 490  0
                 permissionLookupable.setBusinessObjectClass(PermissionImpl.class);
 491  0
                 if ( unbounded ) {
 492  0
                     baseResults = permissionLookupable.getSearchResultsUnbounded( searchCriteria );
 493  
                 } else {
 494  0
                         baseResults = permissionLookupable.getSearchResults(searchCriteria);
 495  
                 }
 496  0
                 List<KimPermissionInfo> results = new ArrayList<KimPermissionInfo>( baseResults.size() );
 497  0
                 for ( KimPermissionImpl resp : (Collection<PermissionImpl>)baseResults ) {
 498  0
                         results.add( resp.toSimpleInfo() );
 499  
                 }
 500  0
                 if ( baseResults instanceof CollectionIncomplete ) {
 501  0
                         results = new CollectionIncomplete<KimPermissionInfo>( results, ((CollectionIncomplete<KimPermissionInfo>)baseResults).getActualSizeIfTruncated() ); 
 502  
                 }                
 503  0
                 return results;
 504  
         }
 505  
 
 506  
         public String getPermissionDetailLabel( String permissionId, String kimTypeId, String attributeName) {
 507  
             // get the type service for this permission
 508  0
                 KimPermissionTypeService typeService = getPermissionTypeService(null, null, null, permissionId);
 509  0
                 if ( typeService != null ) {
 510  
                         // ask the type service for the attribute definition for the given attribute name
 511  0
                         AttributeDefinitionMap attributes = typeService.getAttributeDefinitions( kimTypeId );
 512  0
                         String label = null;
 513  0
                         for ( AttributeDefinition attributeDef : attributes.values() ) {
 514  0
                                 if ( attributeDef.getName().equals(attributeName) ) {
 515  0
                                         label = attributeDef.getLabel();
 516  
                                 }
 517  
                         }
 518  
                         // return the attribute label
 519  0
                         if ( label != null ) {
 520  0
                                 return label;
 521  
                         } else {
 522  0
                                 return "Missing Def: " + attributeName;
 523  
                         }
 524  
                 } else {
 525  0
                         return "No Label: " + attributeName;
 526  
                 }
 527  
         }
 528  
         
 529  
         /**
 530  
          * @see org.kuali.rice.kim.service.PermissionService#getPermissionTemplate(java.lang.String)
 531  
          */
 532  
         public KimPermissionTemplateInfo getPermissionTemplate(String permissionTemplateId) {
 533  0
                 KimPermissionTemplateImpl impl = getBusinessObjectService().findBySinglePrimaryKey( KimPermissionTemplateImpl.class, permissionTemplateId );
 534  0
                 if ( impl != null ) {
 535  0
                         return impl.toSimpleInfo();
 536  
                 }
 537  0
                 return null;
 538  
         }
 539  
 
 540  
         /**
 541  
          * This overridden method ...
 542  
          * 
 543  
          * @see org.kuali.rice.kim.service.PermissionService#getPermissionTemplateByName(java.lang.String, java.lang.String)
 544  
          */
 545  
         public KimPermissionTemplateInfo getPermissionTemplateByName(String namespaceCode,
 546  
                         String permissionTemplateName) {
 547  0
                 Map<String,String> criteria = new HashMap<String,String>(2);
 548  0
                 criteria.put( KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode );
 549  0
                 criteria.put( KimConstants.UniqueKeyConstants.PERMISSION_TEMPLATE_NAME, permissionTemplateName );
 550  0
                 KimPermissionTemplateImpl impl = (KimPermissionTemplateImpl)getBusinessObjectService().findByPrimaryKey( KimPermissionTemplateImpl.class, criteria );
 551  0
                 if ( impl != null ) {
 552  0
                         return impl.toSimpleInfo();
 553  
                 }
 554  0
                 return null;
 555  
         }
 556  
         
 557  
         @SuppressWarnings("unchecked")
 558  
         public List<KimPermissionTemplateInfo> getAllTemplates() {
 559  0
                 if ( allTemplates == null ) {
 560  0
                         Map<String,String> criteria = new HashMap<String,String>(1);
 561  0
                         criteria.put( KNSPropertyConstants.ACTIVE, "Y" );
 562  0
                         List<KimPermissionTemplateImpl> impls = (List<KimPermissionTemplateImpl>)getBusinessObjectService().findMatching( KimPermissionTemplateImpl.class, criteria );
 563  0
                         List<KimPermissionTemplateInfo> infos = new ArrayList<KimPermissionTemplateInfo>( impls.size() );
 564  0
                         for ( KimPermissionTemplateImpl impl : impls ) {
 565  0
                                 infos.add( impl.toSimpleInfo() );
 566  
                         }
 567  0
                         Collections.sort(infos, new Comparator<KimPermissionTemplateInfo>() {
 568  
                                 public int compare(KimPermissionTemplateInfo tmpl1,
 569  
                                                 KimPermissionTemplateInfo tmpl2) {
 570  0
                                         int result = 0;
 571  0
                                         result = tmpl1.getNamespaceCode().compareTo(tmpl2.getNamespaceCode());
 572  0
                                         if ( result != 0 ) {
 573  0
                                                 return result;
 574  
                                         }
 575  0
                                         result = tmpl1.getName().compareTo(tmpl2.getName());
 576  0
                                         return result;
 577  
                                 }
 578  
                         });
 579  0
                         allTemplates = infos;
 580  
                 }
 581  0
                 return allTemplates;
 582  
         }
 583  
 
 584  
     
 585  
         
 586  
         private DataDictionaryService dataDictionaryService;
 587  
         protected DataDictionaryService getDataDictionaryService() {
 588  0
                 if(dataDictionaryService == null){
 589  0
                         dataDictionaryService = KNSServiceLocator.getDataDictionaryService();
 590  
                 }
 591  0
                 return dataDictionaryService;
 592  
         }
 593  
         
 594  
 
 595  
     public List<String> getRoleIdsForPermissionId(String permissionId) {
 596  0
         KimPermissionInfo permissionInfo = getPermission(permissionId);
 597  
 
 598  0
         List<KimPermissionInfo> applicablePermissions = new ArrayList<KimPermissionInfo>();
 599  0
         applicablePermissions.add(permissionInfo);
 600  
 
 601  0
         List<String> roleIds = getRolesForPermissionsFromCache(applicablePermissions);
 602  0
         if (roleIds == null) {
 603  0
             roleIds = permissionDao.getRoleIdsForPermissions(applicablePermissions);
 604  0
             addRolesForPermissionsToCache(applicablePermissions, roleIds);
 605  
         }
 606  
 
 607  0
         return roleIds;
 608  
     }
 609  
 
 610  
     public List<KimPermissionInfo> getPermissionsByNameIncludingInactive(String namespaceCode, String permissionName) {
 611  0
         List<KimPermissionImpl> impls = getPermissionImplsByNameIncludingInactive(namespaceCode, permissionName);
 612  0
         List<KimPermissionInfo> results = new ArrayList<KimPermissionInfo>(impls.size());
 613  0
         for (KimPermissionImpl impl : impls) {
 614  0
             results.add(impl.toSimpleInfo());
 615  
         }
 616  0
         return results;
 617  
     }
 618  
         
 619  
     @SuppressWarnings("unchecked")
 620  
     protected List<KimPermissionImpl> getPermissionImplsByNameIncludingInactive(String namespaceCode, String permissionName) {
 621  0
         String cacheKey = getPermissionImplByNameCacheKey(namespaceCode, permissionName + "inactive");
 622  0
         List<KimPermissionImpl> permissions = (List<KimPermissionImpl>) getCacheAdministrator().getFromCache(cacheKey);
 623  0
         if (permissions == null) {
 624  0
             HashMap<String, Object> pk = new HashMap<String, Object>(2);
 625  0
             pk.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode);
 626  0
             pk.put(KimConstants.UniqueKeyConstants.PERMISSION_NAME, permissionName);
 627  0
             permissions = (List<KimPermissionImpl>) getBusinessObjectService().findMatching(KimPermissionImpl.class, pk);
 628  0
             getCacheAdministrator().putInCache(cacheKey, permissions, PERMISSION_IMPL_CACHE_GROUP);
 629  
         }
 630  0
         return permissions;
 631  
     }
 632  
         
 633  
 }