001    /**
002     * Copyright (c) 2000-2013 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(7);
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 />%' or ");
062                            sb.append("preferences like '' or preferences is null");
063    
064                            String sql = sb.toString();
065    
066                            ps = con.prepareStatement(sql);
067    
068                            rs = ps.executeQuery();
069    
070                            while (rs.next()) {
071                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
072                                    String portletId = GetterUtil.getString(
073                                            rs.getString("portletId"));
074                                    String typeSettings = GetterUtil.getString(
075                                            rs.getString("typeSettings"));
076    
077                                    if (typeSettings.contains(portletId)) {
078                                            continue;
079                                    }
080    
081                                    deletePortletPreferences(portletPreferencesId);
082                            }
083                    }
084                    finally {
085                            DataAccess.cleanUp(con, ps, rs);
086                    }
087            }
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    UpgradePortletPreferences.class);
091    
092    }