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