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