1   /**
2    * Copyright (c) 2000-2008 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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.model.impl.PortletImpl;
28  import com.liferay.portal.spring.hibernate.HibernateUtil;
29  import com.liferay.portal.upgrade.UpgradeException;
30  import com.liferay.portal.upgrade.UpgradeProcess;
31  import com.liferay.util.dao.DataAccess;
32  
33  import java.sql.Connection;
34  import java.sql.PreparedStatement;
35  import java.sql.ResultSet;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * <a href="UpgradePortletId.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class UpgradePortletId extends UpgradeProcess {
47  
48      public void upgrade() throws UpgradeException {
49          _log.info("Upgrading");
50  
51          try {
52              doUpgrade();
53          }
54          catch (Exception e) {
55              throw new UpgradeException(e);
56          }
57      }
58  
59      protected void doUpgrade() throws Exception {
60  
61          // This is only tested to work on instanceable portlets
62  
63          for (int i = 0; i < _PORTLET_IDS.length; i++) {
64              String[] portletIds = _PORTLET_IDS[i];
65  
66              String oldRootPortletId = portletIds[0];
67              String newRootPortletId = portletIds[1];
68  
69              upgradePortlet(oldRootPortletId, newRootPortletId);
70              upgradeResource(oldRootPortletId, newRootPortletId);
71              upgradeResourceCode(oldRootPortletId, newRootPortletId);
72          }
73      }
74  
75      protected void upgradeLayout(
76              long plid, String oldPortletId, String newPortletId)
77          throws Exception {
78  
79          Connection con = null;
80          PreparedStatement ps = null;
81          ResultSet rs = null;
82  
83          try {
84              con = HibernateUtil.getConnection();
85  
86              ps = con.prepareStatement(
87                  "select typeSettings from Layout where plid = " + plid);
88  
89              rs = ps.executeQuery();
90  
91              while (rs.next()) {
92                  String typeSettings = rs.getString("typeSettings");
93  
94                  String newTypeSettings = upgradeTypeSettings(
95                      typeSettings, oldPortletId, newPortletId);
96  
97                  ps = con.prepareStatement(
98                      "update Layout set typeSettings = ? where plid = " +
99                          plid);
100 
101                 ps.setString(1, newTypeSettings);
102 
103                 ps.executeUpdate();
104 
105                 ps.close();
106             }
107         }
108         finally {
109             DataAccess.cleanUp(con, ps, rs);
110         }
111     }
112 
113     protected void upgradePortlet(
114             String oldRootPortletId, String newRootPortletId)
115         throws Exception {
116 
117         runSQL(
118             "update Portlet set portletId = '" + newRootPortletId +
119                 "' where portletId = '" + oldRootPortletId + "'");
120     }
121 
122     protected void upgradePortletPreferences(
123             String oldPortletId, String newPortletId)
124         throws Exception {
125 
126         runSQL(
127             "update PortletPreferences set portletId = '" + newPortletId +
128                 "' where portletId = '" + oldPortletId + "'");
129     }
130 
131     protected void upgradeResource(
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 = HibernateUtil.getConnection();
141 
142             ps = con.prepareStatement(
143                 "select primKey from Resource_ where primKey like ?");
144 
145             String primKeyLike =
146                 "%" + PortletImpl.LAYOUT_SEPARATOR + oldRootPortletId +
147                     PortletImpl.INSTANCE_SEPARATOR + "%";
148 
149             ps.setString(1, primKeyLike);
150 
151             rs = ps.executeQuery();
152 
153             while (rs.next()) {
154                 String oldPrimKey = rs.getString("primKey");
155 
156                 int pos = oldPrimKey.indexOf(PortletImpl.LAYOUT_SEPARATOR);
157 
158                 long plid = GetterUtil.getLong(
159                     oldPrimKey.substring(0, pos));
160 
161                 pos = oldPrimKey.indexOf(PortletImpl.INSTANCE_SEPARATOR);
162 
163                 String instanceId = oldPrimKey.substring(
164                     pos + PortletImpl.INSTANCE_SEPARATOR.length());
165 
166                 String newPrimKey =
167                     plid + PortletImpl.LAYOUT_SEPARATOR + newRootPortletId +
168                         PortletImpl.INSTANCE_SEPARATOR +
169                             instanceId;
170 
171                 runSQL(
172                     "update Resource_ set primKey = '" + newPrimKey +
173                         "' where primKey = '" + oldPrimKey + "'");
174 
175                 String oldPortletId =
176                     oldRootPortletId + PortletImpl.INSTANCE_SEPARATOR +
177                         instanceId;
178                 String newPortletId =
179                     newRootPortletId + PortletImpl.INSTANCE_SEPARATOR +
180                         instanceId;
181 
182                 upgradeLayout(plid, oldPortletId, newPortletId);
183                 upgradePortletPreferences(oldPortletId, newPortletId);
184             }
185         }
186         finally {
187             DataAccess.cleanUp(con, ps, rs);
188         }
189     }
190 
191     protected void upgradeResourceCode(
192             String oldRootPortletId, String newRootPortletId)
193         throws Exception {
194 
195         runSQL(
196             "update ResourceCode set name = '" + newRootPortletId +
197                 "' where name = '" + oldRootPortletId + "'");
198     }
199 
200     protected String upgradeTypeSettings(
201             String typeSettings, String oldPortletId, String newPortletId)
202         throws Exception {
203 
204         return StringUtil.replace(typeSettings, oldPortletId, newPortletId);
205     }
206 
207     private static final String[][] _PORTLET_IDS = new String[][] {
208         new String[] {
209             "94",
210             "google_adsense_portlet_WAR_googleadsenseportlet"
211         },
212         new String[] {
213             "95",
214             "google_gadget_portlet_WAR_googlegadgetportlet"
215         },
216         new String[] {
217             "96",
218             "google_maps_portlet_WAR_googlemapsportlet"
219         },
220     };
221 
222     private static Log _log = LogFactory.getLog(UpgradePortletId.class);
223 
224 }