001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.upgrade.AutoBatchPreparedStatementUtil;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.sql.Connection;
028    import java.sql.PreparedStatement;
029    import java.sql.ResultSet;
030    
031    /**
032     * @author Julio Camarero
033     */
034    public class UpgradePortletPreferences extends UpgradeProcess {
035    
036            protected void deletePortletPreferences(long portletPreferencesId)
037                    throws Exception {
038    
039                    if (_log.isDebugEnabled()) {
040                            _log.debug("Deleting portlet preferences " + portletPreferencesId);
041                    }
042    
043                    runSQL(
044                            "delete from PortletPreferences where portletPreferencesId = " +
045                                    portletPreferencesId);
046            }
047    
048            @Override
049            protected void doUpgrade() throws Exception {
050                    Connection con = null;
051                    PreparedStatement ps1 = null;
052                    PreparedStatement ps2 = null;
053                    ResultSet rs = null;
054    
055                    try {
056                            con = DataAccess.getUpgradeOptimizedConnection();
057    
058                            StringBundler sb = new StringBundler(7);
059    
060                            sb.append("select PortletPreferences.portletPreferencesId, ");
061                            sb.append("PortletPreferences.plid,");
062                            sb.append("PortletPreferences.portletId, Layout.typeSettings ");
063                            sb.append("from PortletPreferences inner join Layout on ");
064                            sb.append("PortletPreferences.plid = Layout.plid where ");
065                            sb.append("preferences like '%<portlet-preferences />%' or ");
066                            sb.append("preferences like '' or ? IS NULL");
067    
068                            String sql = sb.toString();
069    
070                            sql = CustomSQLUtil.replaceIsNull(sql);
071    
072                            sql = StringUtil.replace(sql, "?", "preferences");
073    
074                            ps1 = con.prepareStatement(sql);
075    
076                            rs = ps1.executeQuery();
077    
078                            ps2 = AutoBatchPreparedStatementUtil.autoBatch(
079                                    con.prepareStatement(
080                                            "delete from PortletPreferences where " +
081                                                    "portletPreferencesId = ?"));
082    
083                            while (rs.next()) {
084                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
085                                    String portletId = GetterUtil.getString(
086                                            rs.getString("portletId"));
087                                    String typeSettings = GetterUtil.getString(
088                                            rs.getString("typeSettings"));
089    
090                                    if (typeSettings.contains(portletId)) {
091                                            continue;
092                                    }
093    
094                                    if (_log.isDebugEnabled()) {
095                                            _log.debug(
096                                                    "Deleting portlet preferences " + portletPreferencesId);
097                                    }
098    
099                                    ps2.setLong(1, portletPreferencesId);
100    
101                                    ps2.addBatch();
102                            }
103    
104                            ps2.executeBatch();
105                    }
106                    finally {
107                            DataAccess.cleanUp(ps1);
108                            DataAccess.cleanUp(con, ps2, rs);
109                    }
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    UpgradePortletPreferences.class);
114    
115    }