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