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