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.util.PropsValues;
025    import com.liferay.util.dao.orm.CustomSQLUtil;
026    
027    import java.sql.Connection;
028    import java.sql.DatabaseMetaData;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    
032    /**
033     * @author Julio Camarero
034     */
035    public class UpgradePortletPreferences extends UpgradeProcess {
036    
037            protected void deletePortletPreferences(long portletPreferencesId)
038                    throws Exception {
039    
040                    if (_log.isDebugEnabled()) {
041                            _log.debug("Deleting portlet preferences " + portletPreferencesId);
042                    }
043    
044                    runSQL(
045                            "delete from PortletPreferences where portletPreferencesId = " +
046                                    portletPreferencesId);
047            }
048    
049            @Override
050            protected void doUpgrade() throws Exception {
051                    Connection con = null;
052                    PreparedStatement ps = null;
053                    ResultSet rs = null;
054    
055                    try {
056                            con = DataAccess.getUpgradeOptimizedConnection();
057    
058                            DatabaseMetaData databaseMetaData = con.getMetaData();
059    
060                            boolean supportsBatchUpdates =
061                                    databaseMetaData.supportsBatchUpdates();
062    
063                            StringBundler sb = new StringBundler(7);
064    
065                            sb.append("select PortletPreferences.portletPreferencesId, ");
066                            sb.append("PortletPreferences.plid,");
067                            sb.append("PortletPreferences.portletId, Layout.typeSettings ");
068                            sb.append("from PortletPreferences inner join Layout on ");
069                            sb.append("PortletPreferences.plid = Layout.plid where ");
070                            sb.append("preferences like '%<portlet-preferences />%' or ");
071                            sb.append("preferences like '' or ? IS NULL");
072    
073                            String sql = sb.toString();
074    
075                            sql = CustomSQLUtil.replaceIsNull(sql);
076    
077                            sql = StringUtil.replace(sql, "?", "preferences");
078    
079                            ps = con.prepareStatement(sql);
080    
081                            rs = ps.executeQuery();
082    
083                            ps = con.prepareStatement(
084                                    "delete from PortletPreferences where portletPreferencesId = " +
085                                            "?");
086    
087                            int count = 0;
088    
089                            while (rs.next()) {
090                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
091                                    String portletId = GetterUtil.getString(
092                                            rs.getString("portletId"));
093                                    String typeSettings = GetterUtil.getString(
094                                            rs.getString("typeSettings"));
095    
096                                    if (typeSettings.contains(portletId)) {
097                                            continue;
098                                    }
099    
100                                    if (_log.isDebugEnabled()) {
101                                            _log.debug(
102                                                    "Deleting portlet preferences " + portletPreferencesId);
103                                    }
104    
105                                    ps.setLong(1, portletPreferencesId);
106    
107                                    if (supportsBatchUpdates) {
108                                            ps.addBatch();
109    
110                                            if (count == PropsValues.HIBERNATE_JDBC_BATCH_SIZE) {
111                                                    ps.executeBatch();
112    
113                                                    count = 0;
114                                            }
115                                            else {
116                                                    count++;
117                                            }
118                                    }
119                                    else {
120                                            ps.executeUpdate();
121                                    }
122                            }
123    
124                            if (supportsBatchUpdates && (count > 0)) {
125                                    ps.executeBatch();
126                            }
127                    }
128                    finally {
129                            DataAccess.cleanUp(con, ps, rs);
130                    }
131            }
132    
133            private static Log _log = LogFactoryUtil.getLog(
134                    UpgradePortletPreferences.class);
135    
136    }