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                            updateLayouts(oldRootPortletId, newRootPortletId);
043                    }
044            }
045    
046            protected String[][] getPortletIdsArray() {
047                    return new String[][] {
048                            new String[] {
049                                    "109", "1_WAR_webformportlet"
050                            },
051                            new String[] {
052                                    "google_adsense_portlet_WAR_googleadsenseportlet",
053                                    "1_WAR_googleadsenseportlet"
054                            },
055                            new String[] {
056                                    "google_gadget_portlet_WAR_googlegadgetportlet",
057                                    "1_WAR_googlegadgetportlet"
058                            },
059                            new String[] {
060                                    "google_maps_portlet_WAR_googlemapsportlet",
061                                    "1_WAR_googlemapsportlet"
062                            }
063                    };
064            }
065    
066            protected void updateInstanceablePortletPreferences(
067                            String oldRootPortletId, String newRootPortletId)
068                    throws Exception {
069    
070                    Connection con = null;
071                    PreparedStatement ps = null;
072                    ResultSet rs = null;
073    
074                    try {
075                            con = DataAccess.getUpgradeOptimizedConnection();
076    
077                            ps = con.prepareStatement(
078                                    "select portletPreferencesId, portletId from " +
079                                            "PortletPreferences where portletId like '" +
080                                                    oldRootPortletId + "%'");
081    
082                            rs = ps.executeQuery();
083    
084                            while (rs.next()) {
085                                    long portletPreferencesId = rs.getLong("portletPreferencesId");
086                                    String portletId = rs.getString("portletId");
087    
088                                    String newPortletId = StringUtil.replace(
089                                            portletId, oldRootPortletId, newRootPortletId);
090    
091                                    updatePortletPreference(portletPreferencesId, newPortletId);
092                            }
093                    }
094                    finally {
095                            DataAccess.cleanUp(con, ps, rs);
096                    }
097            }
098    
099            protected void updateLayout(long plid, String typeSettings)
100                    throws Exception {
101    
102                    Connection con = null;
103                    PreparedStatement ps = null;
104    
105                    try {
106                            con = DataAccess.getUpgradeOptimizedConnection();
107    
108                            ps = con.prepareStatement(
109                                    "update Layout set typeSettings = ? where plid = " + plid);
110    
111                            ps.setString(1, typeSettings);
112    
113                            ps.executeUpdate();
114                    }
115                    finally {
116                            DataAccess.cleanUp(con, ps);
117                    }
118            }
119    
120            protected void updateLayout(
121                            long plid, String oldPortletId, String newPortletId)
122                    throws Exception {
123    
124                    Connection con = null;
125                    PreparedStatement ps = null;
126                    ResultSet rs = null;
127    
128                    try {
129                            con = DataAccess.getUpgradeOptimizedConnection();
130    
131                            ps = con.prepareStatement(
132                                    "select typeSettings from Layout where plid = " + plid);
133    
134                            rs = ps.executeQuery();
135    
136                            while (rs.next()) {
137                                    String typeSettings = rs.getString("typeSettings");
138    
139                                    String newTypeSettings = StringUtil.replace(
140                                            typeSettings, oldPortletId, newPortletId);
141    
142                                    updateLayout(plid, newTypeSettings);
143                            }
144                    }
145                    finally {
146                            DataAccess.cleanUp(con, ps, rs);
147                    }
148            }
149    
150            protected void updateLayouts(
151                            String oldRootPortletId, String newRootPortletId)
152                    throws Exception {
153    
154                    Connection con = null;
155                    PreparedStatement ps = null;
156                    ResultSet rs = null;
157    
158                    try {
159                            con = DataAccess.getUpgradeOptimizedConnection();
160    
161                            ps = con.prepareStatement(
162                                    "select plid, typeSettings from Layout where typeSettings " +
163                                            "like '%" + oldRootPortletId + "%'");
164    
165                            rs = ps.executeQuery();
166    
167                            while (rs.next()) {
168                                    long plid = rs.getLong("plid");
169                                    String typeSettings = rs.getString("typeSettings");
170    
171                                    String newTypeSettings = StringUtil.replace(
172                                            typeSettings, oldRootPortletId, newRootPortletId);
173    
174                                    updateLayout(plid, newTypeSettings);
175                            }
176                    }
177                    finally {
178                            DataAccess.cleanUp(con, ps, rs);
179                    }
180            }
181    
182            protected void updatePortlet(
183                            String oldRootPortletId, String newRootPortletId)
184                    throws Exception {
185    
186                    runSQL(
187                            "update Portlet set portletId = '" + newRootPortletId +
188                                    "' where portletId = '" + oldRootPortletId + "'");
189    
190                    runSQL(
191                            "update ResourceAction set name = '" + newRootPortletId +
192                                    "' where name = '" + oldRootPortletId + "'");
193    
194                    updateResourcePermission(oldRootPortletId, newRootPortletId);
195    
196                    updateInstanceablePortletPreferences(
197                            oldRootPortletId, newRootPortletId);
198            }
199    
200            protected void updatePortletPreference(
201                            long portletPreferencesId, String portletId)
202                    throws Exception {
203    
204                    Connection con = null;
205                    PreparedStatement ps = null;
206    
207                    try {
208                            con = DataAccess.getUpgradeOptimizedConnection();
209    
210                            ps = con.prepareStatement(
211                                    "update PortletPreferences set portletId = ? where " +
212                                            "portletPreferencesId = " + portletPreferencesId);
213    
214                            ps.setString(1, portletId);
215    
216                            ps.executeUpdate();
217                    }
218                    finally {
219                            DataAccess.cleanUp(con, ps);
220                    }
221            }
222    
223            protected void updateResourcePermission(
224                            long resourcePermissionId, String name, String primKey)
225                    throws Exception {
226    
227                    Connection con = null;
228                    PreparedStatement ps = null;
229    
230                    try {
231                            con = DataAccess.getUpgradeOptimizedConnection();
232    
233                            ps = con.prepareStatement(
234                                    "update ResourcePermission set name = ?, primKey = ? where " +
235                                            "resourcePermissionId = " + resourcePermissionId);
236    
237                            ps.setString(1, name);
238                            ps.setString(2, primKey);
239    
240                            ps.executeUpdate();
241                    }
242                    finally {
243                            DataAccess.cleanUp(con, ps);
244                    }
245            }
246    
247            protected void updateResourcePermission(
248                            String oldRootPortletId, String newRootPortletId)
249                    throws Exception {
250    
251                    Connection con = null;
252                    PreparedStatement ps = null;
253                    ResultSet rs = null;
254    
255                    try {
256                            con = DataAccess.getUpgradeOptimizedConnection();
257    
258                            ps = con.prepareStatement(
259                                    "select resourcePermissionId, name, primKey from " +
260                                            "ResourcePermission where name = '" + oldRootPortletId +
261                                                    "'");
262    
263                            rs = ps.executeQuery();
264    
265                            while (rs.next()) {
266                                    long resourcePermissionId = rs.getLong("resourcePermissionId");
267                                    String name = rs.getString("name");
268                                    String primKey = rs.getString("primKey");
269    
270                                    String newName = StringUtil.replace(
271                                            name, oldRootPortletId, newRootPortletId);
272                                    String newPrimKey = StringUtil.replace(
273                                            primKey, oldRootPortletId, newRootPortletId);
274    
275                                    updateResourcePermission(
276                                            resourcePermissionId, newName, newPrimKey);
277                            }
278                    }
279                    finally {
280                            DataAccess.cleanUp(con, ps, rs);
281                    }
282            }
283    
284    }