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_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 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 void updateFileEntries() throws Exception {
074                    Connection con = null;
075                    PreparedStatement ps = null;
076                    ResultSet rs = null;
077    
078                    try {
079                            con = DataAccess.getUpgradeOptimizedConnection();
080    
081                            ps = con.prepareStatement(
082                                    "select fileEntryId, extension from DLFileEntry");
083    
084                            rs = ps.executeQuery();
085    
086                            while (rs.next()) {
087                                    long fileEntryId = rs.getLong("fileEntryId");
088                                    String extension = rs.getString("extension");
089    
090                                    String mimeType = MimeTypesUtil.getContentType(
091                                            "A." + extension);
092    
093                                    runSQL(
094                                            "update DLFileEntry set mimeType = '" + mimeType +
095                                                    "' where fileEntryId = " + fileEntryId);
096                            }
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.getUpgradeOptimizedConnection();
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.getUpgradeOptimizedConnection();
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    }