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.upgrade.UpgradeProcess;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.PortletConstants;
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              updatePortlet(oldRootPortletId, newRootPortletId);
55              updateResource(oldRootPortletId, newRootPortletId);
56              updateResourceCode(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 updateLayout(
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 = StringUtil.replace(
97                      typeSettings, oldPortletId, newPortletId);
98  
99                  updateTypeSettings(plid, newTypeSettings);
100             }
101         }
102         finally {
103             DataAccess.cleanUp(con, ps, rs);
104         }
105     }
106 
107     protected void updatePortlet(
108             String oldRootPortletId, String newRootPortletId)
109         throws Exception {
110 
111         runSQL(
112             "update Portlet set portletId = '" + newRootPortletId +
113                 "' where portletId = '" + oldRootPortletId + "'");
114     }
115 
116     protected void updateResource(
117             String oldRootPortletId, String newRootPortletId)
118         throws Exception {
119 
120         Connection con = null;
121         PreparedStatement ps = null;
122         ResultSet rs = null;
123 
124         try {
125             con = DataAccess.getConnection();
126 
127             ps = con.prepareStatement(
128                 "select primKey from Resource_ where primKey like ?");
129 
130             ps.setString(
131                 1,
132                 "%" + PortletConstants.LAYOUT_SEPARATOR + oldRootPortletId +
133                     PortletConstants.INSTANCE_SEPARATOR + "%");
134 
135             rs = ps.executeQuery();
136 
137             while (rs.next()) {
138                 String oldPrimKey = rs.getString("primKey");
139 
140                 int pos = oldPrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
141 
142                 long plid = GetterUtil.getLong(
143                     oldPrimKey.substring(0, pos));
144 
145                 pos = oldPrimKey.indexOf(PortletConstants.INSTANCE_SEPARATOR);
146 
147                 String instanceId = oldPrimKey.substring(
148                     pos + PortletConstants.INSTANCE_SEPARATOR.length());
149 
150                 String newPrimKey =
151                     plid + PortletConstants.LAYOUT_SEPARATOR +
152                         newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
153                             instanceId;
154 
155                 runSQL(
156                     "update Resource_ set primKey = '" + newPrimKey +
157                         "' where primKey = '" + oldPrimKey + "'");
158 
159                 String oldPortletId =
160                     oldRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
161                         instanceId;
162                 String newPortletId =
163                     newRootPortletId + PortletConstants.INSTANCE_SEPARATOR +
164                         instanceId;
165 
166                 updateLayout(plid, oldPortletId, newPortletId);
167 
168                 runSQL(
169                     "update PortletPreferences set portletId = '" +
170                         newPortletId + "' where portletId = '" + oldPortletId +
171                             "'");
172             }
173         }
174         finally {
175             DataAccess.cleanUp(con, ps, rs);
176         }
177     }
178 
179     protected void updateResourceCode(
180             String oldRootPortletId, String newRootPortletId)
181         throws Exception {
182 
183         runSQL(
184             "update ResourceCode set name = '" + newRootPortletId +
185                 "' where name = '" + oldRootPortletId + "'");
186     }
187 
188     protected void updateTypeSettings(long plid, String typeSettings)
189         throws Exception {
190 
191         Connection con = null;
192         PreparedStatement ps = null;
193 
194         try {
195             con = DataAccess.getConnection();
196 
197             ps = con.prepareStatement(
198                 "update Layout set typeSettings = ? where plid = " + plid);
199 
200             ps.setString(1, typeSettings);
201 
202             ps.executeUpdate();
203         }
204         finally {
205             DataAccess.cleanUp(con, ps);
206         }
207     }
208 
209 }