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.StringPool;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.util.PortletKeys;
022
023 import java.sql.Connection;
024 import java.sql.PreparedStatement;
025 import java.sql.ResultSet;
026
027
031 public abstract class BaseUpgradePortletPreferences extends UpgradeProcess {
032
033 protected void deletePortletPreferences(long portletPreferencesId)
034 throws Exception {
035
036 runSQL(
037 "delete from PortletPreferences where portletPreferencesId = " +
038 portletPreferencesId);
039 }
040
041 @Override
042 protected void doUpgrade() throws Exception {
043 updatePortletPreferences();
044 }
045
046 protected long getCompanyId(long userId) throws Exception {
047 long companyId = 0;
048
049 Connection con = null;
050 PreparedStatement ps = null;
051 ResultSet rs = null;
052
053 try {
054 con = DataAccess.getUpgradeOptimizedConnection();
055
056 ps = con.prepareStatement(_GET_USER);
057
058 ps.setLong(1, userId);
059
060 rs = ps.executeQuery();
061
062 while (rs.next()) {
063 companyId = rs.getLong("companyId");
064 }
065 }
066 finally {
067 DataAccess.cleanUp(con, ps, rs);
068 }
069
070 return companyId;
071 }
072
073 protected Object[] getGroup(long groupId) throws Exception {
074 Object[] group = null;
075
076 Connection con = null;
077 PreparedStatement ps = null;
078 ResultSet rs = null;
079
080 try {
081 con = DataAccess.getUpgradeOptimizedConnection();
082
083 ps = con.prepareStatement(_GET_COMPANY_ID);
084
085 ps.setLong(1, groupId);
086
087 rs = ps.executeQuery();
088
089 while (rs.next()) {
090 long companyId = rs.getLong("companyId");
091
092 group = new Object[] {groupId, companyId};
093 }
094 }
095 finally {
096 DataAccess.cleanUp(con, ps, rs);
097 }
098
099 return group;
100 }
101
102 protected Object[] getLayout(long plid) throws Exception {
103 Object[] layout = null;
104
105 Connection con = null;
106 PreparedStatement ps = null;
107 ResultSet rs = null;
108
109 try {
110 con = DataAccess.getUpgradeOptimizedConnection();
111
112 ps = con.prepareStatement(_GET_LAYOUT);
113
114 ps.setLong(1, plid);
115
116 rs = ps.executeQuery();
117
118 while (rs.next()) {
119 long groupId = rs.getLong("groupId");
120 long companyId = rs.getLong("companyId");
121 boolean privateLayout = rs.getBoolean("privateLayout");
122 long layoutId = rs.getLong("layoutId");
123
124 layout = new Object[] {
125 groupId, companyId, privateLayout, layoutId};
126 }
127 }
128 finally {
129 DataAccess.cleanUp(con, ps, rs);
130 }
131
132 return layout;
133 }
134
135 protected String getLayoutUuid(long plid, long layoutId) throws Exception {
136 Object[] layout = getLayout(plid);
137
138 if (layout == null) {
139 return null;
140 }
141
142 String uuid = null;
143
144 Connection con = null;
145 PreparedStatement ps = null;
146 ResultSet rs = null;
147
148 try {
149 con = DataAccess.getUpgradeOptimizedConnection();
150
151 ps = con.prepareStatement(_GET_LAYOUT_UUID);
152
153 long groupId = (Long)layout[0];
154 boolean privateLayout = (Boolean)layout[2];
155
156 ps.setLong(1, groupId);
157 ps.setBoolean(2, privateLayout);
158 ps.setLong(3, layoutId);
159
160 rs = ps.executeQuery();
161
162 if (rs.next()) {
163 uuid = rs.getString("uuid_");
164 }
165 }
166 finally {
167 DataAccess.cleanUp(con, ps, rs);
168 }
169
170 return uuid;
171 }
172
173 protected String[] getPortletIds() {
174 return new String[0];
175 }
176
177 protected String getUpdatePortletPreferencesWhereClause() {
178 String[] portletIds = getPortletIds();
179
180 if (portletIds.length == 0) {
181 throw new IllegalArgumentException(
182 "Subclasses must override getPortletIds or " +
183 "getUpdatePortletPreferencesWhereClause");
184 }
185
186 StringBundler sb = new StringBundler(portletIds.length * 5 - 1);
187
188 for (int i = 0; i < portletIds.length; i++) {
189 String portletId = portletIds[i];
190
191 sb.append("portletId ");
192
193 if (portletId.contains(StringPool.PERCENT)) {
194 sb.append(" like '");
195 sb.append(portletId);
196 sb.append("'");
197 }
198 else {
199 sb.append(" = '");
200 sb.append(portletId);
201 sb.append("'");
202 }
203
204 if ((i + 1) < portletIds.length) {
205 sb.append(" or ");
206 }
207 }
208
209 return sb.toString();
210 }
211
212 protected void updatePortletPreferences() throws Exception {
213 Connection con = null;
214 PreparedStatement ps = null;
215 ResultSet rs = null;
216
217 try {
218 con = DataAccess.getUpgradeOptimizedConnection();
219
220 StringBundler sb = new StringBundler(4);
221
222 sb.append("select portletPreferencesId, ownerId, ownerType, ");
223 sb.append("plid, portletId, preferences from PortletPreferences");
224
225 String whereClause = getUpdatePortletPreferencesWhereClause();
226
227 if (Validator.isNotNull(whereClause)) {
228 sb.append(" where ");
229 sb.append(whereClause);
230 }
231
232 String sql = sb.toString();
233
234 ps = con.prepareStatement(sql);
235
236 rs = ps.executeQuery();
237
238 while (rs.next()) {
239 long portletPreferencesId = rs.getLong("portletPreferencesId");
240 long ownerId = rs.getLong("ownerId");
241 int ownerType = rs.getInt("ownerType");
242 long plid = rs.getLong("plid");
243 String portletId = rs.getString("portletId");
244 String preferences = rs.getString("preferences");
245
246 long companyId = 0;
247
248 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
249 companyId = ownerId;
250 }
251 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
252 Object[] group = getGroup(ownerId);
253
254 if (group != null) {
255 companyId = (Long)group[1];
256 }
257 }
258 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
259 Object[] layout = getLayout(plid);
260
261 if (layout != null) {
262 companyId = (Long)layout[1];
263 }
264 }
265 else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
266 companyId = getCompanyId(ownerId);
267 }
268
269 if (companyId > 0) {
270 String newPreferences = upgradePreferences(
271 companyId, ownerId, ownerType, plid, portletId,
272 preferences);
273
274 if (!preferences.equals(newPreferences)) {
275 updatePortletPreferences(
276 portletPreferencesId, newPreferences);
277 }
278 }
279 else {
280 deletePortletPreferences(portletPreferencesId);
281 }
282 }
283 }
284 finally {
285 DataAccess.cleanUp(con, ps, rs);
286 }
287 }
288
289 protected void updatePortletPreferences(
290 long portletPreferencesId, String preferences)
291 throws Exception {
292
293 Connection con = null;
294 PreparedStatement ps = null;
295
296 try {
297 con = DataAccess.getUpgradeOptimizedConnection();
298
299 ps = con.prepareStatement(
300 "update PortletPreferences set preferences = ? where " +
301 "portletPreferencesId = " + portletPreferencesId);
302
303 ps.setString(1, preferences);
304
305 ps.executeUpdate();
306 }
307 finally {
308 DataAccess.cleanUp(con, ps);
309 }
310 }
311
312 protected abstract String upgradePreferences(
313 long companyId, long ownerId, int ownerType, long plid,
314 String portletId, String xml)
315 throws Exception;
316
317 private static final String _GET_COMPANY_ID =
318 "select companyId from Group_ where groupId = ?";
319
320 private static final String _GET_LAYOUT =
321 "select groupId, companyId, privateLayout, layoutId from Layout " +
322 "where plid = ?";
323
324 private static final String _GET_LAYOUT_UUID =
325 "select uuid_ from Layout where groupId = ? and privateLayout = ? " +
326 "and layoutId = ?";
327
328 private static final String _GET_USER =
329 "select * from User_ where userId = ?";
330
331 }