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