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_2_0;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
19  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portlet.PortletPreferencesImpl;
24  import com.liferay.portlet.PortletPreferencesSerializer;
25  
26  import java.sql.Connection;
27  import java.sql.PreparedStatement;
28  import java.sql.ResultSet;
29  
30  /**
31   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Samuel Kong
34   * @author Brian Wing Shun Chan
35   * @author Edward Shin
36   */
37  public class UpgradeDocumentLibrary extends UpgradeProcess {
38  
39      protected void deletePortletPreferences(long portletPreferencesId)
40          throws Exception {
41  
42          runSQL(
43              "delete from PortletPreferences where portletPreferencesId = " +
44                  portletPreferencesId);
45      }
46  
47      protected void doUpgrade() throws Exception {
48          Connection con = null;
49          PreparedStatement ps = null;
50          ResultSet rs = null;
51  
52          try {
53              con = DataAccess.getConnection();
54  
55              ps = con.prepareStatement(
56                  "select portletPreferencesId, ownerId, ownerType, plid, " +
57                      "portletId, preferences from PortletPreferences where " +
58                          "portletId = '20'");
59  
60              rs = ps.executeQuery();
61  
62              while (rs.next()) {
63                  long portletPreferencesId = rs.getLong("portletPreferencesId");
64                  long ownerId = rs.getLong("ownerId");
65                  int ownerType = rs.getInt("ownerType");
66                  long plid = rs.getLong("plid");
67                  String portletId = rs.getString("portletId");
68                  String preferences = rs.getString("preferences");
69  
70                  try {
71                      String newPreferences = upgradePreferences(
72                          ownerId, ownerType, plid, portletId, preferences);
73  
74                      updatePortletPreferences(
75                          portletPreferencesId, newPreferences);
76                  }
77                  catch (NoSuchLayoutException nsle) {
78                      deletePortletPreferences(portletPreferencesId);
79                  }
80              }
81          }
82          finally {
83              DataAccess.cleanUp(con, ps, rs);
84          }
85      }
86  
87      protected void updatePortletPreferences(
88              long portletPreferencesId, String preferences)
89          throws Exception {
90  
91          Connection con = null;
92          PreparedStatement ps = null;
93  
94          try {
95              con = DataAccess.getConnection();
96  
97              ps = con.prepareStatement(
98                  "update PortletPreferences set preferences = ? where " +
99                      "portletPreferencesId = " + portletPreferencesId);
100 
101             ps.setString(1, preferences);
102 
103             ps.executeUpdate();
104         }
105         finally {
106             DataAccess.cleanUp(con, ps);
107         }
108     }
109 
110     protected String upgradePreferences(
111             long ownerId, int ownerType, long plid, String portletId,
112             String xml)
113         throws Exception {
114 
115         long companyId = 0;
116 
117         Connection con = null;
118         PreparedStatement ps = null;
119         ResultSet rs = null;
120 
121         try {
122             con = DataAccess.getConnection();
123 
124             ps = con.prepareStatement(
125                 "select companyId from Layout where plid = ?");
126 
127             ps.setLong(1, plid);
128 
129             rs = ps.executeQuery();
130 
131             if (rs.next()) {
132                 companyId = rs.getLong("companyId");
133             }
134         }
135         finally {
136             DataAccess.cleanUp(con, ps, rs);
137         }
138 
139         if (companyId <= 0) {
140             throw new NoSuchLayoutException();
141         }
142 
143         PortletPreferencesImpl preferences =
144             PortletPreferencesSerializer.fromXML(
145                 companyId, ownerId, ownerType, plid, portletId, xml);
146 
147         String fileEntryColumns = preferences.getValue(
148             "fileEntryColumns", StringPool.BLANK);
149 
150         if (Validator.isNotNull(fileEntryColumns)) {
151             fileEntryColumns = StringUtil.replace(
152                 fileEntryColumns, "document", "name");
153 
154             preferences.setValue("fileEntryColumns", fileEntryColumns);
155         }
156 
157         return PortletPreferencesSerializer.toXML(preferences);
158     }
159 
160 }