001    /**
002     * Copyright (c) 2000-2012 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.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.UserConstants;
025    
026    /**
027     * @author Michael C. Han
028     */
029    public class DefaultFullNameGenerator implements FullNameGenerator {
030    
031            public String getFullName(
032                    String firstName, String middleName, String lastName) {
033    
034                    String fullName = buildFullName(firstName, middleName, lastName, false);
035    
036                    if (fullName.length() <= UserConstants.FULL_NAME_MAX_LENGTH) {
037                            return fullName;
038                    }
039    
040                    if (_log.isInfoEnabled()) {
041                            StringBundler sb = new StringBundler(5);
042    
043                            sb.append("Full name exceeds ");
044                            sb.append(UserConstants.FULL_NAME_MAX_LENGTH);
045                            sb.append(" characters for user ");
046                            sb.append(fullName);
047                            sb.append(". Full name has been shortened.");
048    
049                            _log.info(sb.toString());
050                    }
051    
052                    fullName = buildFullName(firstName, middleName, lastName, true);
053    
054                    if (fullName.length() <= UserConstants.FULL_NAME_MAX_LENGTH) {
055                            return fullName;
056                    }
057    
058                    return fullName.substring(0, UserConstants.FULL_NAME_MAX_LENGTH);
059            }
060    
061            public String[] splitFullName(String fullName) {
062                    String firstName = StringPool.BLANK;
063                    String middleName = StringPool.BLANK;
064                    String lastName = StringPool.BLANK;
065    
066                    if (Validator.isNotNull(fullName)) {
067                            String[] name = StringUtil.split(fullName, CharPool.SPACE);
068    
069                            firstName = name[0];
070                            middleName = StringPool.BLANK;
071                            lastName = name[name.length - 1];
072    
073                            if (name.length > 2) {
074                                    for (int i = 1; i < name.length - 1; i++) {
075                                            if (Validator.isNull(name[i].trim())) {
076                                                    continue;
077                                            }
078    
079                                            if (i != 1) {
080                                                    middleName += StringPool.SPACE;
081                                            }
082    
083                                            middleName += name[i].trim();
084                                    }
085                            }
086                    }
087    
088                    return new String[] {firstName, middleName, lastName};
089            }
090    
091            protected String buildFullName(
092                    String firstName, String middleName, String lastName,
093                    boolean useInitials) {
094    
095                    StringBundler sb = new StringBundler(5);
096    
097                    if (useInitials) {
098                            firstName = firstName.substring(0, 1);
099                    }
100    
101                    sb.append(firstName);
102    
103                    if (Validator.isNotNull(middleName)) {
104                            if (useInitials) {
105                                    middleName = middleName.substring(0, 1);
106                            }
107    
108                            sb.append(StringPool.SPACE);
109                            sb.append(middleName);
110                    }
111    
112                    if (Validator.isNotNull(lastName)) {
113                            sb.append(StringPool.SPACE);
114                            sb.append(lastName);
115                    }
116    
117                    return sb.toString();
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(
121                    DefaultFullNameGenerator.class);
122    
123    }