001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Company;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.CompanyLocalServiceUtil;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class VerifyGroup extends VerifyProcess {
035    
036            protected void doVerify() throws Exception {
037                    verifyCompanyGroups();
038                    verifyNullFriendlyURLGroups();
039                    verifyStagedGroups();
040            }
041    
042            protected void verifyCompanyGroups() throws Exception {
043                    List<Company> companies = CompanyLocalServiceUtil.getCompanies();
044    
045                    for (Company company : companies) {
046                            GroupLocalServiceUtil.checkCompanyGroup(company.getCompanyId());
047                    }
048            }
049    
050            protected void verifyNullFriendlyURLGroups() throws Exception {
051                    List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
052    
053                    for (Group group : groups) {
054                            String friendlyURL = StringPool.SLASH + group.getGroupId();
055    
056                            User user = null;
057    
058                            if (group.isUser()) {
059                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
060    
061                                    friendlyURL = StringPool.SLASH + user.getScreenName();
062                            }
063                            else if (group.getClassPK() > 0) {
064                                    friendlyURL = StringPool.SLASH + group.getClassPK();
065                            }
066    
067                            try {
068                                    GroupLocalServiceUtil.updateFriendlyURL(
069                                            group.getGroupId(), friendlyURL);
070                            }
071                            catch (GroupFriendlyURLException gfurle) {
072                                    if (user != null) {
073                                            long userId = user.getUserId();
074                                            String screenName = user.getScreenName();
075    
076                                            if (_log.isInfoEnabled()) {
077                                                    _log.info(
078                                                            "Updating user screen name " + screenName + " to " +
079                                                                    userId + " because it is generating an " +
080                                                                            "invalid friendly URL");
081                                            }
082    
083                                            UserLocalServiceUtil.updateScreenName(
084                                                    userId, String.valueOf(userId));
085                                    }
086                                    else {
087                                            _log.error("Invalid Friendly URL " + friendlyURL);
088    
089                                            throw gfurle;
090                                    }
091                            }
092                    }
093            }
094    
095            protected void verifyStagedGroups() throws Exception {
096                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
097    
098                    for (Group group : groups) {
099                            if (!group.hasStagingGroup()) {
100                                    continue;
101                            }
102    
103                            UnicodeProperties typeSettingsProperties =
104                                    group.getTypeSettingsProperties();
105    
106                            typeSettingsProperties.setProperty(
107                                    "staged", Boolean.TRUE.toString());
108                            typeSettingsProperties.setProperty(
109                                    "stagedRemotely", Boolean.FALSE.toString());
110    
111                            GroupLocalServiceUtil.updateGroup(
112                                    group.getGroupId(), typeSettingsProperties.toString());
113                    }
114            }
115    
116            private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
117    
118    }