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.GetterUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.UnicodeProperties;
025    import com.liferay.portal.model.Company;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.LayoutSet;
028    import com.liferay.portal.model.Shard;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.service.CompanyLocalServiceUtil;
031    import com.liferay.portal.service.GroupLocalServiceUtil;
032    import com.liferay.portal.service.ShardLocalServiceUtil;
033    import com.liferay.portal.service.UserLocalServiceUtil;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portal.util.RobotsUtil;
036    
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class VerifyGroup extends VerifyProcess {
043    
044            @Override
045            protected void doVerify() throws Exception {
046                    verifyCompanyGroups();
047                    verifyNullFriendlyURLGroups();
048                    verifyRobots();
049                    verifyStagedGroups();
050            }
051    
052            protected String getRobots(LayoutSet layoutSet) {
053                    if (layoutSet == null) {
054                            return RobotsUtil.getDefaultRobots(null);
055                    }
056    
057                    String virtualHostname = StringPool.BLANK;
058    
059                    try {
060                            virtualHostname = layoutSet.getVirtualHostname();
061                    }
062                    catch (Exception e) {
063                    }
064    
065                    return GetterUtil.get(
066                            layoutSet.getSettingsProperty(
067                                    layoutSet.isPrivateLayout() + "-robots.txt"),
068                            RobotsUtil.getDefaultRobots(virtualHostname));
069            }
070    
071            protected void verifyCompanyGroups() throws Exception {
072                    List<Company> companies = CompanyLocalServiceUtil.getCompanies();
073    
074                    String currentShardName = ShardUtil.getCurrentShardName();
075    
076                    for (Company company : companies) {
077                            String shardName = null;
078    
079                            try {
080                                    shardName = company.getShardName();
081                            }
082                            catch (NoSuchShardException nsse) {
083                                    Shard shard = ShardLocalServiceUtil.addShard(
084                                            Company.class.getName(), company.getCompanyId(),
085                                            PropsValues.SHARD_DEFAULT_NAME);
086    
087                                    shardName = shard.getName();
088                            }
089    
090                            if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
091                                    GroupLocalServiceUtil.checkCompanyGroup(company.getCompanyId());
092                            }
093                    }
094            }
095    
096            protected void verifyNullFriendlyURLGroups() throws Exception {
097                    List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
098    
099                    for (Group group : groups) {
100                            String friendlyURL = StringPool.SLASH + group.getGroupId();
101    
102                            User user = null;
103    
104                            if (group.isUser()) {
105                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
106    
107                                    friendlyURL = StringPool.SLASH + user.getScreenName();
108                            }
109                            else if (group.getClassPK() > 0) {
110                                    friendlyURL = StringPool.SLASH + group.getClassPK();
111                            }
112    
113                            try {
114                                    GroupLocalServiceUtil.updateFriendlyURL(
115                                            group.getGroupId(), friendlyURL);
116                            }
117                            catch (GroupFriendlyURLException gfurle) {
118                                    if (user != null) {
119                                            long userId = user.getUserId();
120                                            String screenName = user.getScreenName();
121    
122                                            if (_log.isInfoEnabled()) {
123                                                    _log.info(
124                                                            "Updating user screen name " + screenName + " to " +
125                                                                    userId + " because it is generating an " +
126                                                                            "invalid friendly URL");
127                                            }
128    
129                                            UserLocalServiceUtil.updateScreenName(
130                                                    userId, String.valueOf(userId));
131                                    }
132                                    else {
133                                            _log.error("Invalid Friendly URL " + friendlyURL);
134    
135                                            throw gfurle;
136                                    }
137                            }
138                    }
139            }
140    
141            protected void verifyRobots() throws Exception {
142                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
143    
144                    for (Group group : groups) {
145                            LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
146                            LayoutSet publicLayoutSet = group.getPublicLayoutSet();
147    
148                            String privateLayoutSetRobots = getRobots(privateLayoutSet);
149                            String publicLayoutSetRobots = getRobots(publicLayoutSet);
150    
151                            UnicodeProperties typeSettingsProperties =
152                                    group.getTypeSettingsProperties();
153    
154                            typeSettingsProperties.setProperty(
155                                    "true-robots.txt", privateLayoutSetRobots);
156                            typeSettingsProperties.setProperty(
157                                    "false-robots.txt", publicLayoutSetRobots);
158    
159                            GroupLocalServiceUtil.updateGroup(
160                                    group.getGroupId(), typeSettingsProperties.toString());
161                    }
162    
163            }
164            protected void verifyStagedGroups() throws Exception {
165                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
166    
167                    for (Group group : groups) {
168                            if (!group.hasStagingGroup()) {
169                                    continue;
170                            }
171    
172                            UnicodeProperties typeSettingsProperties =
173                                    group.getTypeSettingsProperties();
174    
175                            typeSettingsProperties.setProperty(
176                                    "staged", Boolean.TRUE.toString());
177                            typeSettingsProperties.setProperty(
178                                    "stagedRemotely", Boolean.FALSE.toString());
179    
180                            GroupLocalServiceUtil.updateGroup(
181                                    group.getGroupId(), typeSettingsProperties.toString());
182    
183                            Group stagingGroup = group.getStagingGroup();
184    
185                            if (group.getClassNameId() != stagingGroup.getClassNameId()) {
186                                    stagingGroup.setClassNameId(group.getClassNameId());
187    
188                                    GroupLocalServiceUtil.updateGroup(stagingGroup);
189                            }
190                    }
191            }
192    
193            private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
194    
195    }