1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.auth;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  /**
23   * <a href="DefaultFullNameGenerator.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Michael C. Han
26   */
27  public class DefaultFullNameGenerator implements FullNameGenerator {
28  
29      public String getFullName(
30          String firstName, String middleName, String lastName) {
31  
32          StringBuilder sb = new StringBuilder();
33  
34          sb.append(firstName);
35          sb.append(StringPool.SPACE);
36  
37          if (Validator.isNull(middleName)) {
38              sb.append(lastName);
39          }
40          else {
41              sb.append(middleName);
42              sb.append(StringPool.SPACE);
43              sb.append(lastName);
44          }
45  
46          return sb.toString();
47      }
48  
49      public String[] splitFullName(String fullName) {
50          String firstName = StringPool.BLANK;
51          String middleName = StringPool.BLANK;
52          String lastName = StringPool.BLANK;
53  
54          if (Validator.isNotNull(fullName)) {
55              String[] name = StringUtil.split(fullName, StringPool.SPACE);
56  
57              firstName = name[0];
58              middleName = StringPool.BLANK;
59              lastName = name[name.length - 1];
60  
61              if (name.length > 2) {
62                  for (int i = 1; i < name.length - 1; i++) {
63                      if (Validator.isNull(name[i].trim())) {
64                          continue;
65                      }
66  
67                      if (i != 1) {
68                          middleName += StringPool.SPACE;
69                      }
70  
71                      middleName += name[i].trim();
72                  }
73              }
74          }
75          else {
76              firstName = GetterUtil.getString(firstName, lastName);
77              lastName = firstName;
78          }
79  
80          return new String[] {firstName, middleName, lastName};
81      }
82  
83  }