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