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