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.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  }