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_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    
081                    if (PropsValues.DL_FILE_ENTRY_PREVIEW_AUTO_CREATE_ON_UPGRADE) {
082                            updateThumbnails();
083                    }
084    
085                    //updateSyncs();
086            }
087    
088            protected void updateFileVersions() throws Exception {
089                    Connection con = null;
090                    PreparedStatement ps = null;
091                    ResultSet rs = null;
092    
093                    try {
094                            con = DataAccess.getUpgradeOptimizedConnection();
095    
096                            ps = con.prepareStatement(
097                                    "select fileEntryId, folderId from DLFileEntry");
098    
099                            rs = ps.executeQuery();
100    
101                            while (rs.next()) {
102                                    long fileEntryId = rs.getLong("fileEntryId");
103                                    long folderId = rs.getLong("folderId");
104    
105                                    StringBundler sb = new StringBundler(4);
106    
107                                    sb.append("update DLFileVersion set folderId = ");
108                                    sb.append(folderId);
109                                    sb.append(" where fileEntryId = ");
110                                    sb.append(fileEntryId);
111    
112                                    runSQL(sb.toString());
113                            }
114    
115                    }
116                    finally {
117                            DataAccess.cleanUp(con, ps, rs);
118                    }
119            }
120    
121            protected void updateSyncs() throws Exception {
122                    Connection con = null;
123                    PreparedStatement ps = null;
124                    ResultSet rs = null;
125    
126                    try {
127                            con = DataAccess.getUpgradeOptimizedConnection();
128    
129                            StringBundler sb = new StringBundler(10);
130    
131                            sb.append("select DLFileEntry.fileEntryId as fileId, ");
132                            sb.append("DLFileEntry.groupId as groupId, DLFileEntry.companyId");
133                            sb.append(" as companyId, DLFileEntry.createDate as createDate, ");
134                            sb.append("DLFileEntry.folderId as parentFolderId, 'file' as ");
135                            sb.append("type from DLFileEntry union all select ");
136                            sb.append("DLFolder.folderId as fileId, DLFolder.groupId as ");
137                            sb.append("groupId, DLFolder.companyId as companyId, ");
138                            sb.append("DLFolder.createDate as createDate, ");
139                            sb.append("DLFolder.parentFolderId as parentFolderId, 'folder' ");
140                            sb.append("as type from DLFolder");
141    
142                            String sql = sb.toString();
143    
144                            ps = con.prepareStatement(sql);
145    
146                            rs = ps.executeQuery();
147    
148                            while (rs.next()) {
149                                    long fileId = rs.getLong("fileId");
150                                    long groupId = rs.getLong("groupId");
151                                    long companyId = rs.getLong("companyId");
152                                    Timestamp createDate = rs.getTimestamp("createDate");
153                                    long parentFolderId = rs.getLong("parentFolderId");
154                                    String type = rs.getString("type");
155    
156                                    addDLSync(
157                                            increment(), companyId, createDate, createDate, fileId,
158                                            groupId, parentFolderId, "add", type);
159                            }
160                    }
161                    finally {
162                            DataAccess.cleanUp(con, ps, rs);
163                    }
164            }
165    
166            protected void updateThumbnails() throws Exception {
167                    Connection con = null;
168                    PreparedStatement ps = null;
169                    ResultSet rs = null;
170    
171                    try {
172                            con = DataAccess.getUpgradeOptimizedConnection();
173    
174                            ps = con.prepareStatement("select fileEntryId from DLFileEntry");
175    
176                            rs = ps.executeQuery();
177    
178                            while (rs.next()) {
179                                    long fileEntryId = rs.getLong("fileEntryId");
180    
181                                    updateThumbnails(fileEntryId);
182                            }
183                    }
184                    finally {
185                            DataAccess.cleanUp(con, ps, rs);
186                    }
187            }
188    
189            protected void updateThumbnails(long fileEntryId) throws Exception {
190                    Connection con = null;
191                    PreparedStatement ps = null;
192                    ResultSet rs = null;
193    
194                    try {
195                            con = DataAccess.getUpgradeOptimizedConnection();
196    
197                            ps = con.prepareStatement(
198                                    "select fileVersionId, userId, extension, mimeType, version " +
199                                            "from DLFileVersion where fileEntryId = " + fileEntryId +
200                                                    " order by version asc");
201    
202                            rs = ps.executeQuery();
203    
204                            while (rs.next()) {
205                                    long fileVersionId = rs.getLong("fileVersionId");
206                                    long userId = rs.getLong("userId");
207                                    String extension = rs.getString("extension");
208                                    String mimeType = rs.getString("mimeType");
209                                    String version = rs.getString("version");
210    
211                                    if (_imageMimeTypes.contains(mimeType)) {
212                                            DLFileVersion dlFileVersion = new DLFileVersionImpl();
213    
214                                            dlFileVersion.setFileVersionId(fileVersionId);
215                                            dlFileVersion.setUserId(userId);
216                                            dlFileVersion.setFileEntryId(fileEntryId);
217                                            dlFileVersion.setExtension(extension);
218                                            dlFileVersion.setMimeType(mimeType);
219                                            dlFileVersion.setVersion(version);
220    
221                                            FileVersion fileVersion = new LiferayFileVersion(
222                                                    dlFileVersion);
223    
224                                            ImageProcessorUtil.generateImages(fileVersion);
225                                    }
226                            }
227                    }
228                    finally {
229                            DataAccess.cleanUp(con, ps, rs);
230                    }
231            }
232    
233            private static Set<String> _imageMimeTypes = SetUtil.fromArray(
234                    PropsValues.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES);
235    
236    }