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