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