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_11;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
021    import com.liferay.portal.upgrade.v6_0_11.util.DLFileVersionTable;
022    
023    import java.sql.Connection;
024    import java.sql.PreparedStatement;
025    import java.sql.ResultSet;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Douglas Wong
030     * @author Alexander Chow
031     * @author Minhchau Dang
032     */
033    public class UpgradeDocumentLibrary extends UpgradeProcess {
034    
035            @Override
036            protected void doUpgrade() throws Exception {
037                    updateFileRanks();
038                    updateFileShortcuts();
039                    updateFileVersions();
040            }
041    
042            protected long getFileEntryId(long groupId, long folderId, String name)
043                    throws Exception {
044    
045                    Connection con = null;
046                    PreparedStatement ps = null;
047                    ResultSet rs = null;
048    
049                    try {
050                            con = DataAccess.getUpgradeOptimizedConnection();
051    
052                            ps = con.prepareStatement(
053                                    "select fileEntryId from DLFileEntry where groupId = ? and " +
054                                            "folderId = ? and name = ?");
055    
056                            ps.setLong(1, groupId);
057                            ps.setLong(2, folderId);
058                            ps.setString(3, name);
059    
060                            rs = ps.executeQuery();
061    
062                            if (rs.next()) {
063                                    return rs.getLong("fileEntryId");
064                            }
065    
066                            return 0;
067                    }
068                    finally {
069                            DataAccess.cleanUp(con, ps, rs);
070                    }
071            }
072    
073            protected long getGroupId(long folderId) throws Exception {
074                    Connection con = null;
075                    PreparedStatement ps = null;
076                    ResultSet rs = null;
077    
078                    long groupId = 0;
079    
080                    try {
081                            con = DataAccess.getUpgradeOptimizedConnection();
082    
083                            ps = con.prepareStatement(
084                                    "select groupId from DLFolder where folderId = ?");
085    
086                            ps.setLong(1, folderId);
087    
088                            rs = ps.executeQuery();
089    
090                            if (rs.next()) {
091                                    groupId = rs.getLong("groupId");
092                            }
093                    }
094                    finally {
095                            DataAccess.cleanUp(con, ps, rs);
096                    }
097    
098                    return groupId;
099            }
100    
101            protected void updateFileRanks() throws Exception {
102                    Connection con = null;
103                    PreparedStatement ps = null;
104                    ResultSet rs = null;
105    
106                    try {
107                            con = DataAccess.getUpgradeOptimizedConnection();
108    
109                            ps = con.prepareStatement(
110                                    "select groupId, fileRankId, folderId, name from DLFileRank");
111    
112                            rs = ps.executeQuery();
113    
114                            while (rs.next()) {
115                                    long groupId = rs.getLong("groupId");
116                                    long fileRankId = rs.getLong("fileRankId");
117                                    long folderId = rs.getLong("folderId");
118                                    String name = rs.getString("name");
119    
120                                    long fileEntryId = getFileEntryId(groupId, folderId, name);
121    
122                                    runSQL(
123                                            "update DLFileRank set fileEntryId = " + fileEntryId +
124                                                    " where fileRankId = " + fileRankId);
125                            }
126                    }
127                    finally {
128                            DataAccess.cleanUp(con, ps, rs);
129                    }
130    
131                    runSQL("alter table DLFileRank drop column folderId");
132                    runSQL("alter table DLFileRank drop column name");
133            }
134    
135            protected void updateFileShortcuts() throws Exception {
136                    Connection con = null;
137                    PreparedStatement ps = null;
138                    ResultSet rs = null;
139    
140                    try {
141                            con = DataAccess.getUpgradeOptimizedConnection();
142    
143                            ps = con.prepareStatement(
144                                    "select fileShortcutId, toFolderId, toName from " +
145                                            "DLFileShortcut");
146    
147                            rs = ps.executeQuery();
148    
149                            while (rs.next()) {
150                                    long fileShortcutId = rs.getLong("fileShortcutId");
151                                    long toFolderId = rs.getLong("toFolderId");
152                                    String toName = rs.getString("toName");
153    
154                                    long groupId = getGroupId(toFolderId);
155    
156                                    long toFileEntryId = getFileEntryId(
157                                            groupId, toFolderId, toName);
158    
159                                    runSQL(
160                                            "update DLFileShortcut set toFileEntryId = " +
161                                                    toFileEntryId + " where fileShortcutId = " +
162                                                            fileShortcutId);
163                            }
164                    }
165                    finally {
166                            DataAccess.cleanUp(con, ps, rs);
167                    }
168    
169                    runSQL("alter table DLFileShortcut drop column toFolderId");
170                    runSQL("alter table DLFileShortcut drop column toName");
171            }
172    
173            protected void updateFileVersions() throws Exception {
174                    Connection con = null;
175                    PreparedStatement ps = null;
176                    ResultSet rs = null;
177    
178                    try {
179                            con = DataAccess.getUpgradeOptimizedConnection();
180    
181                            ps = con.prepareStatement(
182                                    "select groupId, fileVersionId, folderId, name from " +
183                                            "DLFileVersion");
184    
185                            rs = ps.executeQuery();
186    
187                            while (rs.next()) {
188                                    long groupId = rs.getLong("groupId");
189                                    long fileVersionId = rs.getLong("fileVersionId");
190                                    long folderId = rs.getLong("folderId");
191                                    String name = rs.getString("name");
192    
193                                    long fileEntryId = getFileEntryId(groupId, folderId, name);
194    
195                                    runSQL(
196                                            "update DLFileVersion set fileEntryId = " + fileEntryId +
197                                                    " where fileVersionId = " + fileVersionId);
198                            }
199                    }
200                    finally {
201                            DataAccess.cleanUp(con, ps, rs);
202                    }
203    
204                    try {
205                            runSQL("alter_column_type DLFileVersion extraSettings TEXT null");
206                            runSQL("alter_column_type DLFileVersion title VARCHAR(255) null");
207                            runSQL("alter table DLFileVersion drop column folderId");
208                            runSQL("alter table DLFileVersion drop column name");
209                    }
210                    catch (Exception e) {
211                            UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
212                                    DLFileVersionTable.TABLE_NAME,
213                                    DLFileVersionTable.TABLE_COLUMNS);
214    
215                            upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
216                            upgradeTable.setIndexesSQL(
217                                    DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
218    
219                            upgradeTable.updateTable();
220                    }
221            }
222    
223    }