001
014
015 package com.liferay.portal.kernel.upgrade.v6_2_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.upgrade.UpgradeProcess;
021 import com.liferay.portal.kernel.util.ArrayUtil;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.MimeTypesUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
027 import com.liferay.portal.model.CompanyConstants;
028 import com.liferay.portal.model.ResourceConstants;
029 import com.liferay.portal.model.ResourcePermission;
030 import com.liferay.portal.model.RoleConstants;
031 import com.liferay.portal.security.permission.ActionKeys;
032 import com.liferay.portal.util.PortalUtil;
033 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034 import com.liferay.portlet.documentlibrary.model.DLFolder;
035 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
036 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
037
038 import java.sql.Connection;
039 import java.sql.PreparedStatement;
040 import java.sql.ResultSet;
041 import java.sql.Timestamp;
042
043 import java.util.ArrayList;
044 import java.util.HashMap;
045 import java.util.List;
046 import java.util.Map;
047
048
051 public abstract class BaseUpgradeAttachments extends UpgradeProcess {
052
053 protected long addDLFileEntry(
054 long groupId, long companyId, long userId, String className,
055 long classPK, String userName, Timestamp createDate,
056 long repositoryId, long folderId, String name, String extension,
057 String mimeType, String title, long size)
058 throws Exception {
059
060 Connection con = null;
061 PreparedStatement ps = null;
062
063 try {
064 long fileEntryId = increment();
065
066 con = DataAccess.getUpgradeOptimizedConnection();
067
068 StringBundler sb = new StringBundler(9);
069
070 sb.append("insert into DLFileEntry (uuid_, fileEntryId, groupId, ");
071 sb.append("companyId, userId, userName, createDate, ");
072 sb.append("modifiedDate, classNameId, classPK, repositoryId, ");
073 sb.append("folderId, name, extension, mimeType, title, ");
074 sb.append("description, extraSettings, fileEntryTypeId, version, ");
075 sb.append("size_, readCount, smallImageId, largeImageId, ");
076 sb.append("custom1ImageId, custom2ImageId) values (?, ?, ?, ?, ");
077 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
078 sb.append("?, ?, ?, ?)");
079
080 String sql = sb.toString();
081
082 ps = con.prepareStatement(sql);
083
084 ps.setString(1, PortalUUIDUtil.generate());
085 ps.setLong(2, fileEntryId);
086 ps.setLong(3, groupId);
087 ps.setLong(4, companyId);
088 ps.setLong(5, userId);
089 ps.setString(6, userName);
090 ps.setTimestamp(7, createDate);
091 ps.setTimestamp(8, createDate);
092 ps.setLong(9, PortalUtil.getClassNameId(className));
093 ps.setLong(10, classPK);
094 ps.setLong(11, repositoryId);
095 ps.setLong(12, folderId);
096 ps.setString(13, name);
097 ps.setString(14, extension);
098 ps.setString(15, mimeType);
099 ps.setString(16, title);
100 ps.setString(17, StringPool.BLANK);
101 ps.setString(18, StringPool.BLANK);
102 ps.setLong(19, 0);
103 ps.setString(20, "1.0");
104 ps.setLong(21, size);
105 ps.setInt(22, 0);
106 ps.setLong(23, 0);
107 ps.setLong(24, 0);
108 ps.setLong(25, 0);
109 ps.setLong(26, 0);
110
111 ps.executeUpdate();
112
113 Map<String, Long> bitwiseValues = getBitwiseValues(
114 DLFileEntry.class.getName());
115
116 List<String> actionIds = new ArrayList<>();
117
118 actionIds.add(ActionKeys.VIEW);
119
120 long bitwiseValue = getBitwiseValue(bitwiseValues, actionIds);
121
122 addResourcePermission(
123 companyId, DLFileEntry.class.getName(), fileEntryId,
124 getRoleId(companyId, RoleConstants.GUEST), bitwiseValue);
125 addResourcePermission(
126 companyId, DLFileEntry.class.getName(), fileEntryId,
127 getRoleId(companyId, RoleConstants.SITE_MEMBER), bitwiseValue);
128
129 return fileEntryId;
130 }
131 catch (Exception e) {
132 if (_log.isWarnEnabled()) {
133 _log.warn("Unable to add file entry " + name, e);
134 }
135
136 return -1;
137 }
138 finally {
139 DataAccess.cleanUp(con, ps);
140 }
141 }
142
143 protected void addDLFileVersion(
144 long fileVersionId, long groupId, long companyId, long userId,
145 String userName, Timestamp createDate, long repositoryId,
146 long folderId, long fileEntryId, String extension, String mimeType,
147 String title, long size)
148 throws Exception {
149
150 Connection con = null;
151 PreparedStatement ps = null;
152
153 try {
154 con = DataAccess.getUpgradeOptimizedConnection();
155
156 StringBundler sb = new StringBundler(8);
157
158 sb.append("insert into DLFileVersion (uuid_, fileVersionId, ");
159 sb.append("groupId, companyId, userId, userName, createDate, ");
160 sb.append("modifiedDate, repositoryId, folderId, fileEntryId, ");
161 sb.append("extension, mimeType, title, description, changeLog, ");
162 sb.append("extraSettings, fileEntryTypeId, version, size_, ");
163 sb.append("status, statusByUserId, statusByUserName, statusDate) ");
164 sb.append("values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ");
165 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?)");
166
167 String sql = sb.toString();
168
169 ps = con.prepareStatement(sql);
170
171 ps.setString(1, PortalUUIDUtil.generate());
172 ps.setLong(2, fileVersionId);
173 ps.setLong(3, groupId);
174 ps.setLong(4, companyId);
175 ps.setLong(5, userId);
176 ps.setString(6, userName);
177 ps.setTimestamp(7, createDate);
178 ps.setTimestamp(8, createDate);
179 ps.setLong(9, repositoryId);
180 ps.setLong(10, folderId);
181 ps.setLong(11, fileEntryId);
182 ps.setString(12, extension);
183 ps.setString(13, mimeType);
184 ps.setString(14, title);
185 ps.setString(15, StringPool.BLANK);
186 ps.setString(16, StringPool.BLANK);
187 ps.setString(17, StringPool.BLANK);
188 ps.setLong(18, 0);
189 ps.setString(19, "1.0");
190 ps.setLong(20, size);
191 ps.setInt(21, 0);
192 ps.setLong(22, userId);
193 ps.setString(23, userName);
194 ps.setTimestamp(24, createDate);
195
196 ps.executeUpdate();
197 }
198 catch (Exception e) {
199 if (_log.isWarnEnabled()) {
200 _log.warn(
201 "Unable to add file version 1.0 for file entry " + title,
202 e);
203 }
204 }
205 finally {
206 DataAccess.cleanUp(con, ps);
207 }
208 }
209
210 protected long addDLFolder(
211 long folderId, long groupId, long companyId, long userId,
212 String userName, Timestamp createDate, long repositoryId,
213 boolean mountPoint, long parentFolderId, String name,
214 boolean hidden)
215 throws Exception {
216
217 Connection con = null;
218 PreparedStatement ps = null;
219
220 try {
221 con = DataAccess.getUpgradeOptimizedConnection();
222
223 StringBundler sb = new StringBundler(8);
224
225 sb.append("insert into DLFolder (uuid_, folderId, groupId, ");
226 sb.append("companyId, userId, userName, createDate, ");
227 sb.append("modifiedDate, repositoryId, mountPoint, ");
228 sb.append("parentFolderId, name, description, lastPostDate, ");
229 sb.append("defaultFileEntryTypeId, hidden_, ");
230 sb.append("overrideFileEntryTypes, status, statusByUserId, ");
231 sb.append("statusByUserName, statusDate) values (?, ?, ?, ?, ?, ");
232 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
233
234 String sql = sb.toString();
235
236 ps = con.prepareStatement(sql);
237
238 ps.setString(1, PortalUUIDUtil.generate());
239 ps.setLong(2, folderId);
240 ps.setLong(3, groupId);
241 ps.setLong(4, companyId);
242 ps.setLong(5, userId);
243 ps.setString(6, userName);
244 ps.setTimestamp(7, createDate);
245 ps.setTimestamp(8, createDate);
246 ps.setLong(9, repositoryId);
247 ps.setBoolean(10, mountPoint);
248 ps.setLong(11, parentFolderId);
249 ps.setString(12, name);
250 ps.setString(13, StringPool.BLANK);
251 ps.setTimestamp(14, createDate);
252 ps.setLong(15, 0);
253 ps.setBoolean(16, hidden);
254 ps.setBoolean(17, false);
255 ps.setLong(18, 0);
256 ps.setLong(19, userId);
257 ps.setString(20, userName);
258 ps.setTimestamp(21, createDate);
259
260 ps.executeUpdate();
261
262 Map<String, Long> bitwiseValues = getBitwiseValues(
263 DLFolder.class.getName());
264
265 List<String> guestActionIds = new ArrayList<>();
266
267 guestActionIds.add(ActionKeys.VIEW);
268
269 long guestBitwiseValue = getBitwiseValue(
270 bitwiseValues, guestActionIds);
271
272 addResourcePermission(
273 companyId, DLFolder.class.getName(), folderId,
274 getRoleId(companyId, RoleConstants.GUEST), guestBitwiseValue);
275
276 List<String> siteMemberActionIds = new ArrayList<>();
277
278 siteMemberActionIds.add(ActionKeys.ADD_DOCUMENT);
279 siteMemberActionIds.add(ActionKeys.ADD_SUBFOLDER);
280 siteMemberActionIds.add(ActionKeys.VIEW);
281
282 long siteMemberBitwiseValue = getBitwiseValue(
283 bitwiseValues, siteMemberActionIds);
284
285 addResourcePermission(
286 companyId, DLFolder.class.getName(), folderId,
287 getRoleId(companyId, RoleConstants.SITE_MEMBER),
288 siteMemberBitwiseValue);
289
290 return folderId;
291 }
292 catch (Exception e) {
293 if (_log.isWarnEnabled()) {
294 _log.warn("Unable to add folder " + name, e);
295 }
296
297 return -1;
298 }
299 finally {
300 DataAccess.cleanUp(con, ps);
301 }
302 }
303
304 protected long addRepository(
305 long groupId, long companyId, long userId, String userName,
306 Timestamp createDate, long classNameId, String portletId)
307 throws Exception {
308
309 long repositoryId = increment();
310
311 long folderId = addDLFolder(
312 increment(), groupId, companyId, userId, userName, createDate,
313 repositoryId, true, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
314 portletId, true);
315
316 if (folderId < 0) {
317 return -1;
318 }
319
320 Connection con = null;
321 PreparedStatement ps = null;
322
323 try {
324 con = DataAccess.getUpgradeOptimizedConnection();
325
326 StringBundler sb = new StringBundler(5);
327
328 sb.append("insert into Repository (uuid_, repositoryId, groupId, ");
329 sb.append("companyId, userId, userName, createDate, ");
330 sb.append("modifiedDate, classNameId, name, description, ");
331 sb.append("portletId, typeSettings, dlFolderId) values (?, ?, ?, ");
332 sb.append("?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
333
334 ps = con.prepareStatement(sb.toString());
335
336 ps.setString(1, PortalUUIDUtil.generate());
337 ps.setLong(2, repositoryId);
338 ps.setLong(3, groupId);
339 ps.setLong(4, companyId);
340 ps.setLong(5, userId);
341 ps.setString(6, userName);
342 ps.setTimestamp(7, createDate);
343 ps.setTimestamp(8, createDate);
344 ps.setLong(9, classNameId);
345 ps.setString(10, portletId);
346 ps.setString(11, StringPool.BLANK);
347 ps.setString(12, portletId);
348 ps.setString(13, StringPool.BLANK);
349 ps.setLong(14, folderId);
350
351 ps.executeUpdate();
352
353 return repositoryId;
354 }
355 catch (Exception e) {
356 if (_log.isWarnEnabled()) {
357 _log.warn(
358 "Unable to add repository for portlet " + portletId, e);
359 }
360
361 return -1;
362 }
363 finally {
364 DataAccess.cleanUp(con, ps);
365 }
366 }
367
368 protected void addResourcePermission(
369 long companyId, String className, long primKey, long roleId,
370 long actionIds)
371 throws Exception {
372
373 Connection con = null;
374 PreparedStatement ps = null;
375
376 try {
377 long resourcePermissionId = increment(
378 ResourcePermission.class.getName());
379
380 con = DataAccess.getUpgradeOptimizedConnection();
381
382 StringBundler sb = new StringBundler(3);
383
384 sb.append("insert into ResourcePermission (resourcePermissionId, ");
385 sb.append("companyId, name, scope, primKey, roleId, ownerId, ");
386 sb.append("actionIds) values (?, ?, ?, ?, ?, ?, ?, ?)");
387
388 String sql = sb.toString();
389
390 ps = con.prepareStatement(sql);
391
392 ps.setLong(1, resourcePermissionId);
393 ps.setLong(2, companyId);
394 ps.setString(3, className);
395 ps.setInt(4, ResourceConstants.SCOPE_INDIVIDUAL);
396 ps.setLong(5, primKey);
397 ps.setLong(6, roleId);
398 ps.setLong(7, 0);
399 ps.setLong(8, actionIds);
400
401 ps.executeUpdate();
402 }
403 catch (Exception e) {
404 if (_log.isWarnEnabled()) {
405 _log.warn("Unable to add resource permission " + className, e);
406 }
407 }
408 finally {
409 DataAccess.cleanUp(con, ps);
410 }
411 }
412
413 @Override
414 protected void doUpgrade() throws Exception {
415 updateAttachments();
416 }
417
418 protected String[] getAttachments(
419 long companyId, long containerModelId, long resourcePrimKey)
420 throws Exception {
421
422 String dirName = getDirName(containerModelId, resourcePrimKey);
423
424 return DLStoreUtil.getFileNames(
425 companyId, CompanyConstants.SYSTEM, dirName);
426 }
427
428 protected long getBitwiseValue(
429 Map<String, Long> bitwiseValues, List<String> actionIds) {
430
431 long bitwiseValue = 0;
432
433 for (String actionId : actionIds) {
434 Long actionIdBitwiseValue = bitwiseValues.get(actionId);
435
436 if (actionIdBitwiseValue == null) {
437 continue;
438 }
439
440 bitwiseValue |= actionIdBitwiseValue;
441 }
442
443 return bitwiseValue;
444 }
445
446 protected Map<String, Long> getBitwiseValues(String name) throws Exception {
447 Map<String, Long> bitwiseValues = _bitwiseValues.get(name);
448
449 if (bitwiseValues != null) {
450 return bitwiseValues;
451 }
452
453 Connection con = null;
454 PreparedStatement ps = null;
455 ResultSet rs = null;
456
457 try {
458 con = DataAccess.getUpgradeOptimizedConnection();
459
460 ps = con.prepareStatement(
461 "select actionId, bitwiseValue from ResourceAction " +
462 "where name = ?");
463
464 ps.setString(1, name);
465
466 rs = ps.executeQuery();
467
468 bitwiseValues = new HashMap<>();
469
470 while (rs.next()) {
471 String actionId = rs.getString("actionId");
472 long bitwiseValue = rs.getLong("bitwiseValue");
473
474 bitwiseValues.put(actionId, bitwiseValue);
475 }
476
477 _bitwiseValues.put(name, bitwiseValues);
478
479 return bitwiseValues;
480 }
481 finally {
482 DataAccess.cleanUp(con, ps, rs);
483 }
484 }
485
486 protected abstract String getClassName();
487
488 protected long getClassNameId() {
489 return PortalUtil.getClassNameId(getClassName());
490 }
491
492 protected long getContainerModelFolderId(
493 long groupId, long companyId, long resourcePrimKey,
494 long containerModelId, long userId, String userName,
495 Timestamp createDate)
496 throws Exception {
497
498 return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
499 }
500
501 protected abstract String getDirName(
502 long containerModelId, long resourcePrimKey);
503
504 protected long getFolderId(
505 long groupId, long companyId, long userId, String userName,
506 Timestamp createDate, long repositoryId, long parentFolderId,
507 String name, boolean hidden)
508 throws Exception {
509
510 if ((repositoryId < 0) || (parentFolderId < 0)) {
511 return -1;
512 }
513
514 Connection con = null;
515 PreparedStatement ps = null;
516 ResultSet rs = null;
517
518 try {
519 con = DataAccess.getUpgradeOptimizedConnection();
520
521 ps = con.prepareStatement(
522 "select folderId from DLFolder where repositoryId = ? and " +
523 "parentFolderId = ? and name = ?");
524
525 ps.setLong(1, repositoryId);
526 ps.setLong(2, parentFolderId);
527 ps.setString(3, name);
528
529 rs = ps.executeQuery();
530
531 while (rs.next()) {
532 long folderId = rs.getLong(1);
533
534 return folderId;
535 }
536 }
537 finally {
538 DataAccess.cleanUp(con, ps);
539 }
540
541 return addDLFolder(
542 increment(), groupId, companyId, userId, userName, createDate,
543 repositoryId, false, parentFolderId, name, hidden);
544 }
545
546 protected abstract String getPortletId();
547
548 protected long getRepositoryId(
549 long groupId, long companyId, long userId, String userName,
550 Timestamp createDate, long classNameId, String portletId)
551 throws Exception {
552
553 Connection con = null;
554 PreparedStatement ps = null;
555 ResultSet rs = null;
556
557 try {
558 con = DataAccess.getUpgradeOptimizedConnection();
559
560 ps = con.prepareStatement(
561 "select repositoryId from Repository where groupId = ? and " +
562 "name = ? and portletId = ?");
563
564 ps.setLong(1, groupId);
565 ps.setString(2, portletId);
566 ps.setString(3, portletId);
567
568 rs = ps.executeQuery();
569
570 while (rs.next()) {
571 long repositoryId = rs.getLong(1);
572
573 return repositoryId;
574 }
575 }
576 finally {
577 DataAccess.cleanUp(con, ps);
578 }
579
580 return addRepository(
581 groupId, companyId, userId, userName, createDate, classNameId,
582 portletId);
583 }
584
585 protected long getRoleId(long companyId, String name) throws Exception {
586 String roleIdsKey = companyId + StringPool.POUND + name;
587
588 Long roleId = _roleIds.get(roleIdsKey);
589
590 if (roleId != null) {
591 return roleId;
592 }
593
594 Connection con = null;
595 PreparedStatement ps = null;
596 ResultSet rs = null;
597
598 try {
599 con = DataAccess.getUpgradeOptimizedConnection();
600
601 ps = con.prepareStatement(
602 "select roleId from Role_ where companyId = ? and name = ?");
603
604 ps.setLong(1, companyId);
605 ps.setString(2, name);
606
607 rs = ps.executeQuery();
608
609 if (rs.next()) {
610 roleId = rs.getLong("roleId");
611 }
612
613 _roleIds.put(roleIdsKey, roleId);
614
615 return roleId;
616 }
617 finally {
618 DataAccess.cleanUp(con, ps, rs);
619 }
620 }
621
622 protected abstract void updateAttachments() throws Exception;
623
624 protected void updateEntryAttachments(
625 long companyId, long groupId, long resourcePrimKey,
626 long containerModelId, long userId, String userName)
627 throws Exception {
628
629 String[] attachments = getAttachments(
630 companyId, containerModelId, resourcePrimKey);
631
632 if (ArrayUtil.isEmpty(attachments)) {
633 return;
634 }
635
636 Timestamp createDate = new Timestamp(System.currentTimeMillis());
637
638 long repositoryId = getRepositoryId(
639 groupId, companyId, userId, userName, createDate,
640 PortalUtil.getClassNameId(_LIFERAY_REPOSITORY_CLASS_NAME),
641 getPortletId());
642
643 if (repositoryId < 0) {
644 return;
645 }
646
647 long containerModelFolderId = getContainerModelFolderId(
648 groupId, companyId, resourcePrimKey, containerModelId, userId,
649 userName, createDate);
650
651 if (containerModelFolderId < 0) {
652 return;
653 }
654
655 for (String attachment : attachments) {
656 String name = String.valueOf(
657 increment(DLFileEntry.class.getName()));
658
659 String title = FileUtil.getShortFileName(attachment);
660
661 String extension = FileUtil.getExtension(title);
662
663 String mimeType = MimeTypesUtil.getExtensionContentType(extension);
664
665 try {
666 long size = DLStoreUtil.getFileSize(
667 companyId, CompanyConstants.SYSTEM, attachment);
668
669 long fileEntryId = addDLFileEntry(
670 groupId, companyId, userId, getClassName(), resourcePrimKey,
671 userName, createDate, repositoryId, containerModelFolderId,
672 name, extension, mimeType, title, size);
673
674 if (fileEntryId < 0) {
675 continue;
676 }
677
678 addDLFileVersion(
679 increment(), groupId, companyId, userId, userName,
680 createDate, repositoryId, containerModelFolderId,
681 fileEntryId, extension, mimeType, title, size);
682
683 byte[] bytes = DLStoreUtil.getFileAsBytes(
684 companyId, CompanyConstants.SYSTEM, attachment);
685
686 DLStoreUtil.addFile(
687 companyId, containerModelFolderId, name, false, bytes);
688 }
689 catch (Exception e) {
690 if (_log.isWarnEnabled()) {
691 _log.warn("Unable to add attachment " + attachment, e);
692 }
693 }
694
695 try {
696 DLStoreUtil.deleteFile(
697 companyId, CompanyConstants.SYSTEM, attachment);
698 }
699 catch (Exception e) {
700 if (_log.isWarnEnabled()) {
701 _log.warn("Unable to delete attachment " + attachment, e);
702 }
703 }
704 }
705 }
706
707 private static final String _LIFERAY_REPOSITORY_CLASS_NAME =
708 "com.liferay.portal.repository.liferayrepository.LiferayRepository";
709
710 private static final Log _log = LogFactoryUtil.getLog(
711 BaseUpgradeAttachments.class);
712
713 private final Map<String, Map<String, Long>> _bitwiseValues =
714 new HashMap<>();
715 private final Map<String, Long> _roleIds = new HashMap<>();
716
717 }