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