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.kernel.upgrade;
016    
017    import java.sql.PreparedStatement;
018    import java.sql.ResultSet;
019    import java.sql.SQLException;
020    
021    /**
022     * @author Adolfo P??rez
023     */
024    public abstract class BaseReplacePortletId extends BaseUpgradePortletId {
025    
026            protected boolean hasPortlet(String portletId) throws SQLException {
027                    return hasRow(
028                            "select count(*) from Portlet where portletId = ?", portletId);
029            }
030    
031            protected boolean hasResourceAction(String name) throws SQLException {
032                    return hasRow(
033                            "select count(*) from ResourceAction where name = ?", name);
034            }
035    
036            protected boolean hasResourcePermission(String name) throws SQLException {
037                    return hasRow(
038                            "select count(*) from ResourcePermission where name = ?", name);
039            }
040    
041            protected boolean hasRow(String sql, String value) throws SQLException {
042                    try (PreparedStatement ps = connection.prepareStatement(sql)) {
043                            ps.setString(1, value);
044    
045                            try (ResultSet rs = ps.executeQuery()) {
046                                    if (rs.next()) {
047                                            int count = rs.getInt(1);
048    
049                                            if (count > 0) {
050                                                    return true;
051                                            }
052                                    }
053    
054                                    return false;
055                            }
056                    }
057            }
058    
059            @Override
060            protected void updatePortletId(
061                            String oldRootPortletId, String newRootPortletId)
062                    throws Exception {
063    
064                    if (!hasPortlet(newRootPortletId)) {
065                            super.updatePortletId(oldRootPortletId, newRootPortletId);
066                    }
067            }
068    
069            @Override
070            protected void updateResourceAction(String oldName, String newName)
071                    throws Exception {
072    
073                    if (hasResourceAction(newName)) {
074                            try (PreparedStatement ps = connection.prepareStatement(
075                                            "delete from ResourceAction where name = ?")) {
076    
077                                    ps.setString(1, oldName);
078    
079                                    ps.execute();
080                            }
081                    }
082                    else {
083                            super.updateResourceAction(oldName, newName);
084                    }
085            }
086    
087            @Override
088            protected void updateResourcePermission(
089                            String oldRootPortletId, String newRootPortletId,
090                            boolean updateName)
091                    throws Exception {
092    
093                    if (hasResourcePermission(newRootPortletId)) {
094                            try (PreparedStatement ps = connection.prepareStatement(
095                                            "delete from ResourcePermission where name = ?")) {
096    
097                                    ps.setString(1, oldRootPortletId);
098    
099                                    ps.execute();
100                            }
101                    }
102                    else {
103                            super.updateResourcePermission(
104                                    oldRootPortletId, newRootPortletId, updateName);
105                    }
106            }
107    
108    }