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