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