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          runSQL(
66              "delete from PortletPreferences where portletPreferencesId = " +
67                  portletPreferencesId);
68      }
69  
70      protected void doUpgrade() throws Exception {
71          Connection con = null;
72          PreparedStatement ps = null;
73          ResultSet rs = null;
74  
75          try {
76              con = DataAccess.getConnection();
77  
78              ps = con.prepareStatement(
79                  "select portletPreferencesId, ownerId, ownerType, plid, " +
80                      "portletId, preferences from PortletPreferences where " +
81                          "portletId like '85_%'");
82  
83              rs = ps.executeQuery();
84  
85              while (rs.next()) {
86                  long portletPreferencesId = rs.getLong("portletPreferencesId");
87                  long ownerId = rs.getLong("ownerId");
88                  int ownerType = rs.getInt("ownerType");
89                  long plid = rs.getLong("plid");
90                  String portletId = rs.getString("portletId");
91                  String preferences = rs.getString("preferences");
92  
93                  try {
94                      Layout layout = LayoutLocalServiceUtil.getLayout(plid);
95  
96                      String newPreferences = upgradePreferences(
97                          layout.getCompanyId(), ownerId, ownerType, plid,
98                          portletId, preferences);
99  
100                     updatePortletPreferences(
101                         portletPreferencesId, newPreferences);
102                 }
103                 catch (NoSuchLayoutException nsle) {
104                     deletePortletPreferences(portletPreferencesId);
105                 }
106             }
107         }
108         finally {
109             DataAccess.cleanUp(con, ps, rs);
110         }
111     }
112 
113     protected void updatePortletPreferences(
114             long portletPreferencesId, String preferences)
115         throws Exception {
116 
117         Connection con = null;
118         PreparedStatement ps = null;
119 
120         try {
121             con = DataAccess.getConnection();
122 
123             ps = con.prepareStatement(
124                 "update PortletPreferences set preferences = ? where " +
125                     "portletPreferencesId = " + portletPreferencesId);
126 
127             ps.setString(1, preferences);
128 
129             ps.executeUpdate();
130         }
131         finally {
132             DataAccess.cleanUp(con, ps);
133         }
134     }
135 
136     protected String upgradePreferences(
137             long companyId, long ownerId, int ownerType, long plid,
138             String portletId, String xml)
139         throws Exception {
140 
141         PortletPreferencesImpl preferences =
142             PortletPreferencesSerializer.fromXML(
143                 companyId, ownerId, ownerType, plid, portletId, xml);
144 
145         long rootPlid = GetterUtil.getLong(
146             preferences.getValue("root-plid", StringPool.BLANK));
147 
148         if (rootPlid > 0) {
149             try {
150                 Layout layout = LayoutLocalServiceUtil.getLayout(rootPlid);
151 
152                 preferences.setValue(
153                     "root-layout-id", String.valueOf(layout.getLayoutId()));
154             }
155             catch (NoSuchLayoutException nsle) {
156             }
157         }
158 
159         preferences.setValue("root-plid", null);
160 
161         return PortletPreferencesSerializer.toXML(preferences);
162     }
163 
164     private static Log _log = LogFactoryUtil.getLog(UpgradeSitemap.class);
165 
166 }