protected IPersonAttributes mapPersonAttributes ( final ILocalAccountPerson person ) { final Map < String , List < Object > > mappedAttributes = new LinkedHashMap < String , List < Object > > ( ) ; mappedAttributes . putAll ( person . getAttributes ( ) ) ; // map the user's username to the portal's username attribute in the // attribute map mappedAttributes . put ( this . getUsernameAttributeProvider ( ) . getUsernameAttribute ( ) , Collections . < Object > singletonList ( person . getName ( ) ) ) ; // if the user does not have a display name attribute set, attempt // to build one from the first and last name attributes if ( ! mappedAttributes . containsKey ( displayNameAttribute ) || mappedAttributes . get ( displayNameAttribute ) . size ( ) == 0 || StringUtils . isBlank ( ( String ) mappedAttributes . get ( displayNameAttribute ) . get ( 0 ) ) ) { final List < Object > firstNames = mappedAttributes . get ( "givenName" ) ; final List < Object > lastNames = mappedAttributes . get ( "sn" ) ; final StringBuilder displayName = new StringBuilder ( ) ; if ( firstNames != null && firstNames . size ( ) > 0 ) { displayName . append ( firstNames . get ( 0 ) ) . append ( " " ) ; } if ( lastNames != null && lastNames . size ( ) > 0 ) { displayName . append ( lastNames . get ( 0 ) ) ; } mappedAttributes . put ( displayNameAttribute , Collections . < Object > singletonList ( displayName . toString ( ) ) ) ; } for ( final Map . Entry < String , Set < String > > resultAttrEntry : this . getResultAttributeMapping ( ) . entrySet ( ) ) { final String dataKey = resultAttrEntry . getKey ( ) ; // Only map found data attributes if ( mappedAttributes . containsKey ( dataKey ) ) { Set < String > resultKeys = resultAttrEntry . getValue ( ) ; // If dataKey has no mapped resultKeys just use the dataKey if ( resultKeys == null ) { resultKeys = Collections . singleton ( dataKey ) ; } // Add the value to the mapped attributes for each mapped key final List < Object > value = mappedAttributes . get ( dataKey ) ; for ( final String resultKey : resultKeys ) { if ( resultKey == null ) { // TODO is this possible? if ( ! mappedAttributes . containsKey ( dataKey ) ) { mappedAttributes . put ( dataKey , value ) ; } } else if ( ! mappedAttributes . containsKey ( resultKey ) ) { mappedAttributes . put ( resultKey , value ) ; } } } } final IPersonAttributes newPerson ; final String name = person . getName ( ) ; if ( name != null ) { newPerson = new NamedPersonImpl ( name , mappedAttributes ) ; } else { final String userNameAttribute = this . getConfiguredUserNameAttribute ( ) ; newPerson = new AttributeNamedPersonImpl ( userNameAttribute , mappedAttributes ) ; } return newPerson ; }
This implementation uses the result attribute mapping to supplement rather than replace the attributes returned from the database .