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