001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
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 Michael C. Han
024     */
025    public class DefaultFullNameGenerator implements FullNameGenerator {
026    
027            public String getFullName(
028                    String firstName, String middleName, String lastName) {
029    
030                    StringBuilder sb = new StringBuilder();
031    
032                    sb.append(firstName);
033                    sb.append(StringPool.SPACE);
034    
035                    if (Validator.isNull(middleName)) {
036                            sb.append(lastName);
037                    }
038                    else {
039                            sb.append(middleName);
040                            sb.append(StringPool.SPACE);
041                            sb.append(lastName);
042                    }
043    
044                    return sb.toString();
045            }
046    
047            public String[] splitFullName(String fullName) {
048                    String firstName = StringPool.BLANK;
049                    String middleName = StringPool.BLANK;
050                    String lastName = StringPool.BLANK;
051    
052                    if (Validator.isNotNull(fullName)) {
053                            String[] name = StringUtil.split(fullName, StringPool.SPACE);
054    
055                            firstName = name[0];
056                            middleName = StringPool.BLANK;
057                            lastName = name[name.length - 1];
058    
059                            if (name.length > 2) {
060                                    for (int i = 1; i < name.length - 1; i++) {
061                                            if (Validator.isNull(name[i].trim())) {
062                                                    continue;
063                                            }
064    
065                                            if (i != 1) {
066                                                    middleName += StringPool.SPACE;
067                                            }
068    
069                                            middleName += name[i].trim();
070                                    }
071                            }
072                    }
073                    else {
074                            firstName = GetterUtil.getString(firstName, lastName);
075                            lastName = firstName;
076                    }
077    
078                    return new String[] {firstName, middleName, lastName};
079            }
080    
081    }