001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import com.liferay.portal.NoSuchListTypeException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.CharPool;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.UserConstants;
029    import com.liferay.portal.service.ListTypeServiceUtil;
030    
031    import java.util.HashMap;
032    import java.util.Locale;
033    import java.util.Map;
034    
035    /**
036     * @author Michael C. Han
037     */
038    public class DefaultFullNameGenerator implements FullNameGenerator {
039    
040            @Override
041            public String getFullName(
042                    String firstName, String middleName, String lastName) {
043    
044                    String fullName = buildFullName(firstName, middleName, lastName, false);
045    
046                    if (!isFullNameTooLong(fullName)) {
047                            return fullName;
048                    }
049    
050                    fullName = buildFullName(firstName, middleName, lastName, true);
051    
052                    if (!isFullNameTooLong(fullName)) {
053                            return fullName;
054                    }
055    
056                    return shortenFullName(fullName);
057            }
058    
059            @Override
060            public String getLocalizedFullName(
061                    String firstName, String middleName, String lastName, Locale locale,
062                    long prefixId, long suffixId) {
063    
064                    String fullName = buildLocalizedFullName(
065                            firstName, middleName, lastName, locale, prefixId, suffixId, false);
066    
067                    if (!isFullNameTooLong(fullName)) {
068                            return fullName;
069                    }
070    
071                    fullName = buildLocalizedFullName(
072                            firstName, middleName, lastName, locale, prefixId, suffixId, true);
073    
074                    if (!isFullNameTooLong(fullName)) {
075                            return fullName;
076                    }
077    
078                    return shortenFullName(fullName);
079            }
080    
081            @Override
082            public String[] splitFullName(String fullName) {
083                    String firstName = StringPool.BLANK;
084                    String middleName = StringPool.BLANK;
085                    String lastName = StringPool.BLANK;
086    
087                    if (Validator.isNull(fullName)) {
088                            return new String[] {firstName, middleName, lastName};
089                    }
090    
091                    String[] name = StringUtil.split(fullName, CharPool.SPACE);
092    
093                    firstName = name[0];
094                    middleName = StringPool.BLANK;
095                    lastName = name[name.length - 1];
096    
097                    if (name.length > 2) {
098                            for (int i = 1; i < name.length - 1; i++) {
099                                    if (Validator.isNull(name[i].trim())) {
100                                            continue;
101                                    }
102    
103                                    if (i != 1) {
104                                            middleName += StringPool.SPACE;
105                                    }
106    
107                                    middleName += name[i].trim();
108                            }
109                    }
110    
111                    return new String[] {firstName, middleName, lastName};
112            }
113    
114            protected String buildFullName(
115                    String firstName, String middleName, String lastName,
116                    boolean useInitials) {
117    
118                    StringBundler sb = new StringBundler(5);
119    
120                    if (useInitials) {
121                            firstName = firstName.substring(0, 1);
122                    }
123    
124                    sb.append(firstName);
125    
126                    if (Validator.isNotNull(middleName)) {
127                            if (useInitials) {
128                                    middleName = middleName.substring(0, 1);
129                            }
130    
131                            sb.append(StringPool.SPACE);
132                            sb.append(middleName);
133                    }
134    
135                    if (Validator.isNotNull(lastName)) {
136                            sb.append(StringPool.SPACE);
137                            sb.append(lastName);
138                    }
139    
140                    return sb.toString();
141            }
142    
143            protected String buildLocalizedFullName(
144                    String firstName, String middleName, String lastName, Locale locale,
145                    long prefixId, long suffixId, boolean useInitials) {
146    
147                    StringBundler sb = new StringBundler(5);
148    
149                    Map<String, String> namesMap = new HashMap<>();
150    
151                    if (Validator.isNotNull(firstName)) {
152                            if (useInitials) {
153                                    firstName = firstName.substring(0, 1);
154                            }
155    
156                            namesMap.put("first-name", firstName);
157                    }
158    
159                    if (Validator.isNotNull(middleName)) {
160                            if (useInitials) {
161                                    middleName = middleName.substring(0, 1);
162                            }
163    
164                            namesMap.put("middle-name", middleName);
165                    }
166    
167                    if (Validator.isNotNull(lastName)) {
168                            namesMap.put("last-name", lastName);
169                    }
170    
171                    if (prefixId != 0) {
172                            try {
173                                    String prefix = ListTypeServiceUtil.getListType(
174                                            prefixId).getName();
175    
176                                    prefix = LanguageUtil.get(locale, prefix);
177    
178                                    namesMap.put("prefix", prefix);
179                            }
180                            catch (NoSuchListTypeException nslte) {
181                                    if (_log.isDebugEnabled()) {
182                                            _log.debug("Ignoring full name prefix " + prefixId, nslte);
183                                    }
184                            }
185                            catch (PortalException pe) {
186                                    throw new SystemException(pe);
187                            }
188                    }
189    
190                    if (suffixId != 0) {
191                            try {
192                                    String suffix = ListTypeServiceUtil.getListType(
193                                            suffixId).getName();
194    
195                                    suffix = LanguageUtil.get(locale, suffix);
196    
197                                    namesMap.put("suffix", suffix);
198                            }
199                            catch (NoSuchListTypeException nslte) {
200                                    if (_log.isDebugEnabled()) {
201                                            _log.debug("Ignoring full name suffix " + suffixId, nslte);
202                                    }
203                            }
204                            catch (PortalException pe) {
205                                    throw new SystemException(pe);
206                            }
207                    }
208    
209                    FullNameDefinition fullNameDefinition =
210                            FullNameDefinitionFactory.getInstance(locale);
211    
212                    for (FullNameField fullNameField :
213                                    fullNameDefinition.getFullNameFields()) {
214    
215                            if (namesMap.containsKey(fullNameField.getName())) {
216                                    sb.append(StringPool.SPACE);
217                                    sb.append(namesMap.get(fullNameField.getName()));
218                            }
219                    }
220    
221                    return StringUtil.trim(sb.toString());
222            }
223    
224            protected boolean isFullNameTooLong(String fullName) {
225                    if (fullName.length() > UserConstants.FULL_NAME_MAX_LENGTH) {
226                            return true;
227                    }
228    
229                    return false;
230            }
231    
232            protected String shortenFullName(String fullName) {
233                    if (_log.isInfoEnabled()) {
234                            StringBundler sb = new StringBundler(5);
235    
236                            sb.append("Full name exceeds ");
237                            sb.append(UserConstants.FULL_NAME_MAX_LENGTH);
238                            sb.append(" characters for user ");
239                            sb.append(fullName);
240                            sb.append(". Full name was shortened.");
241    
242                            _log.info(sb.toString());
243                    }
244    
245                    return fullName.substring(0, UserConstants.FULL_NAME_MAX_LENGTH);
246            }
247    
248            private static final Log _log = LogFactoryUtil.getLog(
249                    DefaultFullNameGenerator.class);
250    
251    }