1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.upgrade.v5_1_5;
16  
17  import com.liferay.portal.kernel.dao.jdbc.DataAccess;
18  import com.liferay.portal.kernel.upgrade.UpgradeProcess;
19  import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
20  import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.upgrade.v5_1_5.util.DLFileEntryTable;
24  import com.liferay.portal.upgrade.v5_1_5.util.DLFileRankTable;
25  import com.liferay.portal.upgrade.v5_1_5.util.DLFileShortcutTable;
26  import com.liferay.portal.upgrade.v5_1_5.util.DLFileVersionTable;
27  import com.liferay.portlet.PortletPreferencesImpl;
28  import com.liferay.portlet.PortletPreferencesSerializer;
29  
30  import java.sql.Connection;
31  import java.sql.PreparedStatement;
32  import java.sql.ResultSet;
33  
34  /**
35   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Samuel Kong
38   * @author Brian Wing Shun Chan
39   * @author Douglas Wong
40   */
41  public class UpgradeDocumentLibrary extends UpgradeProcess {
42  
43      protected void deletePortletPreferences(long portletPreferencesId)
44          throws Exception {
45  
46          runSQL(
47              "delete from PortletPreferences where portletPreferencesId = " +
48                  portletPreferencesId);
49      }
50  
51      protected void doUpgrade() throws Exception {
52          try {
53              runSQL("alter_column_type DLFileEntry name VARCHAR(255) null");
54          }
55          catch (Exception e) {
56  
57              // DLFileEntry
58  
59              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
60                  DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS);
61  
62              upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
63  
64              upgradeTable.updateTable();
65          }
66  
67          try {
68              runSQL("alter_column_type DLFileRank name VARCHAR(255) null");
69          }
70          catch (Exception e) {
71  
72              // DLFileRank
73  
74              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
75                  DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS);
76  
77              upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
78  
79              upgradeTable.updateTable();
80          }
81  
82          try {
83              runSQL("alter_column_type DLFileShortcut toName VARCHAR(255) null");
84          }
85          catch (Exception e) {
86  
87              // DLFileShortcut
88  
89              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
90                  DLFileShortcutTable.TABLE_NAME,
91                  DLFileShortcutTable.TABLE_COLUMNS);
92  
93              upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
94  
95              upgradeTable.updateTable();
96          }
97  
98          try {
99              runSQL("alter_column_type DLFileVersion name VARCHAR(255) null");
100         }
101         catch (Exception e) {
102 
103             // DLFileVersion
104 
105             UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
106                 DLFileVersionTable.TABLE_NAME,
107                 DLFileVersionTable.TABLE_COLUMNS);
108 
109             upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
110 
111             upgradeTable.updateTable();
112         }
113 
114         // groupId
115 
116         updateGroupId();
117 
118         // PortletPreferences
119 
120         updatePortletPreferences();
121     }
122 
123     protected Object[] getLayout(long plid) throws Exception {
124         Object[] layout = null;
125 
126         Connection con = null;
127         PreparedStatement ps = null;
128         ResultSet rs = null;
129 
130         try {
131             con = DataAccess.getConnection();
132 
133             ps = con.prepareStatement(_GET_LAYOUT);
134 
135             ps.setLong(1, plid);
136 
137             rs = ps.executeQuery();
138 
139             while (rs.next()) {
140                 long companyId = rs.getLong("companyId");
141 
142                 layout = new Object[] {companyId};
143             }
144         }
145         finally {
146             DataAccess.cleanUp(con, ps, rs);
147         }
148 
149         return layout;
150     }
151 
152     protected void updateGroupId() throws Exception {
153         StringBuilder sb = new StringBuilder();
154 
155         sb.append("update DLFileEntry set groupId = (select groupId from ");
156         sb.append("DLFolder where DLFolder.folderId = DLFileEntry.folderId)");
157 
158         runSQL(sb.toString());
159 
160         sb = new StringBuilder();
161 
162         sb.append("update DLFileRank set groupId = (select groupId from ");
163         sb.append("DLFolder where DLFolder.folderId = DLFileRank.folderId)");
164 
165         runSQL(sb.toString());
166 
167         sb = new StringBuilder();
168 
169         sb.append("update DLFileShortcut set groupId = (select groupId from ");
170         sb.append("DLFolder where DLFolder.folderId = ");
171         sb.append("DLFileShortcut.folderId)");
172 
173         runSQL(sb.toString());
174 
175         sb = new StringBuilder();
176 
177         sb.append("update DLFileVersion set groupId = (select groupId from ");
178         sb.append("DLFolder where DLFolder.folderId = DLFileVersion.folderId)");
179 
180         runSQL(sb.toString());
181     }
182 
183     protected void updatePortletPreferences() throws Exception {
184         Connection con = null;
185         PreparedStatement ps = null;
186         ResultSet rs = null;
187 
188         try {
189             con = DataAccess.getConnection();
190 
191             ps = con.prepareStatement(
192                 "select portletPreferencesId, ownerId, ownerType, plid, " +
193                     "portletId, preferences from PortletPreferences where " +
194                         "portletId = '20' and preferences like " +
195                             "'%<name>fileEntryColumns</name><value></value>%'");
196 
197             rs = ps.executeQuery();
198 
199             while (rs.next()) {
200                 long portletPreferencesId = rs.getLong("portletPreferencesId");
201                 long ownerId = rs.getLong("ownerId");
202                 int ownerType = rs.getInt("ownerType");
203                 long plid = rs.getLong("plid");
204                 String portletId = rs.getString("portletId");
205                 String preferences = rs.getString("preferences");
206 
207                 Object[] layout = getLayout(plid);
208 
209                 if (layout != null) {
210                     long companyId = (Long)layout[0];
211 
212                     String newPreferences = upgradePreferences(
213                         companyId, ownerId, ownerType, plid, portletId,
214                         preferences);
215 
216                     updatePortletPreferences(
217                         portletPreferencesId, newPreferences);
218                 }
219                 else {
220                     deletePortletPreferences(portletPreferencesId);
221                 }
222             }
223         }
224         finally {
225             DataAccess.cleanUp(con, ps, rs);
226         }
227     }
228 
229     protected void updatePortletPreferences(
230             long portletPreferencesId, String preferences)
231         throws Exception {
232 
233         Connection con = null;
234         PreparedStatement ps = null;
235 
236         try {
237             con = DataAccess.getConnection();
238 
239             ps = con.prepareStatement(
240                 "update PortletPreferences set preferences = ? where " +
241                     "portletPreferencesId = " + portletPreferencesId);
242 
243             ps.setString(1, preferences);
244 
245             ps.executeUpdate();
246         }
247         finally {
248             DataAccess.cleanUp(con, ps);
249         }
250     }
251 
252     protected String upgradePreferences(
253             long companyId, long ownerId, int ownerType, long plid,
254             String portletId, String xml)
255         throws Exception {
256 
257         PortletPreferencesImpl preferences =
258             PortletPreferencesSerializer.fromXML(
259                 companyId, ownerId, ownerType, plid, portletId, xml);
260 
261         String fileEntryColumns = preferences.getValue(
262             "fileEntryColumns", StringPool.BLANK);
263 
264         if (Validator.isNull(fileEntryColumns)) {
265             preferences.reset("fileEntryColumns");
266         }
267 
268         return PortletPreferencesSerializer.toXML(preferences);
269     }
270 
271     private static final String _GET_LAYOUT =
272         "select companyId from Layout where plid = ?";
273 
274 }