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.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    /**
023     * @author Hugo Huijser
024     */
025    public class FamilyNameFirstFullNameGenerator extends DefaultFullNameGenerator {
026    
027            @Override
028            public String[] splitFullName(String fullName) {
029                    String firstName = StringPool.BLANK;
030                    String middleName = StringPool.BLANK;
031                    String lastName = StringPool.BLANK;
032    
033                    if (Validator.isNull(fullName)) {
034                            return new String[] {firstName, middleName, lastName};
035                    }
036    
037                    String[] name = StringUtil.split(fullName, StringPool.SPACE);
038    
039                    if (name.length == 1) {
040                            firstName = name[0];
041    
042                            return new String[] {firstName, middleName, lastName};
043                    }
044    
045                    lastName = name[0];
046                    firstName = name[1];
047    
048                    if (name.length > 2) {
049                            for (int i = 2; i < name.length; i++) {
050                                    if (Validator.isNull(name[i].trim())) {
051                                            continue;
052                                    }
053    
054                                    if (i != 2) {
055                                            middleName += StringPool.SPACE;
056                                    }
057    
058                                    middleName += name[i].trim();
059                            }
060                    }
061    
062                    return new String[] {firstName, middleName, lastName};
063            }
064    
065            @Override
066            protected String buildFullName(
067                    String firstName, String middleName, String lastName,
068                    boolean useInitials) {
069    
070                    StringBundler sb = new StringBundler(5);
071    
072                    if (Validator.isNotNull(lastName)) {
073                            sb.append(lastName);
074                            sb.append(StringPool.SPACE);
075                    }
076    
077                    if (useInitials) {
078                            firstName = firstName.substring(0, 1);
079                    }
080    
081                    sb.append(firstName);
082    
083                    if (Validator.isNotNull(middleName)) {
084                            if (useInitials) {
085                                    middleName = middleName.substring(0, 1);
086                            }
087    
088                            sb.append(StringPool.SPACE);
089                            sb.append(middleName);
090                    }
091    
092                    return sb.toString();
093            }
094    
095    }