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