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