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