001    /**
002     * Copyright (c) 2000-2011 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.NoSuchShardException;
019    import com.liferay.portal.kernel.dao.shard.ShardUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Shard;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.service.CompanyLocalServiceUtil;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.ShardLocalServiceUtil;
031    import com.liferay.portal.service.UserLocalServiceUtil;
032    import com.liferay.portal.util.PropsValues;
033    
034    import java.util.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class VerifyGroup extends VerifyProcess {
040    
041            protected void doVerify() throws Exception {
042                    verifyCompanyGroups();
043                    verifyNullFriendlyURLGroups();
044                    verifyStagedGroups();
045            }
046    
047            protected void verifyCompanyGroups() throws Exception {
048                    List<Company> companies = CompanyLocalServiceUtil.getCompanies();
049    
050                    String currentShardName = ShardUtil.getCurrentShardName();
051    
052                    for (Company company : companies) {
053                            String shardName = null;
054    
055                            try {
056                                    shardName = company.getShardName();
057                            }
058                            catch (NoSuchShardException nsse) {
059                                    Shard shard = ShardLocalServiceUtil.addShard(
060                                            Company.class.getName(), company.getCompanyId(),
061                                            PropsValues.SHARD_DEFAULT_NAME);
062    
063                                    shardName = shard.getName();
064                            }
065    
066                            if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
067                                    GroupLocalServiceUtil.checkCompanyGroup(company.getCompanyId());
068                            }
069                    }
070            }
071    
072            protected void verifyNullFriendlyURLGroups() throws Exception {
073                    List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
074    
075                    for (Group group : groups) {
076                            String friendlyURL = StringPool.SLASH + group.getGroupId();
077    
078                            User user = null;
079    
080                            if (group.isUser()) {
081                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
082    
083                                    friendlyURL = StringPool.SLASH + user.getScreenName();
084                            }
085                            else if (group.getClassPK() > 0) {
086                                    friendlyURL = StringPool.SLASH + group.getClassPK();
087                            }
088    
089                            try {
090                                    GroupLocalServiceUtil.updateFriendlyURL(
091                                            group.getGroupId(), friendlyURL);
092                            }
093                            catch (GroupFriendlyURLException gfurle) {
094                                    if (user != null) {
095                                            long userId = user.getUserId();
096                                            String screenName = user.getScreenName();
097    
098                                            if (_log.isInfoEnabled()) {
099                                                    _log.info(
100                                                            "Updating user screen name " + screenName + " to " +
101                                                                    userId + " because it is generating an " +
102                                                                            "invalid friendly URL");
103                                            }
104    
105                                            UserLocalServiceUtil.updateScreenName(
106                                                    userId, String.valueOf(userId));
107                                    }
108                                    else {
109                                            _log.error("Invalid Friendly URL " + friendlyURL);
110    
111                                            throw gfurle;
112                                    }
113                            }
114                    }
115            }
116    
117            protected void verifyStagedGroups() throws Exception {
118                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
119    
120                    for (Group group : groups) {
121                            if (!group.hasStagingGroup()) {
122                                    continue;
123                            }
124    
125                            UnicodeProperties typeSettingsProperties =
126                                    group.getTypeSettingsProperties();
127    
128                            typeSettingsProperties.setProperty(
129                                    "staged", Boolean.TRUE.toString());
130                            typeSettingsProperties.setProperty(
131                                    "stagedRemotely", Boolean.FALSE.toString());
132    
133                            GroupLocalServiceUtil.updateGroup(
134                                    group.getGroupId(), typeSettingsProperties.toString());
135                    }
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
139    
140    }