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