001
014
015 package com.liferay.portal.upgrade.v6_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.CharPool;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.MimeTypesUtil;
025 import com.liferay.portal.kernel.util.SetUtil;
026 import com.liferay.portal.kernel.util.StringBundler;
027 import com.liferay.portal.kernel.util.StringUtil;
028 import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
029 import com.liferay.portal.upgrade.v6_1_0.util.DLFileVersionTable;
030 import com.liferay.portal.util.PropsValues;
031 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
032 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
033 import com.liferay.portlet.documentlibrary.model.impl.DLFileVersionImpl;
034 import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
035
036 import java.sql.Connection;
037 import java.sql.PreparedStatement;
038 import java.sql.ResultSet;
039 import java.sql.SQLException;
040 import java.sql.Timestamp;
041
042 import java.util.Set;
043
044
050 public class UpgradeDocumentLibrary extends UpgradeProcess {
051
052 protected void addSync(
053 long syncId, long companyId, Timestamp createDate,
054 Timestamp modifiedDate, long fileId, long repositoryId,
055 long parentFolderId, String event, String type)
056 throws Exception {
057
058 Connection con = null;
059 PreparedStatement ps = null;
060
061 try {
062 con = DataAccess.getUpgradeOptimizedConnection();
063
064 ps = con.prepareStatement(
065 "insert into DLSync (syncId, companyId, createDate, " +
066 "modifiedDate, fileId, repositoryId, parentFolderId, " +
067 "event, type_) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
068
069 ps.setLong(1, syncId);
070 ps.setLong(2, companyId);
071 ps.setTimestamp(3, createDate);
072 ps.setTimestamp(4, createDate);
073 ps.setLong(5, fileId);
074 ps.setLong(6, repositoryId);
075 ps.setLong(7, parentFolderId);
076 ps.setString(8, event);
077 ps.setString(9, type);
078
079 ps.executeUpdate();
080 }
081 finally {
082 DataAccess.cleanUp(con, ps);
083 }
084 }
085
086 @Override
087 protected void doUpgrade() throws Exception {
088 updateFileEntries();
089 updateFileRanks();
090 updateFileShortcuts();
091 updateFileVersions();
092 updateLocks();
093
094 if (PropsValues.DL_FILE_ENTRY_PREVIEW_AUTO_CREATE_ON_UPGRADE) {
095 updateThumbnails();
096 }
097
098
099 }
100
101 protected long getFileEntryId(long groupId, long folderId, String name)
102 throws Exception {
103
104 Connection con = null;
105 PreparedStatement ps = null;
106 ResultSet rs = null;
107
108 try {
109 con = DataAccess.getUpgradeOptimizedConnection();
110
111 ps = con.prepareStatement(
112 "select fileEntryId from DLFileEntry where groupId = ? and " +
113 "folderId = ? and name = ?");
114
115 ps.setLong(1, groupId);
116 ps.setLong(2, folderId);
117 ps.setString(3, name);
118
119 rs = ps.executeQuery();
120
121 if (rs.next()) {
122 return rs.getLong("fileEntryId");
123 }
124
125 return 0;
126 }
127 finally {
128 DataAccess.cleanUp(con, ps, rs);
129 }
130 }
131
132 protected long getGroupId(long folderId) throws Exception {
133 Connection con = null;
134 PreparedStatement ps = null;
135 ResultSet rs = null;
136
137 long groupId = 0;
138
139 try {
140 con = DataAccess.getUpgradeOptimizedConnection();
141
142 ps = con.prepareStatement(
143 "select groupId from DLFolder where folderId = ?");
144
145 ps.setLong(1, folderId);
146
147 rs = ps.executeQuery();
148
149 if (rs.next()) {
150 groupId = rs.getLong("groupId");
151 }
152 }
153 finally {
154 DataAccess.cleanUp(con, ps, rs);
155 }
156
157 return groupId;
158 }
159
160 protected void updateFileEntries() throws Exception {
161 Connection con = null;
162 PreparedStatement ps = null;
163 ResultSet rs = null;
164
165 try {
166 con = DataAccess.getUpgradeOptimizedConnection();
167
168 ps = con.prepareStatement(
169 "select fileEntryId, extension from DLFileEntry");
170
171 rs = ps.executeQuery();
172
173 while (rs.next()) {
174 long fileEntryId = rs.getLong("fileEntryId");
175 String extension = rs.getString("extension");
176
177 String mimeType = MimeTypesUtil.getExtensionContentType(
178 extension);
179
180 runSQL(
181 "update DLFileEntry set mimeType = '" + mimeType +
182 "' where fileEntryId = " + fileEntryId);
183 }
184 }
185 finally {
186 DataAccess.cleanUp(con, ps, rs);
187 }
188 }
189
190 protected void updateFileRanks() 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 groupId, fileRankId, folderId, name from DLFileRank");
200
201 rs = ps.executeQuery();
202
203 while (rs.next()) {
204 long groupId = rs.getLong("groupId");
205 long fileRankId = rs.getLong("fileRankId");
206 long folderId = rs.getLong("folderId");
207 String name = rs.getString("name");
208
209 long fileEntryId = getFileEntryId(groupId, folderId, name);
210
211 runSQL(
212 "update DLFileRank set fileEntryId = " + fileEntryId +
213 " where fileRankId = " + fileRankId);
214 }
215 }
216 finally {
217 DataAccess.cleanUp(con, ps, rs);
218 }
219
220 runSQL("alter table DLFileRank drop column folderId");
221 runSQL("alter table DLFileRank drop column name");
222 }
223
224 protected void updateFileShortcuts() throws Exception {
225 Connection con = null;
226 PreparedStatement ps = null;
227 ResultSet rs = null;
228
229 try {
230 con = DataAccess.getUpgradeOptimizedConnection();
231
232 ps = con.prepareStatement(
233 "select fileShortcutId, toFolderId, toName from " +
234 "DLFileShortcut");
235
236 rs = ps.executeQuery();
237
238 while (rs.next()) {
239 long fileShortcutId = rs.getLong("fileShortcutId");
240 long toFolderId = rs.getLong("toFolderId");
241 String toName = rs.getString("toName");
242
243 long groupId = getGroupId(toFolderId);
244
245 long toFileEntryId = getFileEntryId(
246 groupId, toFolderId, toName);
247
248 runSQL(
249 "update DLFileShortcut set toFileEntryId = " +
250 toFileEntryId + " where fileShortcutId = " +
251 fileShortcutId);
252 }
253 }
254 finally {
255 DataAccess.cleanUp(con, ps, rs);
256 }
257
258 runSQL("alter table DLFileShortcut drop column toFolderId");
259 runSQL("alter table DLFileShortcut drop column toName");
260 }
261
262 protected void updateFileVersions() throws Exception {
263 Connection con = null;
264 PreparedStatement ps = null;
265 ResultSet rs = null;
266
267 try {
268 con = DataAccess.getUpgradeOptimizedConnection();
269
270 ps = con.prepareStatement(
271 "select groupId, fileVersionId, folderId, name, extension " +
272 "from DLFileVersion");
273
274 rs = ps.executeQuery();
275
276 while (rs.next()) {
277 long groupId = rs.getLong("groupId");
278 long fileVersionId = rs.getLong("fileVersionId");
279 long folderId = rs.getLong("folderId");
280 String name = rs.getString("name");
281 String extension = rs.getString("extension");
282
283 String mimeType = MimeTypesUtil.getExtensionContentType(
284 extension);
285
286 long fileEntryId = getFileEntryId(groupId, folderId, name);
287
288 runSQL(
289 "update DLFileVersion set fileEntryId = " + fileEntryId +
290 ", mimeType = '" + mimeType +
291 "' where fileVersionId = " + fileVersionId);
292 }
293 }
294 finally {
295 DataAccess.cleanUp(con, ps, rs);
296 }
297
298 try {
299 runSQL("alter_column_type DLFileVersion extraSettings TEXT null");
300 runSQL("alter_column_type DLFileVersion title VARCHAR(255) null");
301 runSQL("alter table DLFileVersion drop column name");
302 }
303 catch (SQLException sqle) {
304 upgradeTable(
305 DLFileVersionTable.TABLE_NAME, DLFileVersionTable.TABLE_COLUMNS,
306 DLFileVersionTable.TABLE_SQL_CREATE,
307 DLFileVersionTable.TABLE_SQL_ADD_INDEXES);
308 }
309 }
310
311 protected void updateLocks() throws Exception {
312 Connection con = null;
313 PreparedStatement ps = null;
314 ResultSet rs = null;
315
316 try {
317 con = DataAccess.getUpgradeOptimizedConnection();
318
319 ps = con.prepareStatement(
320 "select lockId, key_ from Lock_ where className = ?");
321
322 ps.setString(1, DLFileEntry.class.getName());
323
324 rs = ps.executeQuery();
325
326 while (rs.next()) {
327 long lockId = rs.getLong("lockId");
328 String key = rs.getString("key_");
329
330 String[] keyArray = StringUtil.split(key, CharPool.POUND);
331
332 if (keyArray.length != 3) {
333 continue;
334 }
335
336 long groupId = GetterUtil.getLong(keyArray[0]);
337 long folderId = GetterUtil.getLong(keyArray[1]);
338 String name = keyArray[2];
339
340 long fileEntryId = getFileEntryId(groupId, folderId, name);
341
342 if (fileEntryId > 0) {
343 runSQL(
344 "update Lock_ set key_ = '" + fileEntryId +
345 "' where lockId = " + lockId);
346 }
347 }
348 }
349 finally {
350 DataAccess.cleanUp(con, ps, rs);
351 }
352 }
353
354 protected void updateSyncs() throws Exception {
355 Connection con = null;
356 PreparedStatement ps = null;
357 ResultSet rs = null;
358
359 try {
360 con = DataAccess.getUpgradeOptimizedConnection();
361
362 StringBundler sb = new StringBundler(10);
363
364 sb.append("select DLFileEntry.fileEntryId as fileId, ");
365 sb.append("DLFileEntry.groupId as groupId, DLFileEntry.companyId");
366 sb.append(" as companyId, DLFileEntry.createDate as createDate, ");
367 sb.append("DLFileEntry.folderId as parentFolderId, 'file' as ");
368 sb.append("type from DLFileEntry union all select ");
369 sb.append("DLFolder.folderId as fileId, DLFolder.groupId as ");
370 sb.append("groupId, DLFolder.companyId as companyId, ");
371 sb.append("DLFolder.createDate as createDate, ");
372 sb.append("DLFolder.parentFolderId as parentFolderId, 'folder' ");
373 sb.append("as type from DLFolder");
374
375 String sql = sb.toString();
376
377 ps = con.prepareStatement(sql);
378
379 rs = ps.executeQuery();
380
381 while (rs.next()) {
382 long fileId = rs.getLong("fileId");
383 long groupId = rs.getLong("groupId");
384 long companyId = rs.getLong("companyId");
385 Timestamp createDate = rs.getTimestamp("createDate");
386 long parentFolderId = rs.getLong("parentFolderId");
387 String type = rs.getString("type");
388
389 addSync(
390 increment(), companyId, createDate, createDate, fileId,
391 groupId, parentFolderId, "add", type);
392 }
393 }
394 finally {
395 DataAccess.cleanUp(con, ps, rs);
396 }
397 }
398
399 protected void updateThumbnails() throws Exception {
400 Connection con = null;
401 PreparedStatement ps = null;
402 ResultSet rs = null;
403
404 try {
405 con = DataAccess.getUpgradeOptimizedConnection();
406
407 ps = con.prepareStatement("select fileEntryId from DLFileEntry");
408
409 rs = ps.executeQuery();
410
411 while (rs.next()) {
412 long fileEntryId = rs.getLong("fileEntryId");
413
414 updateThumbnails(fileEntryId);
415 }
416 }
417 finally {
418 DataAccess.cleanUp(con, ps, rs);
419 }
420 }
421
422 protected void updateThumbnails(long fileEntryId) throws Exception {
423 Connection con = null;
424 PreparedStatement ps = null;
425 ResultSet rs = null;
426
427 try {
428 con = DataAccess.getUpgradeOptimizedConnection();
429
430 ps = con.prepareStatement(
431 "select fileVersionId, userId, extension, mimeType, version " +
432 "from DLFileVersion where fileEntryId = " + fileEntryId +
433 " order by version asc");
434
435 rs = ps.executeQuery();
436
437 while (rs.next()) {
438 long fileVersionId = rs.getLong("fileVersionId");
439 long userId = rs.getLong("userId");
440 String extension = rs.getString("extension");
441 String mimeType = rs.getString("mimeType");
442 String version = rs.getString("version");
443
444 if (_imageMimeTypes.contains(mimeType)) {
445 DLFileVersion dlFileVersion = new DLFileVersionImpl();
446
447 dlFileVersion.setFileVersionId(fileVersionId);
448 dlFileVersion.setUserId(userId);
449 dlFileVersion.setFileEntryId(fileEntryId);
450 dlFileVersion.setExtension(extension);
451 dlFileVersion.setMimeType(mimeType);
452 dlFileVersion.setVersion(version);
453
454 FileVersion fileVersion = new LiferayFileVersion(
455 dlFileVersion);
456
457 try {
458 ImageProcessorUtil.generateImages(null, fileVersion);
459 }
460 catch (Exception e) {
461 if (_log.isWarnEnabled()) {
462 _log.warn(
463 "Unable to generate thumbnails for " +
464 fileVersion.getFileVersionId(),
465 e);
466 }
467 }
468 }
469 }
470 }
471 finally {
472 DataAccess.cleanUp(con, ps, rs);
473 }
474 }
475
476 private static Log _log = LogFactoryUtil.getLog(
477 UpgradeDocumentLibrary.class);
478
479 private static Set<String> _imageMimeTypes = SetUtil.fromArray(
480 PropsValues.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES);
481
482 }