1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upgrade.v4_3_5;
24  
25  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.model.PortletConstants;
31  import com.liferay.portal.upgrade.UpgradeException;
32  import com.liferay.portal.upgrade.UpgradeProcess;
33  
34  import java.sql.Connection;
35  import java.sql.PreparedStatement;
36  import java.sql.ResultSet;
37  
38  /**
39   * <a href="UpgradePortletId.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class UpgradePortletId extends UpgradeProcess {
45  
46      public void upgrade() throws UpgradeException {
47          _log.info("Upgrading");
48  
49          try {
50              doUpgrade();
51          }
52          catch (Exception e) {
53              throw new UpgradeException(e);
54          }
55      }
56  
57      protected void doUpgrade() throws Exception {
58  
59          // This is only tested to work on instanceable portlets
60  
61          for (int i = 0; i < _PORTLET_IDS.length; i++) {
62              String[] portletIds = _PORTLET_IDS[i];
63  
64              String oldRootPortletId = portletIds[0];
65              String newRootPortletId = portletIds[1];
66  
67              upgradePortlet(oldRootPortletId, newRootPortletId);
68              upgradeResource(oldRootPortletId, newRootPortletId);
69              upgradeResourceCode(oldRootPortletId, newRootPortletId);
70          }
71      }
72  
73      protected void upgradeLayout(
74              long plid, String oldPortletId, String newPortletId)
75          throws Exception {
76  
77          Connection con = null;
78          PreparedStatement ps = null;
79          ResultSet rs = null;
80  
81          try {
82              con = DataAccess.getConnection();
83  
84              ps = con.prepareStatement(
85                  "select typeSettings from Layout where plid = " + plid);
86  
87              rs = ps.executeQuery();
88  
89              while (rs.next()) {
90                  String typeSettings = rs.getString("typeSettings");
91  
92                  String newTypeSettings = upgradeTypeSettings(
93                      typeSettings, oldPortletId, newPortletId);
94  
95                  ps = con.prepareStatement(
96                      "update Layout set typeSettings = ? where plid = " +
97                          plid);
98  
99                  ps.setString(1, newTypeSettings);
100 
101                 ps.executeUpdate();
102 
103                 ps.close();
104             }
105         }
106         finally {
107             DataAccess.cleanUp(con, ps, rs);
108         }
109     }
110 
111     protected void upgradePortlet(
112             String oldRootPortletId, String newRootPortletId)
113         throws Exception {
114 
115         runSQL(
116             "update Portlet set portletId = '" + newRootPortletId +
117                 "' where portletId = '" + oldRootPortletId + "'");
118     }
119 
120     protected void upgradePortletPreferences(
121             String oldPortletId, String newPortletId)
122         throws Exception {
123 
124         runSQL(
125             "update PortletPreferences set portletId = '" + newPortletId +
126                 "' where portletId = '" + oldPortletId + "'");
127     }
128 
129     protected void upgradeResource(
130             String oldRootPortletId, String newRootPortletId)
131         throws Exception {
132 
133         Connection con = null;
134         PreparedStatement ps = null;
135         ResultSet rs = null;
136 
137         try {
138             con = DataAccess.getConnection();
139 
140             ps = con.prepareStatement(
141                 "select primKey from Resource_ where primKey like ?");
142 
143             String primKeyLike =
144                 "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
145                     PortletConstants.INSTANCE_SEPARATOR + "%";
146 
147             ps.setString(1, primKeyLike);
148 
149             rs = ps.executeQuery();
150 
151             while (rs.next()) {
152                 String oldPrimKey = rs.getString("primKey");
153 
154                 int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
155 
156                 long plid = GetterUtil.getLong(
157                     oldPrimKey.substring(0, pos));
158 
159                 pos = oldPrimKey.indexOf(PortletConstants.INSTANCE_SEPARATOR);
160 
161                 String instanceId = oldPrimKey.substring(
162                     pos + PortletConstants.INSTANCE_SEPARATOR.length());
163 
164                 String newPrimKey =
165                     plid + PortletConstants.LAYOUT_SEPARATOR +
166                         newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
167                             instanceId;
168 
169                 runSQL(
170                     "update Resource_ set primKey = '" + newPrimKey +
171                         "' where primKey = '" + oldPrimKey + "'");
172 
173                 String oldPortletId =
174                     oldRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
175                         instanceId;
176                 String newPortletId =
177                     newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
178                         instanceId;
179 
180                 upgradeLayout(plid, oldPortletId, newPortletId);
181                 upgradePortletPreferences(oldPortletId, newPortletId);
182             }
183         }
184         finally {
185             DataAccess.cleanUp(con, ps, rs);
186         }
187     }
188 
189     protected void upgradeResourceCode(
190             String oldRootPortletId, String newRootPortletId)
191         throws Exception {
192 
193         runSQL(
194             "update ResourceCode set name = '" + newRootPortletId +
195                 "' where name = '" + oldRootPortletId + "'");
196     }
197 
198     protected String upgradeTypeSettings(
199             String typeSettings, String oldPortletId, String newPortletId)
200         throws Exception {
201 
202         return StringUtil.replace(typeSettings, oldPortletId, newPortletId);
203     }
204 
205     private static final String[][] _PORTLET_IDS = new String[][] {
206         new String[] {
207             "94",
208             "google_adsense_portlet_WAR_googleadsenseportlet"
209         },
210         new String[] {
211             "95",
212             "google_gadget_portlet_WAR_googlegadgetportlet"
213         },
214         new String[] {
215             "96",
216             "google_maps_portlet_WAR_googlemapsportlet"
217         }
218     };
219 
220     private static Log _log = LogFactoryUtil.getLog(UpgradePortletId.class);
221 
222 }