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