1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.security.auth;
16  
17  import com.liferay.portal.NoSuchGroupException;
18  import com.liferay.portal.NoSuchUserException;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.service.GroupLocalServiceUtil;
23  import com.liferay.portal.service.UserLocalServiceUtil;
24  
25  /**
26   * <a href="DefaultScreenNameGenerator.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   * @author Alexander Chow
30   */
31  public class DefaultScreenNameGenerator implements ScreenNameGenerator {
32  
33      public String generate(long companyId, long userId, String emailAddress)
34          throws Exception {
35  
36          String screenName = null;
37  
38          if (Validator.isNotNull(emailAddress)) {
39              screenName = StringUtil.extractFirst(
40                  emailAddress, StringPool.AT).toLowerCase();
41  
42              screenName = StringUtil.replace(
43                  screenName,
44                  new String[] {StringPool.SLASH, StringPool.UNDERLINE},
45                  new String[] {StringPool.PERIOD, StringPool.PERIOD});
46  
47              if (screenName.equals(DefaultScreenNameValidator.CYRUS) ||
48                  screenName.equals(DefaultScreenNameValidator.POSTFIX)) {
49  
50                  screenName += StringPool.PERIOD + userId;
51              }
52          }
53          else {
54              screenName = String.valueOf(userId);
55          }
56  
57          try {
58              UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
59          }
60          catch (NoSuchUserException nsue) {
61              try {
62                  GroupLocalServiceUtil.getFriendlyURLGroup(
63                      companyId, StringPool.SLASH + screenName);
64              }
65              catch (NoSuchGroupException nsge) {
66                  return screenName;
67              }
68          }
69  
70          for (int i = 1;; i++) {
71              String tempScreenName = screenName + StringPool.PERIOD + i;
72  
73              try {
74                  UserLocalServiceUtil.getUserByScreenName(
75                      companyId, tempScreenName);
76              }
77              catch (NoSuchUserException nsue) {
78                  try {
79                      GroupLocalServiceUtil.getFriendlyURLGroup(
80                          companyId, StringPool.SLASH + tempScreenName);
81                  }
82                  catch (NoSuchGroupException nsge) {
83                      screenName = tempScreenName;
84  
85                      break;
86                  }
87              }
88          }
89  
90          return screenName;
91      }
92  
93  }