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.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.upgrade.AutoBatchPreparedStatementUtil;
021    
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.ResultSet;
025    
026    /**
027     * @author Sampsa Sohlman
028     */
029    public class UpgradeResourcePermission extends UpgradeProcess {
030    
031            @Override
032            protected void doUpgrade() throws Exception {
033                    String selectSQL =
034                            "select resourcePermissionId, primKey, primKeyId, actionIds, " +
035                                    "viewActionId from ResourcePermission";
036                    String updateSQL =
037                            "update ResourcePermission set primKeyId = ?, viewActionId = ? " +
038                                    "where resourcePermissionId = ?";
039    
040                    try (Connection con = DataAccess.getUpgradeOptimizedConnection();
041                            PreparedStatement ps1 = con.prepareStatement(selectSQL);
042                            ResultSet rs = ps1.executeQuery();
043                            PreparedStatement ps2 = AutoBatchPreparedStatementUtil.autoBatch(
044                                    con.prepareStatement(updateSQL))) {
045    
046                            while (rs.next()) {
047                                    long resourcePermissionId = rs.getLong("resourcePermissionId");
048                                    long primKeyId = rs.getLong("primKeyId");
049                                    long actionIds = rs.getLong("actionIds");
050                                    boolean viewActionId = rs.getBoolean("viewActionId");
051    
052                                    long newPrimKeyId = GetterUtil.getLong(rs.getString("primKey"));
053                                    boolean newViewActionId = (actionIds % 2 == 1);
054    
055                                    if ((primKeyId == newPrimKeyId) &&
056                                            (newViewActionId == viewActionId)) {
057    
058                                            continue;
059                                    }
060    
061                                    ps2.setLong(1, newPrimKeyId);
062    
063                                    if (newViewActionId) {
064                                            ps2.setBoolean(2, true);
065                                    }
066                                    else {
067                                            ps2.setBoolean(2, false);
068                                    }
069    
070                                    ps2.setLong(3, resourcePermissionId);
071    
072                                    ps2.addBatch();
073                            }
074    
075                            ps2.executeBatch();
076                    }
077            }
078    
079    }