1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.impl.identity; |
17 | |
|
18 | |
import org.apache.commons.beanutils.PropertyUtils; |
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.Logger; |
21 | |
import org.kuali.rice.core.api.CoreApiServiceLocator; |
22 | |
import org.kuali.rice.core.api.criteria.Predicate; |
23 | |
import org.kuali.rice.core.api.criteria.PredicateUtils; |
24 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
25 | |
import org.kuali.rice.kim.api.identity.IdentityService; |
26 | |
import org.kuali.rice.kim.api.identity.Person; |
27 | |
import org.kuali.rice.kim.api.identity.PersonService; |
28 | |
import org.kuali.rice.kim.api.identity.entity.EntityDefault; |
29 | |
import org.kuali.rice.kim.api.identity.entity.EntityDefaultQueryResults; |
30 | |
import org.kuali.rice.kim.api.identity.external.EntityExternalIdentifierType; |
31 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
32 | |
import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault; |
33 | |
import org.kuali.rice.kim.api.role.RoleService; |
34 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
35 | |
import org.kuali.rice.kim.impl.KIMPropertyConstants; |
36 | |
import org.kuali.rice.kns.service.BusinessObjectMetaDataService; |
37 | |
import org.kuali.rice.kns.service.KNSServiceLocator; |
38 | |
import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService; |
39 | |
import org.kuali.rice.krad.bo.BusinessObject; |
40 | |
import org.kuali.rice.krad.bo.DataObjectRelationship; |
41 | |
import org.kuali.rice.krad.lookup.CollectionIncomplete; |
42 | |
import org.kuali.rice.krad.util.KRADConstants; |
43 | |
import org.kuali.rice.krad.util.KRADPropertyConstants; |
44 | |
import org.kuali.rice.krad.util.ObjectUtils; |
45 | |
|
46 | |
import java.security.GeneralSecurityException; |
47 | |
import java.util.ArrayList; |
48 | |
import java.util.Collection; |
49 | |
import java.util.Collections; |
50 | |
import java.util.HashMap; |
51 | |
import java.util.HashSet; |
52 | |
import java.util.Iterator; |
53 | |
import java.util.List; |
54 | |
import java.util.Map; |
55 | |
import java.util.Set; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 0 | public class PersonServiceImpl implements PersonService { |
64 | |
|
65 | 0 | private static Logger LOG = Logger.getLogger( PersonServiceImpl.class ); |
66 | |
protected static final String ENTITY_EXT_ID_PROPERTY_PREFIX = "externalIdentifiers."; |
67 | |
protected static final String ENTITY_AFFILIATION_PROPERTY_PREFIX = "affiliations."; |
68 | |
protected static final String ENTITY_TYPE_PROPERTY_PREFIX = "entityTypeContactInfos."; |
69 | |
protected static final String ENTITY_EMAIL_PROPERTY_PREFIX = "entityTypeContactInfos.emailAddresses."; |
70 | |
protected static final String ENTITY_PHONE_PROPERTY_PREFIX = "entityTypeContactInfos.phoneNumbers."; |
71 | |
protected static final String ENTITY_ADDRESS_PROPERTY_PREFIX = "entityTypeContactInfos.addresses."; |
72 | |
protected static final String ENTITY_NAME_PROPERTY_PREFIX = "names."; |
73 | |
protected static final String PRINCIPAL_PROPERTY_PREFIX = "principals."; |
74 | |
protected static final String ENTITY_EMPLOYEE_ID_PROPERTY_PREFIX = "employmentInformation."; |
75 | |
|
76 | |
protected static final String EXTENSION = "extension"; |
77 | |
|
78 | |
private IdentityService identityService; |
79 | |
private RoleService roleService; |
80 | |
private BusinessObjectMetaDataService businessObjectMetaDataService; |
81 | |
private MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService; |
82 | |
|
83 | 0 | protected List<String> personEntityTypeCodes = new ArrayList<String>( 4 ); |
84 | |
|
85 | 0 | private String personEntityTypeLookupCriteria = null; |
86 | |
|
87 | 0 | protected Map<String,String> baseLookupCriteria = new HashMap<String,String>(); |
88 | 0 | protected Map<String,String> criteriaConversion = new HashMap<String,String>(); |
89 | 0 | protected ArrayList<String> personCachePropertyNames = new ArrayList<String>(); |
90 | |
{ |
91 | |
|
92 | |
|
93 | 0 | baseLookupCriteria.put( KIMPropertyConstants.Person.ACTIVE, "Y" ); |
94 | 0 | baseLookupCriteria.put( ENTITY_TYPE_PROPERTY_PREFIX + KRADPropertyConstants.ACTIVE, "Y" ); |
95 | |
|
96 | |
|
97 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.ENTITY_ID, KIMPropertyConstants.Entity.ID); |
98 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.ACTIVE, PRINCIPAL_PROPERTY_PREFIX + KRADPropertyConstants.ACTIVE ); |
99 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.PRINCIPAL_ID, PRINCIPAL_PROPERTY_PREFIX + KIMPropertyConstants.Person.PRINCIPAL_ID ); |
100 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.PRINCIPAL_NAME, PRINCIPAL_PROPERTY_PREFIX + KIMPropertyConstants.Person.PRINCIPAL_NAME ); |
101 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.FIRST_NAME, "names.firstName" ); |
102 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.LAST_NAME, "names.lastName" ); |
103 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.MIDDLE_NAME, "names.middleName" ); |
104 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.EMAIL_ADDRESS, "entityTypeContactInfos.emailAddresses.emailAddress" ); |
105 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.PHONE_NUMBER, "entityTypeContactInfos.phoneNumbers.phoneNumber" ); |
106 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.ADDRESS_LINE_1, "entityTypeContactInfos.addresses.line1" ); |
107 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.ADDRESS_LINE_2, "entityTypeContactInfos.addresses.line2" ); |
108 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.ADDRESS_LINE_3, "entityTypeContactInfos.addresses.line3" ); |
109 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.CITY, "entityTypeContactInfos.addresses.city" ); |
110 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.STATE_CODE, "entityTypeContactInfos.addresses.stateProvinceCode" ); |
111 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.POSTAL_CODE, "entityTypeContactInfos.addresses.postalCode" ); |
112 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.COUNTRY_CODE, "entityTypeContactInfos.addresses.countryCode" ); |
113 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.CAMPUS_CODE, "affiliations.campusCode" ); |
114 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.AFFILIATION_TYPE_CODE, "affiliations.affiliationTypeCode" ); |
115 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.EXTERNAL_IDENTIFIER_TYPE_CODE, "externalIdentifiers.externalIdentifierTypeCode" ); |
116 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.EXTERNAL_ID, "externalIdentifiers.externalId" ); |
117 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.EMPLOYEE_TYPE_CODE, "employmentInformation.employeeTypeCode" ); |
118 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.EMPLOYEE_STATUS_CODE, "employmentInformation.employeeStatusCode" ); |
119 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.EMPLOYEE_ID, "employmentInformation.employeeId" ); |
120 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.BASE_SALARY_AMOUNT, "employmentInformation.baseSalaryAmount" ); |
121 | 0 | criteriaConversion.put( KIMPropertyConstants.Person.PRIMARY_DEPARTMENT_CODE, "employmentInformation.primaryDepartmentCode" ); |
122 | |
|
123 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.PRINCIPAL_ID ); |
124 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.PRINCIPAL_NAME ); |
125 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.ENTITY_ID ); |
126 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.FIRST_NAME ); |
127 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.LAST_NAME ); |
128 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.MIDDLE_NAME ); |
129 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.CAMPUS_CODE ); |
130 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.EMPLOYEE_ID ); |
131 | 0 | personCachePropertyNames.add( KIMPropertyConstants.Person.PRIMARY_DEPARTMENT_CODE ); |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public Person getPerson(String principalId) { |
139 | 0 | if ( StringUtils.isBlank(principalId) ) { |
140 | 0 | return null; |
141 | |
} |
142 | |
|
143 | |
|
144 | 0 | final Principal principal = getIdentityService().getPrincipal( principalId ); |
145 | |
|
146 | 0 | if ( principal != null ) { |
147 | 0 | final EntityDefault entity = getIdentityService().getEntityDefault(principal.getEntityId()); |
148 | |
|
149 | |
|
150 | 0 | if (entity != null ) { |
151 | 0 | return convertEntityToPerson( entity, principal ); |
152 | |
} |
153 | |
} |
154 | 0 | return null; |
155 | |
} |
156 | |
|
157 | |
protected PersonImpl convertEntityToPerson( EntityDefault entity, Principal principal ) { |
158 | |
try { |
159 | |
|
160 | 0 | for ( String entityTypeCode : personEntityTypeCodes ) { |
161 | 0 | EntityTypeContactInfoDefault entType = entity.getEntityType( entityTypeCode ); |
162 | |
|
163 | 0 | if ( entType == null ) { |
164 | 0 | continue; |
165 | |
} |
166 | |
|
167 | |
|
168 | 0 | return new PersonImpl( principal, entity, entityTypeCode ); |
169 | |
} |
170 | 0 | return null; |
171 | 0 | } catch ( Exception ex ) { |
172 | |
|
173 | 0 | if ( ex instanceof RuntimeException ) { |
174 | 0 | throw (RuntimeException)ex; |
175 | |
} |
176 | 0 | throw new RuntimeException( "Problem building person object", ex ); |
177 | |
} |
178 | |
} |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public Person getPersonByPrincipalName(String principalName) { |
185 | 0 | if ( StringUtils.isBlank(principalName) ) { |
186 | 0 | return null; |
187 | |
} |
188 | |
|
189 | |
|
190 | 0 | final Principal principal = getIdentityService().getPrincipalByPrincipalName( principalName ); |
191 | |
|
192 | 0 | if ( principal != null ) { |
193 | 0 | final EntityDefault entity = getIdentityService().getEntityDefault(principal.getEntityId()); |
194 | |
|
195 | |
|
196 | 0 | if ( entity != null ) { |
197 | 0 | return convertEntityToPerson( entity, principal ); |
198 | |
} |
199 | |
} |
200 | 0 | return null; |
201 | |
} |
202 | |
|
203 | |
public Person getPersonByEmployeeId(String employeeId) { |
204 | 0 | if ( StringUtils.isBlank( employeeId ) ) { |
205 | 0 | return null; |
206 | |
} |
207 | |
|
208 | 0 | final List<Person> people = findPeople( Collections.singletonMap(KIMPropertyConstants.Person.EMPLOYEE_ID, employeeId) ); |
209 | 0 | if ( !people.isEmpty() ) { |
210 | 0 | return people.get(0); |
211 | |
|
212 | |
} |
213 | 0 | return null; |
214 | |
} |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public List<Person> findPeople(Map<String, String> criteria) { |
220 | 0 | return findPeople(criteria, true); |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
public List<Person> findPeople(Map<String, String> criteria, boolean unbounded) { |
227 | 0 | List<Person> people = null; |
228 | |
|
229 | 0 | if ( criteria == null ) { |
230 | 0 | criteria = Collections.emptyMap(); |
231 | |
} |
232 | |
|
233 | 0 | criteria = new HashMap<String, String>( criteria ); |
234 | |
|
235 | |
|
236 | 0 | String roleName = criteria.get( "lookupRoleName" ); |
237 | 0 | String namespaceCode = criteria.get( "lookupRoleNamespaceCode" ); |
238 | 0 | criteria.remove("lookupRoleName"); |
239 | 0 | criteria.remove("lookupRoleNamespaceCode"); |
240 | 0 | if ( StringUtils.isNotBlank(namespaceCode) && StringUtils.isNotBlank(roleName) ) { |
241 | 0 | Integer searchResultsLimit = org.kuali.rice.kns.lookup.LookupUtils.getSearchResultsLimit(PersonImpl.class); |
242 | 0 | int searchResultsLimitInt = Integer.MAX_VALUE; |
243 | 0 | if (searchResultsLimit != null) { |
244 | 0 | searchResultsLimitInt = searchResultsLimit.intValue(); |
245 | |
} |
246 | 0 | if ( LOG.isDebugEnabled() ) { |
247 | 0 | LOG.debug("Performing Person search including role filter: " + namespaceCode + "/" + roleName ); |
248 | |
} |
249 | 0 | if ( criteria.size() == 1 && criteria.containsKey(KIMPropertyConstants.Person.ACTIVE) ) { |
250 | 0 | if ( LOG.isDebugEnabled() ) { |
251 | 0 | LOG.debug( "Only active criteria specified, running role search first" ); |
252 | |
} |
253 | |
|
254 | 0 | Collection<String> principalIds = getRoleService().getRoleMemberPrincipalIds(namespaceCode, roleName, Collections.<String, String>emptyMap()); |
255 | 0 | StringBuffer sb = new StringBuffer(principalIds.size()*15); |
256 | 0 | Iterator<String> pi = principalIds.iterator(); |
257 | 0 | while ( pi.hasNext() ) { |
258 | 0 | sb.append( pi.next() ); |
259 | 0 | if ( pi.hasNext() ) sb.append( '|' ); |
260 | |
} |
261 | |
|
262 | 0 | criteria.put( KIMPropertyConstants.Person.PRINCIPAL_ID, sb.toString() ); |
263 | 0 | people = findPeopleInternal(criteria, false); |
264 | 0 | } else if ( !criteria.isEmpty() ) { |
265 | 0 | if ( LOG.isDebugEnabled() ) { |
266 | 0 | LOG.debug( "Person criteria also specified, running that search first" ); |
267 | |
} |
268 | |
|
269 | 0 | people = findPeopleInternal(criteria, true); |
270 | |
|
271 | |
|
272 | 0 | List<String> principalIds = peopleToPrincipalIds( people ); |
273 | |
|
274 | 0 | principalIds = getRoleService().getPrincipalIdSubListWithRole(principalIds, namespaceCode, roleName, Collections.<String, String>emptyMap()); |
275 | |
|
276 | 0 | if ( !unbounded && principalIds.size() > searchResultsLimitInt ) { |
277 | 0 | int actualResultSize = principalIds.size(); |
278 | |
|
279 | 0 | principalIds = new ArrayList<String>(principalIds).subList(0, searchResultsLimitInt); |
280 | 0 | people = getPeople(principalIds); |
281 | 0 | people = new CollectionIncomplete<Person>( people.subList(0, searchResultsLimitInt), new Long(actualResultSize) ); |
282 | 0 | } else { |
283 | 0 | people = getPeople(principalIds); |
284 | |
} |
285 | 0 | } else { |
286 | 0 | if ( LOG.isDebugEnabled() ) { |
287 | 0 | LOG.debug( "No Person criteria specified - only using role service." ); |
288 | |
} |
289 | |
|
290 | 0 | Collection<String> principalIds = getRoleService().getRoleMemberPrincipalIds(namespaceCode, roleName, Collections.<String, String>emptyMap()); |
291 | 0 | if ( !unbounded && principalIds.size() > searchResultsLimitInt ) { |
292 | 0 | int actualResultSize = principalIds.size(); |
293 | |
|
294 | 0 | principalIds = new ArrayList<String>(principalIds).subList(0, searchResultsLimitInt); |
295 | 0 | people = getPeople(principalIds); |
296 | 0 | people = new CollectionIncomplete<Person>( people.subList(0, searchResultsLimitInt), new Long(actualResultSize) ); |
297 | 0 | } else { |
298 | 0 | people = getPeople(principalIds); |
299 | |
} |
300 | |
} |
301 | 0 | } else { |
302 | 0 | if ( LOG.isDebugEnabled() ) { |
303 | 0 | LOG.debug( "No Role criteria specified, running person lookup as normal." ); |
304 | |
} |
305 | 0 | people = findPeopleInternal(criteria, unbounded); |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | 0 | Set<String> peopleNoDupsSet = new HashSet<String>(); |
313 | 0 | List<Person> peopleNoDupsList = new ArrayList<Person>(); |
314 | |
|
315 | 0 | for (Iterator<Person> iter = people.iterator(); iter.hasNext(); ) { |
316 | 0 | Person person = iter.next(); |
317 | 0 | if (peopleNoDupsSet.add(person.getEntityId() + person.getPrincipalId() + person.getPrincipalName())) { |
318 | 0 | peopleNoDupsList.add(person); |
319 | |
} |
320 | 0 | } |
321 | |
|
322 | 0 | people.clear(); |
323 | 0 | people.addAll(peopleNoDupsList); |
324 | |
|
325 | 0 | return people; |
326 | |
} |
327 | |
|
328 | |
@SuppressWarnings("unchecked") |
329 | |
protected List<Person> findPeopleInternal(Map<String,String> criteria, boolean unbounded ) { |
330 | |
|
331 | |
|
332 | |
|
333 | 0 | Map<String,String> entityCriteria = convertPersonPropertiesToEntityProperties( criteria ); |
334 | |
|
335 | 0 | Predicate predicate = PredicateUtils.convertMapToPredicate(entityCriteria); |
336 | |
|
337 | 0 | QueryByCriteria.Builder queryBuilder = QueryByCriteria.Builder.create(); |
338 | 0 | queryBuilder.setPredicates(predicate); |
339 | |
|
340 | 0 | List<Person> people = new ArrayList<Person>(); |
341 | |
|
342 | 0 | EntityDefaultQueryResults qr = getIdentityService().findEntityDefaults( queryBuilder.build() ); |
343 | |
|
344 | 0 | for ( EntityDefault e : qr.getResults() ) { |
345 | |
|
346 | 0 | for ( Principal p : e.getPrincipals() ) { |
347 | 0 | people.add( convertEntityToPerson( e, p ) ); |
348 | |
} |
349 | |
} |
350 | |
|
351 | 0 | return people; |
352 | |
} |
353 | |
|
354 | |
public Map<String,String> convertPersonPropertiesToEntityProperties( Map<String,String> criteria ) { |
355 | 0 | if ( LOG.isDebugEnabled() ) { |
356 | 0 | LOG.debug( "convertPersonPropertiesToEntityProperties: " + criteria ); |
357 | |
} |
358 | 0 | boolean nameCriteria = false; |
359 | 0 | boolean addressCriteria = false; |
360 | 0 | boolean externalIdentifierCriteria = false; |
361 | 0 | boolean affiliationCriteria = false; |
362 | 0 | boolean affiliationDefaultOnlyCriteria = false; |
363 | 0 | boolean phoneCriteria = false; |
364 | 0 | boolean emailCriteria = false; |
365 | 0 | boolean employeeIdCriteria = false; |
366 | |
|
367 | 0 | HashMap<String,String> newCriteria = new HashMap<String,String>(); |
368 | 0 | newCriteria.putAll( baseLookupCriteria ); |
369 | |
|
370 | 0 | newCriteria.put( "entityTypeContactInfos.entityTypeCode", personEntityTypeLookupCriteria ); |
371 | |
|
372 | 0 | if ( criteria != null ) { |
373 | 0 | for ( String key : criteria.keySet() ) { |
374 | |
|
375 | |
|
376 | 0 | if(key.equals(KIMPropertyConstants.Person.ACTIVE)) { |
377 | 0 | newCriteria.put(KIMPropertyConstants.Person.ACTIVE, criteria.get(KIMPropertyConstants.Person.ACTIVE)); |
378 | |
} |
379 | |
|
380 | |
|
381 | 0 | if ( StringUtils.isEmpty( criteria.get(key) ) ) { |
382 | 0 | continue; |
383 | |
} |
384 | |
|
385 | |
|
386 | 0 | if ( key.equals( KIMPropertyConstants.Person.EXTERNAL_ID ) && StringUtils.isNotBlank(criteria.get(key)) ) { |
387 | |
|
388 | 0 | if ( criteria.containsKey( KIMPropertyConstants.Person.EXTERNAL_IDENTIFIER_TYPE_CODE ) ) { |
389 | 0 | String extIdTypeCode = criteria.get(KIMPropertyConstants.Person.EXTERNAL_IDENTIFIER_TYPE_CODE); |
390 | 0 | if ( StringUtils.isNotBlank(extIdTypeCode) ) { |
391 | |
|
392 | 0 | EntityExternalIdentifierType extIdType = getIdentityService().getExternalIdentifierType(extIdTypeCode); |
393 | |
|
394 | 0 | if ( extIdType != null && extIdType.isEncryptionRequired() ) { |
395 | |
try { |
396 | 0 | criteria.put(key, |
397 | |
CoreApiServiceLocator.getEncryptionService().encrypt(criteria.get(key)) |
398 | |
); |
399 | 0 | } catch (GeneralSecurityException ex) { |
400 | 0 | LOG.error("Unable to encrypt value for external ID search of type " + extIdTypeCode, ex ); |
401 | 0 | } |
402 | |
} |
403 | |
} |
404 | |
} |
405 | |
} |
406 | |
|
407 | |
|
408 | 0 | String entityProperty = criteriaConversion.get( key ); |
409 | 0 | if ( entityProperty != null ) { |
410 | 0 | newCriteria.put( entityProperty, criteria.get( key ) ); |
411 | |
} else { |
412 | 0 | entityProperty = key; |
413 | |
|
414 | 0 | newCriteria.put( key, criteria.get( key ) ); |
415 | |
} |
416 | |
|
417 | 0 | if ( isNameEntityCriteria( entityProperty ) ) { |
418 | 0 | nameCriteria = true; |
419 | |
} |
420 | 0 | if ( isExternalIdentifierEntityCriteria( entityProperty ) ) { |
421 | 0 | externalIdentifierCriteria = true; |
422 | |
} |
423 | 0 | if ( isAffiliationEntityCriteria( entityProperty ) ) { |
424 | 0 | affiliationCriteria = true; |
425 | |
} |
426 | 0 | if ( isAddressEntityCriteria( entityProperty ) ) { |
427 | 0 | addressCriteria = true; |
428 | |
} |
429 | 0 | if ( isPhoneEntityCriteria( entityProperty ) ) { |
430 | 0 | phoneCriteria = true; |
431 | |
} |
432 | 0 | if ( isEmailEntityCriteria( entityProperty ) ) { |
433 | 0 | emailCriteria = true; |
434 | |
} |
435 | 0 | if ( isEmployeeIdEntityCriteria( entityProperty ) ) { |
436 | 0 | employeeIdCriteria = true; |
437 | |
} |
438 | |
|
439 | |
|
440 | 0 | if ( key.equals( "campusCode" ) ) { |
441 | 0 | affiliationDefaultOnlyCriteria = true; |
442 | |
} |
443 | 0 | } |
444 | 0 | if ( nameCriteria ) { |
445 | 0 | newCriteria.put( ENTITY_NAME_PROPERTY_PREFIX + "active", "Y" ); |
446 | 0 | newCriteria.put( ENTITY_NAME_PROPERTY_PREFIX + "defaultValue", "Y" ); |
447 | |
|
448 | |
} |
449 | 0 | if ( addressCriteria ) { |
450 | 0 | newCriteria.put( ENTITY_ADDRESS_PROPERTY_PREFIX + "active", "Y" ); |
451 | 0 | newCriteria.put( ENTITY_ADDRESS_PROPERTY_PREFIX + "defaultValue", "Y" ); |
452 | |
} |
453 | 0 | if ( phoneCriteria ) { |
454 | 0 | newCriteria.put( ENTITY_PHONE_PROPERTY_PREFIX + "active", "Y" ); |
455 | 0 | newCriteria.put( ENTITY_PHONE_PROPERTY_PREFIX + "defaultValue", "Y" ); |
456 | |
} |
457 | 0 | if ( emailCriteria ) { |
458 | 0 | newCriteria.put( ENTITY_EMAIL_PROPERTY_PREFIX + "active", "Y" ); |
459 | 0 | newCriteria.put( ENTITY_EMAIL_PROPERTY_PREFIX + "defaultValue", "Y" ); |
460 | |
} |
461 | 0 | if ( employeeIdCriteria ) { |
462 | 0 | newCriteria.put( ENTITY_EMPLOYEE_ID_PROPERTY_PREFIX + "active", "Y" ); |
463 | 0 | newCriteria.put( ENTITY_EMPLOYEE_ID_PROPERTY_PREFIX + "primary", "Y" ); |
464 | |
} |
465 | 0 | if ( affiliationCriteria ) { |
466 | 0 | newCriteria.put( ENTITY_AFFILIATION_PROPERTY_PREFIX + "active", "Y" ); |
467 | |
} |
468 | 0 | if ( affiliationDefaultOnlyCriteria ) { |
469 | 0 | newCriteria.put( ENTITY_AFFILIATION_PROPERTY_PREFIX + "defaultValue", "Y" ); |
470 | |
} |
471 | |
} |
472 | |
|
473 | 0 | if ( LOG.isDebugEnabled() ) { |
474 | 0 | LOG.debug( "Converted: " + newCriteria ); |
475 | |
} |
476 | 0 | return newCriteria; |
477 | |
} |
478 | |
|
479 | |
protected boolean isNameEntityCriteria( String propertyName ) { |
480 | 0 | return propertyName.startsWith( ENTITY_NAME_PROPERTY_PREFIX ); |
481 | |
} |
482 | |
protected boolean isAddressEntityCriteria( String propertyName ) { |
483 | 0 | return propertyName.startsWith( ENTITY_ADDRESS_PROPERTY_PREFIX ); |
484 | |
} |
485 | |
protected boolean isPhoneEntityCriteria( String propertyName ) { |
486 | 0 | return propertyName.startsWith( ENTITY_PHONE_PROPERTY_PREFIX ); |
487 | |
} |
488 | |
protected boolean isEmailEntityCriteria( String propertyName ) { |
489 | 0 | return propertyName.startsWith( ENTITY_EMAIL_PROPERTY_PREFIX ); |
490 | |
} |
491 | |
protected boolean isEmployeeIdEntityCriteria( String propertyName ) { |
492 | 0 | return propertyName.startsWith( ENTITY_EMPLOYEE_ID_PROPERTY_PREFIX ); |
493 | |
} |
494 | |
protected boolean isAffiliationEntityCriteria( String propertyName ) { |
495 | 0 | return propertyName.startsWith( ENTITY_AFFILIATION_PROPERTY_PREFIX ); |
496 | |
} |
497 | |
protected boolean isExternalIdentifierEntityCriteria( String propertyName ) { |
498 | 0 | return propertyName.startsWith( ENTITY_EXT_ID_PROPERTY_PREFIX ); |
499 | |
} |
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
public List<String> getPersonEntityTypeCodes() { |
507 | 0 | return this.personEntityTypeCodes; |
508 | |
} |
509 | |
|
510 | |
public void setPersonEntityTypeCodes(List<String> personEntityTypeCodes) { |
511 | 0 | this.personEntityTypeCodes = personEntityTypeCodes; |
512 | 0 | personEntityTypeLookupCriteria = null; |
513 | 0 | for ( String entityTypeCode : personEntityTypeCodes ) { |
514 | 0 | if ( personEntityTypeLookupCriteria == null ) { |
515 | 0 | personEntityTypeLookupCriteria = entityTypeCode; |
516 | |
} else { |
517 | 0 | personEntityTypeLookupCriteria = personEntityTypeLookupCriteria + "|" + entityTypeCode; |
518 | |
} |
519 | |
} |
520 | 0 | } |
521 | |
|
522 | |
|
523 | |
protected List<Person> getPeople( Collection<String> principalIds ) { |
524 | 0 | List<Person> people = new ArrayList<Person>( principalIds.size() ); |
525 | 0 | for ( String principalId : principalIds ) { |
526 | 0 | people.add( getPerson(principalId) ); |
527 | |
} |
528 | 0 | return people; |
529 | |
} |
530 | |
|
531 | |
protected List<String> peopleToPrincipalIds( List<Person> people ) { |
532 | 0 | List<String> principalIds = new ArrayList<String>(); |
533 | |
|
534 | 0 | for ( Person person : people ) { |
535 | 0 | principalIds.add( person.getPrincipalId() ); |
536 | |
} |
537 | |
|
538 | 0 | return principalIds; |
539 | |
} |
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
public List<Person> getPersonByExternalIdentifier(String externalIdentifierTypeCode, String externalId) { |
545 | 0 | if (StringUtils.isBlank( externalIdentifierTypeCode ) || StringUtils.isBlank( externalId ) ) { |
546 | 0 | return null; |
547 | |
} |
548 | 0 | Map<String,String> criteria = new HashMap<String,String>( 2 ); |
549 | 0 | criteria.put( KIMPropertyConstants.Person.EXTERNAL_IDENTIFIER_TYPE_CODE, externalIdentifierTypeCode ); |
550 | 0 | criteria.put( KIMPropertyConstants.Person.EXTERNAL_ID, externalId ); |
551 | 0 | return findPeople( criteria ); |
552 | |
} |
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
public Person updatePersonIfNecessary(String sourcePrincipalId, Person currentPerson ) { |
558 | 0 | if (currentPerson == null |
559 | |
|| !StringUtils.equals(sourcePrincipalId, currentPerson.getPrincipalId() ) |
560 | |
|| currentPerson.getEntityId() == null ) { |
561 | 0 | Person person = getPerson( sourcePrincipalId ); |
562 | |
|
563 | |
|
564 | 0 | if ( person == null ) { |
565 | 0 | if ( currentPerson != null && currentPerson.getEntityId() == null ) { |
566 | 0 | return currentPerson; |
567 | |
} |
568 | |
} |
569 | |
|
570 | 0 | if ( person == null && currentPerson == null ) { |
571 | |
try { |
572 | 0 | return new PersonImpl(); |
573 | 0 | } catch ( Exception ex ) { |
574 | 0 | LOG.error( "unable to instantiate an object of type: " + getPersonImplementationClass() + " - returning null", ex ); |
575 | 0 | return null; |
576 | |
} |
577 | |
} |
578 | 0 | return person; |
579 | |
} |
580 | |
|
581 | 0 | return currentPerson; |
582 | |
} |
583 | |
|
584 | |
|
585 | |
|
586 | |
|
587 | |
|
588 | |
private Map<String,String> getNonPersonSearchCriteria( BusinessObject bo, Map<String,String> fieldValues) { |
589 | 0 | Map<String,String> nonUniversalUserSearchCriteria = new HashMap<String,String>(); |
590 | 0 | for ( String propertyName : fieldValues.keySet() ) { |
591 | 0 | if (!isPersonProperty(bo, propertyName)) { |
592 | 0 | nonUniversalUserSearchCriteria.put(propertyName, fieldValues.get(propertyName)); |
593 | |
} |
594 | |
} |
595 | 0 | return nonUniversalUserSearchCriteria; |
596 | |
} |
597 | |
|
598 | |
|
599 | |
private boolean isPersonProperty(BusinessObject bo, String propertyName) { |
600 | |
try { |
601 | 0 | if ( ObjectUtils.isNestedAttribute( propertyName ) |
602 | |
&& !StringUtils.contains(propertyName, "add.") ) { |
603 | 0 | Class<?> type = PropertyUtils.getPropertyType(bo, ObjectUtils.getNestedAttributePrefix( propertyName )); |
604 | |
|
605 | 0 | if ( type != null ) { |
606 | 0 | return Person.class.isAssignableFrom(type); |
607 | |
} |
608 | 0 | LOG.warn( "Unable to determine type of nested property: " + bo.getClass().getName() + " / " + propertyName ); |
609 | |
} |
610 | 0 | } catch (Exception ex) { |
611 | 0 | if ( LOG.isDebugEnabled() ) { |
612 | 0 | LOG.debug("Unable to determine if property on " + bo.getClass().getName() + " to a person object: " + propertyName, ex ); |
613 | |
} |
614 | 0 | } |
615 | 0 | return false; |
616 | |
} |
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
public boolean hasPersonProperty(Class<? extends Person> businessObjectClass, Map<String,String> fieldValues) { |
622 | 0 | if ( businessObjectClass == null || fieldValues == null ) { |
623 | 0 | return false; |
624 | |
} |
625 | |
try { |
626 | 0 | BusinessObject bo = businessObjectClass.newInstance(); |
627 | 0 | for ( String propertyName : fieldValues.keySet() ) { |
628 | 0 | if (isPersonProperty(bo, propertyName)) { |
629 | 0 | return true; |
630 | |
} |
631 | |
} |
632 | 0 | } catch (Exception ex) { |
633 | 0 | if ( LOG.isDebugEnabled() ) { |
634 | 0 | LOG.debug( "Error instantiating business object class passed into hasPersonProperty", ex ); |
635 | |
} |
636 | |
|
637 | 0 | } |
638 | 0 | return false; |
639 | |
} |
640 | |
|
641 | |
|
642 | |
|
643 | |
|
644 | |
@SuppressWarnings("unchecked") |
645 | |
public Map<String,String> resolvePrincipalNamesToPrincipalIds(BusinessObject businessObject, Map<String,String> fieldValues) { |
646 | 0 | if ( fieldValues == null ) { |
647 | 0 | return null; |
648 | |
} |
649 | 0 | if ( businessObject == null ) { |
650 | 0 | return fieldValues; |
651 | |
} |
652 | 0 | StringBuffer resolvedPrincipalIdPropertyName = new StringBuffer(); |
653 | |
|
654 | |
|
655 | 0 | Map<String,String> processedFieldValues = getNonPersonSearchCriteria(businessObject, fieldValues); |
656 | 0 | for ( String propertyName : fieldValues.keySet() ) { |
657 | 0 | if ( !StringUtils.isBlank(fieldValues.get(propertyName)) |
658 | |
&& isPersonProperty(businessObject, propertyName) |
659 | |
) { |
660 | |
|
661 | 0 | String personPropertyName = ObjectUtils.getNestedAttributePrimitive( propertyName ); |
662 | |
|
663 | 0 | if ( StringUtils.equals( KIMPropertyConstants.Person.PRINCIPAL_NAME, personPropertyName) ) { |
664 | 0 | Class targetBusinessObjectClass = null; |
665 | 0 | BusinessObject targetBusinessObject = null; |
666 | 0 | resolvedPrincipalIdPropertyName.setLength( 0 ); |
667 | |
|
668 | |
|
669 | 0 | String personReferenceObjectPropertyName = ObjectUtils.getNestedAttributePrefix( propertyName ); |
670 | |
|
671 | |
|
672 | 0 | if ( ObjectUtils.isNestedAttribute( personReferenceObjectPropertyName ) ) { |
673 | 0 | String targetBusinessObjectPropertyName = ObjectUtils.getNestedAttributePrefix( personReferenceObjectPropertyName ); |
674 | 0 | targetBusinessObject = (BusinessObject)ObjectUtils.getPropertyValue( businessObject, targetBusinessObjectPropertyName ); |
675 | 0 | if (targetBusinessObject != null) { |
676 | 0 | targetBusinessObjectClass = targetBusinessObject.getClass(); |
677 | 0 | resolvedPrincipalIdPropertyName.append(targetBusinessObjectPropertyName).append("."); |
678 | |
} else { |
679 | 0 | LOG.error("Could not find target property '"+propertyName+"' in class "+businessObject.getClass().getName()+". Property value was null."); |
680 | |
} |
681 | 0 | } else { |
682 | 0 | targetBusinessObjectClass = businessObject.getClass(); |
683 | 0 | targetBusinessObject = businessObject; |
684 | |
} |
685 | |
|
686 | 0 | if (targetBusinessObjectClass != null) { |
687 | |
|
688 | |
|
689 | |
|
690 | 0 | String propName = ObjectUtils.getNestedAttributePrimitive( personReferenceObjectPropertyName ); |
691 | 0 | DataObjectRelationship rel = getBusinessObjectMetaDataService().getBusinessObjectRelationship( targetBusinessObject, propName ); |
692 | 0 | if ( rel != null ) { |
693 | 0 | String sourcePrimitivePropertyName = rel.getParentAttributeForChildAttribute(KIMPropertyConstants.Person.PRINCIPAL_ID); |
694 | 0 | resolvedPrincipalIdPropertyName.append(sourcePrimitivePropertyName); |
695 | |
|
696 | 0 | String principalName = fieldValues.get( propertyName ); |
697 | 0 | Principal principal = getIdentityService().getPrincipalByPrincipalName( principalName ); |
698 | 0 | if (principal != null ) { |
699 | 0 | processedFieldValues.put(resolvedPrincipalIdPropertyName.toString(), principal.getPrincipalId()); |
700 | |
} else { |
701 | 0 | processedFieldValues.put(resolvedPrincipalIdPropertyName.toString(), null); |
702 | |
try { |
703 | |
|
704 | |
|
705 | |
|
706 | |
|
707 | 0 | ObjectUtils.setObjectProperty(targetBusinessObject, resolvedPrincipalIdPropertyName.toString(), null ); |
708 | 0 | ObjectUtils.setObjectProperty(targetBusinessObject, propName, null ); |
709 | 0 | ObjectUtils.setObjectProperty(targetBusinessObject, propName + ".principalName", principalName ); |
710 | 0 | } catch ( Exception ex ) { |
711 | 0 | LOG.error( "Unable to blank out the person object after finding that the person with the given principalName does not exist.", ex ); |
712 | 0 | } |
713 | |
} |
714 | 0 | } else { |
715 | 0 | LOG.error( "Missing relationship for " + propName + " on " + targetBusinessObjectClass.getName() ); |
716 | |
} |
717 | 0 | } else { |
718 | 0 | processedFieldValues.put(resolvedPrincipalIdPropertyName.toString(), null); |
719 | |
} |
720 | |
} |
721 | |
|
722 | |
|
723 | |
|
724 | 0 | } else if (propertyName.endsWith("." + KIMPropertyConstants.Person.PRINCIPAL_NAME)){ |
725 | |
|
726 | 0 | String principalName = fieldValues.get(propertyName); |
727 | 0 | if ( StringUtils.isNotEmpty( principalName ) ) { |
728 | 0 | String containerPropertyName = propertyName; |
729 | 0 | if (containerPropertyName.startsWith(KRADConstants.MAINTENANCE_ADD_PREFIX)) { |
730 | 0 | containerPropertyName = StringUtils.substringAfter( propertyName, KRADConstants.MAINTENANCE_ADD_PREFIX ); |
731 | |
} |
732 | |
|
733 | |
|
734 | |
|
735 | 0 | if ( ObjectUtils.isNestedAttribute( containerPropertyName ) ) { |
736 | |
|
737 | 0 | String collectionName = StringUtils.substringBefore( containerPropertyName, "." ); |
738 | |
|
739 | |
|
740 | |
|
741 | 0 | Class<? extends BusinessObject> collectionBusinessObjectClass = getMaintenanceDocumentDictionaryService() |
742 | |
.getCollectionBusinessObjectClass( |
743 | |
getMaintenanceDocumentDictionaryService() |
744 | |
.getDocumentTypeName(businessObject.getClass()), collectionName); |
745 | 0 | if (collectionBusinessObjectClass != null) { |
746 | |
|
747 | |
|
748 | 0 | List<DataObjectRelationship> relationships = |
749 | |
getBusinessObjectMetaDataService().getBusinessObjectRelationships( collectionBusinessObjectClass ); |
750 | |
|
751 | |
|
752 | 0 | for ( DataObjectRelationship rel : relationships ) { |
753 | 0 | String parentAttribute = rel.getParentAttributeForChildAttribute( KIMPropertyConstants.Person.PRINCIPAL_ID ); |
754 | 0 | if ( parentAttribute == null ) { |
755 | 0 | continue; |
756 | |
} |
757 | |
|
758 | 0 | processedFieldValues.remove( propertyName ); |
759 | 0 | String fieldPrefix = StringUtils.substringBeforeLast( StringUtils.substringBeforeLast( propertyName, "." + KIMPropertyConstants.Person.PRINCIPAL_NAME ), "." ); |
760 | 0 | String relatedPrincipalIdPropertyName = fieldPrefix + "." + parentAttribute; |
761 | |
|
762 | 0 | if(EXTENSION.equals(StringUtils.substringAfterLast(fieldPrefix, ".")) && EXTENSION.equals(StringUtils.substringBefore(parentAttribute, "."))) |
763 | |
{ |
764 | 0 | relatedPrincipalIdPropertyName = fieldPrefix + "." + StringUtils.substringAfter(parentAttribute, "."); |
765 | |
} |
766 | 0 | String currRelatedPersonPrincipalId = processedFieldValues.get(relatedPrincipalIdPropertyName); |
767 | 0 | if ( StringUtils.isBlank( currRelatedPersonPrincipalId ) ) { |
768 | 0 | Principal principal = getIdentityService().getPrincipalByPrincipalName( principalName ); |
769 | 0 | if ( principal != null ) { |
770 | 0 | processedFieldValues.put(relatedPrincipalIdPropertyName, principal.getPrincipalId()); |
771 | |
} else { |
772 | 0 | processedFieldValues.put(relatedPrincipalIdPropertyName, null); |
773 | |
} |
774 | |
} |
775 | 0 | } |
776 | 0 | } else { |
777 | 0 | if ( LOG.isDebugEnabled() ) { |
778 | 0 | LOG.debug( "Unable to determine class for collection referenced as part of property: " + containerPropertyName + " on " + businessObject.getClass().getName() ); |
779 | |
} |
780 | |
} |
781 | 0 | } else { |
782 | 0 | if ( LOG.isDebugEnabled() ) { |
783 | 0 | LOG.debug( "Non-nested property ending with 'principalName': " + containerPropertyName + " on " + businessObject.getClass().getName() ); |
784 | |
} |
785 | |
} |
786 | |
} |
787 | 0 | } |
788 | |
} |
789 | 0 | return processedFieldValues; |
790 | |
} |
791 | |
|
792 | |
|
793 | |
|
794 | |
protected IdentityService getIdentityService() { |
795 | 0 | if ( identityService == null ) { |
796 | 0 | identityService = KimApiServiceLocator.getIdentityService(); |
797 | |
} |
798 | 0 | return identityService; |
799 | |
} |
800 | |
|
801 | |
protected RoleService getRoleService() { |
802 | 0 | if ( roleService == null ) { |
803 | 0 | roleService = KimApiServiceLocator.getRoleService(); |
804 | |
} |
805 | 0 | return roleService; |
806 | |
} |
807 | |
|
808 | |
|
809 | |
public Class<? extends Person> getPersonImplementationClass() { |
810 | 0 | return PersonImpl.class; |
811 | |
} |
812 | |
|
813 | |
protected BusinessObjectMetaDataService getBusinessObjectMetaDataService() { |
814 | 0 | if ( businessObjectMetaDataService == null ) { |
815 | 0 | businessObjectMetaDataService = KNSServiceLocator.getBusinessObjectMetaDataService(); |
816 | |
} |
817 | 0 | return businessObjectMetaDataService; |
818 | |
} |
819 | |
|
820 | |
protected MaintenanceDocumentDictionaryService getMaintenanceDocumentDictionaryService() { |
821 | 0 | if ( maintenanceDocumentDictionaryService == null ) { |
822 | 0 | maintenanceDocumentDictionaryService = KNSServiceLocator.getMaintenanceDocumentDictionaryService(); |
823 | |
} |
824 | 0 | return maintenanceDocumentDictionaryService; |
825 | |
} |
826 | |
} |