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