001
014
015 package com.liferay.portal.upgrade.v6_1_0;
016
017 import com.liferay.portal.image.DLHook;
018 import com.liferay.portal.image.DatabaseHook;
019 import com.liferay.portal.image.FileSystemHook;
020 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021 import com.liferay.portal.kernel.dao.shard.ShardUtil;
022 import com.liferay.portal.kernel.image.Hook;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
026 import com.liferay.portal.kernel.util.FileUtil;
027 import com.liferay.portal.kernel.util.MimeTypesUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
032 import com.liferay.portal.model.Company;
033 import com.liferay.portal.model.Image;
034 import com.liferay.portal.service.ImageLocalServiceUtil;
035 import com.liferay.portal.util.ClassLoaderUtil;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portal.util.PropsValues;
038 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
040 import com.liferay.portlet.documentlibrary.model.DLFolder;
041 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
042 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
043 import com.liferay.portlet.documentlibrary.util.ImageProcessorUtil;
044
045 import java.io.InputStream;
046
047 import java.sql.Connection;
048 import java.sql.DatabaseMetaData;
049 import java.sql.PreparedStatement;
050 import java.sql.ResultSet;
051 import java.sql.Timestamp;
052
053 import java.util.ArrayList;
054 import java.util.Collections;
055 import java.util.HashMap;
056 import java.util.List;
057 import java.util.Map;
058
059
063 public class UpgradeImageGallery extends UpgradeProcess {
064
065 public UpgradeImageGallery() throws Exception {
066 ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
067
068 _sourceHookClassName = FileSystemHook.class.getName();
069
070 if (Validator.isNotNull(PropsValues.IMAGE_HOOK_IMPL)) {
071 _sourceHookClassName = PropsValues.IMAGE_HOOK_IMPL;
072 }
073
074 Class<?> clazz = classLoader.loadClass(_sourceHookClassName);
075
076 _sourceHook = (Hook)clazz.newInstance();
077 }
078
079 protected void addDLFileEntry(
080 String uuid, long fileEntryId, long groupId, long companyId,
081 long userId, String userName, long versionUserId,
082 String versionUserName, Timestamp createDate,
083 Timestamp modifiedDate, long repositoryId, long folderId,
084 String name, String extension, String mimeType, String title,
085 String description, String extraSettings, long fileEntryTypeId,
086 String version, long size, int readCount, long smallImageId,
087 long largeImageId, long custom1ImageId, long custom2ImageId)
088 throws Exception {
089
090 Connection con = null;
091 PreparedStatement ps = null;
092
093 try {
094 con = DataAccess.getUpgradeOptimizedConnection();
095
096 StringBundler sb = new StringBundler(9);
097
098 sb.append("insert into DLFileEntry (uuid_, fileEntryId, groupId, ");
099 sb.append("companyId, userId, userName, versionUserId, ");
100 sb.append("versionUserName, createDate, modifiedDate, ");
101 sb.append("repositoryId, folderId, name, extension, mimeType, ");
102 sb.append("title, description, extraSettings, fileEntryTypeId, ");
103 sb.append("version, size_, readCount, smallImageId, ");
104 sb.append("largeImageId, custom1ImageId, custom2ImageId) values (");
105 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
106 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?)");
107
108 String sql = sb.toString();
109
110 ps = con.prepareStatement(sql);
111
112 ps.setString(1, uuid);
113 ps.setLong(2, fileEntryId);
114 ps.setLong(3, groupId);
115 ps.setLong(4, companyId);
116 ps.setLong(5, userId);
117 ps.setString(6, userName);
118 ps.setLong(7, versionUserId);
119 ps.setString(8, versionUserName);
120 ps.setTimestamp(9, createDate);
121 ps.setTimestamp(10, modifiedDate);
122 ps.setLong(11, repositoryId);
123 ps.setLong(12, folderId);
124 ps.setString(13, name);
125 ps.setString(14, extension);
126 ps.setString(15, mimeType);
127 ps.setString(16, title);
128 ps.setString(17, description);
129 ps.setString(18, extraSettings);
130 ps.setLong(19, fileEntryTypeId);
131 ps.setString(20, version);
132 ps.setLong(21, size);
133 ps.setInt(22, readCount);
134 ps.setLong(23, smallImageId);
135 ps.setLong(24, largeImageId);
136 ps.setLong(25, custom1ImageId);
137 ps.setLong(26, custom2ImageId);
138
139 ps.executeUpdate();
140 }
141 finally {
142 DataAccess.cleanUp(con, ps);
143 }
144 }
145
146 protected void addDLFileVersion(
147 long fileVersionId, long groupId, long companyId, long userId,
148 String userName, Timestamp createDate, long repositoryId,
149 long folderId, long fileEntryId, String extension, String mimeType,
150 String title, String description, String changeLog,
151 String extraSettings, long fileEntryTypeId, String version,
152 long size, int status, long statusByUserId, String statusByUserName,
153 Timestamp statusDate)
154 throws Exception {
155
156 Connection con = null;
157 PreparedStatement ps = null;
158
159 try {
160 con = DataAccess.getUpgradeOptimizedConnection();
161
162 StringBundler sb = new StringBundler(9);
163
164 sb.append("insert into DLFileVersion (fileVersionId, groupId, ");
165 sb.append("companyId, userId, userName, createDate, ");
166 sb.append("modifiedDate, repositoryId, folderId, fileEntryId, ");
167 sb.append("extension, mimeType, title, description, changeLog, ");
168 sb.append("extraSettings, fileEntryTypeId, version, size_, ");
169 sb.append("status, statusByUserId, statusByUserName, statusDate) ");
170 sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
171 sb.append("?, ?, ?, ?, ?, ?, ?, ?)");
172
173 String sql = sb.toString();
174
175 ps = con.prepareStatement(sql);
176
177 ps.setLong(1, fileVersionId);
178 ps.setLong(2, groupId);
179 ps.setLong(3, companyId);
180 ps.setLong(4, userId);
181 ps.setString(5, userName);
182 ps.setTimestamp(6, createDate);
183 ps.setTimestamp(7, statusDate);
184 ps.setLong(8, repositoryId);
185 ps.setLong(9, folderId);
186 ps.setLong(10, fileEntryId);
187 ps.setString(11, extension);
188 ps.setString(12, mimeType);
189 ps.setString(13, title);
190 ps.setString(14, description);
191 ps.setString(15, changeLog);
192 ps.setString(16, extraSettings);
193 ps.setLong(17, fileEntryTypeId);
194 ps.setString(18, version);
195 ps.setLong(19, size);
196 ps.setInt(20, status);
197 ps.setLong(21, statusByUserId);
198 ps.setString(22, statusByUserName);
199 ps.setTimestamp(23, statusDate);
200
201 ps.executeUpdate();
202 }
203 finally {
204 DataAccess.cleanUp(con, ps);
205 }
206 }
207
208 protected void addDLFolderEntry(
209 String uuid, long folderId, long groupId, long companyId,
210 long userId, String userName, Timestamp createDate,
211 Timestamp modifiedDate, long repositoryId, long parentFolderId,
212 String name, String description, Timestamp lastPostDate)
213 throws Exception {
214
215 Connection con = null;
216 PreparedStatement ps = null;
217
218 try {
219 con = DataAccess.getUpgradeOptimizedConnection();
220
221 StringBundler sb = new StringBundler(5);
222
223 sb.append("insert into DLFolder (uuid_, folderId, groupId, ");
224 sb.append("companyId, userId, userName, createDate, ");
225 sb.append("modifiedDate, repositoryId, mountPoint, ");
226 sb.append("parentFolderId, name, description, lastPostDate) ");
227 sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
228
229 String sql = sb.toString();
230
231 ps = con.prepareStatement(sql);
232
233 ps.setString(1, uuid);
234 ps.setLong(2, folderId);
235 ps.setLong(3, groupId);
236 ps.setLong(4, companyId);
237 ps.setLong(5, userId);
238 ps.setString(6, userName);
239 ps.setTimestamp(7, createDate);
240 ps.setTimestamp(8, modifiedDate);
241 ps.setLong(9, repositoryId);
242 ps.setBoolean(10, false);
243 ps.setLong(11, parentFolderId);
244 ps.setString(12, name);
245 ps.setString(13, description);
246 ps.setTimestamp(14, lastPostDate);
247
248 ps.executeUpdate();
249 }
250 finally {
251 DataAccess.cleanUp(con, ps);
252 }
253 }
254
255 protected void addIGImageDLFileEntryType() throws Exception {
256 if (!PropsValues.DL_FILE_ENTRY_TYPE_IG_IMAGE_AUTO_CREATE_ON_UPGRADE) {
257 return;
258 }
259
260 Connection con = null;
261 PreparedStatement ps = null;
262 ResultSet rs = null;
263
264 try {
265 con = DataAccess.getUpgradeOptimizedConnection();
266
267 ps = con.prepareStatement("select distinct companyId from IGImage");
268
269 rs = ps.executeQuery();
270
271 while (rs.next()) {
272 long companyId = rs.getLong("companyId");
273
274 long groupId = getCompanyGroupId(companyId);
275 long userId = getDefaultUserId(companyId);
276 Timestamp now = new Timestamp(System.currentTimeMillis());
277
278 addIGImageDLFileEntryType(
279 groupId, companyId, userId, StringPool.BLANK, now, now);
280 }
281 }
282 finally {
283 DataAccess.cleanUp(con, ps, rs);
284 }
285 }
286
287 protected void addIGImageDLFileEntryType(
288 long groupId, long companyId, long userId, String userName,
289 Timestamp createDate, Timestamp modifiedDate)
290 throws Exception {
291
292 Connection con = null;
293 PreparedStatement ps = null;
294 ResultSet rs = null;
295
296 try {
297 con = DataAccess.getUpgradeOptimizedConnection();
298
299 StringBundler sb = new StringBundler(4);
300
301 sb.append("insert into DLFileEntryType (uuid_, groupId, ");
302 sb.append("companyId, userId, userName, createDate, ");
303 sb.append("modifiedDate, name, description, fileEntryTypeId) ");
304 sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
305
306 ps = con.prepareStatement(sb.toString());
307
308 ps.setString(1, PortalUUIDUtil.generate());
309 ps.setLong(2, groupId);
310 ps.setLong(3, companyId);
311 ps.setLong(4, userId);
312 ps.setString(5, userName);
313 ps.setTimestamp(6, createDate);
314 ps.setTimestamp(7, modifiedDate);
315 ps.setString(8, DLFileEntryTypeConstants.NAME_IG_IMAGE);
316 ps.setString(9, DLFileEntryTypeConstants.NAME_IG_IMAGE);
317 ps.setLong(10, increment());
318
319 ps.executeUpdate();
320 }
321 finally {
322 DataAccess.cleanUp(con, ps, rs);
323 }
324 }
325
326 protected void deleteConflictingIGPermissions(
327 String igResourceName, String dlResourceName)
328 throws Exception {
329
330 Connection con = null;
331 PreparedStatement ps = null;
332 ResultSet rs = null;
333
334 try {
335 con = DataAccess.getUpgradeOptimizedConnection();
336
337 DatabaseMetaData databaseMetaData = con.getMetaData();
338
339 boolean supportsBatchUpdates =
340 databaseMetaData.supportsBatchUpdates();
341
342 ps = con.prepareStatement(
343 "select companyId, scope, primKey, roleId from " +
344 "ResourcePermission where name = ?");
345
346 ps.setString(1, igResourceName);
347
348 rs = ps.executeQuery();
349
350 ps = con.prepareStatement(
351 "delete from ResourcePermission where name = ? and " +
352 "companyId = ? and scope = ? and primKey = ? and " +
353 "roleId = ?");
354
355 int count = 0;
356
357 while (rs.next()) {
358 ps.setString(1, dlResourceName);
359 ps.setLong(2, rs.getLong("companyId"));
360 ps.setInt(3, rs.getInt("scope"));
361 ps.setString(4, rs.getString("primKey"));
362 ps.setLong(5, rs.getLong("roleId"));
363
364 if (supportsBatchUpdates) {
365 ps.addBatch();
366
367 if (count == PropsValues.HIBERNATE_JDBC_BATCH_SIZE) {
368 ps.executeBatch();
369
370 count = 0;
371 }
372 else {
373 count++;
374 }
375 }
376 else {
377 ps.executeUpdate();
378 }
379 }
380
381 if (supportsBatchUpdates && (count > 0)) {
382 ps.executeBatch();
383 }
384 }
385 finally {
386 DataAccess.cleanUp(con, ps, rs);
387 }
388 }
389
390 @Override
391 protected void doUpgrade() throws Exception {
392 addIGImageDLFileEntryType();
393 updateIGFolderEntries();
394 updateIGImageEntries();
395 updateIGFolderPermissions();
396 updateIGImagePermissions();
397
398 migrateImageFiles();
399
400 UpgradeDocumentLibrary upgradeDocumentLibrary =
401 new UpgradeDocumentLibrary();
402
403 upgradeDocumentLibrary.updateSyncs();
404 }
405
406 protected long getBitwiseValue(
407 Map<String, Long> bitwiseValues, List<String> actionIds) {
408
409 long bitwiseValue = 0;
410
411 for (String actionId : actionIds) {
412 Long actionIdBitwiseValue = bitwiseValues.get(actionId);
413
414 if (actionIdBitwiseValue == null) {
415 continue;
416 }
417
418 bitwiseValue |= actionIdBitwiseValue;
419 }
420
421 return bitwiseValue;
422 }
423
424 protected Map<String, Long> getBitwiseValues(String name) throws Exception {
425 Connection con = null;
426 PreparedStatement ps = null;
427 ResultSet rs = null;
428
429 String currentShardName = null;
430
431 try {
432 currentShardName = ShardUtil.setTargetSource(
433 PropsValues.SHARD_DEFAULT_NAME);
434
435 con = DataAccess.getUpgradeOptimizedConnection();
436
437 ps = con.prepareStatement(
438 "select actionId, bitwiseValue from ResourceAction " +
439 "where name = ?");
440
441 ps.setString(1, name);
442
443 rs = ps.executeQuery();
444
445 Map<String, Long> bitwiseValues = new HashMap<String, Long>();
446
447 while (rs.next()) {
448 String actionId = rs.getString("actionId");
449 long bitwiseValue = rs.getLong("bitwiseValue");
450
451 bitwiseValues.put(actionId, bitwiseValue);
452 }
453
454 return bitwiseValues;
455 }
456 finally {
457 if (Validator.isNotNull(currentShardName)) {
458 ShardUtil.setTargetSource(currentShardName);
459 }
460
461 DataAccess.cleanUp(con, ps, rs);
462 }
463 }
464
465 protected long getCompanyGroupId(long companyId) throws Exception {
466 Connection con = null;
467 PreparedStatement ps = null;
468 ResultSet rs = null;
469
470 try {
471 con = DataAccess.getUpgradeOptimizedConnection();
472
473 ps = con.prepareStatement(
474 "select groupId from Group_ where classNameId = ? and " +
475 "classPK = ?");
476
477 ps.setLong(1, PortalUtil.getClassNameId(Company.class.getName()));
478 ps.setLong(2, companyId);
479
480 rs = ps.executeQuery();
481
482 if (rs.next()) {
483 return rs.getLong("groupId");
484 }
485
486 return 0;
487 }
488 finally {
489 DataAccess.cleanUp(con, ps, rs);
490 }
491 }
492
493 protected long getDefaultUserId(long companyId) throws Exception {
494 Connection con = null;
495 PreparedStatement ps = null;
496 ResultSet rs = null;
497
498 try {
499 con = DataAccess.getUpgradeOptimizedConnection();
500
501 ps = con.prepareStatement(
502 "select userId from User_ where companyId = ? and " +
503 "defaultUser = ?");
504
505 ps.setLong(1, companyId);
506 ps.setBoolean(2, true);
507
508 rs = ps.executeQuery();
509
510 if (rs.next()) {
511 return rs.getLong("userId");
512 }
513
514 return 0;
515 }
516 finally {
517 DataAccess.cleanUp(con, ps, rs);
518 }
519 }
520
521 protected Object[] getImage(long imageId) throws Exception {
522 Connection con = null;
523 PreparedStatement ps = null;
524 ResultSet rs = null;
525
526 try {
527 con = DataAccess.getUpgradeOptimizedConnection();
528
529 ps = con.prepareStatement(
530 "select type_, size_ from Image where imageId = " + imageId);
531
532 rs = ps.executeQuery();
533
534 if (rs.next()) {
535 String type = rs.getString("type_");
536 long size = rs.getInt("size_");
537
538 return new Object[] {type, size};
539 }
540
541 return null;
542 }
543 finally {
544 DataAccess.cleanUp(con, ps, rs);
545 }
546 }
547
548 protected List<String> getResourceActionIds(
549 Map<String, Long> bitwiseValues, long actionIdsLong) {
550
551 List<String> actionIds = new ArrayList<String>();
552
553 for (String actionId : bitwiseValues.keySet()) {
554 long bitwiseValue = bitwiseValues.get(actionId);
555
556 if ((actionIdsLong & bitwiseValue) == bitwiseValue) {
557 actionIds.add(actionId);
558 }
559 }
560
561 return actionIds;
562 }
563
564 protected void migrateFile(
565 long repositoryId, long companyId, String name, Image image)
566 throws Exception {
567
568 InputStream is = _sourceHook.getImageAsStream(image);
569
570 byte[] bytes = FileUtil.getBytes(is);
571
572 if (name == null) {
573 name = image.getImageId() + StringPool.PERIOD + image.getType();
574 }
575
576 if (DLStoreUtil.hasFile(companyId, repositoryId, name)) {
577 DLStoreUtil.deleteFile(companyId, repositoryId, name);
578 }
579
580 DLStoreUtil.addFile(companyId, repositoryId, name, false, bytes);
581 }
582
583 protected void migrateImage(long imageId) throws Exception {
584 Image image = ImageLocalServiceUtil.getImage(imageId);
585
586 try {
587 migrateFile(0, 0, null, image);
588 }
589 catch (Exception e) {
590 if (_log.isWarnEnabled()) {
591 _log.warn("Ignoring exception for image " + imageId, e);
592 }
593
594 return;
595 }
596
597 _sourceHook.deleteImage(image);
598 }
599
600 protected void migrateImage(
601 long fileEntryId, long companyId, long groupId, long folderId,
602 String name, long smallImageId, long largeImageId,
603 long custom1ImageId, long custom2ImageId)
604 throws Exception {
605
606 Image largeImage = null;
607
608 if (largeImageId != 0) {
609 largeImage = ImageLocalServiceUtil.getImage(largeImageId);
610
611 long repositoryId = DLFolderConstants.getDataRepositoryId(
612 groupId, folderId);
613
614 try {
615 migrateFile(repositoryId, companyId, name, largeImage);
616 }
617 catch (Exception e) {
618 if (_log.isWarnEnabled()) {
619 _log.warn(
620 "Ignoring exception for image " + largeImageId, e);
621 }
622 }
623 }
624
625 long thumbnailImageId = 0;
626
627 if (smallImageId != 0) {
628 thumbnailImageId = smallImageId;
629 }
630 else if (custom1ImageId != 0) {
631 thumbnailImageId = custom1ImageId;
632 }
633 else if (custom2ImageId != 0) {
634 thumbnailImageId = custom2ImageId;
635 }
636
637 Image thumbnailImage = null;
638
639 if (thumbnailImageId != 0) {
640 thumbnailImage = ImageLocalServiceUtil.getImage(thumbnailImageId);
641
642 Connection con = null;
643 PreparedStatement ps = null;
644 ResultSet rs = null;
645
646 try {
647 InputStream is = _sourceHook.getImageAsStream(thumbnailImage);
648
649 con = DataAccess.getUpgradeOptimizedConnection();
650
651 ps = con.prepareStatement(
652 "select max(fileVersionId) from DLFileVersion where " +
653 "fileEntryId = " + fileEntryId);
654
655 rs = ps.executeQuery();
656
657 if (rs.next()) {
658 long fileVersionId = rs.getLong(1);
659
660 ImageProcessorUtil.storeThumbnail(
661 companyId, groupId, fileEntryId, fileVersionId,
662 custom1ImageId, custom2ImageId, is,
663 thumbnailImage.getType());
664 }
665 }
666 catch (Exception e) {
667 if (_log.isWarnEnabled()) {
668 _log.warn(
669 "Ignoring exception for image " + thumbnailImageId, e);
670 }
671 }
672 finally {
673 DataAccess.cleanUp(con, ps, rs);
674 }
675 }
676
677 if (largeImageId != 0) {
678 _sourceHook.deleteImage(largeImage);
679
680 runSQL("delete from Image where imageId = " + largeImageId);
681 }
682
683 if ((largeImageId != thumbnailImageId) && (thumbnailImageId != 0)) {
684 _sourceHook.deleteImage(thumbnailImage);
685
686 runSQL("delete from Image where imageId = " + thumbnailImageId);
687 }
688 }
689
690 protected void migrateImageFiles() throws Exception {
691 Connection con = null;
692 PreparedStatement ps = null;
693 ResultSet rs = null;
694
695 try {
696 con = DataAccess.getUpgradeOptimizedConnection();
697
698 StringBundler sb = new StringBundler(8);
699
700 sb.append("select fileEntryId, companyId, groupId, folderId, ");
701 sb.append("name, smallImageId, largeImageId, custom1ImageId, ");
702 sb.append("custom2ImageId from DLFileEntry where ((smallImageId ");
703 sb.append("is not null) and (smallImageId != 0)) or ");
704 sb.append("((largeImageId is not null) and (largeImageId != 0)) ");
705 sb.append("or ((custom1ImageId is not null) and (custom1ImageId ");
706 sb.append("!= 0)) or ((custom2ImageId is not null) and ");
707 sb.append("(custom2ImageId != 0))");
708
709 ps = con.prepareStatement(sb.toString());
710
711 rs = ps.executeQuery();
712
713 while (rs.next()) {
714 long fileEntryId = rs.getLong("fileEntryId");
715 long companyId = rs.getLong("companyId");
716 long groupId = rs.getLong("groupId");
717 long folderId = rs.getLong("folderId");
718 String name = rs.getString("name");
719 long smallImageId = rs.getLong("smallImageId");
720 long largeImageId = rs.getLong("largeImageId");
721 long custom1ImageId = rs.getLong("custom1ImageId");
722 long custom2ImageId = rs.getLong("custom2ImageId");
723
724 migrateImage(
725 fileEntryId, companyId, groupId, folderId, name,
726 smallImageId, largeImageId, custom1ImageId, custom2ImageId);
727 }
728 }
729 finally {
730 DataAccess.cleanUp(con, ps, rs);
731 }
732
733 if (_sourceHookClassName.equals(DLHook.class.getName())) {
734 return;
735 }
736
737 try {
738 con = DataAccess.getUpgradeOptimizedConnection();
739
740 ps = con.prepareStatement("select imageId from Image");
741
742 rs = ps.executeQuery();
743
744 while (rs.next()) {
745 long imageId = rs.getLong("imageId");
746
747 migrateImage(imageId);
748 }
749 }
750 finally {
751 DataAccess.cleanUp(con, ps, rs);
752 }
753
754 if (_sourceHookClassName.equals(DatabaseHook.class.getName())) {
755 runSQL("update Image set text_ = ''");
756 }
757 }
758
759 protected void updateIGFolderEntries() throws Exception {
760 Connection con = null;
761 PreparedStatement ps = null;
762 ResultSet rs = null;
763
764 try {
765 con = DataAccess.getUpgradeOptimizedConnection();
766
767 ps = con.prepareStatement(
768 "select * from IGFolder order by folderId asc");
769
770 rs = ps.executeQuery();
771
772 Map<Long, Long> folderIds = new HashMap<Long, Long>();
773
774 while (rs.next()) {
775 String uuid = rs.getString("uuid_");
776 long folderId = rs.getLong("folderId");
777 long groupId = rs.getLong("groupId");
778 long companyId = rs.getLong("companyId");
779 long userId = rs.getLong("userId");
780 String userName = rs.getString("userName");
781 Timestamp createDate = rs.getTimestamp("createDate");
782 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
783 long parentFolderId = rs.getLong("parentFolderId");
784 String name = rs.getString("name");
785 String description = rs.getString("description");
786
787 if (folderIds.containsKey(parentFolderId)) {
788 parentFolderId = folderIds.get(parentFolderId);
789 }
790
791 boolean update = updateIGImageFolderId(
792 groupId, name, parentFolderId, folderId, folderIds);
793
794 if (!update) {
795 addDLFolderEntry(
796 uuid, folderId, groupId, companyId, userId, userName,
797 createDate, modifiedDate, groupId, parentFolderId, name,
798 description, modifiedDate);
799 }
800 }
801
802 runSQL("drop table IGFolder");
803 }
804 finally {
805 DataAccess.cleanUp(con, ps, rs);
806 }
807 }
808
809 protected void updateIGFolderPermissions() throws Exception {
810 deleteConflictingIGPermissions(
811 _IG_FOLDER_CLASS_NAME, DLFolder.class.getName());
812
813 updateIGtoDLPermissions(
814 _IG_FOLDER_CLASS_NAME, DLFolder.class.getName());
815 }
816
817 protected void updateIGImageEntries() throws Exception {
818 Connection con = null;
819 PreparedStatement ps = null;
820 ResultSet rs = null;
821
822 try {
823 con = DataAccess.getUpgradeOptimizedConnection();
824
825 ps = con.prepareStatement(
826 "select fileEntryTypeId, companyId from DLFileEntryType " +
827 "where name = ?");
828
829 ps.setString(1, DLFileEntryTypeConstants.NAME_IG_IMAGE);
830
831 rs = ps.executeQuery();
832
833 boolean hasIGImageFileEntryType = false;
834
835 while (rs.next()) {
836 long fileEntryTypeId = rs.getLong("fileEntryTypeId");
837 long companyId = rs.getLong("companyId");
838
839 updateIGImageEntries(companyId, fileEntryTypeId);
840
841 hasIGImageFileEntryType = true;
842 }
843
844 if (!hasIGImageFileEntryType) {
845 updateIGImageEntries(0, 0);
846 }
847
848 runSQL("drop table IGImage");
849 }
850 finally {
851 DataAccess.cleanUp(con, ps, rs);
852 }
853 }
854
855 protected void updateIGImageEntries(long companyId, long fileEntryTypeId)
856 throws Exception {
857
858 Connection con = null;
859 PreparedStatement ps = null;
860 ResultSet rs = null;
861
862 try {
863 con = DataAccess.getUpgradeOptimizedConnection();
864
865 String sql = "select * from IGImage";
866
867 if (companyId != 0) {
868 sql = "select * from IGImage where companyId = ?";
869 }
870
871 ps = con.prepareStatement(sql);
872
873 if (companyId != 0) {
874 ps.setLong(1, companyId);
875 }
876
877 rs = ps.executeQuery();
878
879 while (rs.next()) {
880 String uuid = rs.getString("uuid_");
881 long imageId = rs.getLong("imageId");
882 long groupId = rs.getLong("groupId");
883 companyId = rs.getLong("companyId");
884 long userId = rs.getLong("userId");
885 String userName = rs.getString("userName");
886 Timestamp createDate = rs.getTimestamp("createDate");
887 Timestamp modifiedDate = rs.getTimestamp("modifiedDate");
888 long folderId = rs.getLong("folderId");
889 String title = rs.getString("name");
890 String description = rs.getString("description");
891 long smallImageId = rs.getLong("smallImageId");
892 long largeImageId = rs.getLong("largeImageId");
893 long custom1ImageId = rs.getLong("custom1ImageId");
894 long custom2ImageId = rs.getLong("custom2ImageId");
895
896 Object[] image = getImage(largeImageId);
897
898 if (image == null) {
899 continue;
900 }
901
902 String extension = (String)image[0];
903
904 String mimeType = MimeTypesUtil.getExtensionContentType(
905 extension);
906
907 String name = String.valueOf(
908 increment(DLFileEntry.class.getName()));
909
910 long size = (Long)image[1];
911
912 try {
913 addDLFileEntry(
914 uuid, imageId, groupId, companyId, userId, userName,
915 userId, userName, createDate, modifiedDate, groupId,
916 folderId, name, extension, mimeType, title, description,
917 StringPool.BLANK, fileEntryTypeId, "1.0", size, 0,
918 smallImageId, largeImageId, custom1ImageId,
919 custom2ImageId);
920 }
921 catch (Exception e) {
922 title = title.concat(StringPool.SPACE).concat(
923 String.valueOf(imageId));
924
925 addDLFileEntry(
926 uuid, imageId, groupId, companyId, userId, userName,
927 userId, userName, createDate, modifiedDate, groupId,
928 folderId, name, extension, mimeType, title, description,
929 StringPool.BLANK, fileEntryTypeId, "1.0", size, 0,
930 smallImageId, largeImageId, custom1ImageId,
931 custom2ImageId);
932 }
933
934 addDLFileVersion(
935 increment(), groupId, companyId, userId, userName,
936 createDate, groupId, folderId, imageId, extension, mimeType,
937 title, description, StringPool.BLANK, StringPool.BLANK,
938 fileEntryTypeId, "1.0", size, 0, userId, userName,
939 modifiedDate);
940 }
941 }
942 finally {
943 DataAccess.cleanUp(con, ps, rs);
944 }
945 }
946
947 protected boolean updateIGImageFolderId(
948 long groupId, String name, long parentFolderId, long folderId,
949 Map<Long, Long> folderIds)
950 throws Exception {
951
952 Connection con = null;
953 PreparedStatement ps = null;
954 ResultSet rs = null;
955
956 try {
957 con = DataAccess.getUpgradeOptimizedConnection();
958
959 ps = con.prepareStatement(
960 "select folderId from DLFolder where groupId = " + groupId +
961 " and parentFolderId = " + parentFolderId +
962 " and name = ?");
963
964 ps.setString(1, name);
965
966 rs = ps.executeQuery();
967
968 if (rs.next()) {
969 long newFolderId = rs.getLong("folderId");
970
971 runSQL(
972 "update IGImage set folderId = " + newFolderId +
973 " where folderId = " + folderId);
974
975 folderIds.put(folderId, newFolderId);
976
977 return true;
978 }
979 }
980 finally {
981 DataAccess.cleanUp(con, ps, rs);
982 }
983
984 return false;
985 }
986
987 protected void updateIGImagePermissions() throws Exception {
988 deleteConflictingIGPermissions(
989 _IG_IMAGE_CLASS_NAME, DLFileEntry.class.getName());
990
991 updateIGtoDLPermissions(
992 _IG_IMAGE_CLASS_NAME, DLFileEntry.class.getName());
993 }
994
995 protected void updateIGtoDLPermissions(
996 String igResourceName, String dlResourceName)
997 throws Exception {
998
999 Map<String, Long> igBitwiseValues = getBitwiseValues(igResourceName);
1000
1001 if (igBitwiseValues.isEmpty()) {
1002 if (_log.isWarnEnabled()) {
1003 _log.warn(
1004 "Resource actions do not exist for " + igResourceName);
1005 }
1006
1007 return;
1008 }
1009
1010 Map<String, Long> dlBitwiseValues = getBitwiseValues(dlResourceName);
1011
1012 if (dlBitwiseValues.isEmpty()) {
1013 if (_log.isWarnEnabled()) {
1014 _log.warn(
1015 "Resource actions do not exist for " + dlResourceName);
1016 }
1017
1018 return;
1019 }
1020
1021
1022
1023
1024
1025
1026
1027
1028 for (int i = 0; i < Math.pow(2, igBitwiseValues.size()); i++) {
1029 List<String> igActionIds = getResourceActionIds(igBitwiseValues, i);
1030
1031 if (igResourceName.equals(_IG_FOLDER_CLASS_NAME)) {
1032 Collections.replaceAll(
1033 igActionIds, "ADD_IMAGE", "ADD_DOCUMENT");
1034 }
1035
1036 long dlActionIdsLong = getBitwiseValue(
1037 dlBitwiseValues, igActionIds);
1038
1039 runSQL(
1040 "update ResourcePermission set name = '" + dlResourceName +
1041 "', actionIds = " + dlActionIdsLong + " where name = '" +
1042 igResourceName + "'" + " and actionIds = " + i);
1043 }
1044 }
1045
1046 private static final String _IG_FOLDER_CLASS_NAME =
1047 "com.liferay.portlet.imagegallery.model.IGFolder";
1048
1049 private static final String _IG_IMAGE_CLASS_NAME =
1050 "com.liferay.portlet.imagegallery.model.IGImage";
1051
1052 private static Log _log = LogFactoryUtil.getLog(UpgradeImageGallery.class);
1053
1054 private Hook _sourceHook;
1055 private String _sourceHookClassName;
1056
1057 }