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