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