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.v6_2_0;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LoggingTimer;
022    import com.liferay.portal.kernel.util.StringBundler;
023    
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Julio Camarero
029     */
030    public class UpgradePortletPreferences extends UpgradeProcess {
031    
032            protected void deletePortletPreferences() throws Exception {
033                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
034                            StringBundler sb = new StringBundler(7);
035    
036                            sb.append("select PortletPreferences.portletPreferencesId, ");
037                            sb.append("PortletPreferences.plid,");
038                            sb.append("PortletPreferences.portletId, Layout.typeSettings ");
039                            sb.append("from PortletPreferences inner join Layout on ");
040                            sb.append("PortletPreferences.plid = Layout.plid where ");
041                            sb.append("preferences like '%<portlet-preferences />%' or ");
042                            sb.append("preferences like '' or preferences is null");
043    
044                            String sql = sb.toString();
045    
046                            try (PreparedStatement ps = connection.prepareStatement(sql);
047                                    ResultSet rs = ps.executeQuery()) {
048    
049                                    while (rs.next()) {
050                                            long portletPreferencesId = rs.getLong(
051                                                    "portletPreferencesId");
052                                            String portletId = GetterUtil.getString(
053                                                    rs.getString("portletId"));
054                                            String typeSettings = GetterUtil.getString(
055                                                    rs.getString("typeSettings"));
056    
057                                            if (typeSettings.contains(portletId)) {
058                                                    continue;
059                                            }
060    
061                                            deletePortletPreferences(portletPreferencesId);
062                                    }
063                            }
064                    }
065            }
066    
067            protected void deletePortletPreferences(long portletPreferencesId)
068                    throws Exception {
069    
070                    if (_log.isDebugEnabled()) {
071                            _log.debug("Deleting portlet preferences " + portletPreferencesId);
072                    }
073    
074                    runSQL(
075                            "delete from PortletPreferences where portletPreferencesId = " +
076                                    portletPreferencesId);
077            }
078    
079            @Override
080            protected void doUpgrade() throws Exception {
081                    deletePortletPreferences();
082            }
083    
084            private static final Log _log = LogFactoryUtil.getLog(
085                    UpgradePortletPreferences.class);
086    
087    }