1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.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  public class UpgradePortletId extends UpgradeProcess {
41  
42      protected void doUpgrade() throws Exception {
43  
44          // This is only tested to work on instanceable portlets
45  
46          String[][] portletIdsArray = getPortletIdsArray();
47  
48          for (int i = 0; i < portletIdsArray.length; i++) {
49              String[] portletIds = portletIdsArray[i];
50  
51              String oldRootPortletId = portletIds[0];
52              String newRootPortletId = portletIds[1];
53  
54              upgradePortlet(oldRootPortletId, newRootPortletId);
55              upgradeResource(oldRootPortletId, newRootPortletId);
56              upgradeResourceCode(oldRootPortletId, newRootPortletId);
57          }
58      }
59  
60      protected String[][] getPortletIdsArray() {
61          return new String[][] {
62              new String[] {
63                  "94",
64                  "1_WAR_googleadsenseportlet"
65              },
66              new String[] {
67                  "95",
68                  "1_WAR_googlegadgetportlet"
69              },
70              new String[] {
71                  "96",
72                  "1_WAR_googlemapsportlet"
73              }
74          };
75      }
76  
77      protected void upgradeLayout(
78              long plid, String oldPortletId, String newPortletId)
79          throws Exception {
80  
81          Connection con = null;
82          PreparedStatement ps = null;
83          ResultSet rs = null;
84  
85          try {
86              con = DataAccess.getConnection();
87  
88              ps = con.prepareStatement(
89                  "select typeSettings from Layout where plid = " + plid);
90  
91              rs = ps.executeQuery();
92  
93              while (rs.next()) {
94                  String typeSettings = rs.getString("typeSettings");
95  
96                  String newTypeSettings = upgradeTypeSettings(
97                      typeSettings, oldPortletId, newPortletId);
98  
99                  ps = con.prepareStatement(
100                     "update Layout set typeSettings = ? where plid = " +
101                         plid);
102 
103                 ps.setString(1, newTypeSettings);
104 
105                 ps.executeUpdate();
106 
107                 ps.close();
108             }
109         }
110         finally {
111             DataAccess.cleanUp(con, ps, rs);
112         }
113     }
114 
115     protected void upgradePortlet(
116             String oldRootPortletId, String newRootPortletId)
117         throws Exception {
118 
119         runSQL(
120             "update Portlet set portletId = '" + newRootPortletId +
121                 "' where portletId = '" + oldRootPortletId + "'");
122     }
123 
124     protected void upgradePortletPreferences(
125             String oldPortletId, String newPortletId)
126         throws Exception {
127 
128         runSQL(
129             "update PortletPreferences set portletId = '" + newPortletId +
130                 "' where portletId = '" + oldPortletId + "'");
131     }
132 
133     protected void upgradeResource(
134             String oldRootPortletId, String newRootPortletId)
135         throws Exception {
136 
137         Connection con = null;
138         PreparedStatement ps = null;
139         ResultSet rs = null;
140 
141         try {
142             con = DataAccess.getConnection();
143 
144             ps = con.prepareStatement(
145                 "select primKey from Resource_ where primKey like ?");
146 
147             String primKeyLike =
148                 "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
149                     PortletConstants.INSTANCE_SEPARATOR + "%";
150 
151             ps.setString(1, primKeyLike);
152 
153             rs = ps.executeQuery();
154 
155             while (rs.next()) {
156                 String oldPrimKey = rs.getString("primKey");
157 
158                 int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
159 
160                 long plid = GetterUtil.getLong(
161                     oldPrimKey.substring(0, pos));
162 
163                 pos = oldPrimKey.indexOf(PortletConstants.INSTANCE_SEPARATOR);
164 
165                 String instanceId = oldPrimKey.substring(
166                     pos + PortletConstants.INSTANCE_SEPARATOR.length());
167 
168                 String newPrimKey =
169                     plid + PortletConstants.LAYOUT_SEPARATOR +
170                         newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
171                             instanceId;
172 
173                 runSQL(
174                     "update Resource_ set primKey = '" + newPrimKey +
175                         "' where primKey = '" + oldPrimKey + "'");
176 
177                 String oldPortletId =
178                     oldRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
179                         instanceId;
180                 String newPortletId =
181                     newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
182                         instanceId;
183 
184                 upgradeLayout(plid, oldPortletId, newPortletId);
185                 upgradePortletPreferences(oldPortletId, newPortletId);
186             }
187         }
188         finally {
189             DataAccess.cleanUp(con, ps, rs);
190         }
191     }
192 
193     protected void upgradeResourceCode(
194             String oldRootPortletId, String newRootPortletId)
195         throws Exception {
196 
197         runSQL(
198             "update ResourceCode set name = '" + newRootPortletId +
199                 "' where name = '" + oldRootPortletId + "'");
200     }
201 
202     protected String upgradeTypeSettings(
203             String typeSettings, String oldPortletId, String newPortletId)
204         throws Exception {
205 
206         return StringUtil.replace(typeSettings, oldPortletId, newPortletId);
207     }
208 
209 }