Search in sources :

Example 1 with AttributeNamedPersonImpl

use of org.jasig.services.persondir.support.AttributeNamedPersonImpl in project uPortal by Jasig.

the class LocalAccountPersonAttributeDao method mapPersonAttributes.

/**
     * This implementation uses the result attribute mapping to supplement, rather than replace, the
     * attributes returned from the database.
     */
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()));
    // 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;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AttributeNamedPersonImpl(org.jasig.services.persondir.support.AttributeNamedPersonImpl) LinkedHashMap(java.util.LinkedHashMap) IPersonAttributes(org.jasig.services.persondir.IPersonAttributes) AttributeNamedPersonImpl(org.jasig.services.persondir.support.AttributeNamedPersonImpl) NamedPersonImpl(org.jasig.services.persondir.support.NamedPersonImpl) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 IPersonAttributes (org.jasig.services.persondir.IPersonAttributes)1 AttributeNamedPersonImpl (org.jasig.services.persondir.support.AttributeNamedPersonImpl)1 NamedPersonImpl (org.jasig.services.persondir.support.NamedPersonImpl)1