| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 48 | |
|
| 49 | |
|
| 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 | |
|
| 66 | |
|
| 67 | |
|
| 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 | |
|
| 83 | 0 | decorateWithCommonQualifiers(qualifiers, context, customDocTypeName); |
| 84 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 123 | |
|
| 124 | 0 | String typeId = roleDoc.getRoleTypeId(); |
| 125 | 0 | KimTypeService typeService = getTypeService(typeId); |
| 126 | 0 | if ( typeService != null ) { |
| 127 | |
|
| 128 | |
|
| 129 | 0 | List<RoleMembership> currentRoleMembers = KimApiServiceLocator.getRoleService().getRoleMembers( Collections.singletonList( roleDoc.getRoleId() ), null ); |
| 130 | |
|
| 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() ) { |
| 137 | |
|
| 138 | |
|
| 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 | |
|
| 156 | 0 | String principalId = personDoc.getPrincipalId(); |
| 157 | 0 | if ( GROUP_ROUTE_LEVEL.equals(routeLevel) ) { |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | 0 | List<String> currentGroups = getGroupService().getDirectGroupIdsForPrincipal(principalId); |
| 162 | 0 | List<PersonDocumentGroup> groups = personDoc.getGroups(); |
| 163 | 0 | for ( PersonDocumentGroup group : groups ) { |
| 164 | |
|
| 165 | 0 | if ( group.isActive() && !currentGroups.contains( group.getGroupId() ) ) { |
| 166 | |
|
| 167 | 0 | Group kimGroup = getGroupService().getGroup(group.getGroupId()); |
| 168 | 0 | qualifiers.add( getGroupQualifier( group.getGroupId(), kimGroup.getKimTypeId(), kimGroup.getAttributes(), routeLevel ) ); |
| 169 | 0 | } |
| 170 | |
} |
| 171 | |
|
| 172 | |
|
| 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 | |
|
| 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() ) { |
| 191 | |
|
| 192 | |
|
| 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 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |