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