1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.v5_1_0;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portlet.PortletPreferencesImpl;
22  import com.liferay.portlet.PortletPreferencesSerializer;
23  
24  import java.sql.Connection;
25  import java.sql.PreparedStatement;
26  import java.sql.ResultSet;
27  
28  /**
29   * <a href="UpgradeSitemap.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Jorge Ferrer
32   * @author Brian Wing Shun Chan
33   */
34  public class UpgradeSitemap extends UpgradeProcess {
35  
36      protected void deletePortletPreferences(long portletPreferencesId)
37          throws Exception {
38  
39          runSQL(
40              "delete from PortletPreferences where portletPreferencesId = " +
41                  portletPreferencesId);
42      }
43  
44      protected void doUpgrade() throws Exception {
45          Connection con = null;
46          PreparedStatement ps = null;
47          ResultSet rs = null;
48  
49          try {
50              con = DataAccess.getConnection();
51  
52              ps = con.prepareStatement(
53                  "select portletPreferencesId, ownerId, ownerType, plid, " +
54                      "portletId, preferences from PortletPreferences where " +
55                          "portletId like '85_%'");
56  
57              rs = ps.executeQuery();
58  
59              while (rs.next()) {
60                  long portletPreferencesId = rs.getLong("portletPreferencesId");
61                  long ownerId = rs.getLong("ownerId");
62                  int ownerType = rs.getInt("ownerType");
63                  long plid = rs.getLong("plid");
64                  String portletId = rs.getString("portletId");
65                  String preferences = rs.getString("preferences");
66  
67                  Object[] layout = getLayout(plid);
68  
69                  if (layout != null) {
70                      long companyId = (Long)layout[0];
71  
72                      String newPreferences = upgradePreferences(
73                          companyId, ownerId, ownerType, plid, portletId,
74                          preferences);
75  
76                      updatePortletPreferences(
77                          portletPreferencesId, newPreferences);
78                  }
79                  else {
80                      deletePortletPreferences(portletPreferencesId);
81                  }
82              }
83          }
84          finally {
85              DataAccess.cleanUp(con, ps, rs);
86          }
87      }
88  
89      protected Object[] getLayout(long plid) throws Exception {
90          Object[] layout = null;
91  
92          Connection con = null;
93          PreparedStatement ps = null;
94          ResultSet rs = null;
95  
96          try {
97              con = DataAccess.getConnection();
98  
99              ps = con.prepareStatement(_GET_LAYOUT);
100 
101             ps.setLong(1, plid);
102 
103             rs = ps.executeQuery();
104 
105             while (rs.next()) {
106                 long companyId = rs.getLong("companyId");
107                 long layoutId = rs.getLong("layoutId");
108 
109                 layout = new Object[] {companyId, layoutId};
110             }
111         }
112         finally {
113             DataAccess.cleanUp(con, ps, rs);
114         }
115 
116         return layout;
117     }
118 
119     protected void updatePortletPreferences(
120             long portletPreferencesId, String preferences)
121         throws Exception {
122 
123         Connection con = null;
124         PreparedStatement ps = null;
125 
126         try {
127             con = DataAccess.getConnection();
128 
129             ps = con.prepareStatement(
130                 "update PortletPreferences set preferences = ? where " +
131                     "portletPreferencesId = " + portletPreferencesId);
132 
133             ps.setString(1, preferences);
134 
135             ps.executeUpdate();
136         }
137         finally {
138             DataAccess.cleanUp(con, ps);
139         }
140     }
141 
142     protected String upgradePreferences(
143             long companyId, long ownerId, int ownerType, long plid,
144             String portletId, String xml)
145         throws Exception {
146 
147         PortletPreferencesImpl preferences =
148             PortletPreferencesSerializer.fromXML(
149                 companyId, ownerId, ownerType, plid, portletId, xml);
150 
151         long rootPlid = GetterUtil.getLong(
152             preferences.getValue("root-plid", StringPool.BLANK));
153 
154         if (rootPlid > 0) {
155             Object[] layout = getLayout(rootPlid);
156 
157             if (layout != null) {
158                 long layoutId = (Long)layout[1];
159 
160                 preferences.setValue(
161                     "root-layout-id", String.valueOf(layoutId));
162             }
163         }
164 
165         preferences.setValue("root-plid", null);
166 
167         return PortletPreferencesSerializer.toXML(preferences);
168     }
169 
170     private static final String _GET_LAYOUT =
171         "select * from Layout where plid = ?";
172 
173 }