001    /**
002     * Copyright (c) 2000-present 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.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
020    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.ArrayUtil;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.UnicodeProperties;
029    import com.liferay.portal.model.Company;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.GroupConstants;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.model.Organization;
034    import com.liferay.portal.model.User;
035    import com.liferay.portal.service.CompanyLocalServiceUtil;
036    import com.liferay.portal.service.GroupLocalServiceUtil;
037    import com.liferay.portal.service.UserLocalServiceUtil;
038    import com.liferay.portal.service.impl.GroupLocalServiceImpl;
039    import com.liferay.portal.util.PortalInstances;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portal.util.RobotsUtil;
042    
043    import java.sql.Connection;
044    import java.sql.PreparedStatement;
045    import java.sql.ResultSet;
046    
047    import java.util.Iterator;
048    import java.util.List;
049    import java.util.Set;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     */
054    public class VerifyGroup extends VerifyProcess {
055    
056            @Override
057            protected void doVerify() throws Exception {
058                    verifyCompanyGroups();
059                    verifyNullFriendlyURLGroups();
060                    verifyOrganizationNames();
061                    verifyRobots();
062                    verifySites();
063                    verifyStagedGroups();
064                    verifyTree();
065            }
066    
067            protected String getRobots(LayoutSet layoutSet) {
068                    if (layoutSet == null) {
069                            return RobotsUtil.getDefaultRobots(null);
070                    }
071    
072                    String virtualHostname = StringPool.BLANK;
073    
074                    try {
075                            virtualHostname = layoutSet.getVirtualHostname();
076                    }
077                    catch (Exception e) {
078                    }
079    
080                    return GetterUtil.get(
081                            layoutSet.getSettingsProperty(
082                                    layoutSet.isPrivateLayout() + "-robots.txt"),
083                            RobotsUtil.getDefaultRobots(virtualHostname));
084            }
085    
086            protected void updateName(long groupId, String name) throws Exception {
087                    Connection con = null;
088                    PreparedStatement ps = null;
089    
090                    try {
091                            con = DataAccess.getUpgradeOptimizedConnection();
092    
093                            ps = con.prepareStatement(
094                                    "update Group_ set name = ? where groupId= " + groupId);
095    
096                            ps.setString(1, name);
097    
098                            ps.executeUpdate();
099                    }
100                    finally {
101                            DataAccess.cleanUp(con, ps);
102                    }
103            }
104    
105            protected void verifyCompanyGroups() throws Exception {
106                    List<Company> companies = CompanyLocalServiceUtil.getCompanies();
107    
108                    for (Company company : companies) {
109                            GroupLocalServiceUtil.checkCompanyGroup(company.getCompanyId());
110    
111                            GroupLocalServiceUtil.checkSystemGroups(company.getCompanyId());
112                    }
113            }
114    
115            protected void verifyNullFriendlyURLGroups() throws Exception {
116                    List<Group> groups = GroupLocalServiceUtil.getNullFriendlyURLGroups();
117    
118                    for (Group group : groups) {
119                            String friendlyURL = StringPool.SLASH + group.getGroupId();
120    
121                            User user = null;
122    
123                            if (group.isCompany() && !group.isCompanyStagingGroup()) {
124                                    friendlyURL = GroupConstants.GLOBAL_FRIENDLY_URL;
125                            }
126                            else if (group.isUser()) {
127                                    user = UserLocalServiceUtil.getUserById(group.getClassPK());
128    
129                                    friendlyURL = StringPool.SLASH + user.getScreenName();
130                            }
131                            else if (group.getClassPK() > 0) {
132                                    friendlyURL = StringPool.SLASH + group.getClassPK();
133                            }
134    
135                            try {
136                                    GroupLocalServiceUtil.updateFriendlyURL(
137                                            group.getGroupId(), friendlyURL);
138                            }
139                            catch (GroupFriendlyURLException gfurle) {
140                                    if (user != null) {
141                                            long userId = user.getUserId();
142                                            String screenName = user.getScreenName();
143    
144                                            if (_log.isInfoEnabled()) {
145                                                    _log.info(
146                                                            "Updating user screen name " + screenName + " to " +
147                                                                    userId + " because it is generating an " +
148                                                                            "invalid friendly URL");
149                                            }
150    
151                                            UserLocalServiceUtil.updateScreenName(
152                                                    userId, String.valueOf(userId));
153                                    }
154                                    else {
155                                            _log.error("Invalid Friendly URL " + friendlyURL);
156    
157                                            throw gfurle;
158                                    }
159                            }
160                    }
161            }
162    
163            protected void verifyOrganizationNames() throws Exception {
164                    Connection con = null;
165                    PreparedStatement ps = null;
166                    ResultSet rs = null;
167    
168                    try {
169                            con = DataAccess.getUpgradeOptimizedConnection();
170    
171                            StringBundler sb = new StringBundler(5);
172    
173                            sb.append("select groupId, name from Group_ where name like '%");
174                            sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
175                            sb.append("%' and name not like '%");
176                            sb.append(GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
177                            sb.append("'");
178    
179                            ps = con.prepareStatement(sb.toString());
180    
181                            rs = ps.executeQuery();
182    
183                            while (rs.next()) {
184                                    long groupId = rs.getLong("groupId");
185                                    String name = rs.getString("name");
186    
187                                    if (name.endsWith(
188                                                    GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX) ||
189                                            name.endsWith(
190                                                    GroupLocalServiceImpl.ORGANIZATION_STAGING_SUFFIX)) {
191    
192                                            continue;
193                                    }
194    
195                                    int pos = name.indexOf(
196                                            GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX);
197    
198                                    pos = name.indexOf(" ", pos + 1);
199    
200                                    String newName =
201                                            name.substring(pos + 1) +
202                                                    GroupLocalServiceImpl.ORGANIZATION_NAME_SUFFIX;
203    
204                                    updateName(groupId, newName);
205                            }
206                    }
207                    finally {
208                            DataAccess.cleanUp(con, ps, rs);
209                    }
210            }
211    
212            protected void verifyRobots() throws Exception {
213                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
214    
215                    for (Group group : groups) {
216                            LayoutSet privateLayoutSet = group.getPrivateLayoutSet();
217                            LayoutSet publicLayoutSet = group.getPublicLayoutSet();
218    
219                            String privateLayoutSetRobots = getRobots(privateLayoutSet);
220                            String publicLayoutSetRobots = getRobots(publicLayoutSet);
221    
222                            UnicodeProperties typeSettingsProperties =
223                                    group.getTypeSettingsProperties();
224    
225                            typeSettingsProperties.setProperty(
226                                    "true-robots.txt", privateLayoutSetRobots);
227                            typeSettingsProperties.setProperty(
228                                    "false-robots.txt", publicLayoutSetRobots);
229    
230                            GroupLocalServiceUtil.updateGroup(
231                                    group.getGroupId(), typeSettingsProperties.toString());
232                    }
233            }
234    
235            protected void verifySites() throws Exception {
236                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
237                            Group.class);
238    
239                    dynamicQuery.add(
240                            RestrictionsFactoryUtil.eq(
241                                    "classNameId", PortalUtil.getClassNameId(Organization.class)));
242                    dynamicQuery.add(RestrictionsFactoryUtil.eq("site", false));
243    
244                    List<Group> groups = GroupLocalServiceUtil.dynamicQuery(dynamicQuery);
245    
246                    for (Group group : groups) {
247                            if ((group.getPrivateLayoutsPageCount() > 0) ||
248                                    (group.getPublicLayoutsPageCount() > 0)) {
249    
250                                    group.setSite(true);
251    
252                                    GroupLocalServiceUtil.updateGroup(group);
253                            }
254                    }
255            }
256    
257            protected void verifyStagedGroups() throws Exception {
258                    List<Group> groups = GroupLocalServiceUtil.getLiveGroups();
259    
260                    for (Group group : groups) {
261                            if (!group.hasStagingGroup()) {
262                                    continue;
263                            }
264    
265                            UnicodeProperties typeSettingsProperties =
266                                    group.getTypeSettingsProperties();
267    
268                            typeSettingsProperties.setProperty(
269                                    "staged", Boolean.TRUE.toString());
270                            typeSettingsProperties.setProperty(
271                                    "stagedRemotely", Boolean.FALSE.toString());
272    
273                            verifyStagingTypeSettingsProperties(typeSettingsProperties);
274    
275                            GroupLocalServiceUtil.updateGroup(
276                                    group.getGroupId(), typeSettingsProperties.toString());
277    
278                            Group stagingGroup = group.getStagingGroup();
279    
280                            if (group.getClassNameId() != stagingGroup.getClassNameId()) {
281                                    stagingGroup.setClassNameId(group.getClassNameId());
282    
283                                    GroupLocalServiceUtil.updateGroup(stagingGroup);
284                            }
285                    }
286            }
287    
288            protected void verifyStagingTypeSettingsProperties(
289                    UnicodeProperties typeSettingsProperties) {
290    
291                    Set<String> keys = typeSettingsProperties.keySet();
292    
293                    Iterator<String> iterator = keys.iterator();
294    
295                    while (iterator.hasNext()) {
296                            String key = iterator.next();
297    
298                            if (ArrayUtil.contains(
299                                            _LEGACY_STAGED_PORTLET_TYPE_SETTINGS_KEYS, key)) {
300    
301                                    if (_log.isInfoEnabled()) {
302                                            _log.info("Removing type settings property " + key);
303                                    }
304    
305                                    iterator.remove();
306                            }
307                    }
308            }
309    
310            protected void verifyTree() throws Exception {
311                    long[] companyIds = PortalInstances.getCompanyIdsBySQL();
312    
313                    for (long companyId : companyIds) {
314                            GroupLocalServiceUtil.rebuildTree(companyId);
315                    }
316            }
317    
318            private static final String[] _LEGACY_STAGED_PORTLET_TYPE_SETTINGS_KEYS = {
319                    "staged-portlet_39", "staged-portlet_54", "staged-portlet_56",
320                    "staged-portlet_59", "staged-portlet_107", "staged-portlet_108",
321                    "staged-portlet_110", "staged-portlet_166", "staged-portlet_169"
322            };
323    
324            private static final Log _log = LogFactoryUtil.getLog(VerifyGroup.class);
325    
326    }