001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.webapp.security;
020
021import javax.faces.component.UIComponent;
022import javax.faces.context.FacesContext;
023import javax.faces.convert.Converter;
024
025import org.apache.commons.lang.StringUtils;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
028import org.nuxeo.ecm.platform.ui.web.directory.DirectoryFunctions;
029import org.nuxeo.ecm.platform.ui.web.tag.fn.Functions;
030import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
031
032/**
033 * JSF Converter used for rendering, transforming a user id into the user display name.
034 * <p>
035 * Sample usage:
036 *
037 * <pre>
038 * <code>
039 * lt;h:outputText
040 *  converter=&quot;#{userSuggestionActions.userConverter}&quot;&gt;
041 *  &lt;f:attribute name=&quot;prefixed&quot; value=&quot;false&quot; /&gt;
042 *  &lt;f:attribute name=&quot;userDirectory&quot; value=&quot;#{userManager.userDirectoryName}&quot; /&gt;
043 *  &lt;f:attribute name=&quot;userSchema&quot; value=&quot;#{userManager.userSchemaName}&quot; /&gt;
044 *  &lt;f:attribute name=&quot;firstNameField&quot; value=&quot;firstName&quot; /&gt;
045 *  &lt;f:attribute name=&quot;lastNameField&quot; value=&quot;lastName&quot; /&gt;
046 * lt;/h:outputText&gt;
047 * </code>
048 * </pre>
049 *
050 * @author Anahide Tchertchian
051 */
052public class UserDisplayConverter implements Converter {
053
054    /**
055     * Returns given value (does not do any reverse conversion)
056     */
057    @Override
058    public Object getAsObject(FacesContext context, UIComponent component, String value) {
059        return value;
060    }
061
062    /**
063     * Tries to build the user display name according to information passed as attribute to the component holding the
064     * converter.
065     * <p>
066     * Handled attributes are:
067     * <ul>
068     * <li>userDirectory: user directory name</li>
069     * <li>userSchema: user schema name</li>
070     * <li>firstNameField: field storing the user first name</li>
071     * <li>lastNameField: field storing the user last name</li>
072     * </ul>
073     */
074    @Override
075    public String getAsString(FacesContext context, UIComponent component, Object value) {
076        if (value instanceof String && !StringUtils.isEmpty((String) value)) {
077            String isPrefixed = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, "prefixed",
078                    "false");
079            String username;
080            if (Boolean.valueOf(isPrefixed)) {
081                username = ((String) value).substring(NuxeoPrincipal.PREFIX.length());
082            } else {
083                username = (String) value;
084            }
085            String directory = (String) ComponentUtils.getAttributeOrExpressionValue(context, component,
086                    "userDirectory", null);
087            String firstName = (String) ComponentUtils.getAttributeOrExpressionValue(context, component,
088                    "firstNameField", null);
089            String lastName = (String) ComponentUtils.getAttributeOrExpressionValue(context, component,
090                    "lastNameField", null);
091            String email = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, "emailField", null);
092            String schema = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, "userSchema",
093                    null);
094
095            if (schema != null) {
096                DocumentModel doc = DirectoryFunctions.getDirectoryEntry(directory, username);
097                if (doc != null) {
098                    String firstNameValue = firstName != null ? (String) doc.getProperty(schema, firstName) : null;
099                    String lastNameValue = lastName != null ? (String) doc.getProperty(schema, lastName) : null;
100                    String emailValue = email != null ? (String) doc.getProperty(schema, email) : null;
101                    return Functions.userDisplayNameAndEmail(username, firstNameValue, lastNameValue, emailValue);
102                }
103            } else {
104                // XXX will return cached entry
105                return Functions.userFullName(username);
106            }
107        }
108        if (value != null) {
109            return value.toString();
110        } else {
111            return null;
112        }
113    }
114
115}