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