1   /**
2    * Copyright (c) 2000-2009 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.v5_1_0;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
31  import com.liferay.portal.upgrade.UpgradeProcess;
32  import com.liferay.portlet.PortletPreferencesImpl;
33  import com.liferay.portlet.PortletPreferencesSerializer;
34  
35  import java.sql.Connection;
36  import java.sql.PreparedStatement;
37  import java.sql.ResultSet;
38  
39  /**
40   * <a href="UpgradeSitemap.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Jorge Ferrer
43   * @author Brian Wing Shun Chan
44   */
45  public class UpgradeSitemap extends UpgradeProcess {
46  
47      protected void deletePortletPreferences(long portletPreferencesId)
48          throws Exception {
49  
50          runSQL(
51              "delete from PortletPreferences where portletPreferencesId = " +
52                  portletPreferencesId);
53      }
54  
55      protected void doUpgrade() throws Exception {
56          Connection con = null;
57          PreparedStatement ps = null;
58          ResultSet rs = null;
59  
60          try {
61              con = DataAccess.getConnection();
62  
63              ps = con.prepareStatement(
64                  "select portletPreferencesId, ownerId, ownerType, plid, " +
65                      "portletId, preferences from PortletPreferences where " +
66                          "portletId like '85_%'");
67  
68              rs = ps.executeQuery();
69  
70              while (rs.next()) {
71                  long portletPreferencesId = rs.getLong("portletPreferencesId");
72                  long ownerId = rs.getLong("ownerId");
73                  int ownerType = rs.getInt("ownerType");
74                  long plid = rs.getLong("plid");
75                  String portletId = rs.getString("portletId");
76                  String preferences = rs.getString("preferences");
77  
78                  try {
79                      Layout layout = LayoutLocalServiceUtil.getLayout(plid);
80  
81                      String newPreferences = upgradePreferences(
82                          layout.getCompanyId(), ownerId, ownerType, plid,
83                          portletId, preferences);
84  
85                      updatePortletPreferences(
86                          portletPreferencesId, newPreferences);
87                  }
88                  catch (NoSuchLayoutException nsle) {
89                      deletePortletPreferences(portletPreferencesId);
90                  }
91              }
92          }
93          finally {
94              DataAccess.cleanUp(con, ps, rs);
95          }
96      }
97  
98      protected void updatePortletPreferences(
99              long portletPreferencesId, String preferences)
100         throws Exception {
101 
102         Connection con = null;
103         PreparedStatement ps = null;
104 
105         try {
106             con = DataAccess.getConnection();
107 
108             ps = con.prepareStatement(
109                 "update PortletPreferences set preferences = ? where " +
110                     "portletPreferencesId = " + portletPreferencesId);
111 
112             ps.setString(1, preferences);
113 
114             ps.executeUpdate();
115         }
116         finally {
117             DataAccess.cleanUp(con, ps);
118         }
119     }
120 
121     protected String upgradePreferences(
122             long companyId, long ownerId, int ownerType, long plid,
123             String portletId, String preferences)
124         throws Exception {
125 
126         PortletPreferencesImpl prefs = PortletPreferencesSerializer.fromXML(
127             companyId, ownerId, ownerType, plid, portletId, preferences);
128 
129         long rootPlid = GetterUtil.getLong(
130             prefs.getValue("root-plid", StringPool.BLANK));
131 
132         if (rootPlid > 0) {
133             try {
134                 Layout layout = LayoutLocalServiceUtil.getLayout(rootPlid);
135 
136                 prefs.setValue(
137                     "root-layout-id", String.valueOf(layout.getLayoutId()));
138             }
139             catch (NoSuchLayoutException nsle) {
140             }
141         }
142 
143         prefs.setValue("root-plid", null);
144 
145         return PortletPreferencesSerializer.toXML(prefs);
146     }
147 
148 }