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