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