001/*
002 *  Copyright 2012 Anyware Services
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 */
016package org.ametys.core.user;
017
018import org.apache.commons.lang3.StringUtils;
019
020import org.ametys.core.user.directory.UserDirectory;
021import org.ametys.core.util.SizeUtils.ExcludeFromSizeCalculation;
022
023/**
024 * Implementation of the principal abstraction to represent an user with a login
025 * and eventually a fullname and an email.
026 */
027public class User implements java.security.Principal
028{
029    /**
030     * The identity of this principal.
031     */
032    protected UserIdentity _identity;
033
034    /**
035     * The last name of this principal.
036     */
037    protected String _lastName;
038    
039    /**
040     * The first name of this principal.
041     */
042    protected String _firstName;
043
044    /**
045     * The email of this principal.
046     */
047    protected String _email;
048
049    /**
050     * The user directory this user belongs to.
051     */
052    @ExcludeFromSizeCalculation
053    protected UserDirectory _userDirectory;
054
055    /**
056     * Construct a new UserPrincipal, associated with the specified login et population id.
057     * 
058     * @param login The login of the principal.
059     * @param populationId The id of the population
060     */
061    public User(String login, String populationId)
062    {
063        this(new UserIdentity(login, populationId));
064    }
065    
066    /**
067     * Construct a new UserPrincipal, associated with the specified identity.
068     * @param identity The identity of this user. Cannot be null
069     */
070    public User(UserIdentity identity)
071    {
072        this(identity, null, null, null, null);
073    }
074
075    /**
076     * Construct a new UserPrincipal, associated with a last name, a first name,
077     * an email and identified by a login.
078     * @param identity The identity of this user. Cannot be null
079     * @param lastName The last name
080     * @param firstName The first name 
081     * @param email The email
082     * @param userDirectory The user directory the use rbelongs to. Can be null.
083     */
084    public User(UserIdentity identity, String lastName, String firstName, String email, UserDirectory userDirectory)
085    {
086        _identity = identity;
087        _lastName = StringUtils.defaultString(lastName);
088        _firstName = StringUtils.defaultString(firstName);
089        _email = StringUtils.defaultString(email);
090        _userDirectory = userDirectory;
091    }
092
093    /**
094     * The identity of the user.
095     * 
096     * @return The identity. 
097     */
098    public UserIdentity getIdentity()
099    {
100        return _identity;
101    }
102    
103    @Override
104    public String getName()
105    {
106        return UserIdentity.userIdentityToString(_identity);
107    }
108    
109    /**
110     * The last name of the user
111     * @return The last name.
112     */
113    public String getLastName()
114    {
115        return _lastName;
116    }
117    
118    /**
119     * The first name of the user
120     * @return The first name.
121     */
122    public String getFirstName()
123    {
124        return _firstName;
125    }
126    
127    /**
128     * The email of the user represented by this Principal.
129     * 
130     * @return The email.
131     */
132    public String getEmail()
133    {
134        return _email;
135    }
136    
137    /**
138     * The fullname of this user.
139     * @return The full name
140     */
141    public String getFullName()
142    {
143        return _getFullName(true);
144    }
145    
146    /**
147     * The fullname to use to display if sort is needed.
148     * Ensure the sort will be on 
149     * @return The sortable name
150     */
151    public String getSortableName()
152    {
153        return _getFullName(false);
154    }
155    
156    /**
157     * The user directory this user belongs to.
158     * @return The user directory
159     */
160    public UserDirectory getUserDirectory()
161    {
162        return _userDirectory;
163    }
164    
165    /**
166     * The full name of the user represented by this Principal.
167     * @param firstLast Define the name order if the full name. If true, first name then last name. If false, the contrary.
168     * @return The full name.
169     */
170    protected String _getFullName(boolean firstLast)
171    {
172        StringBuilder sb = new StringBuilder();
173        
174        if (firstLast && StringUtils.isNotEmpty(_firstName))
175        {
176            sb.append(_firstName);
177            
178            if (StringUtils.isNotEmpty(_lastName))
179            {
180                sb.append(' ');
181            }
182        }
183        
184        if (StringUtils.isNotEmpty(_lastName))
185        {
186            sb.append(_lastName);
187        }
188        
189        if (!firstLast && StringUtils.isNotEmpty(_firstName))
190        {
191            if (StringUtils.isNotEmpty(_lastName))
192            {
193                sb.append(' ');
194            }
195            
196            sb.append(_firstName);
197        }
198        
199        return StringUtils.defaultIfEmpty(sb.toString(), _identity.getLogin());
200    }
201    
202    /**
203     * Return a String representation of this object, which exposes only
204     * information that should be public.
205     * 
206     * @return A string representing the user.
207     */
208    @Override
209    public String toString()
210    {
211        StringBuilder sb = new StringBuilder("Principal[");
212        sb.append(_identity.toString());
213        sb.append(" : ");
214        sb.append(getFullName());
215        sb.append(", ");
216        sb.append(_email);
217        sb.append("]");
218        return sb.toString();
219    }
220
221    /**
222     * Test if two principal are equals.
223     * @return true if the given Object represents the same Principal.
224     */
225    @Override
226    public boolean equals(Object another)
227    {
228        if (another == null || !(another instanceof User))
229        {
230            return false;
231        }
232        
233        User otherUser = (User) another;
234        
235        return _identity != null  && _identity.equals(otherUser.getIdentity());
236    }
237    
238    @Override
239    public int hashCode()
240    {
241        return _identity.hashCode();
242    }
243}