001    /**
002     * Copyright (c) 2000-2013 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_9_to_6_0_11;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
023    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileEntryTable;
028    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileVersionTable;
029    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
030    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTitleUpgradeColumnImpl;
031    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryVersionUpgradeColumnImpl;
032    import com.liferay.portal.upgrade.v6_0_0.util.DLFileRankTable;
033    import com.liferay.portal.upgrade.v6_0_0.util.DLFileShortcutTable;
034    import com.liferay.portlet.documentlibrary.NoSuchFileException;
035    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
036    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
037    
038    import java.sql.Connection;
039    import java.sql.PreparedStatement;
040    import java.sql.ResultSet;
041    import java.sql.Timestamp;
042    
043    /**
044     * @author Jorge Ferrer
045     * @author Alexander Chow
046     * @author Douglas Wong
047     */
048    public class UpgradeDocumentLibrary extends UpgradeProcess {
049    
050            protected void addFileVersion(
051                            long groupId, long companyId, long userId, String userName,
052                            long folderId, String name, String title, double version, int size,
053                            long fileEntryId)
054                    throws Exception {
055    
056                    Timestamp now = new Timestamp(System.currentTimeMillis());
057    
058                    Connection con = null;
059                    PreparedStatement ps = null;
060    
061                    try {
062                            con = DataAccess.getUpgradeOptimizedConnection();
063    
064                            StringBundler sb = new StringBundler(5);
065    
066                            sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
067                            sb.append("companyId, userId, userName, createDate, folderId, ");
068                            sb.append("name, title, version, size_, status, statusByUserId, ");
069                            sb.append("statusByUserName, statusDate, fileEntryId) values ");
070                            sb.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
071    
072                            String sql = sb.toString();
073    
074                            ps = con.prepareStatement(sql);
075    
076                            ps.setLong(1, increment());
077                            ps.setLong(2, groupId);
078                            ps.setLong(3, companyId);
079                            ps.setLong(4, userId);
080                            ps.setString(5, userName);
081                            ps.setTimestamp(6, now);
082                            ps.setLong(7, folderId);
083                            ps.setString(8, name);
084                            ps.setString(9, title);
085                            ps.setDouble(10, version);
086                            ps.setInt(11, size);
087                            ps.setInt(12, WorkflowConstants.STATUS_APPROVED);
088                            ps.setLong(13, userId);
089                            ps.setString(14, userName);
090                            ps.setTimestamp(15, now);
091                            ps.setLong(16, fileEntryId);
092    
093                            ps.executeUpdate();
094                    }
095                    finally {
096                            DataAccess.cleanUp(con, ps);
097                    }
098            }
099    
100            @Override
101            protected void doUpgrade() throws Exception {
102                    Connection con = null;
103                    PreparedStatement ps = null;
104                    ResultSet rs = null;
105    
106                    try {
107                            con = DataAccess.getUpgradeOptimizedConnection();
108    
109                            ps = con.prepareStatement("select * from DLFileEntry");
110    
111                            rs = ps.executeQuery();
112    
113                            while (rs.next()) {
114                                    long companyId = rs.getLong("companyId");
115                                    long groupId = rs.getLong("groupId");
116                                    long folderId = rs.getLong("folderId");
117                                    String name = rs.getString("name");
118    
119                                    long repositoryId = folderId;
120    
121                                    if (repositoryId ==
122                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
123    
124                                            repositoryId = groupId;
125                                    }
126    
127                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
128                                            name);
129    
130                                    if (!newName.equals(name)) {
131                                            try {
132                                                    DLStoreUtil.updateFile(
133                                                            companyId, repositoryId, name, newName);
134                                            }
135                                            catch (NoSuchFileException nsfe) {
136                                                    if (_log.isWarnEnabled()) {
137                                                            _log.warn(
138                                                                    "Unable to update file for " + name, nsfe);
139                                                    }
140                                            }
141                                    }
142                            }
143                    }
144                    finally {
145                            DataAccess.cleanUp(con, ps, rs);
146                    }
147    
148                    synchronizeFileVersions();
149    
150                    // DLFileEntry
151    
152                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
153                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
154                            nameColumn, "title");
155                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
156                            "version");
157    
158                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
159                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
160                            nameColumn, titleColumn, versionColumn);
161    
162                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
163                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
164    
165                    upgradeTable.updateTable();
166    
167                    // DLFileRank
168    
169                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
170                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
171                            nameColumn);
172    
173                    upgradeTable.setCreateSQL(
174                            StringUtil.replace(
175                                    DLFileRankTable.TABLE_SQL_CREATE, ",createDate DATE null",
176                                    ",createDate DATE null,fileEntryId LONG"));
177                    upgradeTable.setIndexesSQL(DLFileRankTable.TABLE_SQL_ADD_INDEXES);
178    
179                    upgradeTable.updateTable();
180    
181                    // DLFileShortcut
182    
183                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
184                            "toName");
185    
186                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
187                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
188                            toNameColumn);
189    
190                    upgradeTable.setCreateSQL(
191                            StringUtil.replace(
192                                    DLFileShortcutTable.TABLE_SQL_CREATE, ",folderId LONG",
193                                    ",folderId LONG,toFileEntryId LONG"));
194                    upgradeTable.setIndexesSQL(DLFileShortcutTable.TABLE_SQL_ADD_INDEXES);
195    
196                    upgradeTable.updateTable();
197    
198                    // DLFileVersion
199    
200                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
201                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
202                            nameColumn, versionColumn);
203    
204                    upgradeTable.setCreateSQL(
205                            StringUtil.replace(
206                                    DLFileVersionTable.TABLE_SQL_CREATE, ",title VARCHAR(75) null",
207                                    ",title VARCHAR(255) null,fileEntryId LONG"));
208                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
209    
210                    upgradeTable.updateTable();
211    
212                    try {
213                            runSQL("drop index IX_CE705D48 on DLFileRank");
214                            runSQL("drop index IX_40B56512 on DLFileRank");
215    
216                            runSQL("drop index IX_55C736AC on DLFileShortcut");
217                            runSQL("drop index IX_346A0992 on DLFileShortcut");
218    
219                            runSQL("drop index IX_B413F1EC on DLFileVersion");
220                            runSQL("drop index IX_94E784D2 on DLFileVersion");
221                            runSQL("drop index IX_2F8FED9C on DLFileVersion");
222                    }
223                    catch (Exception e) {
224                            _log.error(e, e);
225                    }
226            }
227    
228            protected void synchronizeFileVersions() throws Exception {
229                    Connection con = null;
230                    PreparedStatement ps = null;
231                    ResultSet rs = null;
232    
233                    try {
234                            con = DataAccess.getUpgradeOptimizedConnection();
235    
236                            StringBundler sb = new StringBundler(5);
237    
238                            sb.append("select * from DLFileEntry dlFileEntry where version ");
239                            sb.append("not in (select version from DLFileVersion ");
240                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
241                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
242                            sb.append("dlFileEntry.name))");
243    
244                            String sql = sb.toString();
245    
246                            ps = con.prepareStatement(sql);
247    
248                            rs = ps.executeQuery();
249    
250                            while (rs.next()) {
251                                    long companyId = rs.getLong("companyId");
252                                    long fileEntryId = rs.getLong("fileEntryId");
253                                    long groupId = rs.getLong("groupId");
254                                    long userId = rs.getLong("userId");
255                                    String userName = rs.getString("userName");
256                                    long folderId = rs.getLong("folderId");
257                                    String name = rs.getString("name");
258                                    String title = rs.getString("title");
259                                    double version = rs.getDouble("version");
260                                    int size = rs.getInt("size_");
261    
262                                    addFileVersion(
263                                            groupId, companyId, userId, userName, folderId, name, title,
264                                            version, size, fileEntryId);
265                            }
266                    }
267                    finally {
268                            DataAccess.cleanUp(con, ps);
269                    }
270            }
271    
272            private static Log _log = LogFactoryUtil.getLog(
273                    UpgradeDocumentLibrary.class);
274    
275    }