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