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