001    /**
002     * Copyright (c) 2000-2010 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_8_to_6_0_5;
016    
017    import com.liferay.documentlibrary.service.DLServiceUtil;
018    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
021    import com.liferay.portal.kernel.upgrade.util.UpgradeTable;
022    import com.liferay.portal.kernel.upgrade.util.UpgradeTableFactoryUtil;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileEntryTable;
025    import com.liferay.portal.upgrade.v5_2_8_to_6_0_5.util.DLFileVersionTable;
026    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryNameUpgradeColumnImpl;
027    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryTitleUpgradeColumnImpl;
028    import com.liferay.portal.upgrade.v6_0_0.util.DLFileEntryVersionUpgradeColumnImpl;
029    import com.liferay.portal.upgrade.v6_0_0.util.DLFileRankTable;
030    import com.liferay.portal.upgrade.v6_0_0.util.DLFileShortcutTable;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
033    
034    import java.sql.Connection;
035    import java.sql.PreparedStatement;
036    import java.sql.ResultSet;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Alexander Chow
041     * @author Douglas Wong
042     */
043    public class UpgradeDocumentLibrary extends UpgradeProcess {
044    
045            protected void doUpgrade() throws Exception {
046                    Connection con = null;
047                    PreparedStatement ps = null;
048                    ResultSet rs = null;
049    
050                    try {
051                            con = DataAccess.getConnection();
052    
053                            ps = con.prepareStatement("select * from DLFileEntry");
054    
055                            rs = ps.executeQuery();
056    
057                            while (rs.next()) {
058                                    long companyId = rs.getLong("companyId");
059                                    long groupId = rs.getLong("groupId");
060                                    long folderId = rs.getLong("folderId");
061                                    String name = rs.getString("name");
062    
063                                    String portletId = PortletKeys.DOCUMENT_LIBRARY;
064                                    long repositoryId = folderId;
065    
066                                    if (repositoryId ==
067                                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
068    
069                                            repositoryId = groupId;
070                                    }
071    
072                                    String newName = DLFileEntryNameUpgradeColumnImpl.getNewName(
073                                            name);
074    
075                                    if (!newName.equals(name)) {
076                                            DLServiceUtil.updateFile(
077                                                    companyId, portletId, groupId, repositoryId, name,
078                                                    newName, false);
079                                    }
080                            }
081                    }
082                    finally {
083                            DataAccess.cleanUp(con, ps, rs);
084                    }
085    
086                    // DLFileEntry
087    
088                    UpgradeColumn nameColumn = new DLFileEntryNameUpgradeColumnImpl("name");
089                    UpgradeColumn titleColumn = new DLFileEntryTitleUpgradeColumnImpl(
090                            nameColumn, "title");
091                    UpgradeColumn versionColumn = new DLFileEntryVersionUpgradeColumnImpl(
092                            "version");
093    
094                    UpgradeTable upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
095                            DLFileEntryTable.TABLE_NAME, DLFileEntryTable.TABLE_COLUMNS,
096                            nameColumn, titleColumn, versionColumn);
097    
098                    upgradeTable.setCreateSQL(DLFileEntryTable.TABLE_SQL_CREATE);
099    
100                    upgradeTable.updateTable();
101    
102                    // DLFileRank
103    
104                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
105                            DLFileRankTable.TABLE_NAME, DLFileRankTable.TABLE_COLUMNS,
106                            nameColumn);
107    
108                    upgradeTable.setCreateSQL(DLFileRankTable.TABLE_SQL_CREATE);
109    
110                    upgradeTable.updateTable();
111    
112                    // DLFileShortcut
113    
114                    UpgradeColumn toNameColumn = new DLFileEntryNameUpgradeColumnImpl(
115                            "toName");
116    
117                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
118                            DLFileShortcutTable.TABLE_NAME, DLFileShortcutTable.TABLE_COLUMNS,
119                            toNameColumn);
120    
121                    upgradeTable.setCreateSQL(DLFileShortcutTable.TABLE_SQL_CREATE);
122    
123                    upgradeTable.updateTable();
124    
125                    // DLFileVersion
126    
127                    upgradeTable = UpgradeTableFactoryUtil.getUpgradeTable(
128                            DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
129                            nameColumn, versionColumn);
130    
131                    upgradeTable.setCreateSQL(DLFileVersionTable.TABLE_SQL_CREATE);
132    
133                    upgradeTable.updateTable();
134    
135                    // File entries
136    
137                    updateFileEntries();
138    
139                    // File versions
140    
141                    updateFileVersions();
142            }
143    
144            protected long getLatestFileVersionId(long folderId, String name)
145                    throws Exception {
146    
147                    Connection con = null;
148                    PreparedStatement ps = null;
149                    ResultSet rs = null;
150    
151                    long fileVersionId = 0;
152    
153                    try {
154                            con = DataAccess.getConnection();
155    
156                            ps = con.prepareStatement(
157                                    "select fileVersionId from DLFileVersion where folderId = ? " +
158                                            "and name = ? order by version desc");
159    
160                            ps.setLong(1, folderId);
161                            ps.setString(2, name);
162    
163                            rs = ps.executeQuery();
164    
165                            if (rs.next()) {
166                                    fileVersionId = rs.getLong("fileVersionId");
167                            }
168                    }
169                    finally {
170                            DataAccess.cleanUp(con, ps, rs);
171                    }
172    
173                    return fileVersionId;
174            }
175    
176            protected void updateFileEntries() throws Exception {
177                    Connection con = null;
178                    PreparedStatement ps = null;
179                    ResultSet rs = null;
180    
181                    try {
182                            con = DataAccess.getConnection();
183    
184                            ps = con.prepareStatement(
185                                    "select uuid_, fileEntryId, groupId, folderId, name, title " +
186                                            "from DLFileEntry");
187    
188                            rs = ps.executeQuery();
189    
190                            while (rs.next()) {
191                                    String uuid_ = rs.getString("uuid_");
192                                    long fileEntryId = rs.getLong("fileEntryId");
193                                    long groupId = rs.getLong("groupId");
194                                    long folderId = rs.getLong("folderId");
195                                    String name = rs.getString("name");
196                                    String title = rs.getString("title");
197    
198                                    String extension = FileUtil.getExtension(title);
199    
200                                    runSQL(
201                                            "update DLFileEntry set extension = '" + extension +
202                                                    "' where uuid_ = '" + uuid_ + "' and groupId = " +
203                                                            groupId);
204    
205                                    long fileVersionId = getLatestFileVersionId(folderId, name);
206    
207                                    runSQL(
208                                            "update ExpandoRow set classPK = " + fileVersionId +
209                                                     " where classPK = " + fileEntryId);
210    
211                                    runSQL(
212                                            "update ExpandoValue set classPK = " + fileVersionId +
213                                                     " where classPK = " + fileEntryId);
214                            }
215                    }
216                    finally {
217                            DataAccess.cleanUp(con, ps, rs);
218                    }
219            }
220    
221            protected void updateFileVersion(
222                            long fileVersionId, String extension, String title, String
223                            description, String extraSettings)
224                    throws Exception {
225    
226                    Connection con = null;
227                    PreparedStatement ps = null;
228    
229                    try {
230                            con = DataAccess.getConnection();
231    
232                            ps = con.prepareStatement(
233                                    "update DLFileVersion set extension = ?, title = ?, " +
234                                            "description = ?,  extraSettings = ? where fileVersionId " +
235                                                    "= ?");
236    
237                            ps.setString(1, extension);
238                            ps.setString(2, title);
239                            ps.setString(3, description);
240                            ps.setString(4, extraSettings);
241                            ps.setLong(5, fileVersionId);
242    
243                            ps.executeUpdate();
244                    }
245                    finally {
246                            DataAccess.cleanUp(con, ps);
247                    }
248            }
249    
250            protected void updateFileVersions() throws Exception {
251                    Connection con = null;
252                    PreparedStatement ps = null;
253                    ResultSet rs = null;
254    
255                    try {
256                            con = DataAccess.getConnection();
257    
258                            ps = con.prepareStatement(
259                                    "select folderId, name, extension, title, description, " +
260                                            "extraSettings from DLFileEntry");
261    
262                            rs = ps.executeQuery();
263    
264                            while (rs.next()) {
265                                    long folderId = rs.getLong("folderId");
266                                    String name = rs.getString("name");
267                                    String extension = rs.getString("extension");
268                                    String title = rs.getString("title");
269                                    String description = rs.getString("description");
270                                    String extraSettings = rs.getString("extraSettings");
271    
272                                    long fileVersionId = getLatestFileVersionId(folderId, name);
273    
274                                    updateFileVersion(
275                                            fileVersionId, extension, title, description,
276                                            extraSettings);
277                            }
278                    }
279                    finally {
280                            DataAccess.cleanUp(con, ps, rs);
281                    }
282            }
283    
284    }