Coverage Report - org.kuali.rice.kim.impl.responsibility.ResponsibilityLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ResponsibilityLookupableHelperServiceImpl
0%
0/121
0%
0/78
4.143
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.impl.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.api.membership.MemberType;
 22  
 import org.kuali.rice.kew.api.KewApiConstants;
 23  
 import org.kuali.rice.kim.api.KimConstants;
 24  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 25  
 import org.kuali.rice.kim.impl.role.RoleBo;
 26  
 import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
 27  
 import org.kuali.rice.kim.lookup.RoleMemberLookupableHelperServiceImpl;
 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  
 
 43  0
 public class ResponsibilityLookupableHelperServiceImpl extends RoleMemberLookupableHelperServiceImpl {
 44  
 
 45  0
         private static final Logger LOG = Logger.getLogger( ResponsibilityLookupableHelperServiceImpl.class );
 46  
         
 47  
         private static final long serialVersionUID = -2882500971924192124L;
 48  
         
 49  
         private static LookupService lookupService;
 50  
 
 51  0
         private static boolean reviewResponsibilityDocumentTypeNameLoaded = false;
 52  0
         private static String reviewResponsibilityDocumentTypeName = null;
 53  
         
 54  
         @SuppressWarnings("unchecked")
 55  
         @Override
 56  
         public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
 57  0
             List<HtmlData> htmlDataList = new ArrayList<HtmlData>();
 58  
             // convert the UberResponsibilityBo class into a ReviewResponsibility object
 59  0
         if ( ((UberResponsibilityBo)businessObject).getTemplate().getName().equals( KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME ) ) {
 60  0
                 ReviewResponsibilityBo reviewResp = new ReviewResponsibilityBo( (UberResponsibilityBo)businessObject );
 61  0
                 businessObject = reviewResp;
 62  0
                 if (allowsMaintenanceEditAction(businessObject)) {
 63  0
                         htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames));
 64  
                 }
 65  0
                 if (allowsMaintenanceNewOrCopyAction()) {
 66  0
                         htmlDataList.add(getUrlData(businessObject, KRADConstants.MAINTENANCE_COPY_METHOD_TO_CALL, pkNames));
 67  
                 }
 68  
         }
 69  0
         return htmlDataList;
 70  
         }
 71  
 
 72  
     @SuppressWarnings("unchecked")
 73  
         protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
 74  0
         Properties parameters = new Properties();
 75  0
         parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
 76  
         // TODO: why is this not using the businessObject parmeter's class?
 77  0
         parameters.put(KRADConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName());
 78  0
         parameters.put(KRADConstants.OVERRIDE_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID);
 79  0
         parameters.put(KRADConstants.COPY_KEYS, KimConstants.PrimaryKeyConstants.RESPONSIBILITY_ID);
 80  0
         if (StringUtils.isNotBlank(getReturnLocation())) {
 81  0
                 parameters.put(KRADConstants.RETURN_LOCATION_PARAMETER, getReturnLocation());
 82  
                 }
 83  0
         parameters.putAll(getParametersFromPrimaryKey(businessObject, pkNames));
 84  0
         return UrlFactory.parameterizeUrl(KRADConstants.MAINTENANCE_ACTION, parameters);
 85  
     }
 86  
         
 87  
         @Override
 88  
         protected String getMaintenanceDocumentTypeName() {
 89  0
                 if ( !reviewResponsibilityDocumentTypeNameLoaded ) {
 90  0
                         reviewResponsibilityDocumentTypeName = getMaintenanceDocumentDictionaryService().getDocumentTypeName(ReviewResponsibilityBo.class);
 91  0
                         reviewResponsibilityDocumentTypeNameLoaded = true;
 92  
                 }
 93  0
                 return reviewResponsibilityDocumentTypeName;
 94  
         }
 95  
         
 96  
         @Override
 97  
         protected List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded) {
 98  0
                 Map<String, String> responsibilitySearchCriteria = buildSearchCriteria(searchCriteria);
 99  0
                 Map<String, String> roleSearchCriteria = buildRoleSearchCriteria(searchCriteria);
 100  0
                 boolean responsibilityCriteriaEmpty = responsibilitySearchCriteria==null || responsibilitySearchCriteria.isEmpty();
 101  0
                 boolean roleCriteriaEmpty = roleSearchCriteria==null || roleSearchCriteria.isEmpty();
 102  
                 
 103  0
                 List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(new ArrayList<UberResponsibilityBo>(), new Long(0));
 104  0
                 if(!responsibilityCriteriaEmpty && !roleCriteriaEmpty){
 105  0
                         responsibilitySearchResultsCopy = getCombinedSearchResults(responsibilitySearchCriteria, roleSearchCriteria, unbounded);
 106  0
                 } else if(responsibilityCriteriaEmpty && !roleCriteriaEmpty){
 107  0
                         responsibilitySearchResultsCopy = getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded);
 108  0
                 } else if(!responsibilityCriteriaEmpty && roleCriteriaEmpty){
 109  0
                         responsibilitySearchResultsCopy = getResponsibilitiesWithResponsibilitySearchCriteria(responsibilitySearchCriteria, unbounded);
 110  0
                 } else if(responsibilityCriteriaEmpty && roleCriteriaEmpty){
 111  0
                         return getAllResponsibilities(unbounded);
 112  
                 }
 113  0
                 return responsibilitySearchResultsCopy;
 114  
         }
 115  
         
 116  
         private List<UberResponsibilityBo> getAllResponsibilities(boolean unbounded){
 117  0
                 List<UberResponsibilityBo> responsibilities = searchResponsibilities(new HashMap<String, String>(), unbounded);
 118  0
                 for(UberResponsibilityBo responsibility: responsibilities) {
 119  0
                         populateAssignedToRoles(responsibility);
 120  
         }
 121  0
                 return responsibilities;
 122  
         }
 123  
         
 124  
         private List<UberResponsibilityBo> getCombinedSearchResults(
 125  
                         Map<String, String> responsibilitySearchCriteria, Map<String, String> roleSearchCriteria, boolean unbounded){
 126  0
                 List<UberResponsibilityBo> responsibilitySearchResults = searchResponsibilities(responsibilitySearchCriteria, unbounded);
 127  0
                 List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 128  0
                 List<UberResponsibilityBo> responsibilitiesForRoleSearchResults = getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded);
 129  0
                 List<UberResponsibilityBo> matchedResponsibilities = new CollectionIncomplete<UberResponsibilityBo>(
 130  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitiesForRoleSearchResults));
 131  0
                 if((responsibilitySearchResults!=null && !responsibilitySearchResults.isEmpty()) && 
 132  
                                 (responsibilitiesForRoleSearchResults!=null && !responsibilitiesForRoleSearchResults.isEmpty())){
 133  0
                         for(UberResponsibilityBo responsibility: responsibilitySearchResults){
 134  0
                                 for(UberResponsibilityBo responsibilityFromRoleSearch: responsibilitiesForRoleSearchResults){
 135  0
                                         if(responsibilityFromRoleSearch.getId().equals(responsibility.getId())) {
 136  0
                                                 matchedResponsibilities.add(responsibilityFromRoleSearch);
 137  
                     }
 138  
                                 }
 139  
                         }
 140  
                 }
 141  
 
 142  0
                 return matchedResponsibilities;
 143  
         }
 144  
         
 145  
         @SuppressWarnings("unchecked")
 146  
         private List<UberResponsibilityBo> searchResponsibilities(Map<String, String> responsibilitySearchCriteria, boolean unbounded){
 147  0
                 return getResponsibilitiesSearchResultsCopy((List<ResponsibilityBo>)
 148  
                                         getLookupService().findCollectionBySearchHelper(
 149  
                                                         ResponsibilityBo.class, responsibilitySearchCriteria, unbounded));
 150  
         }
 151  
         
 152  
         private List<UberResponsibilityBo> getResponsibilitiesWithRoleSearchCriteria(Map<String, String> roleSearchCriteria, boolean unbounded){
 153  0
                 List<RoleBo> roleSearchResults = searchRoles(roleSearchCriteria, unbounded);
 154  0
                 return getResponsibilitiesForRoleSearchResults(roleSearchResults, unbounded);
 155  
         }
 156  
 
 157  
         private List<UberResponsibilityBo> getResponsibilitiesForRoleSearchResults(List<RoleBo> roleSearchResults, boolean unbounded){
 158  0
                 Long actualSizeIfTruncated = getActualSizeIfTruncated(roleSearchResults);
 159  0
                 List<UberResponsibilityBo> responsibilities = new ArrayList<UberResponsibilityBo>();
 160  
                 List<UberResponsibilityBo> tempResponsibilities;
 161  0
                 List<String> collectedResponsibilityIds = new ArrayList<String>();
 162  
                 Map<String, String> responsibilityCriteria;
 163  
                 
 164  0
                 for(RoleBo roleImpl: roleSearchResults){
 165  0
                         responsibilityCriteria = new HashMap<String, String>();
 166  0
                         responsibilityCriteria.put("roleResponsibilities.roleId", roleImpl.getId());
 167  0
                         tempResponsibilities = searchResponsibilities(responsibilityCriteria, unbounded);
 168  0
                         actualSizeIfTruncated += getActualSizeIfTruncated(tempResponsibilities);
 169  0
                         for(UberResponsibilityBo responsibility: tempResponsibilities){
 170  0
                                 if(!collectedResponsibilityIds.contains(responsibility.getId())){
 171  0
                                         populateAssignedToRoles(responsibility);
 172  0
                                         collectedResponsibilityIds.add(responsibility.getId());
 173  0
                                         responsibilities.add(responsibility);
 174  
                                 }
 175  
                                 //need to find roles that current role is a member of and build search string
 176  0
                                 List<String> parentRoleIds = KimApiServiceLocator.getRoleService().getMemberParentRoleIds(MemberType.ROLE.getCode(), roleImpl.getId());
 177  0
                                 for (String parentRoleId : parentRoleIds) {
 178  0
                                         Map<String, String> roleSearchCriteria = new HashMap<String, String>();
 179  0
                                         roleSearchCriteria.put("roleId", parentRoleId);
 180  
                                         //get all parent role permissions and merge them with current permissions
 181  0
                                         responsibilities = mergeResponsibilityLists(responsibilities, getResponsibilitiesWithRoleSearchCriteria(roleSearchCriteria, unbounded));
 182  0
                                 }
 183  0
                         }
 184  
                 }
 185  0
                 return new CollectionIncomplete<UberResponsibilityBo>(responsibilities, actualSizeIfTruncated);
 186  
         }
 187  
 
 188  
         private void populateAssignedToRoles(UberResponsibilityBo responsibility){
 189  0
                 Map<String, String> criteria = new HashMap<String, String>();
 190  0
                 if ( responsibility.getAssignedToRoles().isEmpty() ) {
 191  0
                         for(RoleResponsibilityBo roleResponsibility: responsibility.getRoleResponsibilities()){
 192  0
                                 criteria.put(KimConstants.PrimaryKeyConstants.ID, roleResponsibility.getRoleId());
 193  0
                                 responsibility.getAssignedToRoles().add(getBusinessObjectService().findByPrimaryKey(RoleBo.class, criteria));
 194  
                         }
 195  
                 }
 196  0
         }
 197  
 
 198  
         private List<UberResponsibilityBo> getResponsibilitiesWithResponsibilitySearchCriteria(Map<String, String> responsibilitySearchCriteria, boolean unbounded){
 199  0
                 String detailCriteriaStr = responsibilitySearchCriteria.remove( DETAIL_CRITERIA );
 200  0
                 Map<String, String> detailCriteria = parseDetailCriteria(detailCriteriaStr);
 201  0
                 final List<UberResponsibilityBo> responsibilities = searchResponsibilities(responsibilitySearchCriteria, unbounded);
 202  0
                 List<UberResponsibilityBo> filteredResponsibilities = new CollectionIncomplete<UberResponsibilityBo>(
 203  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilities)); 
 204  0
                 for(UberResponsibilityBo responsibility: responsibilities){
 205  0
                         if ( detailCriteria.isEmpty() ) {
 206  0
                                 filteredResponsibilities.add(responsibility);
 207  0
                                 populateAssignedToRoles(responsibility);
 208  
                         } else {
 209  0
                                 if ( isMapSubset( new HashMap<String, String>(responsibility.getAttributes()), detailCriteria ) ) {
 210  0
                                         filteredResponsibilities.add(responsibility);
 211  0
                                         populateAssignedToRoles(responsibility);
 212  
                                 }
 213  
                         }
 214  
                 }
 215  0
                 return filteredResponsibilities;
 216  
         }
 217  
         
 218  
         private List<UberResponsibilityBo> getResponsibilitiesSearchResultsCopy(List<ResponsibilityBo> responsibilitySearchResults){
 219  0
                 List<UberResponsibilityBo> responsibilitySearchResultsCopy = new CollectionIncomplete<UberResponsibilityBo>(
 220  
                                 new ArrayList<UberResponsibilityBo>(), getActualSizeIfTruncated(responsibilitySearchResults));
 221  0
                 for(ResponsibilityBo responsibilityImpl: responsibilitySearchResults){
 222  0
                         UberResponsibilityBo responsibilityCopy = new UberResponsibilityBo();
 223  
 
 224  
             try {
 225  0
                 PropertyUtils.copyProperties(responsibilityCopy, responsibilityImpl);
 226  0
             } catch (IllegalAccessException e) {
 227  0
                 throw new RuntimeException("unable to copy properties");
 228  0
             } catch (InvocationTargetException e) {
 229  0
                 throw new RuntimeException("unable to copy properties");
 230  0
             } catch (NoSuchMethodException e) {
 231  0
                 throw new RuntimeException("unable to copy properties");
 232  0
             }
 233  
 
 234  0
             responsibilitySearchResultsCopy.add(responsibilityCopy);
 235  0
                 }
 236  0
                 return responsibilitySearchResultsCopy;
 237  
         }
 238  
         
 239  
 
 240  
         /**
 241  
          * @return the lookupService
 242  
          */
 243  
         public LookupService getLookupService() {
 244  0
                 if ( lookupService == null ) {
 245  0
                         lookupService = KRADServiceLocatorWeb.getLookupService();
 246  
                 }
 247  0
                 return lookupService;
 248  
         }
 249  
  
 250  
         private List<UberResponsibilityBo> mergeResponsibilityLists(List<UberResponsibilityBo> perm1, List<UberResponsibilityBo> perm2) {
 251  0
                 List<UberResponsibilityBo> returnList = new ArrayList<UberResponsibilityBo>(perm1);
 252  0
                 List<String> responsibilityIds = new ArrayList<String>(perm1.size());
 253  0
                 for (UberResponsibilityBo perm : returnList) {
 254  0
                         responsibilityIds.add(perm.getId());
 255  
                 }
 256  0
                 for (int i=0; i<perm2.size(); i++) {
 257  0
                     if (!responsibilityIds.contains(perm2.get(i).getId())) {
 258  0
                             returnList.add(perm2.get(i));
 259  
                     }
 260  
                 }
 261  0
                 return returnList;
 262  
         }
 263  
 }