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.verify;
16  
17  import com.liferay.portal.GroupFriendlyURLException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.model.Group;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.service.GroupLocalServiceUtil;
24  import com.liferay.portal.service.UserLocalServiceUtil;
25  
26  import java.util.List;
27  
28  /**
29   * <a href="VerifyGroup.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class VerifyGroup extends VerifyProcess {
34  
35      protected void doVerify() throws Exception {
36          List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
37  
38          for (Group group : groups) {
39              String friendlyURL = StringPool.SLASH + group.getGroupId();
40  
41              User user = null;
42  
43              if (group.isUser()) {
44                  user = UserLocalServiceUtil.getUserById(group.getClassPK());
45  
46                  friendlyURL = StringPool.SLASH + user.getScreenName();
47              }
48              else if (group.getClassPK() > 0) {
49                  friendlyURL = StringPool.SLASH + group.getClassPK();
50              }
51  
52              try {
53                  GroupLocalServiceUtil.updateFriendlyURL(
54                      group.getGroupId(), friendlyURL);
55              }
56              catch (GroupFriendlyURLException gfurle) {
57                  if (user != null) {
58                      long userId = user.getUserId();
59                      String screenName = user.getScreenName();
60  
61                      if (_log.isInfoEnabled()) {
62                          _log.info(
63                              "Updating user screen name " + screenName + " to " +
64                                  userId + " because it is generating an " +
65                                      "invalid friendly URL");
66                      }
67  
68                      UserLocalServiceUtil.updateScreenName(
69                          userId, String.valueOf(userId));
70                  }
71                  else {
72                      _log.error("Invalid Friendly URL " + friendlyURL);
73  
74                      throw gfurle;
75                  }
76              }
77          }
78      }
79  
80      private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
81  
82  }