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.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    import java.util.Map;
025    
026    /**
027     * @author Eduardo Garcia
028     */
029    public abstract class RenameUpgradePortalPreferences extends UpgradeProcess {
030    
031            @Override
032            protected void doUpgrade() throws Exception {
033                    Map<String, String> preferenceNamesMap = getPreferenceNamesMap();
034    
035                    for (String oldName : preferenceNamesMap.keySet()) {
036                            updatePreferences(
037                                    "PortalPreferences", "portalPreferencesId", oldName,
038                                    preferenceNamesMap.get(oldName));
039                    }
040            }
041    
042            protected abstract Map<String, String> getPreferenceNamesMap();
043    
044            protected void updatePreferences(
045                            String tableName, String primaryKeyColumnName, String oldValue,
046                            String newValue)
047                    throws Exception {
048    
049                    PreparedStatement ps = null;
050                    ResultSet rs = null;
051    
052                    StringBundler sb = new StringBundler(9);
053    
054                    sb.append("update ");
055                    sb.append(tableName);
056                    sb.append(" set preferences = replace(preferences, '");
057                    sb.append(oldValue);
058                    sb.append("', '");
059                    sb.append(newValue);
060                    sb.append("') where preferences like '%");
061                    sb.append(oldValue);
062                    sb.append("%'");
063    
064                    try {
065                            runSQL(sb.toString());
066                    }
067                    catch (Exception e) {
068                            sb = new StringBundler(7);
069    
070                            sb.append("select ");
071                            sb.append(primaryKeyColumnName);
072                            sb.append(", preferences from ");
073                            sb.append(tableName);
074                            sb.append(" where preferences like '%");
075                            sb.append(oldValue);
076                            sb.append("%'");
077    
078                            ps = connection.prepareStatement(sb.toString());
079    
080                            rs = ps.executeQuery();
081    
082                            while (rs.next()) {
083                                    long primaryKey = rs.getLong(primaryKeyColumnName);
084                                    String preferences = rs.getString("preferences");
085    
086                                    updatePreferences(
087                                            tableName, primaryKeyColumnName, oldValue, newValue,
088                                            primaryKey, preferences);
089                            }
090                    }
091                    finally {
092                            DataAccess.cleanUp(ps, rs);
093                    }
094            }
095    
096            protected void updatePreferences(
097                            String tableName, String primaryKeyColumnName, String oldValue,
098                            String newValue, long primaryKey, String preferences)
099                    throws Exception {
100    
101                    preferences = StringUtil.replace(preferences, oldValue, newValue);
102    
103                    PreparedStatement ps = null;
104                    ResultSet rs = null;
105    
106                    StringBundler sb = new StringBundler(5);
107    
108                    sb.append("update ");
109                    sb.append(tableName);
110                    sb.append(" set preferences = ? where ");
111                    sb.append(primaryKeyColumnName);
112                    sb.append(" = ?");
113    
114                    try {
115                            ps = connection.prepareStatement(sb.toString());
116    
117                            ps.setString(1, preferences);
118                            ps.setLong(2, primaryKey);
119    
120                            ps.executeUpdate();
121                    }
122                    finally {
123                            DataAccess.cleanUp(ps, rs);
124                    }
125            }
126    
127    }