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.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.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
156                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
157    
158                    upgradeTable.updateTable();
159    
160                    // DLFileRank
161    
162                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
163                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
164                            nameColumn);
165    
166                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
167                    upgradeTable.setIndexesSQL(DLFileRankTable.TABLE_SQL_ADD_INDEXES);
168    
169                    upgradeTable.updateTable();
170    
171                    // DLFileShortcut
172    
173                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
174                            "toName");
175    
176                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
177                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
178                            toNameColumn);
179    
180                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
181                    upgradeTable.setIndexesSQL(DLFileShortcutTable.TABLE_SQL_ADD_INDEXES);
182    
183                    upgradeTable.updateTable();
184    
185                    // DLFileVersion
186    
187                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
188                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
189                            nameColumn, versionColumn);
190    
191                    upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
192                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
193    
194                    upgradeTable.updateTable();
195            }
196    
197            protected void synchronizeFileVersions() throws Exception {
198                    Connection con = null;
199                    PreparedStatement ps = null;
200                    ResultSet rs = null;
201    
202                    try {
203                            con = DataAccess.getUpgradeOptimizedConnection();
204    
205                            StringBundler sb = new StringBundler(5);
206    
207                            sb.append("select * from DLFileEntry dlFileEntry where version ");
208                            sb.append("not in (select version from DLFileVersion ");
209                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
210                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
211                            sb.append("dlFileEntry.name))");
212    
213                            String sql = sb.toString();
214    
215                            ps = con.prepareStatement(sql);
216    
217                            rs = ps.executeQuery();
218    
219                            while (rs.next()) {
220                                    long companyId = rs.getLong("companyId");
221                                    long groupId = rs.getLong("groupId");
222                                    long userId = rs.getLong("userId");
223                                    String userName = rs.getString("userName");
224                                    long folderId = rs.getLong("folderId");
225                                    String name = rs.getString("name");
226                                    double version = rs.getDouble("version");
227                                    int size = rs.getInt("size_");
228    
229                                    addFileVersion(
230                                            groupId, companyId, userId, userName, folderId, name,
231                                            version, size);
232                            }
233                    }
234                    finally {
235                            DataAccess.cleanUp(con, ps);
236                    }
237            }
238    
239            private static Log _log = LogFactoryUtil.getLog(
240                    UpgradeDocumentLibrary.class);
241    
242    }