Coverage Report - org.kuali.rice.kim.lookup.PermissionLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PermissionLookupableHelperServiceImpl
0%
0/126
0%
0/80
3.667
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.lookup;
 17  
 
 18  
 import com.google.common.collect.MapMaker;
 19  
 import org.apache.commons.beanutils.PropertyUtils;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.apache.log4j.Logger;
 22  
 import org.kuali.rice.kim.api.role.RoleService;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 import org.kuali.rice.kim.bo.impl.GenericPermission;
 25  
 import org.kuali.rice.kim.impl.permission.PermissionBo;
 26  
 import org.kuali.rice.kim.impl.role.RoleBo;
 27  
 import org.kuali.rice.kim.impl.role.RolePermissionBo;
 28  
 import org.kuali.rice.kim.util.KimConstants;
 29  
 import org.kuali.rice.kns.lookup.HtmlData;
 30  
 import org.kuali.rice.krad.bo.BusinessObject;
 31  
 import org.kuali.rice.krad.lookup.CollectionIncomplete;
 32  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 33  
 import org.kuali.rice.krad.service.LookupService;
 34  
 import org.kuali.rice.krad.util.KRADConstants;
 35  
 import org.kuali.rice.krad.util.UrlFactory;
 36  
 
 37  
 import java.util.ArrayList;
 38  
 import java.util.HashMap;
 39  
 import java.util.List;
 40  
 import java.util.Map;
 41  
 import java.util.Properties;
 42  
 import java.util.concurrent.ConcurrentMap;
 43  
 import java.util.concurrent.TimeUnit;
 44  
 
 45  
 /**
 46  
  * This is a description of what this class does - bhargavp don't forget to fill this in. 
 47  
  * 
 48  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 49  
  *
 50  
  */
 51  0
 public class PermissionLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl {
 52  
 
 53  
         private static final long serialVersionUID = -3578448525862270477L;
 54  
 
 55  0
         private static final Logger LOG = Logger.getLogger( PermissionLookupableHelperServiceImpl.class );
 56  
         
 57  
         private static LookupService lookupService;
 58  
         private static RoleService roleService;
 59  
 
 60  0
         private static boolean genericPermissionDocumentTypeNameLoaded = false;
 61  0
         private static String genericPermissionDocumentTypeName = null;
 62  
         
 63  
         /**
 64  
          * This overridden method ...
 65  
          * 
 66  
          * @see org.kuali.rice.krad.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject, java.util.List)
 67  
          */
 68  
         @SuppressWarnings("unchecked")
 69  
         @Override
 70  
         public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
 71  0
             List<HtmlData> htmlDataList = new ArrayList<HtmlData>();
 72  
             // convert the PermissionImpl class into a GenericPermission object
 73  0
             businessObject = new GenericPermission( (PermissionBo)businessObject );
 74  0
         if (allowsMaintenanceEditAction(businessObject)) {
 75  0
                 htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
 76  
         }
 77  0
         if (allowsMaintenanceNewOrCopyAction()) {
 78  0
                 htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames));
 79  
         }
 80  0
         return htmlDataList;
 81  
         }
 82  
 
 83  
     @SuppressWarnings("unchecked")
 84  
         protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
 85  0
         Properties parameters = new Properties();
 86  0
         parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
 87  0
         parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName());
 88  0
         parameters.put(KRADConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.PERMISSION_ID);
 89  0
         parameters.put(KRADConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.PERMISSION_ID);
 90  0
         if (StringUtils.isNotBlank(getReturnLocation())) {
 91  0
                 parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
 92  
                 }
 93  0
         parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames));
 94  0
         return UrlFactory.parameterizeUrl(KRADConstants.MAINTENANCE_ACTION, parameters);
 95  
     }
 96  
         
 97  
         /**
 98  
          * This overridden method ...
 99  
          * 
 100  
          * @see org.kuali.rice.krad.lookup.AbstractLookupableHelperServiceImpl#getMaintenanceDocumentTypeName()
 101  
          */
 102  
         @Override
 103  
         protected String getMaintenanceDocumentTypeName() {
 104  0
                 if ( !genericPermissionDocumentTypeNameLoaded ) {
 105  0
                         genericPermissionDocumentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(GenericPermission.class);
 106  0
                         genericPermissionDocumentTypeNameLoaded = true;
 107  
                 }
 108  0
                 return genericPermissionDocumentTypeName;
 109  
         }
 110  
                 
 111  
         /**
 112  
          * @see org.kuali.rice.krad.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
 113  
          */
 114  
         @Override
 115  
         protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) {
 116  0
                 Map<String, String> permissionSearchCriteria = buildSearchCriteria(searchCriteria);
 117  0
                 Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria);
 118  0
                 boolean permissionCriteriaEmpty = permissionSearchCriteria==null || permissionSearchCriteria.isEmpty();
 119  0
                 boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty();
 120  
                 
 121  0
                 List<PermissionBo> permissionSearchResultsCopy = new CollectionIncomplete<PermissionBo>(new ArrayList<PermissionBo>(), new Long(0));
 122  0
                 if(!permissionCriteriaEmpty && !roleCriteriaEmpty){
 123  0
                         permissionSearchResultsCopy = getCombinedSearchResults(permissionSearchCriteria, roleSearchCriteria, unbounded);
 124  0
                 } else if(permissionCriteriaEmpty && !roleCriteriaEmpty){
 125  0
                         permissionSearchResultsCopy = getPermissionsWithRoleSearchCriteria(roleSearchCriteria, unbounded);
 126  0
                 } else if(!permissionCriteriaEmpty && roleCriteriaEmpty){
 127  0
                         permissionSearchResultsCopy = getPermissionsWithPermissionSearchCriteria(permissionSearchCriteria, unbounded);
 128  0
                 } else if(permissionCriteriaEmpty && roleCriteriaEmpty){
 129  0
                         return getAllPermissions(unbounded);
 130  
                 }
 131  0
                 return permissionSearchResultsCopy;
 132  
         }
 133  
         
 134  
         private List<PermissionBo> getAllPermissions(boolean unbounded){
 135  0
                 List<PermissionBo> permissions = searchPermissions(new HashMap<String, String>(), unbounded);
 136  0
                 for(PermissionBo permission: permissions) {
 137  0
                         populateAssignedToRoles(permission);
 138  
         }
 139  0
                 return permissions;
 140  
         }
 141  
         
 142  
         private List<PermissionBo> getCombinedSearchResults(
 143  
                         Map<String, String> permissionSearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){
 144  0
                 List<PermissionBo> permissionSearchResults = searchPermissions(permissionSearchCriteria, unbounded);
 145  0
                 List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 146  0
                 List<PermissionBo> permissionsForRoleSearchResults = getPermissionsForRoleSearchResults(roleSearchResults, unbounded);
 147  0
                 List<PermissionBo> matchedPermissions = new CollectionIncomplete<PermissionBo>(
 148  
                         new ArrayList<PermissionBo>(), getActualSizeIfTruncated(permissionsForRoleSearchResults));
 149  0
                 if((permissionSearchResults!=null && !permissionSearchResults.isEmpty()) && 
 150  
                                 (permissionsForRoleSearchResults!=null && !permissionsForRoleSearchResults.isEmpty())){
 151  0
                         for(PermissionBo permission: permissionSearchResults){
 152  0
                                 for(PermissionBo permissionFromRoleSearch: permissionsForRoleSearchResults){
 153  0
                                         if(permissionFromRoleSearch.getId().equals(permission.getId())) {
 154  0
                                                 matchedPermissions.add(permissionFromRoleSearch);
 155  
                     }
 156  
                                 }
 157  
                         }
 158  
                 }
 159  0
                 return matchedPermissions;
 160  
         }
 161  
         
 162  
         @SuppressWarnings("unchecked")
 163  
         private List<PermissionBo> searchPermissions(Map<String, String> permissionSearchCriteria, boolean unbounded){
 164  0
                 return getPermissionsSearchResultsCopy(new ArrayList<PermissionBo>(getLookupService().findCollectionBySearchHelper(
 165  
                                 PermissionBo.class, permissionSearchCriteria, unbounded)));
 166  
 
 167  
         }
 168  
         
 169  
         private List<PermissionBo> getPermissionsWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){
 170  0
                 return getPermissionsForRoleSearchResults(searchRoles(roleSearchCriteria, unbounded), unbounded);
 171  
         }
 172  
 
 173  
         private List<PermissionBo> getPermissionsForRoleSearchResults(List<RoleBo> roleSearchResults, boolean unbounded){
 174  0
                 Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults);
 175  0
                 List<PermissionBo> permissions = new ArrayList<PermissionBo>();
 176  
                 List<PermissionBo> tempPermissions;
 177  0
                 List<String> collectedPermissionIds = new ArrayList<String>();
 178  
                 Map<String, String> permissionCriteria;
 179  
                 
 180  0
                 for(RoleBo roleImpl: roleSearchResults){
 181  0
                         permissionCriteria = new HashMap<String, String>();
 182  0
                         permissionCriteria.put("rolePermissions.roleId", roleImpl.getId());
 183  0
                         tempPermissions = searchPermissions(permissionCriteria, unbounded);
 184  0
                         actualSizeIfTruncated += getActualSizeIfTruncated(tempPermissions);
 185  0
                         for(PermissionBo permission: tempPermissions){
 186  0
                                 if(!collectedPermissionIds.contains(permission.getId())){
 187  0
                                         populateAssignedToRoles(permission);
 188  0
                                         collectedPermissionIds.add(permission.getId());
 189  0
                                         permissions.add(permission);
 190  
                                 }
 191  
                         }
 192  
                         //need to find roles that current role is a member of and build search string
 193  0
                         List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, roleImpl.getId());
 194  0
                         for (String parentRoleId : parentRoleIds) {
 195  0
                                 Map<String, String> roleSearchCriteria = new HashMap<String, String>();
 196  0
                                 roleSearchCriteria.put("roleId", parentRoleId);
 197  
                                 //get all parent role permissions and merge them with current permissions
 198  0
                                 permissions = mergePermissionLists(permissions, getPermissionsWithRoleSearchCriteria(roleSearchCriteria, unbounded));
 199  0
                         }
 200  0
                 }
 201  
                 
 202  0
                 return new CollectionIncomplete<PermissionBo>(permissions, actualSizeIfTruncated);
 203  
         }
 204  
         
 205  
 
 206  
         private void populateAssignedToRoles(PermissionBo permission){
 207  
                 Map<String, String> criteria;
 208  0
                 for(RolePermissionBo rolePermission: permission.getRolePermissions()){
 209  0
                         if ( rolePermission.isActive() ) {
 210  0
                                 criteria = new HashMap<String, String>();
 211  0
                                 criteria.put("id", rolePermission.getRoleId());
 212  
         //                        permission.getAssignedToRoles().add((RoleBo)getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria));
 213  0
                 RoleBo roleBo = getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria);
 214  
 
 215  0
                         }
 216  
                 }
 217  0
         }
 218  
         
 219  
         /* Since most queries will only be on the template namespace and name, cache the results for 30 seconds
 220  
          * so that queries against the details, which are done in memory, do not require repeated database trips.
 221  
          */
 222  
     private static final long PERM_CACHE_EXPIRE_SECONDS = 30L;
 223  0
     private static final ConcurrentMap<Map<String,String>,List<PermissionBo>> permResultCache = new MapMaker().expireAfterAccess(PERM_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).softValues().makeMap();
 224  
 
 225  
         
 226  
         private List<PermissionBo> getPermissionsWithPermissionSearchCriteria(
 227  
                         Map<String, String> permissionSearchCriteria, boolean unbounded){
 228  0
                 String detailCriteriaStr = permissionSearchCriteria.remove( DETAIL_CRITERIA );
 229  0
                 Map<String, String> detailCriteria = parseDetailCriteria(detailCriteriaStr);
 230  
 
 231  0
                 List<PermissionBo> cachedResult = permResultCache.get(permissionSearchCriteria);
 232  
                 List<PermissionBo> permissions;
 233  0
                 if ( cachedResult == null ) {
 234  0
                         permissions = searchPermissions(permissionSearchCriteria, unbounded);
 235  0
                         synchronized (permResultCache) {
 236  0
                                 permResultCache.put(permissionSearchCriteria, permissions);
 237  0
                         } 
 238  
                 } else {
 239  0
                         permissions = cachedResult;
 240  
                 }
 241  0
                 List<PermissionBo> filteredPermissions = new CollectionIncomplete<PermissionBo>(
 242  
                                 new ArrayList<PermissionBo>(), getActualSizeIfTruncated(permissions));
 243  0
                 for(PermissionBo perm: permissions){
 244  0
                         if ( detailCriteria.isEmpty() ) {
 245  0
                                 filteredPermissions.add(perm);
 246  0
                                 populateAssignedToRoles(perm);
 247  
                         } else {
 248  0
                                 if ( isMapSubset( new HashMap<String, String>(perm.getDetails()), detailCriteria ) ) {
 249  0
                                         filteredPermissions.add(perm);
 250  0
                                         populateAssignedToRoles(perm);
 251  
                                 }
 252  
                         }
 253  
                 }
 254  0
                 return filteredPermissions;
 255  
         }
 256  
         
 257  
         private List<PermissionBo> getPermissionsSearchResultsCopy(List<PermissionBo> permissionSearchResults){
 258  0
                 List<PermissionBo> permissionSearchResultsCopy = new CollectionIncomplete<PermissionBo>(
 259  
                         new ArrayList<PermissionBo>(), getActualSizeIfTruncated(permissionSearchResults));
 260  0
                 for(PermissionBo permissionImpl: permissionSearchResults){
 261  0
                         PermissionBo permissionCopy = new PermissionBo();
 262  
                         try{
 263  0
                                 PropertyUtils.copyProperties(permissionCopy, permissionImpl);
 264  0
                         } catch(Exception ex){
 265  0
                                 LOG.error( "Unable to copy properties from KimPermissionImpl to PermissionImpl, skipping.", ex );
 266  0
                                 continue;
 267  0
                         }
 268  0
                         permissionSearchResultsCopy.add(permissionCopy);
 269  0
                 }
 270  0
                 return permissionSearchResultsCopy;
 271  
         }
 272  
 
 273  
         /**
 274  
          * @return the lookupService
 275  
          */
 276  
         public LookupService getLookupService() {
 277  0
                 if ( lookupService == null ) {
 278  0
                         lookupService = KRADServiceLocatorWeb.getLookupService();
 279  
                 }
 280  0
                 return lookupService;
 281  
         }
 282  
 
 283  
         public RoleService getRoleService() {
 284  0
                 if (roleService == null) {
 285  0
                         roleService = KimApiServiceLocator.getRoleService();
 286  
                 }
 287  0
                 return roleService;
 288  
         }
 289  
 
 290  
         private List<PermissionBo> mergePermissionLists(List<PermissionBo> perm1, List<PermissionBo> perm2) {
 291  0
                 List<PermissionBo> returnList = new ArrayList<PermissionBo>(perm1);
 292  0
                 List<String> permissionIds = new ArrayList<String>(perm1.size());
 293  0
                 for (PermissionBo perm : returnList) {
 294  0
                         permissionIds.add(perm.getId());
 295  
                 }
 296  0
                 for (int i=0; i<perm2.size(); i++) {
 297  0
                     if (!permissionIds.contains(perm2.get(i).getId())) {
 298  0
                             returnList.add(perm2.get(i));
 299  
                     }
 300  
                 }
 301  0
                 return returnList;
 302  
         }
 303  
 }