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