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 =
041                                                    AutoBatchPreparedStatementUtil.autoBatch(
042                                                            connection.prepareStatement(updateSQL))) {
043    
044                            while (rs.next()) {
045                                    long resourcePermissionId = rs.getLong("resourcePermissionId");
046                                    long primKeyId = rs.getLong("primKeyId");
047                                    long actionIds = rs.getLong("actionIds");
048                                    boolean viewActionId = rs.getBoolean("viewActionId");
049    
050                                    long newPrimKeyId = GetterUtil.getLong(rs.getString("primKey"));
051                                    boolean newViewActionId = (actionIds % 2 == 1);
052    
053                                    if ((primKeyId == newPrimKeyId) &&
054                                            (newViewActionId == viewActionId)) {
055    
056                                            continue;
057                                    }
058    
059                                    ps2.setLong(1, newPrimKeyId);
060    
061                                    if (newViewActionId) {
062                                            ps2.setBoolean(2, true);
063                                    }
064                                    else {
065                                            ps2.setBoolean(2, false);
066                                    }
067    
068                                    ps2.setLong(3, resourcePermissionId);
069    
070                                    ps2.addBatch();
071                            }
072    
073                            ps2.executeBatch();
074                    }
075            }
076    
077    }