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.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.service.LayoutLocalServiceUtil;
33  import com.liferay.portal.upgrade.UpgradeException;
34  import com.liferay.portal.upgrade.UpgradeProcess;
35  import com.liferay.portal.upgrade.v4_4_0.UpgradeLayout;
36  import com.liferay.portlet.PortletPreferencesImpl;
37  import com.liferay.portlet.PortletPreferencesSerializer;
38  
39  import java.sql.Connection;
40  import java.sql.PreparedStatement;
41  import java.sql.ResultSet;
42  
43  /**
44   * <a href="UpgradeSitemap.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   * @author Brian Wing Shun Chan
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 deletePortletPreferences(long portletPreferencesId)
64          throws Exception {
65  
66          Connection con = null;
67          PreparedStatement ps = null;
68  
69          try {
70              con = DataAccess.getConnection();
71  
72              ps = con.prepareStatement(
73                  "delete from PortletPreferences where portletPreferencesId = " +
74                      portletPreferencesId);
75  
76              ps.executeUpdate();
77          }
78          finally {
79              DataAccess.cleanUp(con, ps);
80          }
81      }
82  
83      protected void doUpgrade() throws Exception {
84          Connection con = null;
85          PreparedStatement ps = null;
86          ResultSet rs = null;
87  
88          try {
89              con = DataAccess.getConnection();
90  
91              ps = con.prepareStatement(
92                  "select portletPreferencesId, ownerId, ownerType, plid, " +
93                      "portletId, preferences from PortletPreferences where " +
94                          "portletId like '85_%'");
95  
96              rs = ps.executeQuery();
97  
98              while (rs.next()) {
99                  long portletPreferencesId = rs.getLong("portletPreferencesId");
100                 long ownerId = rs.getLong("ownerId");
101                 int ownerType = rs.getInt("ownerType");
102                 long plid = rs.getLong("plid");
103                 String portletId = rs.getString("portletId");
104                 String preferences = rs.getString("preferences");
105 
106                 try {
107                     Layout layout = LayoutLocalServiceUtil.getLayout(plid);
108 
109                     String newPreferences = upgradePreferences(
110                         layout.getCompanyId(), ownerId, ownerType, plid,
111                         portletId, preferences);
112 
113                     updatePortletPreferences(
114                         portletPreferencesId, newPreferences);
115                 }
116                 catch (NoSuchLayoutException nsle) {
117                     deletePortletPreferences(portletPreferencesId);
118                 }
119             }
120         }
121         finally {
122             DataAccess.cleanUp(con, ps, rs);
123         }
124     }
125 
126     protected void updatePortletPreferences(
127             long portletPreferencesId, String preferences)
128         throws Exception {
129 
130         Connection con = null;
131         PreparedStatement ps = null;
132 
133         try {
134             con = DataAccess.getConnection();
135 
136             ps = con.prepareStatement(
137                 "update PortletPreferences set preferences = ? where " +
138                     "portletPreferencesId = " + portletPreferencesId);
139 
140             ps.setString(1, preferences);
141 
142             ps.executeUpdate();
143         }
144         finally {
145             DataAccess.cleanUp(con, ps);
146         }
147     }
148 
149     protected String upgradePreferences(
150             long companyId, long ownerId, int ownerType, long plid,
151             String portletId, String preferences)
152         throws Exception {
153 
154         PortletPreferencesImpl prefs = PortletPreferencesSerializer.fromXML(
155             companyId, ownerId, ownerType, plid, portletId, preferences);
156 
157         long rootPlid = GetterUtil.getLong(
158             prefs.getValue("root-plid", StringPool.BLANK));
159 
160         if (rootPlid > 0) {
161             try {
162                 Layout layout = LayoutLocalServiceUtil.getLayout(rootPlid);
163 
164                 prefs.setValue(
165                     "root-layout-id", String.valueOf(layout.getLayoutId()));
166             }
167             catch (NoSuchLayoutException nsle) {
168             }
169         }
170 
171         prefs.setValue("root-plid", null);
172 
173         return PortletPreferencesSerializer.toXML(prefs);
174     }
175 
176     private static Log _log = LogFactoryUtil.getLog(UpgradeLayout.class);
177 
178 }