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.dao.jdbc.AutoBatchPreparedStatementUtil;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LoggingTimer;
021    
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Sampsa Sohlman
027     */
028    public class UpgradeResourcePermission extends UpgradeProcess {
029    
030            protected void createIndex() throws Exception {
031                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
032                            runSQLTemplateString(
033                                    "create index IX_D5F1E2A2 on ResourcePermission " +
034                                            "(name[$COLUMN_LENGTH:255$])",
035                                    false, false);
036                    }
037            }
038    
039            @Override
040            protected void doUpgrade() throws Exception {
041                    createIndex();
042    
043                    upgradeResourcePermissions();
044            }
045    
046            protected void upgradeResourcePermissions() throws Exception {
047                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
048                            String selectSQL =
049                                    "select resourcePermissionId, primKey, actionIds from " +
050                                            "ResourcePermission";
051                            String updateSQL =
052                                    "update ResourcePermission set primKeyId = ?, viewActionId = " +
053                                            "? where resourcePermissionId = ?";
054    
055                            try (PreparedStatement ps1 = connection.prepareStatement(selectSQL);
056                                    ResultSet rs = ps1.executeQuery();
057                                    PreparedStatement ps2 =
058                                            AutoBatchPreparedStatementUtil.concurrentAutoBatch(
059                                                    connection, updateSQL)) {
060    
061                                    while (rs.next()) {
062                                            long resourcePermissionId = rs.getLong(
063                                                    "resourcePermissionId");
064                                            long actionIds = rs.getLong("actionIds");
065    
066                                            long newPrimKeyId = GetterUtil.getLong(
067                                                    rs.getString("primKey"));
068                                            boolean newViewActionId = (actionIds % 2 == 1);
069    
070                                            if ((newPrimKeyId == 0) && !newViewActionId) {
071                                                    continue;
072                                            }
073    
074                                            ps2.setLong(1, newPrimKeyId);
075                                            ps2.setBoolean(2, newViewActionId);
076                                            ps2.setLong(3, resourcePermissionId);
077    
078                                            ps2.addBatch();
079                                    }
080    
081                                    ps2.executeBatch();
082                            }
083                    }
084            }
085    
086    }