001    /**
002     * Copyright (c) 2000-2012 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.v6_0_12_to_6_1_0;
016    
017    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018    import com.liferay.portal.kernel.repository.model.FileVersion;
019    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
020    import com.liferay.portal.kernel.util.SetUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
023    import com.liferay.portal.util.PropsValues;
024    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
025    import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
026    import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
027    
028    import java.sql.Connection;
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    import java.sql.Timestamp;
032    
033    import java.util.Set;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Douglas Wong
038     * @author Alexander Chow
039     * @author Minhchau Dang
040     */
041    public class UpgradeDocumentLibrary extends UpgradeProcess {
042    
043            protected void addDLSync(
044                            long syncId, long companyId, Timestamp createDate,
045                            Timestamp modifiedDate, long fileId, long repositoryId,
046                            long parentFolderId, String event, String type)
047                    throws Exception {
048    
049                    Connection con = null;
050                    PreparedStatement ps = null;
051    
052                    try {
053                            con = DataAccess.getUpgradeOptimizedConnection();
054    
055                            ps = con.prepareStatement(
056                                    "insert into DLSync (syncId, companyId, createDate, " +
057                                            "modifiedDate, fileId, repositoryId, parentFolderId, " +
058                                                    "event, type_) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
059    
060                            ps.setLong(1, syncId);
061                            ps.setLong(2, companyId);
062                            ps.setTimestamp(3, createDate);
063                            ps.setTimestamp(4, createDate);
064                            ps.setLong(5, fileId);
065                            ps.setLong(6, repositoryId);
066                            ps.setLong(7, parentFolderId);
067                            ps.setString(8, event);
068                            ps.setString(9, type);
069    
070                            ps.executeUpdate();
071                    }
072                    finally {
073                            DataAccess.cleanUp(con, ps);
074                    }
075            }
076    
077            @Override
078            protected void doUpgrade() throws Exception {
079                    updateFileVersions();
080                    updateThumbnails();
081                    //updateSyncs();
082            }
083    
084            protected void updateFileVersions() throws Exception {
085                    Connection con = null;
086                    PreparedStatement ps = null;
087                    ResultSet rs = null;
088    
089                    try {
090                            con = DataAccess.getUpgradeOptimizedConnection();
091    
092                            ps = con.prepareStatement(
093                                    "select fileEntryId, folderId from DLFileEntry");
094    
095                            rs = ps.executeQuery();
096    
097                            while (rs.next()) {
098                                    long fileEntryId = rs.getLong("fileEntryId");
099                                    long folderId = rs.getLong("folderId");
100    
101                                    StringBundler sb = new StringBundler(4);
102    
103                                    sb.append("update DLFileVersion set folderId = ");
104                                    sb.append(folderId);
105                                    sb.append(" where fileEntryId = ");
106                                    sb.append(fileEntryId);
107    
108                                    runSQL(sb.toString());
109                            }
110    
111                    }
112                    finally {
113                            DataAccess.cleanUp(con, ps, rs);
114                    }
115            }
116    
117            protected void updateSyncs() throws Exception {
118                    Connection con = null;
119                    PreparedStatement ps = null;
120                    ResultSet rs = null;
121    
122                    try {
123                            con = DataAccess.getUpgradeOptimizedConnection();
124    
125                            StringBundler sb = new StringBundler(10);
126    
127                            sb.append("select DLFileEntry.fileEntryId as fileId, ");
128                            sb.append("DLFileEntry.groupId as groupId, DLFileEntry.companyId");
129                            sb.append(" as companyId, DLFileEntry.createDate as createDate, ");
130                            sb.append("DLFileEntry.folderId as parentFolderId, 'file' as ");
131                            sb.append("type from DLFileEntry union all select ");
132                            sb.append("DLFolder.folderId as fileId, DLFolder.groupId as ");
133                            sb.append("groupId, DLFolder.companyId as companyId, ");
134                            sb.append("DLFolder.createDate as createDate, ");
135                            sb.append("DLFolder.parentFolderId as parentFolderId, 'folder' ");
136                            sb.append("as type from DLFolder");
137    
138                            String sql = sb.toString();
139    
140                            ps = con.prepareStatement(sql);
141    
142                            rs = ps.executeQuery();
143    
144                            while (rs.next()) {
145                                    long fileId = rs.getLong("fileId");
146                                    long groupId = rs.getLong("groupId");
147                                    long companyId = rs.getLong("companyId");
148                                    Timestamp createDate = rs.getTimestamp("createDate");
149                                    long parentFolderId = rs.getLong("parentFolderId");
150                                    String type = rs.getString("type");
151    
152                                    addDLSync(
153                                            increment(), companyId, createDate, createDate, fileId,
154                                            groupId, parentFolderId, "add", type);
155                            }
156                    }
157                    finally {
158                            DataAccess.cleanUp(con, ps, rs);
159                    }
160            }
161    
162            protected void updateThumbnails() throws Exception {
163                    Connection con = null;
164                    PreparedStatement ps = null;
165                    ResultSet rs = null;
166    
167                    try {
168                            con = DataAccess.getUpgradeOptimizedConnection();
169    
170                            ps = con.prepareStatement("select fileEntryId from DLFileEntry");
171    
172                            rs = ps.executeQuery();
173    
174                            while (rs.next()) {
175                                    long fileEntryId = rs.getLong("fileEntryId");
176    
177                                    updateThumbnails(fileEntryId);
178                            }
179                    }
180                    finally {
181                            DataAccess.cleanUp(con, ps, rs);
182                    }
183            }
184    
185            protected void updateThumbnails(long fileEntryId) throws Exception {
186                    Connection con = null;
187                    PreparedStatement ps = null;
188                    ResultSet rs = null;
189    
190                    try {
191                            con = DataAccess.getUpgradeOptimizedConnection();
192    
193                            ps = con.prepareStatement(
194                                    "select fileVersionId, userId, extension, mimeType, version " +
195                                            "from DLFileVersion where fileEntryId = " + fileEntryId +
196                                                    " order by version asc");
197    
198                            rs = ps.executeQuery();
199    
200                            while (rs.next()) {
201                                    long fileVersionId = rs.getLong("fileVersionId");
202                                    long userId = rs.getLong("userId");
203                                    String extension = rs.getString("extension");
204                                    String mimeType = rs.getString("mimeType");
205                                    String version = rs.getString("version");
206    
207                                    if (_imageMimeTypes.contains(mimeType)) {
208                                            DLFileVersion dlFileVersion = new DLFileVersionImpl();
209    
210                                            dlFileVersion.setFileVersionId(fileVersionId);
211                                            dlFileVersion.setUserId(userId);
212                                            dlFileVersion.setFileEntryId(fileEntryId);
213                                            dlFileVersion.setExtension(extension);
214                                            dlFileVersion.setMimeType(mimeType);
215                                            dlFileVersion.setVersion(version);
216    
217                                            FileVersion fileVersion = new LiferayFileVersion(
218                                                    dlFileVersion);
219    
220                                            ImageProcessorUtil.generateImages(fileVersion);
221                                    }
222                            }
223                    }
224                    finally {
225                            DataAccess.cleanUp(con, ps, rs);
226                    }
227            }
228    
229            private static Set<String> _imageMimeTypes = SetUtil.fromArray(
230                    PropsValues.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES);
231    
232    }