001    /**
002     * Copyright (c) 2000-present 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.v6_0_0;
016    
017    import com.liferay.document.library.kernel.model.DLFolderConstants;
018    import com.liferay.document.library.kernel.store.DLStoreUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
023    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
024    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
025    import com.liferay.portal.kernel.util.LoggingTimer;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
029    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTable;
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.portal.upgrade.v6_0_0.util.DLFileVersionTable;
035    
036    import java.sql.PreparedStatement;
037    import java.sql.ResultSet;
038    import java.sql.Timestamp;
039    
040    /**
041     * @author Jorge Ferrer
042     * @author Alexander Chow
043     */
044    public class UpgradeDocumentLibrary extends UpgradeProcess {
045    
046            protected void addFileVersion(
047                            long groupId, long companyId, long userId, String userName,
048                            long folderId, String name, double version, int size)
049                    throws Exception {
050    
051                    Timestamp now = new Timestamp(System.currentTimeMillis());
052    
053                    StringBundler sb = new StringBundler(5);
054    
055                    sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
056                    sb.append("companyId, userId, userName, createDate, folderId, name, ");
057                    sb.append("version, size_, status, statusByUserId, ");
058                    sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ?, ");
059                    sb.append("?, ?, ?, ?, ?, ?, ?, ?)");
060    
061                    String sql = sb.toString();
062    
063                    try (PreparedStatement ps = connection.prepareStatement(sql)) {
064                            ps.setLong(1, increment());
065                            ps.setLong(2, groupId);
066                            ps.setLong(3, companyId);
067                            ps.setLong(4, userId);
068                            ps.setString(5, userName);
069                            ps.setTimestamp(6, now);
070                            ps.setLong(7, folderId);
071                            ps.setString(8, name);
072                            ps.setDouble(9, version);
073                            ps.setInt(10, size);
074                            ps.setInt(11, WorkflowConstants.STATUS_APPROVED);
075                            ps.setLong(12, userId);
076                            ps.setString(13, userName);
077                            ps.setTimestamp(14, now);
078    
079                            ps.executeUpdate();
080                    }
081            }
082    
083            @Override
084            protected void doUpgrade() throws Exception {
085                    updateFileEntries();
086    
087                    synchronizeFileVersions();
088    
089                    // DLFileEntry
090    
091                    updateDLFileEntryTable();
092    
093                    // DLFileRank
094    
095                    upgradeTable(
096                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
097                            DLFileRankTable.TABLE_SQL_CREATE,
098                            DLFileRankTable.TABLE_SQL_ADD_INDEXES,
099                            new DLFileEntryNameUpgradeColumnImpl("name"));
100    
101                    // DLFileShortcut
102    
103                    upgradeTable(
104                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
105                            DLFileShortcutTable.TABLE_SQL_CREATE,
106                            DLFileShortcutTable.TABLE_SQL_ADD_INDEXES,
107                            new DLFileEntryNameUpgradeColumnImpl("toName"));
108    
109                    // DLFileVersion
110    
111                    upgradeTable(
112                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
113                            DLFileVersionTable.TABLE_SQL_CREATE,
114                            DLFileVersionTable.TABLE_SQL_ADD_INDEXES,
115                            new DLFileEntryNameUpgradeColumnImpl("name"),
116                            new DLFileEntryVersionUpgradeColumnImpl("version"));
117            }
118    
119            protected void synchronizeFileVersions() throws Exception {
120                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
121                            StringBundler sb = new StringBundler(5);
122    
123                            sb.append("select * from DLFileEntry dlFileEntry where version ");
124                            sb.append("not in (select version from DLFileVersion ");
125                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
126                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
127                            sb.append("dlFileEntry.name))");
128    
129                            String sql = sb.toString();
130    
131                            try (PreparedStatement ps = connection.prepareStatement(sql);
132                                    ResultSet rs = ps.executeQuery()) {
133    
134                                    while (rs.next()) {
135                                            long companyId = rs.getLong("companyId");
136                                            long groupId = rs.getLong("groupId");
137                                            long userId = rs.getLong("userId");
138                                            String userName = rs.getString("userName");
139                                            long folderId = rs.getLong("folderId");
140                                            String name = rs.getString("name");
141                                            double version = rs.getDouble("version");
142                                            int size = rs.getInt("size_");
143    
144                                            addFileVersion(
145                                                    groupId, companyId, userId, userName, folderId, name,
146                                                    version, size);
147                                    }
148                            }
149                    }
150            }
151    
152            protected void updateDLFileEntryTable() throws Exception {
153                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
154                            UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl(
155                                    "name");
156                            UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
157                                    nameColumn, "title");
158                            UpgradeColumn versionColumn =
159                                    new DLFileEntryVersionUpgradeColumnImpl("version");
160    
161                            UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
162                                    DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
163                                    nameColumn, titleColumn, versionColumn);
164    
165                            upgradeTable.setAllowUniqueIndexes(true);
166                            upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
167                            upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
168    
169                            upgradeTable.updateTable();
170                    }
171            }
172    
173            protected void updateFileEntries() throws Exception {
174                    try (LoggingTimer loggingTimer = new LoggingTimer();
175                            PreparedStatement ps = connection.prepareStatement(
176                                    "select * from DLFileEntry");
177                            ResultSet rs = ps.executeQuery()) {
178    
179                            while (rs.next()) {
180                                    long companyId = rs.getLong("companyId");
181                                    long groupId = rs.getLong("groupId");
182                                    long folderId = rs.getLong("folderId");
183                                    String name = rs.getString("name");
184    
185                                    long repositoryId = folderId;
186    
187                                    if (repositoryId ==
188                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
189    
190                                            repositoryId = groupId;
191                                    }
192    
193                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
194                                            name);
195    
196                                    if (!newName.equals(name)) {
197                                            try {
198                                                    DLStoreUtil.updateFile(
199                                                            companyId, repositoryId, name, newName);
200                                            }
201                                            catch (Exception e) {
202                                                    if (_log.isWarnEnabled()) {
203                                                            _log.warn("Unable to update file for " + name, e);
204                                                    }
205                                            }
206                                    }
207                            }
208                    }
209            }
210    
211            private static final Log _log = LogFactoryUtil.getLog(
212                    UpgradeDocumentLibrary.class);
213    
214    }