Coverage Report - org.kuali.rice.kim.impl.responsibility.ResponsibilityLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponsibilityLookupableHelperServiceImpl
0%
0/126
0%
0/82
3.929
 
 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 org.apache.commons.beanutils.PropertyUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.log4j.Logger;
 21  
 import org.kuali.rice.core.util.AttributeSet;
 22  
 import org.kuali.rice.core.util.MaxAgeSoftReference;
 23  
 import org.kuali.rice.kew.util.KEWConstants;
 24  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 25  
 import org.kuali.rice.kim.bo.impl.RoleImpl;
 26  
 import org.kuali.rice.kim.impl.responsibility.ResponsibilityBo;
 27  
 import org.kuali.rice.kim.impl.responsibility.ReviewResponsibilityBo;
 28  
 import org.kuali.rice.kim.impl.responsibility.UberResponsibilityBo;
 29  
 import org.kuali.rice.kim.impl.role.RoleBo;
 30  
 import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
 31  
 import org.kuali.rice.kim.lookup.RoleMemberLookupableHelperServiceImpl;
 32  
 import org.kuali.rice.kim.util.KimConstants;
 33  
 import org.kuali.rice.kns.bo.BusinessObject;
 34  
 import org.kuali.rice.kns.lookup.CollectionIncomplete;
 35  
 import org.kuali.rice.kns.lookup.HtmlData;
 36  
 import org.kuali.rice.kns.service.KNSServiceLocatorWeb;
 37  
 import org.kuali.rice.kns.service.LookupService;
 38  
 import org.kuali.rice.kns.util.KNSConstants;
 39  
 import org.kuali.rice.kns.util.UrlFactory;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.HashMap;
 43  
 import java.util.List;
 44  
 import java.util.Map;
 45  
 import java.util.Properties;
 46  
 
 47  
 /**
 48  
  * This is a description of what this class does - bhargavp don't forget to fill this in. 
 49  
  * 
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  *
 52  
  */
 53  0
 public class ResponsibilityLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl {
 54  
 
 55  0
         private static final Logger LOG = Logger.getLogger( ResponsibilityLookupableHelperServiceImpl.class );
 56  
         
 57  
         private static final long serialVersionUID = -2882500971924192124L;
 58  
         
 59  
         private static LookupService lookupService;
 60  
 
 61  0
         private static boolean reviewResponsibilityDocumentTypeNameLoaded = false;
 62  0
         private static String reviewResponsibilityDocumentTypeName = null;
 63  
         
 64  
         /**
 65  
          * This overridden method ...
 66  
          * 
 67  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject, java.util.List)
 68  
          */
 69  
         @SuppressWarnings("unchecked")
 70  
         @Override
 71  
         public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
 72  0
             List<HtmlData> htmlDataList = new ArrayList<HtmlData>();
 73  
             // convert the UberResponsibilityBo class into a ReviewResponsibility object
 74  0
         if ( ((UberResponsibilityBo)businessObject).getTemplate().getName().equals( KEWConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME ) ) {
 75  0
                 ReviewResponsibilityBo reviewResp = new ReviewResponsibilityBo( (UberResponsibilityBo)businessObject );
 76  0
                 businessObject = reviewResp;
 77  0
                 if (allowsMaintenanceEditAction(businessObject)) {
 78  0
                         htmlDataList.add(getUrlData(businessObject, KNSConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
 79  
                 }
 80  0
                 if (allowsMaintenanceNewOrCopyAction()) {
 81  0
                         htmlDataList.add(getUrlData(businessObject, KNSConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames));
 82  
                 }
 83  
         }
 84  0
         return htmlDataList;
 85  
         }
 86  
 
 87  
     @SuppressWarnings("unchecked")
 88  
         protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
 89  0
         Properties parameters = new Properties();
 90  0
         parameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
 91  
         // TODO: why is this not using the businessObject parmeter's class?
 92  0
         parameters.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName());
 93  0
         parameters.put(KNSConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID);
 94  0
         parameters.put(KNSConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID);
 95  0
         if (StringUtils.isNotBlank(getReturnLocation())) {
 96  0
                 parameters.put(KNSConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());         
 97  
                 }
 98  0
         parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames));
 99  0
         return UrlFactory.parameterizeUrl(KNSConstants.MAINTENANCE_ACTION, parameters);
 100  
     }
 101  
         
 102  
         /**
 103  
          * This overridden method ...
 104  
          * 
 105  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getMaintenanceDocumentTypeName()
 106  
          */
 107  
         @Override
 108  
         protected String getMaintenanceDocumentTypeName() {
 109  0
                 if ( !reviewResponsibilityDocumentTypeNameLoaded ) {
 110  0
                         reviewResponsibilityDocumentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(ReviewResponsibilityBo.class);
 111  0
                         reviewResponsibilityDocumentTypeNameLoaded = true;
 112  
                 }
 113  0
                 return reviewResponsibilityDocumentTypeName;
 114  
         }
 115  
         
 116  
         /**
 117  
          * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
 118  
          */
 119  
         @Override
 120  
         protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) {
 121  0
                 Map<String, String> responsibilitySearchCriteria = buildSearchCriteria(searchCriteria);
 122  0
                 Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria);
 123  0
                 boolean responsibilityCriteriaEmpty = responsibilitySearchCriteria==null || responsibilitySearchCriteria.isEmpty();
 124  0
                 boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty();
 125  
                 
 126  0
                 List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(new ArrayList<UberResponsibilityBo>(), new Long(0));
 127  0
                 if(!responsibilityCriteriaEmpty && !roleCriteriaEmpty){
 128  0
                         responsibilitySearchResultsCopy = getCombinedSearchResults(responsibilitySearchCriteria, roleSearchCriteria, unbounded);
 129  0
                 } else if(responsibilityCriteriaEmpty && !roleCriteriaEmpty){
 130  0
                         responsibilitySearchResultsCopy = getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded);
 131  0
                 } else if(!responsibilityCriteriaEmpty && roleCriteriaEmpty){
 132  0
                         responsibilitySearchResultsCopy = getResponsibilitiesWithResponsibilitySearchCriteria(responsibilitySearchCriteria, unbounded);
 133  0
                 } else if(responsibilityCriteriaEmpty && roleCriteriaEmpty){
 134  0
                         return getAllResponsibilities(unbounded);
 135  
                 }
 136  0
                 return responsibilitySearchResultsCopy;
 137  
         }
 138  
         
 139  
         private List<UberResponsibilityBo> getAllResponsibilities(boolean unbounded){
 140  0
                 List<UberResponsibilityBo> responsibilities = searchResponsibilities(new HashMap<String, String>(), unbounded);
 141  0
                 for(UberResponsibilityBo responsibility: responsibilities)
 142  0
                         populateAssignedToRoles(responsibility);
 143  0
                 return responsibilities;
 144  
         }
 145  
         
 146  
         private List<UberResponsibilityBo> getCombinedSearchResults(
 147  
                         Map<String, String> responsibilitySearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){
 148  0
                 List<UberResponsibilityBo> responsibilitySearchResults = searchResponsibilities(responsibilitySearchCriteria, unbounded);
 149  0
                 List<RoleImpl> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 150  0
                 List<UberResponsibilityBo> responsibilitiesForRoleSearchResults = getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded);
 151  0
                 List<UberResponsibilityBo> matchedResponsibilities = new CollectionIncomplete<UberResponsibilityBo>(
 152  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitiesForRoleSearchResults));
 153  0
                 if((responsibilitySearchResults!=null && !responsibilitySearchResults.isEmpty()) && 
 154  
                                 (responsibilitiesForRoleSearchResults!=null && !responsibilitiesForRoleSearchResults.isEmpty())){
 155  0
                         for(UberResponsibilityBo responsibility: responsibilitySearchResults){
 156  0
                                 for(UberResponsibilityBo responsibilityFromRoleSearch: responsibilitiesForRoleSearchResults){
 157  0
                                         if(responsibilityFromRoleSearch.getId().equals(responsibility.getId()))
 158  0
                                                 matchedResponsibilities.add(responsibilityFromRoleSearch);
 159  
                                 }
 160  
                         }
 161  
                 }
 162  
 
 163  0
                 return matchedResponsibilities;
 164  
         }
 165  
         
 166  
         @SuppressWarnings("unchecked")
 167  
         private List<UberResponsibilityBo> searchResponsibilities(Map<String, String> responsibilitySearchCriteria, boolean unbounded){
 168  0
                 return getResponsibilitiesSearchResultsCopy((List<ResponsibilityBo>)
 169  
                                         getLookupService().findCollectionBySearchHelper(
 170  
                                                         ResponsibilityBo.class, responsibilitySearchCriteria, unbounded));
 171  
         }
 172  
         
 173  
         private List<UberResponsibilityBo> getResponsibilitiesWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){
 174  0
                 List<RoleImpl> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 175  0
                 return getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded);
 176  
         }
 177  
 
 178  
         private List<UberResponsibilityBo> getResponsibilitiesForRoleSearchResults(List<RoleImpl> roleSearchResults, boolean unbounded){
 179  0
                 Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults);
 180  0
                 List<UberResponsibilityBo> responsibilities = new ArrayList<UberResponsibilityBo>();
 181  
                 List<UberResponsibilityBo> tempResponsibilities;
 182  0
                 List<String> collectedResponsibilityIds = new ArrayList<String>();
 183  
                 Map<String, String> responsibilityCriteria;
 184  
                 
 185  0
                 for(RoleImpl roleImpl: roleSearchResults){
 186  0
                         responsibilityCriteria = new HashMap<String, String>();
 187  0
                         responsibilityCriteria.put("roleResponsibilities.roleId", roleImpl.getRoleId());
 188  0
                         tempResponsibilities = searchResponsibilities(responsibilityCriteria, unbounded);
 189  0
                         actualSizeIfTruncated += getActualSizeIfTruncated(tempResponsibilities);
 190  0
                         for(UberResponsibilityBo responsibility: tempResponsibilities){
 191  0
                                 if(!collectedResponsibilityIds.contains(responsibility.getId())){
 192  0
                                         populateAssignedToRoles(responsibility);
 193  0
                                         collectedResponsibilityIds.add(responsibility.getId());
 194  0
                                         responsibilities.add(responsibility);
 195  
                                 }
 196  
                                 //need to find roles that current role is a member of and build search string
 197  0
                                 List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(KimConstants.KimUIConstants.MEMBER_TYPE_ROLE_CODE, roleImpl.getRoleId());
 198  0
                                 for (String parentRoleId : parentRoleIds) {
 199  0
                                         Map<String, String> roleSearchCriteria = new HashMap<String, String>();
 200  0
                                         roleSearchCriteria.put("roleId", parentRoleId);
 201  
                                         //get all parent role permissions and merge them with current permissions
 202  0
                                         responsibilities = mergeResponsibilityLists(responsibilities, getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded));
 203  0
                                 }
 204  0
                         }
 205  
                 }
 206  0
                 return new CollectionIncomplete<UberResponsibilityBo>(responsibilities, actualSizeIfTruncated);
 207  
         }
 208  
 
 209  
         private void populateAssignedToRoles(UberResponsibilityBo responsibility){
 210  0
                 AttributeSet criteria = new AttributeSet();
 211  0
                 if ( responsibility.getAssignedToRoles().isEmpty() ) {
 212  0
                         for(RoleResponsibilityBo roleResponsibility: responsibility.getRoleResponsibilities()){
 213  0
                                 criteria.put(KimConstants.PrimaryKeyConstants.ROLE_ID, roleResponsibility.getRoleId());
 214  0
                                 responsibility.getAssignedToRoles().add((RoleBo)getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria));
 215  
                         }
 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  0
     private static final Map<Map<String,String>,MaxAgeSoftReference<List<UberResponsibilityBo>>> respResultCache = new HashMap<Map<String,String>, MaxAgeSoftReference<List<UberResponsibilityBo>>>(); 
 223  
         private static final long RESP_CACHE_EXPIRE_SECONDS = 30L;
 224  
         
 225  
         private List<UberResponsibilityBo> getResponsibilitiesWithResponsibilitySearchCriteria(Map<String, String> responsibilitySearchCriteria, boolean unbounded){
 226  0
                 String detailCriteriaStr = responsibilitySearchCriteria.remove( DETAIL_CRITERIA );
 227  0
                 AttributeSet detailCriteria = parseDetailCriteria(detailCriteriaStr);
 228  0
                 MaxAgeSoftReference<List<UberResponsibilityBo>> cachedResult = respResultCache.get(responsibilitySearchCriteria);
 229  0
                 List<UberResponsibilityBo> responsibilities = null;
 230  0
                 if ( cachedResult == null || cachedResult.get() == null ) {
 231  0
                         responsibilities = searchResponsibilities(responsibilitySearchCriteria, unbounded);
 232  0
                         synchronized( respResultCache ) {
 233  0
                                 respResultCache.put(responsibilitySearchCriteria, new MaxAgeSoftReference<List<UberResponsibilityBo>>( RESP_CACHE_EXPIRE_SECONDS, responsibilities ) ); 
 234  0
                         }
 235  
                 } else {
 236  0
                         responsibilities = cachedResult.get();
 237  
                 }
 238  0
                 List<UberResponsibilityBo> filteredResponsibilities = new CollectionIncomplete<UberResponsibilityBo>(
 239  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilities)); 
 240  0
                 for(UberResponsibilityBo responsibility: responsibilities){
 241  0
                         if ( detailCriteria.isEmpty() ) {
 242  0
                                 filteredResponsibilities.add(responsibility);
 243  0
                                 populateAssignedToRoles(responsibility);
 244  
                         } else {
 245  0
                                 if ( isMapSubset( new AttributeSet(responsibility.getAttributes().toMap()), detailCriteria ) ) {
 246  0
                                         filteredResponsibilities.add(responsibility);
 247  0
                                         populateAssignedToRoles(responsibility);
 248  
                                 }
 249  
                         }
 250  
                 }
 251  0
                 return filteredResponsibilities;
 252  
         }
 253  
         
 254  
         private List<UberResponsibilityBo> getResponsibilitiesSearchResultsCopy(List<ResponsibilityBo> responsibilitySearchResults){
 255  0
                 List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(
 256  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitySearchResults));
 257  0
                 for(ResponsibilityBo responsibilityImpl: responsibilitySearchResults){
 258  0
                         UberResponsibilityBo responsibilityCopy = new UberResponsibilityBo();
 259  
                         try{
 260  0
                                 PropertyUtils.copyProperties(responsibilityCopy, responsibilityImpl);
 261  0
                         } catch(Exception ex){
 262  0
                                 LOG.error( "Unable to copy properties from KimUberResponsibilityBo to UberResponsibilityBo, skipping.", ex );
 263  0
                                 continue;
 264  0
                         }
 265  0
                         responsibilitySearchResultsCopy.add(responsibilityCopy);
 266  0
                 }
 267  0
                 return responsibilitySearchResultsCopy;
 268  
         }
 269  
         
 270  
 
 271  
         /**
 272  
          * @return the lookupService
 273  
          */
 274  
         public LookupService getLookupService() {
 275  0
                 if ( lookupService == null ) {
 276  0
                         lookupService = KNSServiceLocatorWeb.getLookupService();
 277  
                 }
 278  0
                 return lookupService;
 279  
         }
 280  
  
 281  
         private List<UberResponsibilityBo> mergeResponsibilityLists(List<UberResponsibilityBo> perm1, List<UberResponsibilityBo> perm2) {
 282  0
                 List<UberResponsibilityBo> returnList = new ArrayList<UberResponsibilityBo>(perm1);
 283  0
                 List<String> responsibilityIds = new ArrayList<String>(perm1.size());
 284  0
                 for (UberResponsibilityBo perm : returnList) {
 285  0
                         responsibilityIds.add(perm.getId());
 286  
                 }
 287  0
                 for (int i=0; i<perm2.size(); i++) {
 288  0
                     if (!responsibilityIds.contains(perm2.get(i).getId())) {
 289  0
                             returnList.add(perm2.get(i));
 290  
                     }
 291  
                 }
 292  0
                 return returnList;
 293  
         }
 294  
 }