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.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.UpgradeException;
32  import com.liferay.portal.upgrade.UpgradeProcess;
33  import com.liferay.portal.upgrade.v4_4_0.UpgradeLayout;
34  import com.liferay.portlet.PortletPreferencesImpl;
35  import com.liferay.portlet.PortletPreferencesSerializer;
36  
37  import java.sql.Connection;
38  import java.sql.PreparedStatement;
39  import java.sql.ResultSet;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  /**
45   * <a href="UpgradeSitemap.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Jorge Ferrer
48   *
49   */
50  public class UpgradeSitemap extends UpgradeProcess {
51  
52      public void upgrade() throws UpgradeException {
53          _log.info("Upgrading");
54  
55          try {
56              doUpgrade();
57          }
58          catch (Exception e) {
59              throw new UpgradeException(e);
60          }
61      }
62  
63      protected void doUpgrade() throws Exception {
64          Connection con = null;
65          PreparedStatement ps = null;
66          ResultSet rs = null;
67  
68          try {
69              con = DataAccess.getConnection();
70  
71              ps = con.prepareStatement(
72                  "select ownerId, ownerType, plid, portletId, preferences " +
73                      "from PortletPreferences where portletId like '85_%'");
74  
75              rs = ps.executeQuery();
76  
77              while (rs.next()) {
78                  long ownerId = rs.getLong("ownerId");
79                  int ownerType = rs.getInt("ownerType");
80                  long plid = rs.getLong("plid");
81                  String portletId = rs.getString("portletId");
82                  String preferences = rs.getString("preferences");
83  
84                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
85  
86                  String newPreferences = upgradePreferences(
87                      layout.getCompanyId(), ownerId, ownerType, plid, portletId,
88                      preferences);
89  
90                  ps = con.prepareStatement(
91                      "update PortletPreferences set preferences = ? where " +
92                          "plid = " + plid + " and portletId = ?");
93  
94                  ps.setString(1, newPreferences);
95                  ps.setString(2, portletId);
96  
97                  ps.executeUpdate();
98  
99                  ps.close();
100             }
101         }
102         finally {
103             DataAccess.cleanUp(con, ps, rs);
104         }
105     }
106 
107     protected String upgradePreferences(
108             long companyId, long ownerId, int ownerType, long plid,
109             String portletId, String preferences)
110         throws Exception {
111 
112         PortletPreferencesImpl prefs = PortletPreferencesSerializer.fromXML(
113             companyId, ownerId, ownerType, plid, portletId, preferences);
114 
115         long rootPlid = GetterUtil.getLong(
116             prefs.getValue("root-plid", StringPool.BLANK));
117 
118         if (rootPlid > 0) {
119             try {
120                 Layout layout = LayoutLocalServiceUtil.getLayout(rootPlid);
121 
122                 prefs.setValue(
123                     "root-layout-id", String.valueOf(layout.getLayoutId()));
124             }
125             catch (NoSuchLayoutException nsle) {
126             }
127         }
128 
129         prefs.setValue("root-plid", null);
130 
131         return PortletPreferencesSerializer.toXML(prefs);
132     }
133 
134     private static Log _log = LogFactory.getLog(UpgradeLayout.class);
135 
136 }