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_3;
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.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.upgrade.v5_2_3.util.DLFileEntryTable;
25  import com.liferay.portal.upgrade.v5_2_3.util.DLFileRankTable;
26  import com.liferay.portal.upgrade.v5_2_3.util.DLFileShortcutTable;
27  import com.liferay.portal.upgrade.v5_2_3.util.DLFileVersionTable;
28  import com.liferay.portlet.PortletPreferencesImpl;
29  import com.liferay.portlet.PortletPreferencesSerializer;
30  
31  import java.sql.Connection;
32  import java.sql.PreparedStatement;
33  import java.sql.ResultSet;
34  
35  /**
36   * <a href="UpgradeDocumentLibrary.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Samuel Kong
39   * @author Brian Wing Shun Chan
40   * @author Douglas Wong
41   */
42  public class UpgradeDocumentLibrary 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          try {
54              runSQL("alter_column_type DLFileEntry name VARCHAR(255) null");
55          }
56          catch (Exception e) {
57  
58              // DLFileEntry
59  
60              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
61                  DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS);
62  
63              upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
64  
65              upgradeTable.updateTable();
66          }
67  
68          try {
69              runSQL("alter_column_type DLFileRank name VARCHAR(255) null");
70          }
71          catch (Exception e) {
72  
73              // DLFileRank
74  
75              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
76                  DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS);
77  
78              upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
79  
80              upgradeTable.updateTable();
81          }
82  
83          try {
84              runSQL("alter_column_type DLFileShortcut toName VARCHAR(255) null");
85          }
86          catch (Exception e) {
87  
88              // DLFileShortcut
89  
90              UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
91                  DLFileShortcutTable.TABLE_NAME,
92                  DLFileShortcutTable.TABLE_COLUMNS);
93  
94              upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
95  
96              upgradeTable.updateTable();
97          }
98  
99          try {
100             runSQL("alter_column_type DLFileVersion name VARCHAR(255) null");
101         }
102         catch (Exception e) {
103 
104             // DLFileVersion
105 
106             UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
107                 DLFileVersionTable.TABLE_NAME,
108                 DLFileVersionTable.TABLE_COLUMNS);
109 
110             upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
111 
112             upgradeTable.updateTable();
113         }
114 
115         // groupId
116 
117         updateGroupId();
118 
119         // PortletPreferences
120 
121         updatePortletPreferences();
122     }
123 
124     protected Object[] getLayout(long plid) throws Exception {
125         Object[] layout = null;
126 
127         Connection con = null;
128         PreparedStatement ps = null;
129         ResultSet rs = null;
130 
131         try {
132             con = DataAccess.getConnection();
133 
134             ps = con.prepareStatement(_GET_LAYOUT);
135 
136             ps.setLong(1, plid);
137 
138             rs = ps.executeQuery();
139 
140             while (rs.next()) {
141                 long companyId = rs.getLong("companyId");
142 
143                 layout = new Object[] {companyId};
144             }
145         }
146         finally {
147             DataAccess.cleanUp(con, ps, rs);
148         }
149 
150         return layout;
151     }
152 
153     protected void updateGroupId() throws Exception {
154         StringBundler sb = new StringBundler(3);
155 
156         sb.append("update DLFileEntry set groupId = (select groupId from ");
157         sb.append("DLFolder where DLFolder.folderId = DLFileEntry.folderId)");
158 
159         runSQL(sb.toString());
160 
161         sb.setIndex(0);
162 
163         sb.append("update DLFileRank set groupId = (select groupId from ");
164         sb.append("DLFolder where DLFolder.folderId = DLFileRank.folderId)");
165 
166         runSQL(sb.toString());
167 
168         sb.setIndex(0);
169 
170         sb.append("update DLFileShortcut set groupId = (select groupId from ");
171         sb.append("DLFolder where DLFolder.folderId = ");
172         sb.append("DLFileShortcut.folderId)");
173 
174         runSQL(sb.toString());
175 
176         sb.setIndex(0);
177 
178         sb.append("update DLFileVersion set groupId = (select groupId from ");
179         sb.append("DLFolder where DLFolder.folderId = DLFileVersion.folderId)");
180 
181         runSQL(sb.toString());
182     }
183 
184     protected void updatePortletPreferences() throws Exception {
185         Connection con = null;
186         PreparedStatement ps = null;
187         ResultSet rs = null;
188 
189         try {
190             con = DataAccess.getConnection();
191 
192             ps = con.prepareStatement(
193                 "select portletPreferencesId, ownerId, ownerType, plid, " +
194                     "portletId, preferences from PortletPreferences where " +
195                         "portletId = '20' and preferences like " +
196                             "'%<name>fileEntryColumns</name><value></value>%'");
197 
198             rs = ps.executeQuery();
199 
200             while (rs.next()) {
201                 long portletPreferencesId = rs.getLong("portletPreferencesId");
202                 long ownerId = rs.getLong("ownerId");
203                 int ownerType = rs.getInt("ownerType");
204                 long plid = rs.getLong("plid");
205                 String portletId = rs.getString("portletId");
206                 String preferences = rs.getString("preferences");
207 
208                 Object[] layout = getLayout(plid);
209 
210                 if (layout != null) {
211                     long companyId = (Long)layout[0];
212 
213                     String newPreferences = upgradePreferences(
214                         companyId, ownerId, ownerType, plid, portletId,
215                         preferences);
216 
217                     updatePortletPreferences(
218                         portletPreferencesId, newPreferences);
219                 }
220                 else {
221                     deletePortletPreferences(portletPreferencesId);
222                 }
223             }
224         }
225         finally {
226             DataAccess.cleanUp(con, ps, rs);
227         }
228     }
229 
230     protected void updatePortletPreferences(
231             long portletPreferencesId, String preferences)
232         throws Exception {
233 
234         Connection con = null;
235         PreparedStatement ps = null;
236 
237         try {
238             con = DataAccess.getConnection();
239 
240             ps = con.prepareStatement(
241                 "update PortletPreferences set preferences = ? where " +
242                     "portletPreferencesId = " + portletPreferencesId);
243 
244             ps.setString(1, preferences);
245 
246             ps.executeUpdate();
247         }
248         finally {
249             DataAccess.cleanUp(con, ps);
250         }
251     }
252 
253     protected String upgradePreferences(
254             long companyId, long ownerId, int ownerType, long plid,
255             String portletId, String xml)
256         throws Exception {
257 
258         PortletPreferencesImpl preferences =
259             PortletPreferencesSerializer.fromXML(
260                 companyId, ownerId, ownerType, plid, portletId, xml);
261 
262         String fileEntryColumns = preferences.getValue(
263             "fileEntryColumns", StringPool.BLANK);
264 
265         if (Validator.isNull(fileEntryColumns)) {
266             preferences.reset("fileEntryColumns");
267         }
268 
269         return PortletPreferencesSerializer.toXML(preferences);
270     }
271 
272     private static final String _GET_LAYOUT =
273         "select * from Layout where plid = ?";
274 
275 }