001    /**
002     * Copyright (c) 2000-2010 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.v4_3_5;
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.StringUtil;
021    import com.liferay.portal.model.PortletConstants;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class UpgradePortletId extends UpgradeProcess {
031    
032            protected void doUpgrade() throws Exception {
033    
034                    // This is only tested to work on instanceable portlets
035    
036                    String[][] portletIdsArray = getPortletIdsArray();
037    
038                    for (int i = 0; i < portletIdsArray.length; i++) {
039                            String[] portletIds = portletIdsArray[i];
040    
041                            String oldRootPortletId = portletIds[0];
042                            String newRootPortletId = portletIds[1];
043    
044                            updatePortlet(oldRootPortletId, newRootPortletId);
045                            updateResource(oldRootPortletId, newRootPortletId);
046                            updateResourceCode(oldRootPortletId, newRootPortletId);
047                    }
048            }
049    
050            protected String[][] getPortletIdsArray() {
051                    return new String[][] {
052                            new String[] {
053                                    "94",
054                                    "1_WAR_googleadsenseportlet"
055                            },
056                            new String[] {
057                                    "95",
058                                    "1_WAR_googlegadgetportlet"
059                            },
060                            new String[] {
061                                    "96",
062                                    "1_WAR_googlemapsportlet"
063                            }
064                    };
065            }
066    
067            protected void updateLayout(
068                            long plid, String oldPortletId, String newPortletId)
069                    throws Exception {
070    
071                    Connection con = null;
072                    PreparedStatement ps = null;
073                    ResultSet rs = null;
074    
075                    try {
076                            con = DataAccess.getConnection();
077    
078                            ps = con.prepareStatement(
079                                    "select typeSettings from Layout where plid = " + plid);
080    
081                            rs = ps.executeQuery();
082    
083                            while (rs.next()) {
084                                    String typeSettings = rs.getString("typeSettings");
085    
086                                    String newTypeSettings = StringUtil.replace(
087                                            typeSettings, oldPortletId, newPortletId);
088    
089                                    updateTypeSettings(plid, newTypeSettings);
090                            }
091                    }
092                    finally {
093                            DataAccess.cleanUp(con, ps, rs);
094                    }
095            }
096    
097            protected void updatePortlet(
098                            String oldRootPortletId, String newRootPortletId)
099                    throws Exception {
100    
101                    runSQL(
102                            "update Portlet set portletId = '" + newRootPortletId +
103                                    "' where portletId = '" + oldRootPortletId + "'");
104            }
105    
106            protected void updateResource(
107                            String oldRootPortletId, String newRootPortletId)
108                    throws Exception {
109    
110                    Connection con = null;
111                    PreparedStatement ps = null;
112                    ResultSet rs = null;
113    
114                    try {
115                            con = DataAccess.getConnection();
116    
117                            ps = con.prepareStatement(
118                                    "select primKey from Resource_ where primKey like ?");
119    
120                            ps.setString(
121                                    1,
122                                    "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
123                                            PortletConstants.INSTANCE_SEPARATOR + "%");
124    
125                            rs = ps.executeQuery();
126    
127                            while (rs.next()) {
128                                    String oldPrimKey = rs.getString("primKey");
129    
130                                    int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
131    
132                                    long plid = GetterUtil.getLong(
133                                            oldPrimKey.substring(0, pos));
134    
135                                    pos = oldPrimKey.indexOf(PortletConstants.INSTANCE_SEPARATOR);
136    
137                                    String instanceId = oldPrimKey.substring(
138                                            pos + PortletConstants.INSTANCE_SEPARATOR.length());
139    
140                                    String newPrimKey =
141                                            plid + PortletConstants.LAYOUT_SEPARATOR +
142                                                    newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
143                                                            instanceId;
144    
145                                    runSQL(
146                                            "update Resource_ set primKey = '" + newPrimKey +
147                                                    "' where primKey = '" + oldPrimKey + "'");
148    
149                                    String oldPortletId =
150                                            oldRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
151                                                    instanceId;
152                                    String newPortletId =
153                                            newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
154                                                    instanceId;
155    
156                                    updateLayout(plid, oldPortletId, newPortletId);
157    
158                                    runSQL(
159                                            "update PortletPreferences set portletId = '" +
160                                                    newPortletId + "' where portletId = '" + oldPortletId +
161                                                            "'");
162                            }
163                    }
164                    finally {
165                            DataAccess.cleanUp(con, ps, rs);
166                    }
167            }
168    
169            protected void updateResourceCode(
170                            String oldRootPortletId, String newRootPortletId)
171                    throws Exception {
172    
173                    runSQL(
174                            "update ResourceCode set name = '" + newRootPortletId +
175                                    "' where name = '" + oldRootPortletId + "'");
176            }
177    
178            protected void updateTypeSettings(long plid, String typeSettings)
179                    throws Exception {
180    
181                    Connection con = null;
182                    PreparedStatement ps = null;
183    
184                    try {
185                            con = DataAccess.getConnection();
186    
187                            ps = con.prepareStatement(
188                                    "update Layout set typeSettings = ? where plid = " + plid);
189    
190                            ps.setString(1, typeSettings);
191    
192                            ps.executeUpdate();
193                    }
194                    finally {
195                            DataAccess.cleanUp(con, ps);
196                    }
197            }
198    
199    }