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.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 | |
|
50 | |
|
51 | |
|
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 | |
|
68 | |
|
69 | |
|
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 | |
|
85 | 0 | decorateWithCommonQualifiers(qualifiers, context, customDocTypeName); |
86 | |
|
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 | |
|
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 | |
|
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 | |
|
125 | |
|
126 | 0 | String typeId = roleDoc.getRoleTypeId(); |
127 | 0 | KimTypeService typeService = getTypeService(typeId); |
128 | 0 | if ( typeService != null ) { |
129 | |
|
130 | |
|
131 | 0 | List<RoleMembershipInfo> currentRoleMembers = KIMServiceLocator.getRoleService().getRoleMembers( Collections.singletonList( roleDoc.getRoleId() ), null ); |
132 | |
|
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() ) { |
139 | |
|
140 | |
|
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 | |
|
158 | 0 | String principalId = personDoc.getPrincipalId(); |
159 | 0 | if ( GROUP_ROUTE_LEVEL.equals(routeLevel) ) { |
160 | |
|
161 | |
|
162 | |
|
163 | 0 | List<String> currentGroups = getGroupService().getDirectGroupIdsForPrincipal(principalId); |
164 | 0 | List<PersonDocumentGroup> groups = personDoc.getGroups(); |
165 | 0 | for ( PersonDocumentGroup group : groups ) { |
166 | |
|
167 | 0 | if ( group.isActive() && !currentGroups.contains( group.getGroupId() ) ) { |
168 | |
|
169 | 0 | GroupInfo kimGroup = getGroupService().getGroupInfo(group.getGroupId()); |
170 | 0 | qualifiers.add( getGroupQualifier( group.getGroupId(), kimGroup.getKimTypeId(), kimGroup.getAttributes(), routeLevel ) ); |
171 | 0 | } |
172 | |
} |
173 | |
|
174 | |
|
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 | |
|
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() ) { |
193 | |
|
194 | |
|
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 | |
|
206 | |
|
207 | |
|
208 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
} |