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