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.util.LoggingTimer;
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 (Map.Entry<String, String> entry : preferenceNamesMap.entrySet()) {
036                            updatePreferences(
037                                    "PortalPreferences", "portalPreferencesId", entry.getKey(),
038                                    entry.getValue());
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                    try (LoggingTimer loggingTimer = new LoggingTimer(tableName)) {
050                            StringBundler sb = new StringBundler(9);
051    
052                            sb.append("update ");
053                            sb.append(tableName);
054                            sb.append(" set preferences = replace(preferences, '");
055                            sb.append(oldValue);
056                            sb.append("', '");
057                            sb.append(newValue);
058                            sb.append("') where preferences like '%");
059                            sb.append(oldValue);
060                            sb.append("%'");
061    
062                            try {
063                                    runSQL(sb.toString());
064                            }
065                            catch (Exception e) {
066                                    sb = new StringBundler(7);
067    
068                                    sb.append("select ");
069                                    sb.append(primaryKeyColumnName);
070                                    sb.append(", preferences from ");
071                                    sb.append(tableName);
072                                    sb.append(" where preferences like '%");
073                                    sb.append(oldValue);
074                                    sb.append("%'");
075    
076                                    try (PreparedStatement ps = connection.prepareStatement(
077                                                    sb.toString());
078                                            ResultSet rs = ps.executeQuery();) {
079    
080                                            while (rs.next()) {
081                                                    long primaryKey = rs.getLong(primaryKeyColumnName);
082                                                    String preferences = rs.getString("preferences");
083    
084                                                    updatePreferences(
085                                                            tableName, primaryKeyColumnName, oldValue, newValue,
086                                                            primaryKey, preferences);
087                                            }
088                                    }
089                            }
090                    }
091            }
092    
093            protected void updatePreferences(
094                            String tableName, String primaryKeyColumnName, String oldValue,
095                            String newValue, long primaryKey, String preferences)
096                    throws Exception {
097    
098                    preferences = StringUtil.replace(preferences, oldValue, newValue);
099    
100                    StringBundler sb = new StringBundler(5);
101    
102                    sb.append("update ");
103                    sb.append(tableName);
104                    sb.append(" set preferences = ? where ");
105                    sb.append(primaryKeyColumnName);
106                    sb.append(" = ?");
107    
108                    try (PreparedStatement ps = connection.prepareStatement(
109                                    sb.toString())) {
110    
111                            ps.setString(1, preferences);
112                            ps.setLong(2, primaryKey);
113    
114                            ps.executeUpdate();
115                    }
116            }
117    
118    }