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.v5_2_8_to_6_0_5;
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.util.LoggingTimer;
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    
035    import java.sql.PreparedStatement;
036    import java.sql.ResultSet;
037    import java.sql.Timestamp;
038    
039    /**
040     * @author Jorge Ferrer
041     * @author Alexander Chow
042     * @author Douglas Wong
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                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
092                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
093                            nameColumn, "title");
094                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
095                            "version");
096    
097                    upgradeTable(
098                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
099                            DLFileEntryTable.TABLE_SQL_CREATE,
100                            DLFileEntryTable.TABLE_SQL_ADD_INDEXES, nameColumn, titleColumn,
101                            versionColumn);
102    
103                    // DLFileRank
104    
105                    upgradeTable(
106                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
107                            DLFileRankTable.TABLE_SQL_CREATE,
108                            DLFileRankTable.TABLE_SQL_ADD_INDEXES, nameColumn);
109    
110                    // DLFileShortcut
111    
112                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
113                            "toName");
114    
115                    upgradeTable(
116                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
117                            DLFileShortcutTable.TABLE_SQL_CREATE,
118                            DLFileShortcutTable.TABLE_SQL_ADD_INDEXES, toNameColumn);
119    
120                    // DLFileVersion
121    
122                    String tableSqlCreate = StringUtil.replace(
123                            DLFileVersionTable.TABLE_SQL_CREATE,
124                            new String[] {
125                                    ",extraSettings VARCHAR(75) null",
126                                    ",title VARCHAR(75) null"
127                            },
128                            new String[] {
129                                    ",extraSettings STRING null",
130                                    ",title VARCHAR(255) null"
131                            });
132    
133                    upgradeTable(
134                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
135                            tableSqlCreate, DLFileVersionTable.TABLE_SQL_ADD_INDEXES,
136                            nameColumn, versionColumn);
137            }
138    
139            protected void synchronizeFileVersions() throws Exception {
140                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
141                            StringBundler sb = new StringBundler(5);
142    
143                            sb.append("select * from DLFileEntry dlFileEntry where version ");
144                            sb.append("not in (select version from DLFileVersion ");
145                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
146                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
147                            sb.append("dlFileEntry.name))");
148    
149                            String sql = sb.toString();
150    
151                            try (PreparedStatement ps = connection.prepareStatement(sql);
152                                    ResultSet rs = ps.executeQuery()) {
153    
154                                    while (rs.next()) {
155                                            long companyId = rs.getLong("companyId");
156                                            long groupId = rs.getLong("groupId");
157                                            long userId = rs.getLong("userId");
158                                            String userName = rs.getString("userName");
159                                            long folderId = rs.getLong("folderId");
160                                            String name = rs.getString("name");
161                                            double version = rs.getDouble("version");
162                                            int size = rs.getInt("size_");
163    
164                                            addFileVersion(
165                                                    groupId, companyId, userId, userName, folderId, name,
166                                                    version, size);
167                                    }
168                            }
169                    }
170            }
171    
172            protected void updateFileEntries() throws Exception {
173                    try (LoggingTimer loggingTimer = new LoggingTimer();
174                            PreparedStatement ps = connection.prepareStatement(
175                                    "select * from DLFileEntry");
176                            ResultSet rs = ps.executeQuery()) {
177    
178                            while (rs.next()) {
179                                    long companyId = rs.getLong("companyId");
180                                    long groupId = rs.getLong("groupId");
181                                    long folderId = rs.getLong("folderId");
182                                    String name = rs.getString("name");
183    
184                                    long repositoryId = folderId;
185    
186                                    if (repositoryId ==
187                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
188    
189                                            repositoryId = groupId;
190                                    }
191    
192                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
193                                            name);
194    
195                                    if (!newName.equals(name)) {
196                                            try {
197                                                    DLStoreUtil.updateFile(
198                                                            companyId, repositoryId, name, newName);
199                                            }
200                                            catch (Exception e) {
201                                                    if (_log.isWarnEnabled()) {
202                                                            _log.warn("Unable to update file for " + name, e);
203                                                    }
204                                            }
205                                    }
206                            }
207                    }
208            }
209    
210            private static final Log _log = LogFactoryUtil.getLog(
211                    UpgradeDocumentLibrary.class);
212    
213    }