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.exception.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.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    /**
037     * @author Michael C. Han
038     */
039    public class DefaultFullNameGenerator implements FullNameGenerator {
040    
041            @Override
042            public String getFullName(
043                    String firstName, String middleName, String lastName) {
044    
045                    String fullName = buildFullName(firstName, middleName, lastName, false);
046    
047                    if (!isFullNameTooLong(fullName)) {
048                            return fullName;
049                    }
050    
051                    fullName = buildFullName(firstName, middleName, lastName, true);
052    
053                    if (!isFullNameTooLong(fullName)) {
054                            return fullName;
055                    }
056    
057                    return shortenFullName(fullName);
058            }
059    
060            @Override
061            public String getLocalizedFullName(
062                    String firstName, String middleName, String lastName, Locale locale,
063                    long prefixId, long suffixId) {
064    
065                    String fullName = buildLocalizedFullName(
066                            firstName, middleName, lastName, locale, prefixId, suffixId, false);
067    
068                    if (!isFullNameTooLong(fullName)) {
069                            return fullName;
070                    }
071    
072                    fullName = buildLocalizedFullName(
073                            firstName, middleName, lastName, locale, prefixId, suffixId, true);
074    
075                    if (!isFullNameTooLong(fullName)) {
076                            return fullName;
077                    }
078    
079                    return shortenFullName(fullName);
080            }
081    
082            @Override
083            public String[] splitFullName(String fullName) {
084                    String firstName = StringPool.BLANK;
085                    String middleName = StringPool.BLANK;
086                    String lastName = StringPool.BLANK;
087    
088                    if (Validator.isNull(fullName)) {
089                            return new String[] {firstName, middleName, lastName};
090                    }
091    
092                    String[] name = StringUtil.split(fullName, CharPool.SPACE);
093    
094                    firstName = name[0];
095                    middleName = StringPool.BLANK;
096                    lastName = name[name.length - 1];
097    
098                    if (name.length > 2) {
099                            for (int i = 1; i < name.length - 1; i++) {
100                                    if (Validator.isNull(name[i].trim())) {
101                                            continue;
102                                    }
103    
104                                    if (i != 1) {
105                                            middleName += StringPool.SPACE;
106                                    }
107    
108                                    middleName += name[i].trim();
109                            }
110                    }
111    
112                    return new String[] {firstName, middleName, lastName};
113            }
114    
115            protected String buildFullName(
116                    String firstName, String middleName, String lastName,
117                    boolean useInitials) {
118    
119                    StringBundler sb = new StringBundler(5);
120    
121                    if (useInitials) {
122                            firstName = firstName.substring(0, 1);
123                    }
124    
125                    sb.append(firstName);
126    
127                    if (Validator.isNotNull(middleName)) {
128                            if (useInitials) {
129                                    middleName = middleName.substring(0, 1);
130                            }
131    
132                            sb.append(StringPool.SPACE);
133                            sb.append(middleName);
134                    }
135    
136                    if (Validator.isNotNull(lastName)) {
137                            sb.append(StringPool.SPACE);
138                            sb.append(lastName);
139                    }
140    
141                    return sb.toString();
142            }
143    
144            protected String buildLocalizedFullName(
145                    String firstName, String middleName, String lastName, Locale locale,
146                    long prefixId, long suffixId, boolean useInitials) {
147    
148                    Map<String, String> namesMap = new HashMap<>();
149    
150                    if (Validator.isNotNull(firstName)) {
151                            if (useInitials) {
152                                    firstName = firstName.substring(0, 1);
153                            }
154    
155                            namesMap.put("first-name", firstName);
156                    }
157    
158                    if (Validator.isNotNull(middleName)) {
159                            if (useInitials) {
160                                    middleName = middleName.substring(0, 1);
161                            }
162    
163                            namesMap.put("middle-name", middleName);
164                    }
165    
166                    if (Validator.isNotNull(lastName)) {
167                            namesMap.put("last-name", lastName);
168                    }
169    
170                    if (prefixId != 0) {
171                            try {
172                                    String prefix = ListTypeServiceUtil.getListType(
173                                            prefixId).getName();
174    
175                                    prefix = LanguageUtil.get(locale, prefix);
176    
177                                    namesMap.put("prefix", prefix);
178                            }
179                            catch (NoSuchListTypeException nslte) {
180                                    if (_log.isDebugEnabled()) {
181                                            _log.debug("Ignoring full name prefix " + prefixId, nslte);
182                                    }
183                            }
184                            catch (PortalException pe) {
185                                    throw new SystemException(pe);
186                            }
187                    }
188    
189                    if (suffixId != 0) {
190                            try {
191                                    String suffix = ListTypeServiceUtil.getListType(
192                                            suffixId).getName();
193    
194                                    suffix = LanguageUtil.get(locale, suffix);
195    
196                                    namesMap.put("suffix", suffix);
197                            }
198                            catch (NoSuchListTypeException nslte) {
199                                    if (_log.isDebugEnabled()) {
200                                            _log.debug("Ignoring full name suffix " + suffixId, nslte);
201                                    }
202                            }
203                            catch (PortalException pe) {
204                                    throw new SystemException(pe);
205                            }
206                    }
207    
208                    FullNameDefinition fullNameDefinition =
209                            FullNameDefinitionFactory.getInstance(locale);
210    
211                    List<FullNameField> fullNameFields =
212                            fullNameDefinition.getFullNameFields();
213    
214                    StringBundler sb = new StringBundler(2 * fullNameFields.size());
215    
216                    for (FullNameField fullNameField : fullNameFields) {
217                            String name = namesMap.get(fullNameField.getName());
218    
219                            if (name != null) {
220                                    sb.append(StringPool.SPACE);
221                                    sb.append(name);
222                            }
223                    }
224    
225                    return StringUtil.trim(sb.toString());
226            }
227    
228            protected boolean isFullNameTooLong(String fullName) {
229                    if (fullName.length() > UserConstants.FULL_NAME_MAX_LENGTH) {
230                            return true;
231                    }
232    
233                    return false;
234            }
235    
236            protected String shortenFullName(String fullName) {
237                    if (_log.isInfoEnabled()) {
238                            StringBundler sb = new StringBundler(5);
239    
240                            sb.append("Full name exceeds ");
241                            sb.append(UserConstants.FULL_NAME_MAX_LENGTH);
242                            sb.append(" characters for user ");
243                            sb.append(fullName);
244                            sb.append(". Full name was shortened.");
245    
246                            _log.info(sb.toString());
247                    }
248    
249                    return fullName.substring(0, UserConstants.FULL_NAME_MAX_LENGTH);
250            }
251    
252            private static final Log _log = LogFactoryUtil.getLog(
253                    DefaultFullNameGenerator.class);
254    
255    }