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.kernel.util.StringBundler;
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                    Connection con = null;
034                    PreparedStatement ps = null;
035                    ResultSet rs = null;
036    
037                    try {
038                            con = DataAccess.getUpgradeOptimizedConnection();
039    
040                            ps = con.prepareStatement(
041                                    "select resourcePermissionId, primKey, primKeyId, actionIds, " +
042                                            "viewActionId from ResourcePermission");
043    
044                            rs = ps.executeQuery();
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                                    StringBundler sb = new StringBundler(6);
062    
063                                    sb.append("update ResourcePermission set primKeyId = ");
064                                    sb.append(newPrimKeyId);
065                                    sb.append(", viewActionId = ");
066    
067                                    if (newViewActionId) {
068                                            sb.append("[$TRUE$]");
069                                    }
070                                    else {
071                                            sb.append("[$FALSE$]");
072                                    }
073    
074                                    sb.append(" where resourcePermissionId = ");
075                                    sb.append(resourcePermissionId);
076    
077                                    runSQL(sb.toString());
078                            }
079                    }
080                    finally {
081                            DataAccess.cleanUp(con, ps, rs);
082                    }
083            }
084    
085    }