001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upgrade.v5_2_8_to_6_0_5;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileEntryTable;
025    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileVersionTable;
026    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
027    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTitleUpgradeColumnImpl;
028    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryVersionUpgradeColumnImpl;
029    import com.liferay.portal.upgrade.v6_0_0.util.DLFileRankTable;
030    import com.liferay.portal.upgrade.v6_0_0.util.DLFileShortcutTable;
031    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
032    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
033    
034    import java.sql.Connection;
035    import java.sql.PreparedStatement;
036    import java.sql.ResultSet;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Alexander Chow
041     * @author Douglas Wong
042     */
043    public class UpgradeDocumentLibrary extends UpgradeProcess {
044    
045            @Override
046            protected void doUpgrade() throws Exception {
047                    Connection con = null;
048                    PreparedStatement ps = null;
049                    ResultSet rs = null;
050    
051                    try {
052                            con = DataAccess.getConnection();
053    
054                            ps = con.prepareStatement("select * from DLFileEntry");
055    
056                            rs = ps.executeQuery();
057    
058                            while (rs.next()) {
059                                    long companyId = rs.getLong("companyId");
060                                    long groupId = rs.getLong("groupId");
061                                    long folderId = rs.getLong("folderId");
062                                    String name = rs.getString("name");
063    
064                                    long repositoryId = folderId;
065    
066                                    if (repositoryId ==
067                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
068    
069                                            repositoryId = groupId;
070                                    }
071    
072                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
073                                            name);
074    
075                                    if (!newName.equals(name)) {
076                                            DLStoreUtil.updateFile(
077                                                    companyId, repositoryId, name, newName);
078                                    }
079                            }
080                    }
081                    finally {
082                            DataAccess.cleanUp(con, ps, rs);
083                    }
084    
085                    // DLFileEntry
086    
087                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
088                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
089                            nameColumn, "title");
090                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
091                            "version");
092    
093                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
094                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
095                            nameColumn, titleColumn, versionColumn);
096    
097                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
098                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
099    
100                    upgradeTable.updateTable();
101    
102                    // DLFileRank
103    
104                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
105                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
106                            nameColumn);
107    
108                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
109                    upgradeTable.setIndexesSQL(DLFileRankTable.TABLE_SQL_ADD_INDEXES);
110    
111                    upgradeTable.updateTable();
112    
113                    // DLFileShortcut
114    
115                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
116                            "toName");
117    
118                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
119                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
120                            toNameColumn);
121    
122                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
123                    upgradeTable.setIndexesSQL(DLFileShortcutTable.TABLE_SQL_ADD_INDEXES);
124    
125                    upgradeTable.updateTable();
126    
127                    // DLFileVersion
128    
129                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
130                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
131                            nameColumn, versionColumn);
132    
133                    upgradeTable.setCreateSQL(
134                            StringUtil.replace(
135                                    DLFileVersionTable.TABLE_SQL_CREATE,
136                                    new String[] {
137                                            ",extraSettings VARCHAR(75) null",
138                                            ",title VARCHAR(75) null"
139                                    },
140                                    new String[] {
141                                            ",extraSettings STRING null",
142                                            ",title VARCHAR(255) null"
143                                    }));
144    
145                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
146    
147                    upgradeTable.updateTable();
148    
149                    // File entries
150    
151                    updateFileEntries();
152    
153                    // File versions
154    
155                    updateFileVersions();
156            }
157    
158            protected long getLatestFileVersionId(long folderId, String name)
159                    throws Exception {
160    
161                    Connection con = null;
162                    PreparedStatement ps = null;
163                    ResultSet rs = null;
164    
165                    long fileVersionId = 0;
166    
167                    try {
168                            con = DataAccess.getConnection();
169    
170                            ps = con.prepareStatement(
171                                    "select fileVersionId from DLFileVersion where folderId = ? " +
172                                            "and name = ? order by version desc");
173    
174                            ps.setLong(1, folderId);
175                            ps.setString(2, name);
176    
177                            rs = ps.executeQuery();
178    
179                            if (rs.next()) {
180                                    fileVersionId = rs.getLong("fileVersionId");
181                            }
182                    }
183                    finally {
184                            DataAccess.cleanUp(con, ps, rs);
185                    }
186    
187                    return fileVersionId;
188            }
189    
190            protected void updateFileEntries() throws Exception {
191                    Connection con = null;
192                    PreparedStatement ps = null;
193                    ResultSet rs = null;
194    
195                    try {
196                            con = DataAccess.getConnection();
197    
198                            ps = con.prepareStatement(
199                                    "select uuid_, fileEntryId, groupId, folderId, name, title " +
200                                            "from DLFileEntry");
201    
202                            rs = ps.executeQuery();
203    
204                            while (rs.next()) {
205                                    String uuid_ = rs.getString("uuid_");
206                                    long fileEntryId = rs.getLong("fileEntryId");
207                                    long groupId = rs.getLong("groupId");
208                                    long folderId = rs.getLong("folderId");
209                                    String name = rs.getString("name");
210                                    String title = rs.getString("title");
211    
212                                    String extension = FileUtil.getExtension(title);
213    
214                                    runSQL(
215                                            "update DLFileEntry set extension = '" + extension +
216                                                    "' where uuid_ = '" + uuid_ + "' and groupId = " +
217                                                            groupId);
218    
219                                    long fileVersionId = getLatestFileVersionId(folderId, name);
220    
221                                    runSQL(
222                                            "update ExpandoRow set classPK = " + fileVersionId +
223                                                     " where classPK = " + fileEntryId);
224    
225                                    runSQL(
226                                            "update ExpandoValue set classPK = " + fileVersionId +
227                                                     " where classPK = " + fileEntryId);
228                            }
229                    }
230                    finally {
231                            DataAccess.cleanUp(con, ps, rs);
232                    }
233            }
234    
235            protected void updateFileVersion(
236                            long fileVersionId, String extension, String title, String
237                            description, String extraSettings)
238                    throws Exception {
239    
240                    Connection con = null;
241                    PreparedStatement ps = null;
242    
243                    try {
244                            con = DataAccess.getConnection();
245    
246                            ps = con.prepareStatement(
247                                    "update DLFileVersion set extension = ?, title = ?, " +
248                                            "description = ?, extraSettings = ? where fileVersionId " +
249                                                    "= ?");
250    
251                            ps.setString(1, extension);
252                            ps.setString(2, title);
253                            ps.setString(3, description);
254                            ps.setString(4, extraSettings);
255                            ps.setLong(5, fileVersionId);
256    
257                            ps.executeUpdate();
258                    }
259                    finally {
260                            DataAccess.cleanUp(con, ps);
261                    }
262            }
263    
264            protected void updateFileVersions() throws Exception {
265                    Connection con = null;
266                    PreparedStatement ps = null;
267                    ResultSet rs = null;
268    
269                    try {
270                            con = DataAccess.getConnection();
271    
272                            ps = con.prepareStatement(
273                                    "select folderId, name, extension, title, description, " +
274                                            "extraSettings from DLFileEntry");
275    
276                            rs = ps.executeQuery();
277    
278                            while (rs.next()) {
279                                    long folderId = rs.getLong("folderId");
280                                    String name = rs.getString("name");
281                                    String extension = rs.getString("extension");
282                                    String title = rs.getString("title");
283                                    String description = rs.getString("description");
284                                    String extraSettings = rs.getString("extraSettings");
285    
286                                    long fileVersionId = getLatestFileVersionId(folderId, name);
287    
288                                    updateFileVersion(
289                                            fileVersionId, extension, title, description,
290                                            extraSettings);
291                            }
292                    }
293                    finally {
294                            DataAccess.cleanUp(con, ps, rs);
295                    }
296            }
297    
298    }