| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 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.core.xml.dto.AttributeSet; | 
  | 27 |  |  import org.kuali.rice.kew.engine.RouteContext; | 
  | 28 |  |  import org.kuali.rice.kim.bo.group.dto.GroupInfo; | 
  | 29 |  |  import org.kuali.rice.kim.bo.role.dto.RoleMembershipInfo; | 
  | 30 |  |  import org.kuali.rice.kim.bo.types.dto.KimTypeInfo; | 
  | 31 |  |  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember; | 
  | 32 |  |  import org.kuali.rice.kim.bo.ui.PersonDocumentGroup; | 
  | 33 |  |  import org.kuali.rice.kim.bo.ui.PersonDocumentRole; | 
  | 34 |  |  import org.kuali.rice.kim.document.IdentityManagementGroupDocument; | 
  | 35 |  |  import org.kuali.rice.kim.document.IdentityManagementPersonDocument; | 
  | 36 |  |  import org.kuali.rice.kim.document.IdentityManagementRoleDocument; | 
  | 37 |  |  import org.kuali.rice.kim.service.*; | 
  | 38 |  |  import org.kuali.rice.kim.service.support.KimTypeService; | 
  | 39 |  |  import org.kuali.rice.kim.util.KimConstants; | 
  | 40 |  |  import org.kuali.rice.kns.document.Document; | 
  | 41 |  |  import org.kuali.rice.kns.workflow.attribute.QualifierResolverBase; | 
  | 42 |  |   | 
  | 43 |  |   | 
  | 44 |  |   | 
  | 45 |  |   | 
  | 46 |  |   | 
  | 47 |  |   | 
  | 48 |  |   | 
  | 49 | 0 |  public class KimTypeQualifierResolver extends QualifierResolverBase { | 
  | 50 | 0 |          private static final Logger LOG = Logger.getLogger(KimTypeQualifierResolver.class); | 
  | 51 |  |           | 
  | 52 |  |          protected static final String GROUP_ROUTE_LEVEL = "GroupType"; | 
  | 53 |  |          protected static final String ROLE_ROUTE_LEVEL = "RoleType"; | 
  | 54 |  |   | 
  | 55 |  |          private static KimTypeInfoService kimTypeInfoService; | 
  | 56 |  |          private static GroupService groupService; | 
  | 57 |  |          private static RoleService roleService; | 
  | 58 |  |           | 
  | 59 | 0 |          protected static Map<String,KimTypeService> typeServices = new HashMap<String, KimTypeService>(); | 
  | 60 |  |           | 
  | 61 |  |           | 
  | 62 |  |   | 
  | 63 |  |   | 
  | 64 |  |   | 
  | 65 |  |   | 
  | 66 |  |          public List<AttributeSet> resolve(RouteContext context) { | 
  | 67 | 0 |          String routeLevel = context.getNodeInstance().getName(); | 
  | 68 | 0 |          Document document = getDocument(context); | 
  | 69 | 0 |          List<AttributeSet> qualifiers = new ArrayList<AttributeSet>(); | 
  | 70 | 0 |          String customDocTypeName = null; | 
  | 71 |  |           | 
  | 72 | 0 |          if ( document instanceof IdentityManagementGroupDocument ) { | 
  | 73 | 0 |                  customDocTypeName = handleGroupDocument(qualifiers, (IdentityManagementGroupDocument)document, routeLevel); | 
  | 74 | 0 |          } else if ( document instanceof IdentityManagementRoleDocument ) { | 
  | 75 | 0 |                  customDocTypeName = handleRoleDocument(qualifiers, (IdentityManagementRoleDocument)document, routeLevel); | 
  | 76 | 0 |          } else if ( document instanceof IdentityManagementPersonDocument ) { | 
  | 77 | 0 |                  customDocTypeName = handlePersonDocument(qualifiers, (IdentityManagementPersonDocument)document, routeLevel); | 
  | 78 |  |          } | 
  | 79 |  |               | 
  | 80 | 0 |          decorateWithCommonQualifiers(qualifiers, context, customDocTypeName); | 
  | 81 |  |               | 
  | 82 | 0 |                  return qualifiers; | 
  | 83 |  |          } | 
  | 84 |  |   | 
  | 85 |  |          protected KimTypeService getTypeService( String typeId ) { | 
  | 86 | 0 |              KimTypeService typeService = typeServices.get(typeId); | 
  | 87 | 0 |              if ( typeService == null ) {                        | 
  | 88 | 0 |                  KimTypeInfo typeInfo = getKimTypeInfoService().getKimType(typeId); | 
  | 89 | 0 |                  if ( typeInfo != null ) { | 
  | 90 | 0 |                          typeService = KIMServiceLocatorWeb.getKimTypeService(typeInfo); | 
  | 91 | 0 |                          typeServices.put(typeId, typeService); | 
  | 92 |  |                  } else { | 
  | 93 | 0 |                          LOG.warn( "Unable to retrieve KIM Type Info object for id: " + typeId ); | 
  | 94 |  |                  } | 
  | 95 |  |              } | 
  | 96 | 0 |              return typeService; | 
  | 97 |  |          } | 
  | 98 |  |           | 
  | 99 |  |          protected void putMatchingAttributesIntoQualifier( AttributeSet qualifier, AttributeSet itemAttributes, List<String> routingAttributes ) { | 
  | 100 | 0 |                  if ( routingAttributes != null && !routingAttributes.isEmpty() ) { | 
  | 101 |  |                   | 
  | 102 | 0 |                      for ( String attribName : routingAttributes ) { | 
  | 103 | 0 |                              qualifier.put( attribName, itemAttributes.get(attribName)); | 
  | 104 |  |                      } | 
  | 105 |  |                  } | 
  | 106 | 0 |          } | 
  | 107 |  |           | 
  | 108 |  |          protected String handleGroupDocument( List<AttributeSet> qualifiers, IdentityManagementGroupDocument groupDoc, String routeLevel ) { | 
  | 109 |  |               | 
  | 110 | 0 |              String typeId = groupDoc.getGroupTypeId(); | 
  | 111 | 0 |              qualifiers.add( getGroupQualifier(groupDoc.getGroupId(), typeId, groupDoc.getQualifiersAsAttributeSet(), routeLevel) ); | 
  | 112 |  |               | 
  | 113 | 0 |          return null; | 
  | 114 |  |          } | 
  | 115 |  |   | 
  | 116 |  |          protected String handleRoleDocument( List<AttributeSet> qualifiers, IdentityManagementRoleDocument roleDoc, String routeLevel ) { | 
  | 117 | 0 |          String customDocTypeName = null; | 
  | 118 |  |   | 
  | 119 |  |   | 
  | 120 |  |               | 
  | 121 | 0 |              String typeId = roleDoc.getRoleTypeId(); | 
  | 122 | 0 |              KimTypeService typeService = getTypeService(typeId); | 
  | 123 | 0 |              if ( typeService != null ) { | 
  | 124 |  |                       | 
  | 125 |  |                       | 
  | 126 | 0 |                      List<RoleMembershipInfo> currentRoleMembers = KIMServiceLocator.getRoleService().getRoleMembers( Collections.singletonList( roleDoc.getRoleId() ), null ); | 
  | 127 |  |                       | 
  | 128 | 0 |                      for ( KimDocumentRoleMember rm : roleDoc.getMembers() ) { | 
  | 129 | 0 |                              boolean foundMember = false; | 
  | 130 | 0 |                              for ( RoleMembershipInfo rmi : currentRoleMembers ) { | 
  | 131 | 0 |                                      if ( rmi.getRoleMemberId().equals( rm.getRoleMemberId() ) ) { | 
  | 132 | 0 |                                              foundMember = true; | 
  | 133 | 0 |                                              if ( !rm.isActive() ) {  | 
  | 134 |  |                                                                                               | 
  | 135 |  |                                                       | 
  | 136 | 0 |                                                      qualifiers.add( getRoleQualifier(rm.getRoleId(), typeId, typeService, rm.getQualifierAsAttributeSet(), routeLevel) ); | 
  | 137 |  |                                              } | 
  | 138 |  |                                              break; | 
  | 139 |  |                                      } | 
  | 140 |  |                              } | 
  | 141 | 0 |                              if ( !foundMember ) { | 
  | 142 | 0 |                                      qualifiers.add( getRoleQualifier(rm.getRoleId(), typeId, typeService, rm.getQualifierAsAttributeSet(), routeLevel) ); | 
  | 143 |  |                              } | 
  | 144 | 0 |                      } | 
  | 145 |  |                       | 
  | 146 | 0 |                      customDocTypeName = typeService.getWorkflowDocumentTypeName(); | 
  | 147 |  |              }                 | 
  | 148 | 0 |              return customDocTypeName; | 
  | 149 |  |          } | 
  | 150 |  |           | 
  | 151 |  |          protected String handlePersonDocument( List<AttributeSet> qualifiers, IdentityManagementPersonDocument personDoc, String routeLevel ) { | 
  | 152 |  |               | 
  | 153 | 0 |          String principalId = personDoc.getPrincipalId(); | 
  | 154 | 0 |          if ( GROUP_ROUTE_LEVEL.equals(routeLevel) ) { | 
  | 155 |  |                   | 
  | 156 |  |                   | 
  | 157 |  |                   | 
  | 158 | 0 |                  List<String> currentGroups = getGroupService().getDirectGroupIdsForPrincipal(principalId); | 
  | 159 | 0 |                  List<PersonDocumentGroup> groups = personDoc.getGroups(); | 
  | 160 | 0 |                  for ( PersonDocumentGroup group : groups ) { | 
  | 161 |  |                           | 
  | 162 | 0 |                          if ( group.isActive() && !currentGroups.contains( group.getGroupId() ) ) { | 
  | 163 |  |                               | 
  | 164 | 0 |                              GroupInfo kimGroup = getGroupService().getGroupInfo(group.getGroupId()); | 
  | 165 | 0 |                                  qualifiers.add( getGroupQualifier( group.getGroupId(), kimGroup.getKimTypeId(), kimGroup.getAttributes(), routeLevel ) ); | 
  | 166 | 0 |                          } | 
  | 167 |  |                  } | 
  | 168 |  |                   | 
  | 169 |  |                   | 
  | 170 | 0 |                  for ( String groupId : currentGroups ) { | 
  | 171 | 0 |                          for ( PersonDocumentGroup group : groups ) { | 
  | 172 | 0 |                                  if ( !group.isActive() ) { | 
  | 173 | 0 |                                  GroupInfo kimGroup = getGroupService().getGroupInfo(groupId); | 
  | 174 | 0 |                                      qualifiers.add( getGroupQualifier( groupId, kimGroup.getKimTypeId(), kimGroup.getAttributes(), routeLevel ) ); | 
  | 175 | 0 |                                  } | 
  | 176 |  |                          } | 
  | 177 |  |                  } | 
  | 178 | 0 |          } else if ( ROLE_ROUTE_LEVEL.equals(routeLevel) ) { | 
  | 179 |  |   | 
  | 180 | 0 |                  for ( PersonDocumentRole pdr : personDoc.getRoles() ) { | 
  | 181 | 0 |                      KimTypeService typeService = getTypeService(pdr.getKimTypeId()); | 
  | 182 | 0 |                          for ( KimDocumentRoleMember rm : pdr.getRolePrncpls() ) { | 
  | 183 | 0 |                                  boolean foundMember = false; | 
  | 184 | 0 |                              for ( RoleMembershipInfo rmi : getRoleService().getRoleMembers( Collections.singletonList( rm.getRoleId() ), null ) ) { | 
  | 185 | 0 |                                      if ( StringUtils.equals( rmi.getRoleMemberId(), rm.getRoleMemberId() ) ) { | 
  | 186 | 0 |                                              foundMember = true; | 
  | 187 | 0 |                                                  if ( !rm.isActive() ) {  | 
  | 188 |  |                                                                   | 
  | 189 |  |                                                           | 
  | 190 | 0 |                                                                  qualifiers.add( getRoleQualifier(rm.getRoleId(), pdr.getKimRoleType().getKimTypeId(), typeService, rm.getQualifierAsAttributeSet(), routeLevel) ); | 
  | 191 |  |                                                          } | 
  | 192 |  |                                                          break; | 
  | 193 |  |                                      } | 
  | 194 |  |                              } | 
  | 195 | 0 |                                  if ( !foundMember ) { | 
  | 196 | 0 |                                          qualifiers.add( getRoleQualifier(rm.getRoleId(), pdr.getKimRoleType().getKimTypeId(), typeService, rm.getQualifierAsAttributeSet(), routeLevel) ); | 
  | 197 |  |                                  } | 
  | 198 | 0 |                          } | 
  | 199 | 0 |                  } | 
  | 200 |  |                   | 
  | 201 |  |                   | 
  | 202 |  |                   | 
  | 203 |  |   | 
  | 204 |  |          } | 
  | 205 |  |           | 
  | 206 | 0 |              return null; | 
  | 207 |  |          } | 
  | 208 |  |   | 
  | 209 |  |      protected AttributeSet getGroupQualifier( String groupId, String kimTypeId, AttributeSet groupAttributes, String routeLevel ) { | 
  | 210 | 0 |                  AttributeSet qualifier = new AttributeSet();                                 | 
  | 211 |  |                   | 
  | 212 | 0 |          qualifier.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID, kimTypeId); | 
  | 213 | 0 |          qualifier.put(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, kimTypeId); | 
  | 214 | 0 |          qualifier.put(KimConstants.PrimaryKeyConstants.GROUP_ID, groupId); | 
  | 215 | 0 |              KimTypeService typeService = getTypeService(kimTypeId); | 
  | 216 | 0 |              if ( typeService != null ) { | 
  | 217 |  |                       | 
  | 218 | 0 |                      String customDocTypeName = typeService.getWorkflowDocumentTypeName(); | 
  | 219 | 0 |                      if ( StringUtils.isNotBlank(customDocTypeName)) { | 
  | 220 | 0 |                              qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, customDocTypeName ); | 
  | 221 |  |                      } | 
  | 222 | 0 |                      putMatchingAttributesIntoQualifier(qualifier, groupAttributes, typeService.getWorkflowRoutingAttributes(routeLevel) ); | 
  | 223 |  |              } | 
  | 224 | 0 |              return qualifier; | 
  | 225 |  |      } | 
  | 226 |  |       | 
  | 227 |  |      protected AttributeSet getRoleQualifier( String roleId, String kimTypeId, KimTypeService typeService, AttributeSet roleAttributes, String routeLevel ) { | 
  | 228 | 0 |                  AttributeSet qualifier = new AttributeSet();                                 | 
  | 229 |  |                   | 
  | 230 | 0 |          qualifier.put(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID, kimTypeId); | 
  | 231 | 0 |          qualifier.put(KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, kimTypeId); | 
  | 232 | 0 |          qualifier.put(KimConstants.PrimaryKeyConstants.ROLE_ID, roleId); | 
  | 233 |  |                   | 
  | 234 | 0 |                  String customDocTypeName = typeService.getWorkflowDocumentTypeName(); | 
  | 235 | 0 |                  if ( StringUtils.isNotBlank(customDocTypeName)) { | 
  | 236 | 0 |                          qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, customDocTypeName ); | 
  | 237 |  |                  } | 
  | 238 | 0 |                  putMatchingAttributesIntoQualifier(qualifier, roleAttributes, typeService.getWorkflowRoutingAttributes(routeLevel) ); | 
  | 239 | 0 |              return qualifier; | 
  | 240 |  |      } | 
  | 241 |  |           | 
  | 242 |  |          public KimTypeInfoService getKimTypeInfoService() { | 
  | 243 | 0 |                  if ( kimTypeInfoService == null ) { | 
  | 244 | 0 |                          kimTypeInfoService = KIMServiceLocatorWeb.getTypeInfoService(); | 
  | 245 |  |                  } | 
  | 246 | 0 |                  return kimTypeInfoService; | 
  | 247 |  |          } | 
  | 248 |  |   | 
  | 249 |  |          public static GroupService getGroupService() { | 
  | 250 | 0 |                  if ( groupService == null ) { | 
  | 251 | 0 |                          groupService = KIMServiceLocator.getGroupService(); | 
  | 252 |  |                  } | 
  | 253 | 0 |                  return groupService; | 
  | 254 |  |          } | 
  | 255 |  |   | 
  | 256 |  |          public static RoleService getRoleService() { | 
  | 257 | 0 |                  if ( roleService == null ) { | 
  | 258 | 0 |                          roleService = KIMServiceLocator.getRoleService(); | 
  | 259 |  |                  } | 
  | 260 | 0 |                  return roleService; | 
  | 261 |  |          } | 
  | 262 |  |  } |