001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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",
055                                    "1_WAR_webformportlet"
056                            },
057                            new String[] {
058                                    "google_adsense_portlet_WAR_googleadsenseportlet",
059                                    "1_WAR_googleadsenseportlet"
060                            },
061                            new String[] {
062                                    "google_gadget_portlet_WAR_googlegadgetportlet",
063                                    "1_WAR_googlegadgetportlet"
064                            },
065                            new String[] {
066                                    "google_maps_portlet_WAR_googlemapsportlet",
067                                    "1_WAR_googlemapsportlet"
068                            }
069                    };
070            }
071    
072            protected void updateLayout(long plid, String typeSettings)
073                    throws Exception {
074    
075                    Connection con = null;
076                    PreparedStatement ps = null;
077    
078                    try {
079                            con = DataAccess.getConnection();
080    
081                            ps = con.prepareStatement(
082                                    "update Layout set typeSettings = ? where plid = " + plid);
083    
084                            ps.setString(1, typeSettings);
085    
086                            ps.executeUpdate();
087                    }
088                    finally {
089                            DataAccess.cleanUp(con, ps);
090                    }
091            }
092    
093            protected void updateLayout(
094                            long plid, String oldPortletId, String newPortletId)
095                    throws Exception {
096    
097                    Connection con = null;
098                    PreparedStatement ps = null;
099                    ResultSet rs = null;
100    
101                    try {
102                            con = DataAccess.getConnection();
103    
104                            ps = con.prepareStatement(
105                                    "select typeSettings from Layout where plid = " + plid);
106    
107                            rs = ps.executeQuery();
108    
109                            while (rs.next()) {
110                                    String typeSettings = rs.getString("typeSettings");
111    
112                                    String newTypeSettings = StringUtil.replace(
113                                            typeSettings, oldPortletId, newPortletId);
114    
115                                    updateLayout(plid, newTypeSettings);
116                            }
117                    }
118                    finally {
119                            DataAccess.cleanUp(con, ps, rs);
120                    }
121            }
122    
123            protected void updatePortlet(
124                            String oldRootPortletId, String newRootPortletId)
125                    throws Exception {
126    
127                    runSQL(
128                            "update Portlet set portletId = '" + newRootPortletId +
129                                    "' where portletId = '" + oldRootPortletId + "'");
130            }
131    
132            protected void updateResource(
133                            String oldRootPortletId, String newRootPortletId)
134                    throws Exception {
135    
136                    Connection con = null;
137                    PreparedStatement ps = null;
138                    ResultSet rs = null;
139    
140                    try {
141                            con = DataAccess.getConnection();
142    
143                            ps = con.prepareStatement(
144                                    "select primKey from Resource_ where primKey like ?");
145    
146                            ps.setString(
147                                    1,
148                                    "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
149                                            "%");
150    
151                            rs = ps.executeQuery();
152    
153                            while (rs.next()) {
154                                    String oldPrimKey = rs.getString("primKey");
155    
156                                    int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
157    
158                                    long plid = GetterUtil.getLong(oldPrimKey.substring(0, pos));
159    
160                                    String portletId = oldPrimKey.substring(
161                                            pos + PortletConstants.LAYOUT_SEPARATOR.length());
162    
163                                    String newPrimKey =
164                                            plid + PortletConstants.LAYOUT_SEPARATOR +
165                                                    newRootPortletId;
166    
167                                    String oldPortletId = oldRootPortletId;
168                                    String newPortletId = newRootPortletId ;
169    
170                                    pos = portletId.indexOf(PortletConstants.INSTANCE_SEPARATOR);
171    
172                                    if (pos != -1) {
173                                            portletId = portletId.substring(0, pos);
174    
175                                            String instanceId = oldPrimKey.substring(
176                                                    pos + PortletConstants.INSTANCE_SEPARATOR.length());
177    
178                                            newPrimKey +=
179                                                    PortletConstants.INSTANCE_SEPARATOR + instanceId;
180    
181                                            oldPortletId +=
182                                                    PortletConstants.INSTANCE_SEPARATOR + instanceId;
183                                            newPortletId +=
184                                                    PortletConstants.INSTANCE_SEPARATOR + instanceId;
185                                    }
186    
187                                    if (!portletId.equals(oldRootPortletId)) {
188                                            continue;
189                                    }
190    
191                                    runSQL(
192                                            "update Resource_ set primKey = '" + newPrimKey +
193                                                    "' where primKey = '" + oldPrimKey + "'");
194    
195                                    updateLayout(plid, oldPortletId, newPortletId);
196    
197                                    runSQL(
198                                            "update PortletPreferences set portletId = '" +
199                                                    newPortletId + "' where portletId = '" + oldPortletId +
200                                                            "'");
201                            }
202                    }
203                    finally {
204                            DataAccess.cleanUp(con, ps, rs);
205                    }
206            }
207    
208            protected void updateResourceCode(
209                            String oldRootPortletId, String newRootPortletId)
210                    throws Exception {
211    
212                    runSQL(
213                            "update ResourceCode set name = '" + newRootPortletId +
214                                    "' where name = '" + oldRootPortletId + "'");
215            }
216    
217    }