Coverage Report - org.kuali.rice.kim.lookup.RoleMemberLookupableHelperServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
RoleMemberLookupableHelperServiceImpl
0%
0/160
0%
0/122
6.667
RoleMemberLookupableHelperServiceImpl$1
0%
0/3
N/A
6.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 org.apache.commons.collections.CollectionUtils;
 19  
 import org.apache.commons.collections.Predicate;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.util.AttributeSet;
 22  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 23  
 import org.kuali.rice.kim.api.group.Group;
 24  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 25  
 import org.kuali.rice.kim.bo.entity.dto.KimEntityInfo;
 26  
 import org.kuali.rice.kim.bo.impl.RoleImpl;
 27  
 import org.kuali.rice.kim.bo.role.impl.RoleMemberImpl;
 28  
 import org.kuali.rice.kim.util.KimConstants;
 29  
 import org.kuali.rice.kns.bo.BusinessObject;
 30  
 import org.kuali.rice.kns.lookup.CollectionIncomplete;
 31  
 import org.kuali.rice.kns.util.KNSPropertyConstants;
 32  
 import org.kuali.rice.kns.web.ui.Field;
 33  
 import org.kuali.rice.kns.web.ui.Row;
 34  
 
 35  
 import java.util.ArrayList;
 36  
 import java.util.Arrays;
 37  
 import java.util.HashMap;
 38  
 import java.util.Iterator;
 39  
 import java.util.List;
 40  
 import java.util.Map;
 41  
 
 42  
 /**
 43  
  * This is a description of what this class does - bhargavp don't forget to fill this in. 
 44  
  * 
 45  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 46  
  *
 47  
  */
 48  0
 public abstract class RoleMemberLookupableHelperServiceImpl extends KimLookupableHelperServiceImpl {
 49  
 
 50  
         protected static final String DETAIL_CRITERIA = "detailCriteria";
 51  
         protected static final String WILDCARD = "*";
 52  
     protected static final String TEMPLATE_NAMESPACE_CODE = "template." + KimConstants.UniqueKeyConstants.NAMESPACE_CODE;
 53  
     protected static final String TEMPLATE_NAME = "template.name";
 54  
     protected static final String NAMESPACE_CODE = KimConstants.UniqueKeyConstants.NAMESPACE_CODE;
 55  
     protected static final String NAME = "name";
 56  
     protected static final String GROUP_NAME = KimConstants.UniqueKeyConstants.GROUP_NAME;
 57  
     protected static final String ASSIGNED_TO_PRINCIPAL_NAME = "assignedToPrincipal.principalName";
 58  
     protected static final String ASSIGNED_TO_GROUP_NAMESPACE_CODE = "assignedToGroupNamespaceForLookup";
 59  
     protected static final String ASSIGNED_TO_GROUP_NAME = "assignedToGroup." + KimConstants.UniqueKeyConstants.GROUP_NAME;
 60  
     protected static final String ASSIGNED_TO_NAMESPACE_FOR_LOOKUP = "assignedToRoleNamespaceForLookup";
 61  
     protected static final String ASSIGNED_TO_ROLE_NAME = "assignedToRole." + KimConstants.UniqueKeyConstants.ROLE_NAME;
 62  
     protected static final String ATTRIBUTE_NAME = "attributeName";
 63  
     protected static final String ATTRIBUTE_VALUE = "attributeValue";
 64  
     protected static final String ASSIGNED_TO_ROLE_NAMESPACE_CODE = KimConstants.UniqueKeyConstants.NAMESPACE_CODE;
 65  
     protected static final String ASSIGNED_TO_ROLE_ROLE_NAME = KimConstants.UniqueKeyConstants.ROLE_NAME;
 66  
     protected static final String ASSIGNED_TO_ROLE_MEMBER_ID = "members.memberId";
 67  
     protected static final String DETAIL_OBJECTS_ATTRIBUTE_VALUE = "detailObjects.attributeValue";
 68  
     protected static final String DETAIL_OBJECTS_ATTRIBUTE_NAME = "detailObjects.kimAttribute.attributeName";
 69  
     
 70  
     @Override
 71  
     protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
 72  0
             Map<String, String> searchCriteria = buildRoleSearchCriteria(fieldValues);
 73  0
             if(searchCriteria == null)
 74  0
                     return new ArrayList<BusinessObject>();
 75  0
         return getMemberSearchResults(fieldValues, unbounded);
 76  
     }
 77  
 
 78  
     protected abstract List<? extends BusinessObject> getMemberSearchResults(Map<String, String> searchCriteria, boolean unbounded);
 79  
     
 80  
     protected Map<String, String> buildSearchCriteria(Map<String, String> fieldValues){
 81  0
         String templateNamespaceCode = fieldValues.get(TEMPLATE_NAMESPACE_CODE);
 82  0
         String templateName = fieldValues.get(TEMPLATE_NAME);
 83  0
         String namespaceCode = fieldValues.get(NAMESPACE_CODE);
 84  0
         String name = fieldValues.get(NAME);
 85  0
         String attributeDetailValue = fieldValues.get(ATTRIBUTE_VALUE);
 86  0
         String attributeDetailName = fieldValues.get(ATTRIBUTE_NAME);
 87  0
         String detailCriteria = fieldValues.get( DETAIL_CRITERIA );
 88  0
         String active = fieldValues.get( KNSPropertyConstants.ACTIVE );
 89  
 
 90  0
             Map<String,String> searchCriteria = new HashMap<String, String>();
 91  0
             if(StringUtils.isNotEmpty(templateNamespaceCode)) {
 92  0
                     searchCriteria.put(TEMPLATE_NAMESPACE_CODE, WILDCARD+templateNamespaceCode+WILDCARD);
 93  
             }
 94  0
         if(StringUtils.isNotEmpty(templateName)) {
 95  0
                 searchCriteria.put(TEMPLATE_NAME, WILDCARD+templateName+WILDCARD);
 96  
         }
 97  0
         if(StringUtils.isNotEmpty(namespaceCode)) {
 98  0
                 searchCriteria.put(NAMESPACE_CODE, WILDCARD+namespaceCode+WILDCARD);
 99  
         }
 100  0
         if(StringUtils.isNotEmpty(name)) {
 101  0
                 searchCriteria.put(NAME, WILDCARD+name+WILDCARD);
 102  
         }
 103  0
         if(StringUtils.isNotEmpty(attributeDetailValue)) {
 104  0
                 searchCriteria.put(DETAIL_OBJECTS_ATTRIBUTE_VALUE, WILDCARD+attributeDetailValue+WILDCARD);
 105  
         }
 106  0
         if(StringUtils.isNotEmpty(attributeDetailName)) {
 107  0
                 searchCriteria.put(DETAIL_OBJECTS_ATTRIBUTE_NAME, WILDCARD+attributeDetailName+WILDCARD);
 108  
         }
 109  0
         if ( StringUtils.isNotBlank( detailCriteria ) ) {
 110  0
                 searchCriteria.put(DETAIL_CRITERIA, detailCriteria);
 111  
         }
 112  0
         if ( StringUtils.isNotBlank( active ) ) {
 113  0
                 searchCriteria.put(KNSPropertyConstants.ACTIVE, active);
 114  
         }
 115  
 
 116  0
         return searchCriteria;
 117  
     }
 118  
     
 119  
     protected String getQueryString(String parameter){
 120  0
             if(StringUtils.isEmpty(parameter))
 121  0
                     return WILDCARD;
 122  
             else
 123  0
                     return WILDCARD+parameter+WILDCARD;
 124  
     }
 125  
     
 126  
     /**
 127  
      * - detail value: 
 128  
      * if this is provided a full (template namespace and template name) or namespace must be supplied 
 129  
      * - may need to do further restrictions once we see how this performs
 130  
      *  
 131  
      * @param fieldValues the values of the query
 132  
      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
 133  
      */
 134  
     @SuppressWarnings("unchecked")
 135  
     @Override
 136  
     public void validateSearchParameters(Map fieldValues) {
 137  0
         super.validateSearchParameters(fieldValues);
 138  
 /*
 139  
         String valueTemplateNamespaceCode = (String) fieldValues.get(TEMPLATE_NAMESPACE_CODE);
 140  
         String valueTemplateName = (String) fieldValues.get(TEMPLATE_NAME);
 141  
         String name = (String) fieldValues.get(NAME);
 142  
         
 143  
         if (!((StringUtils.isNotEmpty(valueTemplateNamespaceCode) && StringUtils.isNotEmpty(valueTemplateName)) 
 144  
                         || StringUtils.isNotEmpty(name))){
 145  
             throw new ValidationException("For a search to be performed on an attribute detail value, " +
 146  
                             "a combination of template namespace and template name, or a namespace must be supplied");
 147  
         }
 148  
         */
 149  0
     }
 150  
 
 151  
     @SuppressWarnings({ "unchecked" })
 152  
         protected Map<String, String> buildRoleSearchCriteria(Map<String, String> fieldValues){
 153  0
                String assignedToPrincipalName = fieldValues.get(ASSIGNED_TO_PRINCIPAL_NAME);
 154  
             Map<String, String> searchCriteria;
 155  0
             List<Principal> principals = new ArrayList<Principal>();
 156  0
         if(StringUtils.isNotEmpty(assignedToPrincipalName)){
 157  0
                 searchCriteria = new HashMap<String, String>();
 158  0
                 searchCriteria.put("principals.principalName", WILDCARD+assignedToPrincipalName+WILDCARD);
 159  0
                 List<KimEntityInfo> kimEntityInfoList = KimApiServiceLocator.getIdentityManagementService().lookupEntityInfo(searchCriteria, true);
 160  0
                 if(kimEntityInfoList == null || kimEntityInfoList.isEmpty()) {
 161  0
                         return null;
 162  
                 }
 163  
                 else {
 164  0
                         for (KimEntityInfo kimEntityInfo : kimEntityInfoList) {
 165  0
                                 if(kimEntityInfo.getPrincipals() != null){
 166  0
                                         principals.addAll(kimEntityInfo.getPrincipals());
 167  
                                 }
 168  
                         }
 169  
                 }
 170  
         }
 171  0
         String assignedToGroupNamespaceCode = fieldValues.get(ASSIGNED_TO_GROUP_NAMESPACE_CODE);
 172  0
         String assignedToGroupName = fieldValues.get(ASSIGNED_TO_GROUP_NAME);
 173  0
         List<Group> groups = null;
 174  0
         if(StringUtils.isNotEmpty(assignedToGroupNamespaceCode) && StringUtils.isEmpty(assignedToGroupName) ||
 175  
                         StringUtils.isEmpty(assignedToGroupNamespaceCode) && StringUtils.isNotEmpty(assignedToGroupName) ||
 176  
                         StringUtils.isNotEmpty(assignedToGroupNamespaceCode) && StringUtils.isNotEmpty(assignedToGroupName)){
 177  0
                 searchCriteria = new HashMap<String, String>();
 178  0
                 searchCriteria.put(NAMESPACE_CODE, getQueryString(assignedToGroupNamespaceCode));
 179  0
                 searchCriteria.put(GROUP_NAME, getQueryString(assignedToGroupName));
 180  0
                 groups = (List<Group>) KimApiServiceLocator.getGroupService().lookupGroups(searchCriteria);
 181  0
                 if(CollectionUtils.isEmpty(groups))
 182  0
                         return null;
 183  
         }
 184  
 
 185  0
         String assignedToRoleNamespaceCode = fieldValues.get(ASSIGNED_TO_NAMESPACE_FOR_LOOKUP);
 186  0
         String assignedToRoleName = fieldValues.get(ASSIGNED_TO_ROLE_NAME);
 187  
 
 188  0
             searchCriteria = new HashMap<String, String>();
 189  0
         if(StringUtils.isNotEmpty(assignedToRoleNamespaceCode))
 190  0
                 searchCriteria.put(ASSIGNED_TO_ROLE_NAMESPACE_CODE, WILDCARD+assignedToRoleNamespaceCode+WILDCARD);
 191  0
         if(StringUtils.isNotEmpty(assignedToRoleName))
 192  0
                 searchCriteria.put(ASSIGNED_TO_ROLE_ROLE_NAME, WILDCARD+assignedToRoleName+WILDCARD);
 193  
 
 194  0
             StringBuffer memberQueryString = null;
 195  0
         if(principals!=null){
 196  0
                 memberQueryString = new StringBuffer();
 197  0
                 for(Principal principal: principals){
 198  0
                         memberQueryString.append(principal.getPrincipalId()+KimConstants.KimUIConstants.OR_OPERATOR);
 199  
                 }
 200  0
             if(memberQueryString.toString().endsWith(KimConstants.KimUIConstants.OR_OPERATOR))
 201  0
                     memberQueryString.delete(memberQueryString.length()-KimConstants.KimUIConstants.OR_OPERATOR.length(), memberQueryString.length());
 202  
         }
 203  0
         if(groups!=null){
 204  0
                 if(memberQueryString==null)
 205  0
                         memberQueryString = new StringBuffer();
 206  0
                 else if(StringUtils.isNotEmpty(memberQueryString.toString()))
 207  0
                         memberQueryString.append(KimConstants.KimUIConstants.OR_OPERATOR);
 208  0
                 for(Group group: groups){
 209  0
                         memberQueryString.append(group.getId()+KimConstants.KimUIConstants.OR_OPERATOR);
 210  
                 }
 211  0
             if(memberQueryString.toString().endsWith(KimConstants.KimUIConstants.OR_OPERATOR))
 212  0
                     memberQueryString.delete(memberQueryString.length()-KimConstants.KimUIConstants.OR_OPERATOR.length(), memberQueryString.length());
 213  0
                 searchCriteria.put(ASSIGNED_TO_ROLE_MEMBER_ID, memberQueryString.toString());
 214  
         }
 215  0
         if(memberQueryString!=null && StringUtils.isNotEmpty(memberQueryString.toString()))
 216  0
                 searchCriteria.put(ASSIGNED_TO_ROLE_MEMBER_ID, memberQueryString.toString());
 217  
 
 218  0
         return searchCriteria;
 219  
     }
 220  
 
 221  
     
 222  
     /** Checks whether the 2nd map is a subset of the first. */
 223  
         protected boolean isMapSubset( AttributeSet mainMap, AttributeSet subsetMap ) {
 224  0
                 for ( Map.Entry<String, String> keyValue : subsetMap.entrySet() ) {
 225  0
                         if ( !mainMap.containsKey(keyValue.getKey()) 
 226  
                                         || !StringUtils.equals( mainMap.get(keyValue.getKey()), keyValue.getValue() ) ) {
 227  
 //                                if ( LOG.isDebugEnabled() ) {
 228  
 //                                        LOG.debug( "Maps did not match:\n" + mainMap + "\n" + subsetMap );
 229  
 //                                }
 230  0
                                 return false;
 231  
                         }
 232  
                 }
 233  
 //                if ( LOG.isDebugEnabled() ) {
 234  
 //                        LOG.debug( "Maps matched:\n" + mainMap + "\n" + subsetMap );
 235  
 //                }
 236  0
                 return true;
 237  
         }
 238  
 
 239  
         /** Converts a special criteria string that is in the form key=value,key2=value2 into a map */
 240  
         protected AttributeSet parseDetailCriteria( String detailCritiera ) {
 241  0
             if ( StringUtils.isBlank(detailCritiera) ) {
 242  0
                 return new AttributeSet(0);
 243  
             }
 244  0
                 String[] keyValuePairs = StringUtils.split(detailCritiera, ',');
 245  0
                 if ( keyValuePairs == null || keyValuePairs.length == 0 ) {
 246  0
                     return new AttributeSet(0);
 247  
                 }
 248  0
                 AttributeSet parsedDetails = new AttributeSet( keyValuePairs.length );
 249  0
                 for ( String keyValueStr : keyValuePairs ) {
 250  0
                         String[] keyValue = StringUtils.split(keyValueStr, '=');
 251  0
                         if ( keyValue.length >= 2 ) {
 252  0
                                 parsedDetails.put(keyValue[0], keyValue[1]);
 253  
                         }
 254  
                 }
 255  0
                 return parsedDetails;
 256  
         }
 257  
         
 258  
         
 259  
         /**
 260  
          * This overridden method ...
 261  
          * 
 262  
          * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getRows()
 263  
          */
 264  
         @Override
 265  
         public List<Row> getRows() {
 266  0
                 List<Row> rows = super.getRows();
 267  0
                 Iterator<Row> i = rows.iterator();
 268  0
                 while ( i.hasNext() ) {
 269  0
                         Row row = i.next();
 270  0
                         int numFieldsRemoved = 0;
 271  0
                         boolean rowIsBlank = true;
 272  0
                         for (Iterator<Field> fieldIter = row.getFields().iterator(); fieldIter.hasNext();) {
 273  0
                                 Field field = fieldIter.next();
 274  0
                                 String propertyName = field.getPropertyName();
 275  0
                                 if ( propertyName.equals(DETAIL_CRITERIA) ) {
 276  0
                                         Object val = getParameters().get( propertyName );
 277  0
                                         String propVal = null;
 278  0
                                         if ( val != null ) {
 279  0
                                                 propVal = (val instanceof String)?(String)val:((String[])val)[0];
 280  
                                         }
 281  0
                                         if ( StringUtils.isBlank( propVal ) ) {
 282  0
                                                 fieldIter.remove();
 283  0
                                                 numFieldsRemoved++;
 284  
                                         } else {
 285  0
                                                 field.setReadOnly(true);
 286  0
                                                 rowIsBlank = false;
 287  
                                                 // leaving this in would prevent the "clear" button from resetting this value
 288  
 //                                                field.setDefaultValue( propVal );
 289  
                                         }
 290  0
                                 } else if (!Field.BLANK_SPACE.equals(field.getFieldType())) {
 291  0
                                         rowIsBlank = false;
 292  
                                 }
 293  0
                         }
 294  
                         // Check if any fields were removed from the row.
 295  0
                         if (numFieldsRemoved > 0) {
 296  
                                 // If fields were removed, check whether or not the remainder of the row is empty or only has blank space fields.
 297  0
                                 if (rowIsBlank) {
 298  
                                         // If so, then remove the row entirely.
 299  0
                                         i.remove();
 300  
                                 } else {
 301  
                                         // Otherwise, add one blank space for each field that was removed, using a technique similar to FieldUtils.createBlankSpace.
 302  
                                         // It is safe to just add blank spaces as needed, since the removable field is the last of the visible ones in the list (or
 303  
                                         // at least for the Permission and Responsibility lookups).
 304  0
                                         while (numFieldsRemoved > 0) {
 305  0
                                                 Field blankSpace = new Field();
 306  0
                                                 blankSpace.setFieldType(Field.BLANK_SPACE);
 307  0
                                                 blankSpace.setPropertyName(Field.BLANK_SPACE);
 308  0
                                                 row.getFields().add(blankSpace);
 309  0
                                                 numFieldsRemoved--;
 310  0
                                         }
 311  
                                 }
 312  
                         }
 313  0
                 }
 314  0
                 return rows;
 315  
         }
 316  
     
 317  
         protected Long getActualSizeIfTruncated(List result){
 318  0
                 Long actualSizeIfTruncated = new Long(0); 
 319  0
                 if(result instanceof CollectionIncomplete)
 320  0
                         actualSizeIfTruncated = ((CollectionIncomplete)result).getActualSizeIfTruncated();
 321  0
                 return actualSizeIfTruncated;
 322  
         }
 323  
         
 324  
         @SuppressWarnings("unchecked")
 325  
         protected List<RoleImpl> searchRoles(Map<String, String> roleSearchCriteria, boolean unbounded){
 326  0
                 List<RoleImpl> roles = (List<RoleImpl>)getLookupService().findCollectionBySearchHelper(
 327  
                                 RoleImpl.class, roleSearchCriteria, unbounded);
 328  0
                 String membersCrt = roleSearchCriteria.get("members.memberId");
 329  0
                 List<RoleImpl> roles2Remove = new ArrayList<RoleImpl>();
 330  0
                 if(StringUtils.isNotBlank(membersCrt)){
 331  0
                         List<String> memberSearchIds = new ArrayList<String>();
 332  0
                         List<String> memberIds = new ArrayList<String>(); 
 333  0
                         if(membersCrt.contains(KimConstants.KimUIConstants.OR_OPERATOR))
 334  0
                                 memberSearchIds = new ArrayList<String>(Arrays.asList(membersCrt.split("\\|")));
 335  
                         else
 336  0
                                 memberSearchIds.add(membersCrt);
 337  0
                         for(RoleImpl roleImpl : roles){        
 338  0
                                 List<RoleMemberImpl> roleMembers = roleImpl.getMembers();
 339  0
                                 memberIds.clear(); 
 340  0
                         CollectionUtils.filter(roleMembers, new Predicate() {
 341  
                                         public boolean evaluate(Object object) {
 342  0
                                                 RoleMemberImpl member = (RoleMemberImpl) object;
 343  
                                                 // keep active member
 344  0
                                                 return member.isActive();
 345  
                                         }
 346  
                                 });
 347  
                        
 348  0
                         if(roleMembers != null && !roleMembers.isEmpty()){
 349  0
                                 for(RoleMemberImpl memberImpl : roleMembers)
 350  0
                                         memberIds.add(memberImpl.getMemberId());
 351  0
                                 if(((List<String>)CollectionUtils.intersection(memberSearchIds, memberIds)).isEmpty())
 352  0
                                         roles2Remove.add(roleImpl);
 353  
                         }
 354  
                         else
 355  
                         {
 356  0
                                 roles2Remove.add(roleImpl);
 357  
                         }
 358  0
                         }
 359  
                 }
 360  0
                 if(!roles2Remove.isEmpty())
 361  0
                         roles.removeAll(roles2Remove);
 362  0
                 return roles;
 363  
         }
 364  
 
 365  
 
 366  
 }