1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v4_3_5;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.model.PortletConstants;
22  
23  import java.sql.Connection;
24  import java.sql.PreparedStatement;
25  import java.sql.ResultSet;
26  
27  /**
28   * <a href="UpgradePortletId.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class UpgradePortletId extends UpgradeProcess {
33  
34      protected void doUpgrade() throws Exception {
35  
36          // This is only tested to work on instanceable portlets
37  
38          String[][] portletIdsArray = getPortletIdsArray();
39  
40          for (int i = 0; i < portletIdsArray.length; i++) {
41              String[] portletIds = portletIdsArray[i];
42  
43              String oldRootPortletId = portletIds[0];
44              String newRootPortletId = portletIds[1];
45  
46              updatePortlet(oldRootPortletId, newRootPortletId);
47              updateResource(oldRootPortletId, newRootPortletId);
48              updateResourceCode(oldRootPortletId, newRootPortletId);
49          }
50      }
51  
52      protected String[][] getPortletIdsArray() {
53          return new String[][] {
54              new String[] {
55                  "94",
56                  "1_WAR_googleadsenseportlet"
57              },
58              new String[] {
59                  "95",
60                  "1_WAR_googlegadgetportlet"
61              },
62              new String[] {
63                  "96",
64                  "1_WAR_googlemapsportlet"
65              }
66          };
67      }
68  
69      protected void updateLayout(
70              long plid, String oldPortletId, String newPortletId)
71          throws Exception {
72  
73          Connection con = null;
74          PreparedStatement ps = null;
75          ResultSet rs = null;
76  
77          try {
78              con = DataAccess.getConnection();
79  
80              ps = con.prepareStatement(
81                  "select typeSettings from Layout where plid = " + plid);
82  
83              rs = ps.executeQuery();
84  
85              while (rs.next()) {
86                  String typeSettings = rs.getString("typeSettings");
87  
88                  String newTypeSettings = StringUtil.replace(
89                      typeSettings, oldPortletId, newPortletId);
90  
91                  updateTypeSettings(plid, newTypeSettings);
92              }
93          }
94          finally {
95              DataAccess.cleanUp(con, ps, rs);
96          }
97      }
98  
99      protected void updatePortlet(
100             String oldRootPortletId, String newRootPortletId)
101         throws Exception {
102 
103         runSQL(
104             "update Portlet set portletId = '" + newRootPortletId +
105                 "' where portletId = '" + oldRootPortletId + "'");
106     }
107 
108     protected void updateResource(
109             String oldRootPortletId, String newRootPortletId)
110         throws Exception {
111 
112         Connection con = null;
113         PreparedStatement ps = null;
114         ResultSet rs = null;
115 
116         try {
117             con = DataAccess.getConnection();
118 
119             ps = con.prepareStatement(
120                 "select primKey from Resource_ where primKey like ?");
121 
122             ps.setString(
123                 1,
124                 "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
125                     PortletConstants.INSTANCE_SEPARATOR + "%");
126 
127             rs = ps.executeQuery();
128 
129             while (rs.next()) {
130                 String oldPrimKey = rs.getString("primKey");
131 
132                 int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
133 
134                 long plid = GetterUtil.getLong(
135                     oldPrimKey.substring(0, pos));
136 
137                 pos = oldPrimKey.indexOf(PortletConstants.INSTANCE_SEPARATOR);
138 
139                 String instanceId = oldPrimKey.substring(
140                     pos + PortletConstants.INSTANCE_SEPARATOR.length());
141 
142                 String newPrimKey =
143                     plid + PortletConstants.LAYOUT_SEPARATOR +
144                         newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
145                             instanceId;
146 
147                 runSQL(
148                     "update Resource_ set primKey = '" + newPrimKey +
149                         "' where primKey = '" + oldPrimKey + "'");
150 
151                 String oldPortletId =
152                     oldRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
153                         instanceId;
154                 String newPortletId =
155                     newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
156                         instanceId;
157 
158                 updateLayout(plid, oldPortletId, newPortletId);
159 
160                 runSQL(
161                     "update PortletPreferences set portletId = '" +
162                         newPortletId + "' where portletId = '" + oldPortletId +
163                             "'");
164             }
165         }
166         finally {
167             DataAccess.cleanUp(con, ps, rs);
168         }
169     }
170 
171     protected void updateResourceCode(
172             String oldRootPortletId, String newRootPortletId)
173         throws Exception {
174 
175         runSQL(
176             "update ResourceCode set name = '" + newRootPortletId +
177                 "' where name = '" + oldRootPortletId + "'");
178     }
179 
180     protected void updateTypeSettings(long plid, String typeSettings)
181         throws Exception {
182 
183         Connection con = null;
184         PreparedStatement ps = null;
185 
186         try {
187             con = DataAccess.getConnection();
188 
189             ps = con.prepareStatement(
190                 "update Layout set typeSettings = ? where plid = " + plid);
191 
192             ps.setString(1, typeSettings);
193 
194             ps.executeUpdate();
195         }
196         finally {
197             DataAccess.cleanUp(con, ps);
198         }
199     }
200 
201 }