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