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.v7_0_0;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.upgrade.AutoBatchPreparedStatementUtil;
021    
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    import java.sql.SQLException;
025    
026    /**
027     * @author Preston Crary
028     */
029    public class UpgradeKernelPackage extends UpgradeProcess {
030    
031            @Override
032            protected void doUpgrade() throws SQLException {
033                    upgradeTable("Counter", "name");
034                    upgradeTable("ClassName_", "value");
035                    upgradeTable("ResourceBlock", "name");
036                    upgradeTable("ResourcePermission", "name");
037            }
038    
039            protected String[][] getClassNames() {
040                    return _CLASS_NAMES;
041            }
042    
043            protected void upgradeTable(String tableName, String columnName)
044                    throws SQLException {
045    
046                    StringBundler updateSB = new StringBundler(7);
047    
048                    updateSB.append("update ");
049                    updateSB.append(tableName);
050                    updateSB.append(" set ");
051                    updateSB.append(columnName);
052                    updateSB.append(" = ? where ");
053                    updateSB.append(columnName);
054                    updateSB.append(" = ?");
055    
056                    String updateSQL = updateSB.toString();
057    
058                    for (String[] className : getClassNames()) {
059                            StringBundler selectSB = new StringBundler(9);
060    
061                            selectSB.append("select ");
062                            selectSB.append(columnName);
063                            selectSB.append(" from ");
064                            selectSB.append(tableName);
065                            selectSB.append(" where ");
066                            selectSB.append(columnName);
067                            selectSB.append(" like '%");
068                            selectSB.append(className[0]);
069                            selectSB.append("%'");
070    
071                            upgradeTable(columnName, selectSB.toString(), updateSQL, className);
072                    }
073            }
074    
075            protected void upgradeTable(
076                            String columnName, String selectSQL, String updateSQL,
077                            String[] className)
078                    throws SQLException {
079    
080                    try (PreparedStatement ps1 = connection.prepareStatement(selectSQL);
081                                    ResultSet rs = ps1.executeQuery();
082                                            PreparedStatement ps2 =
083                                                    AutoBatchPreparedStatementUtil.autoBatch(
084                                                            connection.prepareStatement(updateSQL))) {
085    
086                            while (rs.next()) {
087                                    String oldValue = rs.getString(columnName);
088    
089                                    String newValue = StringUtil.replace(
090                                            oldValue, className[0], className[1]);
091    
092                                    ps2.setString(1, newValue);
093                                    ps2.setString(2, oldValue);
094    
095                                    ps2.addBatch();
096                            }
097    
098                            ps2.executeBatch();
099                    }
100            }
101    
102            private static final String[][] _CLASS_NAMES = new String[][] {
103                    {
104                            "com.liferay.portlet.announcements.model.AnnouncementsDelivery",
105                            "com.liferay.announcements.kernel.model.AnnouncementsDelivery"
106                    },
107                    {
108                            "com.liferay.portlet.announcements.model.AnnouncementsEntry",
109                            "com.liferay.announcements.kernel.model.AnnouncementsEntry"
110                    },
111                    {
112                            "com.liferay.portlet.announcements.model.AnnouncementsFlag",
113                            "com.liferay.announcements.kernel.model.AnnouncementsFlag"
114                    }
115            };
116    
117    }