001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v5_2_9_to_6_0_11;
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, String title, double version, int size,
052                            long fileEntryId)
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, title, version, size_, status, statusByUserId, ");
068                            sb.append("statusByUserName, statusDate, fileEntryId) 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.setString(9, title);
084                            ps.setDouble(10, version);
085                            ps.setInt(11, size);
086                            ps.setInt(12, WorkflowConstants.STATUS_APPROVED);
087                            ps.setLong(13, userId);
088                            ps.setString(14, userName);
089                            ps.setTimestamp(15, now);
090                            ps.setLong(16, fileEntryId);
091    
092                            ps.executeUpdate();
093                    }
094                    finally {
095                            DataAccess.cleanUp(con, ps);
096                    }
097            }
098    
099            @Override
100            protected void doUpgrade() throws Exception {
101                    Connection con = null;
102                    PreparedStatement ps = null;
103                    ResultSet rs = null;
104    
105                    try {
106                            con = DataAccess.getUpgradeOptimizedConnection();
107    
108                            ps = con.prepareStatement("select * from DLFileEntry");
109    
110                            rs = ps.executeQuery();
111    
112                            while (rs.next()) {
113                                    long companyId = rs.getLong("companyId");
114                                    long groupId = rs.getLong("groupId");
115                                    long folderId = rs.getLong("folderId");
116                                    String name = rs.getString("name");
117    
118                                    long repositoryId = folderId;
119    
120                                    if (repositoryId ==
121                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
122    
123                                            repositoryId = groupId;
124                                    }
125    
126                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
127                                            name);
128    
129                                    if (!newName.equals(name)) {
130                                            DLStoreUtil.updateFile(
131                                                    companyId, repositoryId, name, newName);
132                                    }
133                            }
134                    }
135                    finally {
136                            DataAccess.cleanUp(con, ps, rs);
137                    }
138    
139                    synchronizeFileVersions();
140    
141                    // DLFileEntry
142    
143                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
144                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
145                            nameColumn, "title");
146                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
147                            "version");
148    
149                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
150                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
151                            nameColumn, titleColumn, versionColumn);
152    
153                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
154                    upgradeTable.setIndexesSQL(DLFileEntryTable.TABLE_SQL_ADD_INDEXES);
155    
156                    upgradeTable.updateTable();
157    
158                    // DLFileRank
159    
160                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
161                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
162                            nameColumn);
163    
164                    upgradeTable.setCreateSQL(
165                            StringUtil.replace(
166                                    DLFileRankTable.TABLE_SQL_CREATE, ",createDate DATE null",
167                                    ",createDate DATE null,fileEntryId LONG"));
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(
182                            StringUtil.replace(
183                                    DLFileShortcutTable.TABLE_SQL_CREATE, ",folderId LONG",
184                                    ",folderId LONG,toFileEntryId LONG"));
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, ",title VARCHAR(75) null",
198                                    ",title VARCHAR(255) null,fileEntryId LONG"));
199                    upgradeTable.setIndexesSQL(DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
200    
201                    upgradeTable.updateTable();
202    
203                    try {
204                            runSQL("drop index IX_CE705D48 on DLFileRank");
205                            runSQL("drop index IX_40B56512 on DLFileRank");
206    
207                            runSQL("drop index IX_55C736AC on DLFileShortcut");
208                            runSQL("drop index IX_346A0992 on DLFileShortcut");
209    
210                            runSQL("drop index IX_B413F1EC on DLFileVersion");
211                            runSQL("drop index IX_94E784D2 on DLFileVersion");
212                            runSQL("drop index IX_2F8FED9C on DLFileVersion");
213                    }
214                    catch (Exception e) {
215                            _log.error(e, e);
216                    }
217            }
218    
219            protected void synchronizeFileVersions() throws Exception {
220                    Connection con = null;
221                    PreparedStatement ps = null;
222                    ResultSet rs = null;
223    
224                    try {
225                            con = DataAccess.getUpgradeOptimizedConnection();
226    
227                            StringBundler sb = new StringBundler(5);
228    
229                            sb.append("select * from DLFileEntry dlFileEntry where version ");
230                            sb.append("not in (select version from DLFileVersion ");
231                            sb.append("dlFileVersion where (dlFileVersion.folderId = ");
232                            sb.append("dlFileEntry.folderId) and (dlFileVersion.name = ");
233                            sb.append("dlFileEntry.name))");
234    
235                            String sql = sb.toString();
236    
237                            ps = con.prepareStatement(sql);
238    
239                            rs = ps.executeQuery();
240    
241                            while (rs.next()) {
242                                    long companyId = rs.getLong("companyId");
243                                    long fileEntryId = rs.getLong("fileEntryId");
244                                    long groupId = rs.getLong("groupId");
245                                    long userId = rs.getLong("userId");
246                                    String userName = rs.getString("userName");
247                                    long folderId = rs.getLong("folderId");
248                                    String name = rs.getString("name");
249                                    String title = rs.getString("title");
250                                    double version = rs.getDouble("version");
251                                    int size = rs.getInt("size_");
252    
253                                    addFileVersion(
254                                            groupId, companyId, userId, userName, folderId, name, title,
255                                            version, size, fileEntryId);
256                            }
257                    }
258                    finally {
259                            DataAccess.cleanUp(con, ps);
260                    }
261            }
262    
263            private static Log _log = LogFactoryUtil.getLog(
264                    UpgradeDocumentLibrary.class);
265    
266    }