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