001    /**
002     * Copyright (c) 2000-2012 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.NoSuchShardException;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.dao.shard.ShardUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.model.Company;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.LayoutSet;
030    import com.liferay.portal.model.Shard;
031    import com.liferay.portal.model.User;
032    import com.liferay.portal.service.CompanyLocalServiceUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.ShardLocalServiceUtil;
035    import com.liferay.portal.service.UserLocalServiceUtil;
036    import com.liferay.portal.service.impl.GroupLocalServiceImpl;
037    import com.liferay.portal.util.PropsValues;
038    import com.liferay.portal.util.RobotsUtil;
039    
040    import java.sql.Connection;
041    import java.sql.PreparedStatement;
042    import java.sql.ResultSet;
043    
044    import java.util.List;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     */
049    public class VerifyGroup extends VerifyProcess {
050    
051            @Override
052            protected void doVerify() throws Exception {
053                    verifyCompanyGroups();
054                    verifyNullFriendlyURLGroups();
055                    verifyOrganizationNames();
056                    verifyRobots();
057                    verifyStagedGroups();
058            }
059    
060            protected String getRobots(LayoutSet layoutSet) {
061                    if (layoutSet == null) {
062                            return RobotsUtil.getDefaultRobots(null);
063                    }
064    
065                    String virtualHostname = StringPool.BLANK;
066    
067                    try {
068                            virtualHostname = layoutSet.getVirtualHostname();
069                    }
070                    catch (Exception e) {
071                    }
072    
073                    return GetterUtil.get(
074                            layoutSet.getSettingsProperty(
075                                    layoutSet.isPrivateLayout() + "-robots.txt"),
076                            RobotsUtil.getDefaultRobots(virtualHostname));
077            }
078    
079            protected void updateName(long groupId, String name) throws Exception {
080                    Connection con = null;
081                    PreparedStatement ps = null;
082    
083                    try {
084                            con = DataAccess.getUpgradeOptimizedConnection();
085    
086                            ps = con.prepareStatement(
087                                    "update Group_ set name = ? where groupId= " + groupId);
088    
089                            ps.setString(1, name);
090    
091                            ps.executeUpdate();
092                    }
093                    finally {
094                            DataAccess.cleanUp(con, ps);
095                    }
096            }
097    
098            protected void verifyCompanyGroups() throws Exception {
099                    List<Company> companies = CompanyLocalServiceUtil.getCompanies();
100    
101                    String currentShardName = ShardUtil.getCurrentShardName();
102    
103                    for (Company company : companies) {
104                            String shardName = null;
105    
106                            try {
107                                    shardName = company.getShardName();
108                            }
109                            catch (NoSuchShardException nsse) {
110                                    Shard shard = ShardLocalServiceUtil.addShard(
111                                            Company.class.getName(), company.getCompanyId(),
112                                            PropsValues.SHARD_DEFAULT_NAME);
113    
114                                    shardName = shard.getName();
115                            }
116    
117                            if (!ShardUtil.isEnabled() || shardName.equals(currentShardName)) {
118                                    GroupLocalServiceUtil.checkCompanyGroup(company.getCompanyId());
119                            }
120                    }
121            }
122    
123            protected void verifyNullFriendlyURLGroups() throws Exception {
124                    List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
125    
126                    for (Group group : groups) {
127                            String friendlyURL = StringPool.SLASH + group.getGroupId();
128    
129                            User user = null;
130    
131                            if (group.isUser()) {
132                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
133    
134                                    friendlyURL = StringPool.SLASH + user.getScreenName();
135                            }
136                            else if (group.getClassPK() > 0) {
137                                    friendlyURL = StringPool.SLASH + group.getClassPK();
138                            }
139    
140                            try {
141                                    GroupLocalServiceUtil.updateFriendlyURL(
142                                            group.getGroupId(), friendlyURL);
143                            }
144                            catch (GroupFriendlyURLException gfurle) {
145                                    if (user != null) {
146                                            long userId = user.getUserId();
147                                            String screenName = user.getScreenName();
148    
149                                            if (_log.isInfoEnabled()) {
150                                                    _log.info(
151                                                            "Updating user screen name " + screenName + " to " +
152                                                                    userId + " because it is generating an " +
153                                                                            "invalid friendly URL");
154                                            }
155    
156                                            UserLocalServiceUtil.updateScreenName(
157                                                    userId, String.valueOf(userId));
158                                    }
159                                    else {
160                                            _log.error("Invalid Friendly URL " + friendlyURL);
161    
162                                            throw gfurle;
163                                    }
164                            }
165                    }
166            }
167    
168            protected void verifyOrganizationNames() throws Exception {
169                    Connection con = null;
170                    PreparedStatement ps = null;
171                    ResultSet rs = null;
172    
173                    try {
174                            con = DataAccess.getUpgradeOptimizedConnection();
175    
176                            StringBundler sb = new StringBundler(5);
177    
178                            sb.append("select groupId, name from Group_ where name like '%");
179                            sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
180                            sb.append("%' and name not like '%");
181                            sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
182                            sb.append("'");
183    
184                            ps = con.prepareStatement(sb.toString());
185    
186                            rs = ps.executeQuery();
187    
188                            while (rs.next()) {
189                                    long groupId = rs.getLong("groupId");
190                                    String name = rs.getString("name");
191    
192                                    int pos = name.indexOf(
193                                            GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
194    
195                                    pos = name.indexOf(" ", pos + 1);
196    
197                                    String newName =
198                                            name.substring(pos + 1) +
199                                                    GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX;
200    
201                                    updateName(groupId, newName);
202                            }
203                    }
204                    finally {
205                            DataAccess.cleanUp(con, ps, rs);
206                    }
207            }
208    
209            protected void verifyRobots() throws Exception {
210                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
211    
212                    for (Group group : groups) {
213                            LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
214                            LayoutSet publicLayoutSet = group.getPublicLayoutSet();
215    
216                            String privateLayoutSetRobots = getRobots(privateLayoutSet);
217                            String publicLayoutSetRobots = getRobots(publicLayoutSet);
218    
219                            UnicodeProperties typeSettingsProperties =
220                                    group.getTypeSettingsProperties();
221    
222                            typeSettingsProperties.setProperty(
223                                    "true-robots.txt", privateLayoutSetRobots);
224                            typeSettingsProperties.setProperty(
225                                    "false-robots.txt", publicLayoutSetRobots);
226    
227                            GroupLocalServiceUtil.updateGroup(
228                                    group.getGroupId(), typeSettingsProperties.toString());
229                    }
230            }
231    
232            protected void verifyStagedGroups() throws Exception {
233                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
234    
235                    for (Group group : groups) {
236                            if (!group.hasStagingGroup()) {
237                                    continue;
238                            }
239    
240                            UnicodeProperties typeSettingsProperties =
241                                    group.getTypeSettingsProperties();
242    
243                            typeSettingsProperties.setProperty(
244                                    "staged", Boolean.TRUE.toString());
245                            typeSettingsProperties.setProperty(
246                                    "stagedRemotely", Boolean.FALSE.toString());
247    
248                            GroupLocalServiceUtil.updateGroup(
249                                    group.getGroupId(), typeSettingsProperties.toString());
250    
251                            Group stagingGroup = group.getStagingGroup();
252    
253                            if (group.getClassNameId() != stagingGroup.getClassNameId()) {
254                                    stagingGroup.setClassNameId(group.getClassNameId());
255    
256                                    GroupLocalServiceUtil.updateGroup(stagingGroup);
257                            }
258                    }
259            }
260    
261            private static Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
262    
263    }