1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.security.auth;
21  
22  import com.liferay.portal.NoSuchGroupException;
23  import com.liferay.portal.NoSuchUserException;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.service.GroupLocalServiceUtil;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  
29  /**
30   * <a href="ScreenNameGenerator.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Alexander Chow
34   *
35   */
36  public class ScreenNameGenerator {
37  
38      public String generate(long companyId, long userId, String emailAddress)
39          throws Exception {
40  
41          String screenName = StringUtil.extractFirst(
42              emailAddress, StringPool.AT).toLowerCase();
43  
44          screenName = StringUtil.replace(
45              screenName,
46              new String[] {StringPool.SLASH, StringPool.UNDERLINE},
47              new String[] {StringPool.PERIOD, StringPool.PERIOD});
48  
49          if (screenName.equals(ScreenNameValidator.CYRUS) ||
50              screenName.equals(ScreenNameValidator.POSTFIX)) {
51  
52              screenName += StringPool.PERIOD + userId;
53          }
54  
55          try {
56              UserLocalServiceUtil.getUserByScreenName(companyId, screenName);
57          }
58          catch (NoSuchUserException nsue) {
59              try {
60                  GroupLocalServiceUtil.getFriendlyURLGroup(
61                      companyId, StringPool.SLASH + screenName);
62              }
63              catch (NoSuchGroupException nsge) {
64                  return screenName;
65              }
66          }
67  
68          for (int i = 1;; i++) {
69              String tempScreenName = screenName + StringPool.PERIOD + i;
70  
71              try {
72                  UserLocalServiceUtil.getUserByScreenName(
73                      companyId, tempScreenName);
74              }
75              catch (NoSuchUserException nsue) {
76                  try {
77                      GroupLocalServiceUtil.getFriendlyURLGroup(
78                          companyId, StringPool.SLASH + tempScreenName);
79                  }
80                  catch (NoSuchGroupException nsge) {
81                      screenName = tempScreenName;
82  
83                      break;
84                  }
85              }
86          }
87  
88          return screenName;
89      }
90  
91  }