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