Coverage Report - org.kuali.rice.kim.impl.type.KimTypeQualifierResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
KimTypeQualifierResolver
0%
0/106
0%
0/68
4.091
 
 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.impl.type;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.kew.engine.RouteContext;
 21  
 import org.kuali.rice.kim.api.group.Group;
 22  
 import org.kuali.rice.kim.api.group.GroupService;
 23  
 import org.kuali.rice.kim.api.role.RoleMembership;
 24  
 import org.kuali.rice.kim.api.role.RoleService;
 25  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 26  
 import org.kuali.rice.kim.api.type.KimType;
 27  
 import org.kuali.rice.kim.api.type.KimTypeInfoService;
 28  
 import org.kuali.rice.kim.api.type.KimTypeService;
 29  
 import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
 30  
 import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
 31  
 import org.kuali.rice.kim.bo.ui.PersonDocumentRole;
 32  
 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
 33  
 import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
 34  
 import org.kuali.rice.kim.document.IdentityManagementRoleDocument;
 35  
 import org.kuali.rice.kim.service.KIMServiceLocatorWeb;
 36  
 import org.kuali.rice.kim.util.KimConstants;
 37  
 import org.kuali.rice.krad.document.Document;
 38  
 import org.kuali.rice.krad.workflow.attribute.QualifierResolverBase;
 39  
 
 40  
 import java.util.ArrayList;
 41  
 import java.util.Collections;
 42  
 import java.util.HashMap;
 43  
 import java.util.List;
 44  
 import java.util.Map;
 45  
 
 46  
 /**
 47  
  * This is a description of what this class does - kellerj don't forget to fill this in. 
 48  
  * 
 49  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 50  
  *
 51  
  */
 52  0
 public class KimTypeQualifierResolver extends QualifierResolverBase {
 53  0
         private static final Logger LOG = Logger.getLogger(KimTypeQualifierResolver.class);
 54  
         
 55  
         protected static final String GROUP_ROUTE_LEVEL = "GroupType";
 56  
         protected static final String ROLE_ROUTE_LEVEL = "RoleType";
 57  
 
 58  
         private static KimTypeInfoService kimTypeInfoService;
 59  
         private static GroupService groupService;
 60  
         private static RoleService roleService;
 61  
         
 62  0
         protected static Map<String,KimTypeService> typeServices = new HashMap<String, KimTypeService>();
 63  
         
 64  
         /**
 65  
          * This overridden method ...
 66  
          * 
 67  
          * @see org.kuali.rice.kew.role.QualifierResolver#resolve(org.kuali.rice.kew.engine.RouteContext)
 68  
          */
 69  
         public List<Map<String, String>> resolve(RouteContext context) {
 70  0
         String routeLevel = context.getNodeInstance().getName();
 71  0
         Document document = getDocument(context);
 72  0
         List<Map<String, String>> qualifiers = new ArrayList<Map<String, String>>();
 73  0
         String customDocTypeName = null;
 74  
         
 75  0
         if ( document instanceof IdentityManagementGroupDocument ) {
 76  0
                 customDocTypeName = handleGroupDocument(qualifiers, (IdentityManagementGroupDocument)document, routeLevel);
 77  0
         } else if ( document instanceof IdentityManagementRoleDocument ) {
 78  0
                 customDocTypeName = handleRoleDocument(qualifiers, (IdentityManagementRoleDocument)document, routeLevel);
 79  0
         } else if ( document instanceof IdentityManagementPersonDocument ) {
 80  0
                 customDocTypeName = handlePersonDocument(qualifiers, (IdentityManagementPersonDocument)document, routeLevel);
 81  
         }
 82  
             // add standard components
 83  0
         decorateWithCommonQualifiers(qualifiers, context, customDocTypeName);
 84  
             // return the resulting list of Map<String, String>s
 85  0
                 return qualifiers;
 86  
         }
 87  
 
 88  
         protected KimTypeService getTypeService( String typeId ) {
 89  0
             KimTypeService typeService = typeServices.get(typeId);
 90  0
             if ( typeService == null ) {                       
 91  0
                 KimType typeInfo = getKimTypeInfoService().getKimType(typeId);
 92  0
                 if ( typeInfo != null ) {
 93  0
                         typeService = KIMServiceLocatorWeb.getKimTypeService(typeInfo);
 94  0
                         typeServices.put(typeId, typeService);
 95  
                 } else {
 96  0
                         LOG.warn( "Unable to retrieve KIM Type Info object for id: " + typeId );
 97  
                 }
 98  
             }
 99  0
             return typeService;
 100  
         }
 101  
 
 102  
     protected void putMatchingAttributesIntoQualifier( Map<String, String> qualifier, Map<String, String> itemAttributes, List<String> routingAttributes ) {
 103  0
                 if ( routingAttributes != null && !routingAttributes.isEmpty() ) {
 104  
                 // pull the qualifiers off the document object (group or role member)
 105  0
                     for ( String attribName : routingAttributes ) {
 106  0
                             qualifier.put( attribName, itemAttributes.get(attribName));
 107  
                     }
 108  
                 }
 109  0
         }
 110  
         
 111  
         protected String handleGroupDocument( List<Map<String, String>> qualifiers, IdentityManagementGroupDocument groupDoc, String routeLevel ) {
 112  
             // get the appropriate type service for the group being edited
 113  0
             String typeId = groupDoc.getGroupTypeId();
 114  0
             qualifiers.add( getGroupQualifier(groupDoc.getGroupId(), typeId, groupDoc.getQualifiersAsAttributes(), routeLevel) );
 115  
             
 116  0
         return null;
 117  
         }
 118  
 
 119  
         protected String handleRoleDocument( List<Map<String, String>> qualifiers, IdentityManagementRoleDocument roleDoc, String routeLevel ) {
 120  0
         String customDocTypeName = null;
 121  
 
 122  
 //        LOG.warn( "Role member data routing not implemented for the Role document yet!" );
 123  
             // get the appropriate type service for the group being edited
 124  0
             String typeId = roleDoc.getRoleTypeId();
 125  0
             KimTypeService typeService = getTypeService(typeId);
 126  0
             if ( typeService != null ) {
 127  
                     // QUESTION: can roles be modified in a way which requires routing?
 128  
                     // get the existing role members
 129  0
                     List<RoleMembership> currentRoleMembers = KimApiServiceLocator.getRoleService().getRoleMembers( Collections.singletonList( roleDoc.getRoleId() ), null );
 130  
                     // loop over the role members on the document, check  if added or removed
 131  0
                     for ( KimDocumentRoleMember rm : roleDoc.getMembers() ) {
 132  0
                             boolean foundMember = false;
 133  0
                             for ( RoleMembership rmi : currentRoleMembers ) {
 134  0
                                     if ( rmi.getRoleMemberId().equals( rm.getRoleMemberId() ) ) {
 135  0
                                             foundMember = true;
 136  0
                                             if ( !rm.isActive() ) { // don't need to check the role member information 
 137  
                                                                                             // - only active members are returned
 138  
                                                     // inactivated member, add a qualifier
 139  0
                                                     qualifiers.add( getRoleQualifier(rm.getRoleId(), typeId, typeService, rm.getQualifierAsMap(), routeLevel) );
 140  
                                             }
 141  
                                             break;
 142  
                                     }
 143  
                             }
 144  0
                             if ( !foundMember ) {
 145  0
                                     qualifiers.add( getRoleQualifier(rm.getRoleId(), typeId, typeService, rm.getQualifierAsMap(), routeLevel) );
 146  
                             }
 147  0
                     }
 148  
                     
 149  0
                     customDocTypeName = typeService.getWorkflowDocumentTypeName();
 150  
             }                
 151  0
             return customDocTypeName;
 152  
         }
 153  
         
 154  
         protected String handlePersonDocument( List<Map<String, String>> qualifiers, IdentityManagementPersonDocument personDoc, String routeLevel ) {
 155  
             // check the route level - see if we are doing groups or roles at the moment
 156  0
         String principalId = personDoc.getPrincipalId();
 157  0
         if ( GROUP_ROUTE_LEVEL.equals(routeLevel) ) {
 158  
                 // if groups, find any groups to which the user was added or removed
 159  
                 // get the type and service for each group
 160  
                 // handle as per the group document, a qualifier for each group
 161  0
                 List<String> currentGroups = getGroupService().getDirectGroupIdsForPrincipal(principalId);
 162  0
                 List<PersonDocumentGroup> groups = personDoc.getGroups();
 163  0
                 for ( PersonDocumentGroup group : groups ) {
 164  
                         // if they are being added to the group, add a qualifier set
 165  0
                         if ( group.isActive() && !currentGroups.contains( group.getGroupId() ) ) {
 166  
                             // pull the group to get its attributes for adding to the qualifier 
 167  0
                             Group kimGroup = getGroupService().getGroup(group.getGroupId());
 168  0
                                 qualifiers.add( getGroupQualifier( group.getGroupId(), kimGroup.getKimTypeId(), kimGroup.getAttributes(), routeLevel ) );
 169  0
                         }
 170  
                 }
 171  
                 // detect removed groups
 172  
                 // get the existing directly assigned groups for the person
 173  0
                 for ( String groupId : currentGroups ) {
 174  0
                         for ( PersonDocumentGroup group : groups ) {
 175  0
                                 if ( !group.isActive() ) {
 176  0
                                 Group kimGroup = getGroupService().getGroup(groupId);
 177  0
                                     qualifiers.add( getGroupQualifier( groupId, kimGroup.getKimTypeId(), kimGroup.getAttributes(), routeLevel ) );
 178  0
                                 }
 179  
                         }
 180  
                 }
 181  0
         } else if ( ROLE_ROUTE_LEVEL.equals(routeLevel) ) {
 182  
 //                getRoleService().get
 183  0
                 for ( PersonDocumentRole pdr : personDoc.getRoles() ) {
 184  0
                     KimTypeService typeService = getTypeService(pdr.getKimTypeId());
 185  0
                         for ( KimDocumentRoleMember rm : pdr.getRolePrncpls() ) {
 186  0
                                 boolean foundMember = false;
 187  0
                             for ( RoleMembership rmi : getRoleService().getRoleMembers( Collections.singletonList( rm.getRoleId() ), null ) ) {
 188  0
                                     if ( StringUtils.equals( rmi.getRoleMemberId(), rm.getRoleMemberId() ) ) {
 189  0
                                             foundMember = true;
 190  0
                                                 if ( !rm.isActive() ) { // don't need to check the role member information 
 191  
                                                                 // - only active members are returned
 192  
                                                         // inactivated member, add a qualifier
 193  0
                                                                 qualifiers.add( getRoleQualifier(rm.getRoleId(), pdr.getKimRoleType().getId(), typeService, rm.getQualifierAsMap(), routeLevel) );
 194  
                                                         }
 195  
                                                         break;
 196  
                                     }
 197  
                             }
 198  0
                                 if ( !foundMember ) {
 199  0
                                         qualifiers.add( getRoleQualifier(rm.getRoleId(), pdr.getKimRoleType().getId(), typeService, rm.getQualifierAsMap(), routeLevel) );
 200  
                                 }
 201  0
                         }
 202  0
                 }
 203  
                 // if roles, check the role member data for any roles added
 204  
                 // get the type and service for each role
 205  
                 // handle as for the role document, a qualifier for each role membership added
 206  
 //                LOG.warn( "Role-based data routing on the person document not implemented!" );
 207  
         }
 208  
         
 209  0
             return null;
 210  
         }
 211  
 
 212  
     protected Map<String, String> getGroupQualifier( String groupId, String kimTypeId, Map<String, String> groupAttributes, String routeLevel ) {
 213  0
                 Map<String, String> qualifier = new HashMap<String, String>();
 214  
                 // pull the group to get its attributes for adding to the qualifier 
 215  0
         qualifier.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID, kimTypeId);
 216  0
         qualifier.put(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, kimTypeId);
 217  0
         qualifier.put(KimConstants.PrimaryKeyConstants.GROUP_ID, groupId);
 218  0
             KimTypeService typeService = getTypeService(kimTypeId);
 219  0
             if ( typeService != null ) {
 220  
                     // check for the custom document type for the group
 221  0
                     String customDocTypeName = typeService.getWorkflowDocumentTypeName();
 222  0
                     if ( StringUtils.isNotBlank(customDocTypeName)) {
 223  0
                             qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, customDocTypeName );
 224  
                     }
 225  0
                     putMatchingAttributesIntoQualifier(qualifier, groupAttributes, typeService.getWorkflowRoutingAttributes(routeLevel) );
 226  
             }
 227  0
             return qualifier;
 228  
     }
 229  
     
 230  
     protected Map<String, String> getRoleQualifier( String roleId, String kimTypeId, KimTypeService typeService, Map<String, String> roleAttributes, String routeLevel ) {
 231  0
                 Map<String, String> qualifier = new HashMap<String, String>();
 232  
                 // pull the group to get its attributes for adding to the qualifier 
 233  0
         qualifier.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID, kimTypeId);
 234  0
         qualifier.put(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, kimTypeId);
 235  0
         qualifier.put("roleId", roleId);
 236  
                 // check for the custom document type for the group
 237  0
                 String customDocTypeName = typeService.getWorkflowDocumentTypeName();
 238  0
                 if ( StringUtils.isNotBlank(customDocTypeName)) {
 239  0
                         qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, customDocTypeName );
 240  
                 }
 241  0
                 putMatchingAttributesIntoQualifier(qualifier, roleAttributes, typeService.getWorkflowRoutingAttributes(routeLevel) );
 242  0
             return qualifier;
 243  
     }
 244  
         
 245  
         public KimTypeInfoService getKimTypeInfoService() {
 246  0
                 if ( kimTypeInfoService == null ) {
 247  0
                         kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
 248  
                 }
 249  0
                 return kimTypeInfoService;
 250  
         }
 251  
 
 252  
         public static GroupService getGroupService() {
 253  0
                 if ( groupService == null ) {
 254  0
                         groupService = KimApiServiceLocator.getGroupService();
 255  
                 }
 256  0
                 return groupService;
 257  
         }
 258  
 
 259  
         public static RoleService getRoleService() {
 260  0
                 if ( roleService == null ) {
 261  0
                         roleService = KimApiServiceLocator.getRoleService();
 262  
                 }
 263  0
                 return roleService;
 264  
         }
 265  
 }