001    /**
002     * Copyright (c) 2000-2010 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_3;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
019    import com.liferay.portal.kernel.util.FileUtil;
020    
021    import java.sql.Connection;
022    import java.sql.PreparedStatement;
023    import java.sql.ResultSet;
024    
025    /**
026     * @author Sergio González
027     */
028    public class UpgradeDocumentLibrary extends UpgradeProcess {
029    
030            protected void doUpgrade() throws Exception {
031                    updateFileEntries();
032                    updateFileVersions();
033            }
034    
035            protected long getLatestFileVersionId(long folderId, String name)
036                    throws Exception {
037    
038                    Connection con = null;
039                    PreparedStatement ps = null;
040                    ResultSet rs = null;
041    
042                    long fileVersionId = 0;
043    
044                    try {
045                            con = DataAccess.getConnection();
046    
047                            ps = con.prepareStatement(
048                                    "select fileVersionId from DLFileVersion where folderId = ? " +
049                                            "and name = ? order by version desc");
050    
051                            ps.setLong(1, folderId);
052                            ps.setString(2, name);
053    
054                            rs = ps.executeQuery();
055    
056                            if (rs.next()) {
057                                    fileVersionId = rs.getLong("fileVersionId");
058                            }
059                    }
060                    finally {
061                            DataAccess.cleanUp(con, ps, rs);
062                    }
063    
064                    return fileVersionId;
065            }
066    
067            protected void updateFileEntries() throws Exception {
068                    Connection con = null;
069                    PreparedStatement ps = null;
070                    ResultSet rs = null;
071    
072                    try {
073                            con = DataAccess.getConnection();
074    
075                            ps = con.prepareStatement(
076                                    "select uuid_, fileEntryId, groupId, folderId, name, title " +
077                                            "from DLFileEntry");
078    
079                            rs = ps.executeQuery();
080    
081                            while (rs.next()) {
082                                    String uuid_ = rs.getString("uuid_");
083                                    long fileEntryId = rs.getLong("fileEntryId");
084                                    long groupId = rs.getLong("groupId");
085                                    long folderId = rs.getLong("folderId");
086                                    String name = rs.getString("name");
087                                    String title = rs.getString("title");
088    
089                                    String extension = FileUtil.getExtension(title);
090    
091                                    runSQL(
092                                            "update DLFileEntry set extension = '" + extension +
093                                                    "' where uuid_ = '" + uuid_ + "' and groupId = " +
094                                                            groupId);
095    
096                                    long fileVersionId = getLatestFileVersionId(folderId, name);
097    
098                                    runSQL(
099                                            "update ExpandoRow set classPK = " + fileVersionId +
100                                                     " where classPK = " + fileEntryId);
101    
102                                    runSQL(
103                                            "update ExpandoValue set classPK = " + fileVersionId +
104                                                     " where classPK = " + fileEntryId);
105                            }
106                    }
107                    finally {
108                            DataAccess.cleanUp(con, ps, rs);
109                    }
110            }
111    
112            protected void updateFileVersion(
113                            long fileVersionId, String extension, String title, String
114                            description, String extraSettings)
115                    throws Exception {
116    
117                    Connection con = null;
118                    PreparedStatement ps = null;
119    
120                    try {
121                            con = DataAccess.getConnection();
122    
123                            ps = con.prepareStatement(
124                                    "update DLFileVersion set extension = ?, title = ?, " +
125                                            "description = ?,  extraSettings = ? where fileVersionId " +
126                                                    "= ?");
127    
128                            ps.setString(1, extension);
129                            ps.setString(2, title);
130                            ps.setString(3, description);
131                            ps.setString(4, extraSettings);
132                            ps.setLong(5, fileVersionId);
133    
134                            ps.executeUpdate();
135                    }
136                    finally {
137                            DataAccess.cleanUp(con, ps);
138                    }
139            }
140    
141            protected void updateFileVersions() throws Exception {
142                    Connection con = null;
143                    PreparedStatement ps = null;
144                    ResultSet rs = null;
145    
146                    try {
147                            con = DataAccess.getConnection();
148    
149                            ps = con.prepareStatement(
150                                    "select folderId, name, extension, title, description, " +
151                                            "extraSettings from DLFileEntry");
152    
153                            rs = ps.executeQuery();
154    
155                            while (rs.next()) {
156                                    long folderId = rs.getLong("folderId");
157                                    String name = rs.getString("name");
158                                    String extension = rs.getString("extension");
159                                    String title = rs.getString("title");
160                                    String description = rs.getString("description");
161                                    String extraSettings = rs.getString("extraSettings");
162    
163                                    long fileVersionId = getLatestFileVersionId(folderId, name);
164    
165                                    updateFileVersion(
166                                            fileVersionId, extension, title, description,
167                                            extraSettings);
168                            }
169                    }
170                    finally {
171                            DataAccess.cleanUp(con, ps, rs);
172                    }
173            }
174    
175    }