001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.GroupFriendlyURLException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class VerifyGroup extends VerifyProcess {
033    
034            protected void doVerify() throws Exception {
035                    verifyNullFriendlyURLGroups();
036                    verifyStagedGroups();
037            }
038    
039            protected void verifyNullFriendlyURLGroups() throws Exception {
040                    List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
041    
042                    for (Group group : groups) {
043                            String friendlyURL = StringPool.SLASH + group.getGroupId();
044    
045                            User user = null;
046    
047                            if (group.isUser()) {
048                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
049    
050                                    friendlyURL = StringPool.SLASH + user.getScreenName();
051                            }
052                            else if (group.getClassPK() > 0) {
053                                    friendlyURL = StringPool.SLASH + group.getClassPK();
054                            }
055    
056                            try {
057                                    GroupLocalServiceUtil.updateFriendlyURL(
058                                            group.getGroupId(), friendlyURL);
059                            }
060                            catch (GroupFriendlyURLException gfurle) {
061                                    if (user != null) {
062                                            long userId = user.getUserId();
063                                            String screenName = user.getScreenName();
064    
065                                            if (_log.isInfoEnabled()) {
066                                                    _log.info(
067                                                            "Updating user screen name " + screenName + " to " +
068                                                                    userId + " because it is generating an " +
069                                                                            "invalid friendly URL");
070                                            }
071    
072                                            UserLocalServiceUtil.updateScreenName(
073                                                    userId, String.valueOf(userId));
074                                    }
075                                    else {
076                                            _log.error("Invalid Friendly URL " + friendlyURL);
077    
078                                            throw gfurle;
079                                    }
080                            }
081                    }
082            }
083    
084            protected void verifyStagedGroups() throws Exception {
085                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
086    
087                    for (Group group : groups) {
088                            if (!group.hasStagingGroup()) {
089                                    continue;
090                            }
091    
092                            UnicodeProperties typeSettingsProperties =
093                                    group.getTypeSettingsProperties();
094    
095                            typeSettingsProperties.setProperty(
096                                    "staged", Boolean.TRUE.toString());
097                            typeSettingsProperties.setProperty(
098                                    "stagedRemotely", Boolean.FALSE.toString());
099    
100                            GroupLocalServiceUtil.updateGroup(
101                                    group.getGroupId(), typeSettingsProperties.toString());
102                    }
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
106    
107    }