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_12;
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.GetterUtil;
020    import com.liferay.portal.kernel.util.MimeTypesUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Alexander Chow
031     * @author Amos Fong
032     */
033    public class UpgradeDocumentLibrary extends UpgradeProcess {
034    
035            @Override
036            protected void doUpgrade() throws Exception {
037                    updateFileEntries();
038                    updateFileVersions();
039                    updateLocks();
040            }
041    
042            protected void updateFileEntries() throws Exception {
043                    Connection con = null;
044                    PreparedStatement ps = null;
045                    ResultSet rs = null;
046    
047                    try {
048                            con = DataAccess.getConnection();
049    
050                            ps = con.prepareStatement(
051                                    "select fileEntryId, extension from DLFileEntry");
052    
053                            rs = ps.executeQuery();
054    
055                            while (rs.next()) {
056                                    long fileEntryId = rs.getLong("fileEntryId");
057                                    String extension = rs.getString("extension");
058    
059                                    String mimeType = MimeTypesUtil.getContentType(
060                                            "A." + extension);
061    
062                                    runSQL(
063                                            "update DLFileEntry set mimeType = '" + mimeType +
064                                                    "' where fileEntryId = " + fileEntryId);
065                            }
066                    }
067                    finally {
068                            DataAccess.cleanUp(con, ps, rs);
069                    }
070            }
071    
072            protected long getFileEntryId(long groupId, long folderId, String name)
073                    throws Exception {
074    
075                    Connection con = null;
076                    PreparedStatement ps = null;
077                    ResultSet rs = null;
078    
079                    try {
080                            con = DataAccess.getConnection();
081    
082                            ps = con.prepareStatement(
083                                    "select fileEntryId from DLFileEntry where groupId = ? and " +
084                                            "folderId = ? and name = ?");
085    
086                            ps.setLong(1, groupId);
087                            ps.setLong(2, folderId);
088                            ps.setString(3, name);
089    
090                            rs = ps.executeQuery();
091    
092                            if (rs.next()) {
093                                    return rs.getLong("fileEntryId");
094                            }
095    
096                            return 0;
097                    }
098                    finally {
099                            DataAccess.cleanUp(con, ps, rs);
100                    }
101            }
102    
103            protected void updateFileVersions() throws Exception {
104                    Connection con = null;
105                    PreparedStatement ps = null;
106                    ResultSet rs = null;
107    
108                    try {
109                            con = DataAccess.getConnection();
110    
111                            ps = con.prepareStatement(
112                                    "select fileVersionId, extension from DLFileVersion");
113    
114                            rs = ps.executeQuery();
115    
116                            while (rs.next()) {
117                                    long fileVersionId = rs.getLong("fileVersionId");
118                                    String extension = rs.getString("extension");
119    
120                                    String mimeType = MimeTypesUtil.getContentType(
121                                            "A." + extension);
122    
123                                    runSQL(
124                                            "update DLFileVersion set mimeType = '" + mimeType +
125                                                    "' where fileVersionId = " + fileVersionId);
126                            }
127                    }
128                    finally {
129                            DataAccess.cleanUp(con, ps, rs);
130                    }
131            }
132    
133            protected void updateLocks() throws Exception {
134                    Connection con = null;
135                    PreparedStatement ps = null;
136                    ResultSet rs = null;
137    
138                    try {
139                            con = DataAccess.getConnection();
140    
141                            ps = con.prepareStatement(
142                                    "select lockId, key_ from Lock_ where className = ?");
143    
144                            ps.setString(1, DLFileEntry.class.getName());
145    
146                            rs = ps.executeQuery();
147    
148                            while (rs.next()) {
149                                    long lockId = rs.getLong("lockId");
150                                    String key = rs.getString("key_");
151    
152                                    String[] keyArray = StringUtil.split(key, StringPool.POUND);
153    
154                                    if (keyArray.length != 3) {
155                                            continue;
156                                    }
157    
158                                    long groupId = GetterUtil.getLong(keyArray[0]);
159                                    long folderId = GetterUtil.getLong(keyArray[1]);
160                                    String name = keyArray[2];
161    
162                                    long fileEntryId = getFileEntryId(groupId, folderId, name);
163    
164                                    if (fileEntryId > 0) {
165                                            runSQL(
166                                                    "update Lock_ set key_ = '" + fileEntryId +
167                                                            "' where lockId = " + lockId);
168                                    }
169                            }
170                    }
171                    finally {
172                            DataAccess.cleanUp(con, ps, rs);
173                    }
174            }
175    
176    }