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