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.upgrade.v6_1_0;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.LoggingTimer;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Miguel Pastor
028     */
029    public class UpgradeCommunityProperties extends UpgradeProcess {
030    
031            @Override
032            protected void doUpgrade() throws Exception {
033                    upgradePortletPreferences();
034    
035                    upgradePortalPreferences();
036            }
037    
038            protected void updatePreferences(
039                            String tableName, String primaryKeyColumnName, String oldValue,
040                            String newValue)
041                    throws Exception {
042    
043                    StringBundler sb = new StringBundler(9);
044    
045                    sb.append("update ");
046                    sb.append(tableName);
047                    sb.append(" set preferences = replace(preferences, '");
048                    sb.append(oldValue);
049                    sb.append("', '");
050                    sb.append(newValue);
051                    sb.append("') where preferences like '%");
052                    sb.append(oldValue);
053                    sb.append("%'");
054    
055                    try {
056                            runSQL(sb.toString());
057                    }
058                    catch (Exception e) {
059                            sb = new StringBundler(7);
060    
061                            sb.append("select ");
062                            sb.append(primaryKeyColumnName);
063                            sb.append(", preferences from ");
064                            sb.append(tableName);
065                            sb.append(" where preferences like '%");
066                            sb.append(oldValue);
067                            sb.append("%'");
068    
069                            try (PreparedStatement ps = connection.prepareStatement(
070                                            sb.toString());
071                                    ResultSet rs = ps.executeQuery()) {
072    
073                                    while (rs.next()) {
074                                            long primaryKey = rs.getLong(primaryKeyColumnName);
075                                            String preferences = rs.getString("preferences");
076    
077                                            updatePreferences(
078                                                    tableName, primaryKeyColumnName, oldValue, newValue,
079                                                    primaryKey, preferences);
080                                    }
081                            }
082                    }
083            }
084    
085            protected void updatePreferences(
086                            String tableName, String primaryKeyColumnName, String oldValue,
087                            String newValue, long primaryKey, String preferences)
088                    throws Exception {
089    
090                    preferences = StringUtil.replace(preferences, oldValue, newValue);
091    
092                    StringBundler sb = new StringBundler(5);
093    
094                    sb.append("update ");
095                    sb.append(tableName);
096                    sb.append(" set preferences = ? where ");
097                    sb.append(primaryKeyColumnName);
098                    sb.append(" = ?");
099    
100                    try (PreparedStatement ps = connection.prepareStatement(
101                                    sb.toString())) {
102    
103                            ps.setString(1, preferences);
104                            ps.setLong(2, primaryKey);
105    
106                            ps.executeUpdate();
107                    }
108            }
109    
110            protected void upgradePortalPreferences() throws Exception {
111                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
112                            for (int i = 0; i < _OLD_PORTAL_PREFERENCES.length; i++) {
113                                    updatePreferences(
114                                            "PortalPreferences", "portalPreferencesId",
115                                            _OLD_PORTAL_PREFERENCES[i], _NEW_PORTAL_PREFERENCES[i]);
116                            }
117                    }
118            }
119    
120            protected void upgradePortletPreferences() throws Exception {
121                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
122                            for (int i = 0; i < _OLD_PORTLET_PREFERENCES.length; i++) {
123                                    updatePreferences(
124                                            "PortletPreferences", "portletPreferencesId",
125                                            _OLD_PORTLET_PREFERENCES[i], _NEW_PORTLET_PREFERENCES[i]);
126                            }
127                    }
128            }
129    
130            private static final String[] _NEW_PORTAL_PREFERENCES = {
131                    PropsKeys.COMPANY_SECURITY_SITE_LOGO,
132                    PropsKeys.SITES_EMAIL_FROM_ADDRESS, PropsKeys.SITES_EMAIL_FROM_NAME,
133                    PropsKeys.SITES_EMAIL_MEMBERSHIP_REPLY_BODY,
134                    PropsKeys.SITES_EMAIL_MEMBERSHIP_REPLY_SUBJECT,
135                    PropsKeys.SITES_EMAIL_MEMBERSHIP_REQUEST_BODY,
136                    PropsKeys.SITES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT
137            };
138    
139            private static final String[] _NEW_PORTLET_PREFERENCES = {
140                    "site-role", "[$SITE_NAME$]"
141            };
142    
143            private static final String[] _OLD_PORTAL_PREFERENCES = {
144                    "company.security.community.logo", "communities.email.from.address",
145                    "communities.email.from.name",
146                    "communities.email.membership.reply.body",
147                    "communities.email.membership.reply.subject",
148                    "communities.email.membership.request.body",
149                    "communities.email.membership.request.subject"
150            };
151    
152            private static final String[] _OLD_PORTLET_PREFERENCES = {
153                    "community-role", "[$COMMUNITY_NAME$]"
154            };
155    
156    }