001    /**
002     * Copyright (c) 2000-2012 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 com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.sql.Connection;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class UpgradePortletId extends UpgradeProcess {
029    
030            @Override
031            protected void doUpgrade() throws Exception {
032    
033                    // This is only tested to work on instanceable portlets
034    
035                    String[][] portletIdsArray = getPortletIdsArray();
036    
037                    for (String[] portletIds : portletIdsArray) {
038                            String oldRootPortletId = portletIds[0];
039                            String newRootPortletId = portletIds[1];
040    
041                            updatePortlet(oldRootPortletId, newRootPortletId);
042                    }
043            }
044    
045            protected String[][] getPortletIdsArray() {
046                    return new String[][] {
047                            new String[] {
048                                    "109", "1_WAR_webformportlet"
049                            },
050                            new String[] {
051                                    "google_adsense_portlet_WAR_googleadsenseportlet",
052                                    "1_WAR_googleadsenseportlet"
053                            },
054                            new String[] {
055                                    "google_gadget_portlet_WAR_googlegadgetportlet",
056                                    "1_WAR_googlegadgetportlet"
057                            },
058                            new String[] {
059                                    "google_maps_portlet_WAR_googlemapsportlet",
060                                    "1_WAR_googlemapsportlet"
061                            }
062                    };
063            }
064    
065            protected void updateLayout(long plid, String typeSettings)
066                    throws Exception {
067    
068                    Connection con = null;
069                    PreparedStatement ps = null;
070    
071                    try {
072                            con = DataAccess.getUpgradeOptimizedConnection();
073    
074                            ps = con.prepareStatement(
075                                    "update Layout set typeSettings = ? where plid = " + plid);
076    
077                            ps.setString(1, typeSettings);
078    
079                            ps.executeUpdate();
080                    }
081                    finally {
082                            DataAccess.cleanUp(con, ps);
083                    }
084            }
085    
086            protected void updateLayout(
087                            long plid, String oldPortletId, String newPortletId)
088                    throws Exception {
089    
090                    Connection con = null;
091                    PreparedStatement ps = null;
092                    ResultSet rs = null;
093    
094                    try {
095                            con = DataAccess.getUpgradeOptimizedConnection();
096    
097                            ps = con.prepareStatement(
098                                    "select typeSettings from Layout where plid = " + plid);
099    
100                            rs = ps.executeQuery();
101    
102                            while (rs.next()) {
103                                    String typeSettings = rs.getString("typeSettings");
104    
105                                    String newTypeSettings = StringUtil.replace(
106                                            typeSettings, oldPortletId, newPortletId);
107    
108                                    updateLayout(plid, newTypeSettings);
109                            }
110                    }
111                    finally {
112                            DataAccess.cleanUp(con, ps, rs);
113                    }
114            }
115    
116            protected void updatePortlet(
117                            String oldRootPortletId, String newRootPortletId)
118                    throws Exception {
119    
120                    runSQL(
121                            "update Portlet set portletId = '" + newRootPortletId +
122                                    "' where portletId = '" + oldRootPortletId + "'");
123            }
124    
125    }