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