1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.security.auth;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.NoSuchUserException;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.service.UserLocalServiceUtil;
32  
33  /**
34   * <a href="ScreenNameGenerator.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Alexander Chow
38   *
39   */
40  public class ScreenNameGenerator {
41  
42      public String generate(long companyId, long userId, String emailAddress)
43          throws Exception {
44  
45          String screenName = null;
46  
47          if (Validator.isNotNull(emailAddress)) {
48              screenName = StringUtil.extractFirst(
49                  emailAddress, StringPool.AT).toLowerCase();
50  
51              screenName = StringUtil.replace(
52                  screenName,
53                  new String[] {StringPool.SLASH, StringPool.UNDERLINE},
54                  new String[] {StringPool.PERIOD, StringPool.PERIOD});
55  
56              if (screenName.equals(ScreenNameValidator.CYRUS) ||
57                  screenName.equals(ScreenNameValidator.POSTFIX)) {
58  
59                  screenName += StringPool.PERIOD + userId;
60              }
61          }
62          else {
63              screenName = String.valueOf(userId);
64          }
65  
66          try {
67              UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
68          }
69          catch (NoSuchUserException nsue) {
70              try {
71                  GroupLocalServiceUtil.getFriendlyURLGroup(
72                      companyId, StringPool.SLASH + screenName);
73              }
74              catch (NoSuchGroupException nsge) {
75                  return screenName;
76              }
77          }
78  
79          for (int i = 1;; i++) {
80              String tempScreenName = screenName + StringPool.PERIOD + i;
81  
82              try {
83                  UserLocalServiceUtil.getUserByScreenName(
84                      companyId, tempScreenName);
85              }
86              catch (NoSuchUserException nsue) {
87                  try {
88                      GroupLocalServiceUtil.getFriendlyURLGroup(
89                          companyId, StringPool.SLASH + tempScreenName);
90                  }
91                  catch (NoSuchGroupException nsge) {
92                      screenName = tempScreenName;
93  
94                      break;
95                  }
96              }
97          }
98  
99          return screenName;
100     }
101 
102 }