001
014
015 package com.liferay.portal.upgrade.v7_0_0;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.upgrade.v7_0_0.util.PortletPreferencesRow;
024 import com.liferay.portal.util.PortletKeys;
025 import com.liferay.portlet.PortletPreferencesFactoryUtil;
026 import com.liferay.portlet.blogs.BlogsPortletInstanceSettings;
027 import com.liferay.portlet.blogs.BlogsSettings;
028 import com.liferay.portlet.blogs.util.BlogsConstants;
029 import com.liferay.portlet.bookmarks.BookmarksSettings;
030 import com.liferay.portlet.bookmarks.util.BookmarksConstants;
031 import com.liferay.portlet.documentlibrary.DLPortletInstanceSettings;
032 import com.liferay.portlet.documentlibrary.DLSettings;
033 import com.liferay.portlet.documentlibrary.util.DLConstants;
034 import com.liferay.portlet.messageboards.MBSettings;
035 import com.liferay.portlet.messageboards.util.MBConstants;
036 import com.liferay.portlet.shopping.ShoppingSettings;
037 import com.liferay.portlet.shopping.util.ShoppingConstants;
038 import com.liferay.portlet.wiki.WikiPortletInstanceSettings;
039 import com.liferay.portlet.wiki.WikiSettings;
040 import com.liferay.portlet.wiki.util.WikiConstants;
041
042 import java.sql.Connection;
043 import java.sql.PreparedStatement;
044 import java.sql.ResultSet;
045
046 import java.util.Enumeration;
047
048
052 public class UpgradePortletSettings extends UpgradeProcess {
053
054 protected void addPortletPreferences(
055 PortletPreferencesRow portletPreferencesRow)
056 throws Exception {
057
058 Connection con = null;
059 PreparedStatement ps = null;
060
061 try {
062 con = DataAccess.getUpgradeOptimizedConnection();
063
064 ps = con.prepareStatement(
065 "insert into PortletPreferences (mvccVersion, " +
066 "portletPreferencesId, ownerId, ownerType, plid, " +
067 "portletId, preferences) values (?, ?, ?, ?, ?, ?, ?)");
068
069 ps.setLong(1, portletPreferencesRow.getMvccVersion());
070 ps.setLong(2, portletPreferencesRow.getPortletPreferencesId());
071 ps.setLong(3, portletPreferencesRow.getOwnerId());
072 ps.setInt(4, portletPreferencesRow.getOwnerType());
073 ps.setLong(5, portletPreferencesRow.getPlid());
074 ps.setString(6, portletPreferencesRow.getPortletId());
075 ps.setString(7, portletPreferencesRow.getPreferences());
076
077 ps.executeUpdate();
078 }
079 finally {
080 DataAccess.cleanUp(con, ps);
081 }
082 }
083
084 protected void copyPortletSettingsAsServiceSettings(
085 String portletId, int ownerType, String serviceName)
086 throws Exception {
087
088 if (_log.isDebugEnabled()) {
089 _log.debug("Copy portlet settings as service settings");
090 }
091
092 ResultSet rs = null;
093
094 try {
095 rs = getPortletPreferencesResultSet(portletId, ownerType);
096
097 while (rs.next()) {
098 PortletPreferencesRow portletPreferencesRow =
099 getPortletPreferencesRow(rs);
100
101 portletPreferencesRow.setPortletPreferencesId(increment());
102 portletPreferencesRow.setOwnerType(
103 PortletKeys.PREFS_OWNER_TYPE_GROUP);
104 portletPreferencesRow.setPortletId(serviceName);
105
106 if (ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT) {
107 long plid = portletPreferencesRow.getPlid();
108
109 long groupId = getGroupId(plid);
110
111 portletPreferencesRow.setOwnerId(groupId);
112 portletPreferencesRow.setPlid(0);
113
114 if (_log.isInfoEnabled()) {
115 StringBundler sb = new StringBundler(8);
116
117 sb.append("Copying portlet ");
118 sb.append(portletId);
119 sb.append(" settings from layout ");
120 sb.append(plid);
121 sb.append(" to service ");
122 sb.append(serviceName);
123 sb.append(" in group ");
124 sb.append(groupId);
125
126 _log.info(sb.toString());
127 }
128 }
129
130 addPortletPreferences(portletPreferencesRow);
131 }
132 }
133 finally {
134 DataAccess.deepCleanUp(rs);
135 }
136 }
137
138 @Override
139 protected void doUpgrade() throws Exception {
140
141
142
143 upgradeMainPortlet(
144 PortletKeys.BLOGS, BlogsConstants.SERVICE_NAME,
145 PortletKeys.PREFS_OWNER_TYPE_GROUP,
146 BlogsPortletInstanceSettings.ALL_KEYS, BlogsSettings.ALL_KEYS);
147 upgradeMainPortlet(
148 PortletKeys.BOOKMARKS, BookmarksConstants.SERVICE_NAME,
149 PortletKeys.PREFS_OWNER_TYPE_LAYOUT, StringPool.EMPTY_ARRAY,
150 BookmarksSettings.ALL_KEYS);
151 upgradeMainPortlet(
152 PortletKeys.DOCUMENT_LIBRARY, DLConstants.SERVICE_NAME,
153 PortletKeys.PREFS_OWNER_TYPE_GROUP,
154 DLPortletInstanceSettings.ALL_KEYS, DLSettings.ALL_KEYS);
155 upgradeMainPortlet(
156 PortletKeys.MESSAGE_BOARDS, MBConstants.SERVICE_NAME,
157 PortletKeys.PREFS_OWNER_TYPE_GROUP, StringPool.EMPTY_ARRAY,
158 MBSettings.ALL_KEYS);
159 upgradeMainPortlet(
160 PortletKeys.SHOPPING, ShoppingConstants.SERVICE_NAME,
161 PortletKeys.PREFS_OWNER_TYPE_GROUP, StringPool.EMPTY_ARRAY,
162 ShoppingSettings.ALL_KEYS);
163 upgradeMainPortlet(
164 PortletKeys.WIKI, WikiConstants.SERVICE_NAME,
165 PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
166 WikiPortletInstanceSettings.ALL_KEYS, WikiSettings.ALL_KEYS);
167
168
169
170 upgradeDisplayPortlet(
171 PortletKeys.DOCUMENT_LIBRARY_DISPLAY,
172 PortletKeys.PREFS_OWNER_TYPE_LAYOUT, DLSettings.ALL_KEYS);
173 upgradeDisplayPortlet(
174 PortletKeys.MEDIA_GALLERY_DISPLAY,
175 PortletKeys.PREFS_OWNER_TYPE_LAYOUT, DLSettings.ALL_KEYS);
176 upgradeDisplayPortlet(
177 PortletKeys.WIKI_DISPLAY, PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
178 WikiSettings.ALL_KEYS);
179 }
180
181 protected long getGroupId(long plid) throws Exception {
182 Connection con = null;
183 PreparedStatement ps = null;
184 ResultSet rs = null;
185
186 long groupId = 0;
187
188 try {
189 con = DataAccess.getUpgradeOptimizedConnection();
190
191 ps = con.prepareStatement(
192 "select groupId from Layout where plid = ?");
193
194 ps.setLong(1, plid);
195
196 rs = ps.executeQuery();
197
198 if (rs.next()) {
199 groupId = rs.getLong("groupId");
200 }
201 }
202 finally {
203 DataAccess.cleanUp(con, ps, rs);
204 }
205
206 return groupId;
207 }
208
209 protected ResultSet getPortletPreferencesResultSet(
210 String portletId, int ownerType)
211 throws Exception {
212
213 Connection con = DataAccess.getUpgradeOptimizedConnection();
214
215 PreparedStatement ps = con.prepareStatement(
216 "select portletPreferencesId, ownerId, ownerType, plid, " +
217 "portletId, preferences from PortletPreferences where " +
218 "ownerType = ? and portletId = ?");
219
220 ps.setInt(1, ownerType);
221 ps.setString(2, portletId);
222
223 return ps.executeQuery();
224 }
225
226 protected void resetPortletPreferencesValues(
227 String portletId, int ownerType, String[] keys)
228 throws Exception {
229
230 ResultSet rs = null;
231
232 try {
233 rs = getPortletPreferencesResultSet(portletId, ownerType);
234
235 while (rs.next()) {
236 PortletPreferencesRow portletPreferencesRow =
237 getPortletPreferencesRow(rs);
238
239 javax.portlet.PortletPreferences jxPortletPreferences =
240 PortletPreferencesFactoryUtil.fromDefaultXML(
241 portletPreferencesRow.getPreferences());
242
243 Enumeration<String> names = jxPortletPreferences.getNames();
244
245 while (names.hasMoreElements()) {
246 String name = names.nextElement();
247
248 for (String key : keys) {
249 if (name.startsWith(key)) {
250 jxPortletPreferences.reset(key);
251
252 break;
253 }
254 }
255 }
256
257 portletPreferencesRow.setPreferences(
258 PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));
259
260 updatePortletPreferences(portletPreferencesRow);
261 }
262 }
263 finally {
264 DataAccess.deepCleanUp(rs);
265 }
266 }
267
268 protected void updatePortletPreferences(
269 PortletPreferencesRow portletPreferencesRow)
270 throws Exception {
271
272 Connection con = null;
273 PreparedStatement ps = null;
274
275 try {
276 con = DataAccess.getUpgradeOptimizedConnection();
277
278 ps = con.prepareStatement(
279 "update PortletPreferences set mvccVersion = ?, ownerId = ?, " +
280 "ownerType = ?, plid = ?, portletId = ?, preferences = ? " +
281 "where portletPreferencesId = ?");
282
283 ps.setLong(1, portletPreferencesRow.getMvccVersion());
284 ps.setLong(2, portletPreferencesRow.getOwnerId());
285 ps.setInt(3, portletPreferencesRow.getOwnerType());
286 ps.setLong(4, portletPreferencesRow.getPlid());
287 ps.setString(5, portletPreferencesRow.getPortletId());
288 ps.setString(6, portletPreferencesRow.getPreferences());
289 ps.setLong(7, portletPreferencesRow.getPortletPreferencesId());
290
291 ps.executeUpdate();
292 }
293 finally {
294 DataAccess.cleanUp(con, ps);
295 }
296 }
297
298 protected void upgradeDisplayPortlet(
299 String portletId, int ownerType, String[] serviceKeys)
300 throws Exception {
301
302 if (_log.isDebugEnabled()) {
303 _log.debug("Upgrading display portlet " + portletId + " settings");
304 }
305
306 if (_log.isDebugEnabled()) {
307 _log.debug("Delete service keys from portlet settings");
308 }
309
310 resetPortletPreferencesValues(portletId, ownerType, serviceKeys);
311
312 resetPortletPreferencesValues(
313 portletId, PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, serviceKeys);
314 }
315
316 protected void upgradeMainPortlet(
317 String portletId, String serviceName, int ownerType,
318 String[] portletInstanceKeys, String[] serviceKeys)
319 throws Exception {
320
321 if (_log.isDebugEnabled()) {
322 _log.debug("Upgrading main portlet " + portletId + " settings");
323 }
324
325 copyPortletSettingsAsServiceSettings(portletId, ownerType, serviceName);
326
327 if (_log.isDebugEnabled()) {
328 _log.debug("Delete portlet instance keys from service settings");
329 }
330
331 resetPortletPreferencesValues(
332 serviceName, PortletKeys.PREFS_OWNER_TYPE_GROUP,
333 portletInstanceKeys);
334
335 if (_log.isDebugEnabled()) {
336 _log.debug("Delete service keys from portlet settings");
337 }
338
339 resetPortletPreferencesValues(portletId, ownerType, serviceKeys);
340
341 resetPortletPreferencesValues(
342 portletId, PortletKeys.PREFS_OWNER_TYPE_ARCHIVED, serviceKeys);
343 }
344
345 private PortletPreferencesRow getPortletPreferencesRow(ResultSet rs)
346 throws Exception {
347
348 return new PortletPreferencesRow(
349 rs.getLong("portletPreferencesId"), rs.getLong("ownerId"),
350 rs.getInt("ownerType"), rs.getLong("plid"),
351 rs.getString("portletId"), rs.getString("preferences"));
352 }
353
354 private static final Log _log = LogFactoryUtil.getLog(
355 UpgradePortletSettings.class);
356
357 }