Coverage Report - org.kuali.rice.kim.impl.responsibility.ResponsibilityLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponsibilityLookupableHelperServiceImpl
0%
0/129
0%
0/80
4.214
 
 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 responsibilities and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.impl.responsibility;
 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.kew.util.KEWConstants;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 import org.kuali.rice.kim.impl.role.RoleBo;
 25  
 import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
 26  
 import org.kuali.rice.kim.lookup.RoleMemberLookupableHelperServiceImpl;
 27  
 import org.kuali.rice.kim.util.KimConstants;
 28  
 import org.kuali.rice.kns.lookup.HtmlData;
 29  
 import org.kuali.rice.krad.bo.BusinessObject;
 30  
 import org.kuali.rice.krad.lookup.CollectionIncomplete;
 31  
 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
 32  
 import org.kuali.rice.krad.service.LookupService;
 33  
 import org.kuali.rice.krad.util.KRADConstants;
 34  
 import org.kuali.rice.krad.util.UrlFactory;
 35  
 
 36  
 import java.lang.reflect.InvocationTargetException;
 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  0
 public class ResponsibilityLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl {
 46  
 
 47  0
         private static final Logger LOG = Logger.getLogger( ResponsibilityLookupableHelperServiceImpl.class );
 48  
         
 49  
         private static final long serialVersionUID = -2882500971924192124L;
 50  
         
 51  
         private static LookupService lookupService;
 52  
 
 53  0
         private static boolean reviewResponsibilityDocumentTypeNameLoaded = false;
 54  0
         private static String reviewResponsibilityDocumentTypeName = null;
 55  
         
 56  
         @SuppressWarnings("unchecked")
 57  
         @Override
 58  
         public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
 59  0
             List<HtmlData> htmlDataList = new ArrayList<HtmlData>();
 60  
             // convert the UberResponsibilityBo class into a ReviewResponsibility object
 61  0
         if ( ((UberResponsibilityBo)businessObject).getTemplate().getName().equals( KEWConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME ) ) {
 62  0
                 ReviewResponsibilityBo reviewResp = new ReviewResponsibilityBo( (UberResponsibilityBo)businessObject );
 63  0
                 businessObject = reviewResp;
 64  0
                 if (allowsMaintenanceEditAction(businessObject)) {
 65  0
                         htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
 66  
                 }
 67  0
                 if (allowsMaintenanceNewOrCopyAction()) {
 68  0
                         htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames));
 69  
                 }
 70  
         }
 71  0
         return htmlDataList;
 72  
         }
 73  
 
 74  
     @SuppressWarnings("unchecked")
 75  
         protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
 76  0
         Properties parameters = new Properties();
 77  0
         parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
 78  
         // TODO: why is this not using the businessObject parmeter's class?
 79  0
         parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName());
 80  0
         parameters.put(KRADConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID);
 81  0
         parameters.put(KRADConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID);
 82  0
         if (StringUtils.isNotBlank(getReturnLocation())) {
 83  0
                 parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
 84  
                 }
 85  0
         parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames));
 86  0
         return UrlFactory.parameterizeUrl(KRADConstants.MAINTENANCE_ACTION, parameters);
 87  
     }
 88  
         
 89  
         @Override
 90  
         protected String getMaintenanceDocumentTypeName() {
 91  0
                 if ( !reviewResponsibilityDocumentTypeNameLoaded ) {
 92  0
                         reviewResponsibilityDocumentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(ReviewResponsibilityBo.class);
 93  0
                         reviewResponsibilityDocumentTypeNameLoaded = true;
 94  
                 }
 95  0
                 return reviewResponsibilityDocumentTypeName;
 96  
         }
 97  
         
 98  
         @Override
 99  
         protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) {
 100  0
                 Map<String, String> responsibilitySearchCriteria = buildSearchCriteria(searchCriteria);
 101  0
                 Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria);
 102  0
                 boolean responsibilityCriteriaEmpty = responsibilitySearchCriteria==null || responsibilitySearchCriteria.isEmpty();
 103  0
                 boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty();
 104  
                 
 105  0
                 List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(new ArrayList<UberResponsibilityBo>(), new Long(0));
 106  0
                 if(!responsibilityCriteriaEmpty && !roleCriteriaEmpty){
 107  0
                         responsibilitySearchResultsCopy = getCombinedSearchResults(responsibilitySearchCriteria, roleSearchCriteria, unbounded);
 108  0
                 } else if(responsibilityCriteriaEmpty && !roleCriteriaEmpty){
 109  0
                         responsibilitySearchResultsCopy = getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded);
 110  0
                 } else if(!responsibilityCriteriaEmpty && roleCriteriaEmpty){
 111  0
                         responsibilitySearchResultsCopy = getResponsibilitiesWithResponsibilitySearchCriteria(responsibilitySearchCriteria, unbounded);
 112  0
                 } else if(responsibilityCriteriaEmpty && roleCriteriaEmpty){
 113  0
                         return getAllResponsibilities(unbounded);
 114  
                 }
 115  0
                 return responsibilitySearchResultsCopy;
 116  
         }
 117  
         
 118  
         private List<UberResponsibilityBo> getAllResponsibilities(boolean unbounded){
 119  0
                 List<UberResponsibilityBo> responsibilities = searchResponsibilities(new HashMap<String, String>(), unbounded);
 120  0
                 for(UberResponsibilityBo responsibility: responsibilities) {
 121  0
                         populateAssignedToRoles(responsibility);
 122  
         }
 123  0
                 return responsibilities;
 124  
         }
 125  
         
 126  
         private List<UberResponsibilityBo> getCombinedSearchResults(
 127  
                         Map<String, String> responsibilitySearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){
 128  0
                 List<UberResponsibilityBo> responsibilitySearchResults = searchResponsibilities(responsibilitySearchCriteria, unbounded);
 129  0
                 List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 130  0
                 List<UberResponsibilityBo> responsibilitiesForRoleSearchResults = getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded);
 131  0
                 List<UberResponsibilityBo> matchedResponsibilities = new CollectionIncomplete<UberResponsibilityBo>(
 132  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitiesForRoleSearchResults));
 133  0
                 if((responsibilitySearchResults!=null && !responsibilitySearchResults.isEmpty()) && 
 134  
                                 (responsibilitiesForRoleSearchResults!=null && !responsibilitiesForRoleSearchResults.isEmpty())){
 135  0
                         for(UberResponsibilityBo responsibility: responsibilitySearchResults){
 136  0
                                 for(UberResponsibilityBo responsibilityFromRoleSearch: responsibilitiesForRoleSearchResults){
 137  0
                                         if(responsibilityFromRoleSearch.getId().equals(responsibility.getId())) {
 138  0
                                                 matchedResponsibilities.add(responsibilityFromRoleSearch);
 139  
                     }
 140  
                                 }
 141  
                         }
 142  
                 }
 143  
 
 144  0
                 return matchedResponsibilities;
 145  
         }
 146  
         
 147  
         @SuppressWarnings("unchecked")
 148  
         private List<UberResponsibilityBo> searchResponsibilities(Map<String, String> responsibilitySearchCriteria, boolean unbounded){
 149  0
                 return getResponsibilitiesSearchResultsCopy((List<ResponsibilityBo>)
 150  
                                         getLookupService().findCollectionBySearchHelper(
 151  
                                                         ResponsibilityBo.class, responsibilitySearchCriteria, unbounded));
 152  
         }
 153  
         
 154  
         private List<UberResponsibilityBo> getResponsibilitiesWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){
 155  0
                 List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 156  0
                 return getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded);
 157  
         }
 158  
 
 159  
         private List<UberResponsibilityBo> getResponsibilitiesForRoleSearchResults(List<RoleBo> roleSearchResults, boolean unbounded){
 160  0
                 Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults);
 161  0
                 List<UberResponsibilityBo> responsibilities = new ArrayList<UberResponsibilityBo>();
 162  
                 List<UberResponsibilityBo> tempResponsibilities;
 163  0
                 List<String> collectedResponsibilityIds = new ArrayList<String>();
 164  
                 Map<String, String> responsibilityCriteria;
 165  
                 
 166  0
                 for(RoleBo roleImpl: roleSearchResults){
 167  0
                         responsibilityCriteria = new HashMap<String, String>();
 168  0
                         responsibilityCriteria.put("roleResponsibilities.roleId", roleImpl.getId());
 169  0
                         tempResponsibilities = searchResponsibilities(responsibilityCriteria, unbounded);
 170  0
                         actualSizeIfTruncated += getActualSizeIfTruncated(tempResponsibilities);
 171  0
                         for(UberResponsibilityBo responsibility: tempResponsibilities){
 172  0
                                 if(!collectedResponsibilityIds.contains(responsibility.getId())){
 173  0
                                         populateAssignedToRoles(responsibility);
 174  0
                                         collectedResponsibilityIds.add(responsibility.getId());
 175  0
                                         responsibilities.add(responsibility);
 176  
                                 }
 177  
                                 //need to find roles that current role is a member of and build search string
 178  0
                                 List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, roleImpl.getId());
 179  0
                                 for (String parentRoleId : parentRoleIds) {
 180  0
                                         Map<String, String> roleSearchCriteria = new HashMap<String, String>();
 181  0
                                         roleSearchCriteria.put("roleId", parentRoleId);
 182  
                                         //get all parent role permissions and merge them with current permissions
 183  0
                                         responsibilities = mergeResponsibilityLists(responsibilities, getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded));
 184  0
                                 }
 185  0
                         }
 186  
                 }
 187  0
                 return new CollectionIncomplete<UberResponsibilityBo>(responsibilities, actualSizeIfTruncated);
 188  
         }
 189  
 
 190  
         private void populateAssignedToRoles(UberResponsibilityBo responsibility){
 191  0
                 Map<String, String> criteria = new HashMap<String, String>();
 192  0
                 if ( responsibility.getAssignedToRoles().isEmpty() ) {
 193  0
                         for(RoleResponsibilityBo roleResponsibility: responsibility.getRoleResponsibilities()){
 194  0
                                 criteria.put(KimConstants.PrimaryKeyConstants.ID, roleResponsibility.getRoleId());
 195  0
                                 responsibility.getAssignedToRoles().add(getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria));
 196  
                         }
 197  
                 }
 198  0
         }
 199  
         
 200  
         /* Since most queries will only be on the template namespace and name, cache the results for 30 seconds
 201  
          * so that queries against the details, which are done in memory, do not require repeated database trips.
 202  
          */
 203  
     //TODO: use the concurrentMap properties rather than synchronized blocks
 204  
     //TODO: no copying list/bos?
 205  
     private static final long RESP_CACHE_EXPIRE_SECONDS = 30L;
 206  0
     private static final ConcurrentMap<Map<String,String>,List<UberResponsibilityBo>> respResultCache = new MapMaker().expireAfterAccess(RESP_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).softValues().makeMap();
 207  
 
 208  
         
 209  
         private List<UberResponsibilityBo> getResponsibilitiesWithResponsibilitySearchCriteria(Map<String, String> responsibilitySearchCriteria, boolean unbounded){
 210  0
                 String detailCriteriaStr = responsibilitySearchCriteria.remove( DETAIL_CRITERIA );
 211  0
                 Map<String, String> detailCriteria = parseDetailCriteria(detailCriteriaStr);
 212  0
                 List<UberResponsibilityBo> cachedResult = respResultCache.get(responsibilitySearchCriteria);
 213  0
                 List<UberResponsibilityBo> responsibilities = null;
 214  0
                 if ( cachedResult == null ) {
 215  0
                         responsibilities = searchResponsibilities(responsibilitySearchCriteria, unbounded);
 216  0
             synchronized( respResultCache ) {
 217  0
                                 respResultCache.put(responsibilitySearchCriteria, responsibilities );
 218  0
                         }
 219  
                 } else {
 220  0
                         responsibilities = cachedResult;
 221  
                 }
 222  0
                 List<UberResponsibilityBo> filteredResponsibilities = new CollectionIncomplete<UberResponsibilityBo>(
 223  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilities)); 
 224  0
                 for(UberResponsibilityBo responsibility: responsibilities){
 225  0
                         if ( detailCriteria.isEmpty() ) {
 226  0
                                 filteredResponsibilities.add(responsibility);
 227  0
                                 populateAssignedToRoles(responsibility);
 228  
                         } else {
 229  0
                                 if ( isMapSubset( new HashMap<String, String>(responsibility.getAttributes()), detailCriteria ) ) {
 230  0
                                         filteredResponsibilities.add(responsibility);
 231  0
                                         populateAssignedToRoles(responsibility);
 232  
                                 }
 233  
                         }
 234  
                 }
 235  0
                 return filteredResponsibilities;
 236  
         }
 237  
         
 238  
         private List<UberResponsibilityBo> getResponsibilitiesSearchResultsCopy(List<ResponsibilityBo> responsibilitySearchResults){
 239  0
                 List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(
 240  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitySearchResults));
 241  0
                 for(ResponsibilityBo responsibilityImpl: responsibilitySearchResults){
 242  0
                         UberResponsibilityBo responsibilityCopy = new UberResponsibilityBo();
 243  
 
 244  
             try {
 245  0
                 PropertyUtils.copyProperties(responsibilityCopy, responsibilityImpl);
 246  0
             } catch (IllegalAccessException e) {
 247  0
                 throw new RuntimeException("unable to copy properties");
 248  0
             } catch (InvocationTargetException e) {
 249  0
                 throw new RuntimeException("unable to copy properties");
 250  0
             } catch (NoSuchMethodException e) {
 251  0
                 throw new RuntimeException("unable to copy properties");
 252  0
             }
 253  
 
 254  0
             responsibilitySearchResultsCopy.add(responsibilityCopy);
 255  0
                 }
 256  0
                 return responsibilitySearchResultsCopy;
 257  
         }
 258  
         
 259  
 
 260  
         /**
 261  
          * @return the lookupService
 262  
          */
 263  
         public LookupService getLookupService() {
 264  0
                 if ( lookupService == null ) {
 265  0
                         lookupService = KRADServiceLocatorWeb.getLookupService();
 266  
                 }
 267  0
                 return lookupService;
 268  
         }
 269  
  
 270  
         private List<UberResponsibilityBo> mergeResponsibilityLists(List<UberResponsibilityBo> perm1, List<UberResponsibilityBo> perm2) {
 271  0
                 List<UberResponsibilityBo> returnList = new ArrayList<UberResponsibilityBo>(perm1);
 272  0
                 List<String> responsibilityIds = new ArrayList<String>(perm1.size());
 273  0
                 for (UberResponsibilityBo perm : returnList) {
 274  0
                         responsibilityIds.add(perm.getId());
 275  
                 }
 276  0
                 for (int i=0; i<perm2.size(); i++) {
 277  0
                     if (!responsibilityIds.contains(perm2.get(i).getId())) {
 278  0
                             returnList.add(perm2.get(i));
 279  
                     }
 280  
                 }
 281  0
                 return returnList;
 282  
         }
 283  
 }