001
014
015 package com.liferay.portlet.documentlibrary.service.impl;
016
017 import com.liferay.portal.ExpiredLockException;
018 import com.liferay.portal.InvalidLockException;
019 import com.liferay.portal.NoSuchLockException;
020 import com.liferay.portal.NoSuchModelException;
021 import com.liferay.portal.kernel.dao.orm.QueryDefinition;
022 import com.liferay.portal.kernel.dao.orm.QueryUtil;
023 import com.liferay.portal.kernel.exception.PortalException;
024 import com.liferay.portal.kernel.exception.SystemException;
025 import com.liferay.portal.kernel.image.ImageBag;
026 import com.liferay.portal.kernel.image.ImageToolUtil;
027 import com.liferay.portal.kernel.increment.BufferedIncrement;
028 import com.liferay.portal.kernel.increment.NumberIncrement;
029 import com.liferay.portal.kernel.log.Log;
030 import com.liferay.portal.kernel.log.LogFactoryUtil;
031 import com.liferay.portal.kernel.search.Indexable;
032 import com.liferay.portal.kernel.search.IndexableType;
033 import com.liferay.portal.kernel.search.Indexer;
034 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
035 import com.liferay.portal.kernel.search.SearchException;
036 import com.liferay.portal.kernel.util.DigesterUtil;
037 import com.liferay.portal.kernel.util.GetterUtil;
038 import com.liferay.portal.kernel.util.ListUtil;
039 import com.liferay.portal.kernel.util.ObjectValuePair;
040 import com.liferay.portal.kernel.util.OrderByComparator;
041 import com.liferay.portal.kernel.util.ParamUtil;
042 import com.liferay.portal.kernel.util.PropsKeys;
043 import com.liferay.portal.kernel.util.StreamUtil;
044 import com.liferay.portal.kernel.util.StringBundler;
045 import com.liferay.portal.kernel.util.StringPool;
046 import com.liferay.portal.kernel.util.StringUtil;
047 import com.liferay.portal.kernel.util.UnicodeProperties;
048 import com.liferay.portal.kernel.util.Validator;
049 import com.liferay.portal.kernel.workflow.WorkflowConstants;
050 import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
051 import com.liferay.portal.model.Image;
052 import com.liferay.portal.model.Lock;
053 import com.liferay.portal.model.ModelHintsUtil;
054 import com.liferay.portal.model.Repository;
055 import com.liferay.portal.model.ResourceConstants;
056 import com.liferay.portal.model.User;
057 import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
058 import com.liferay.portal.repository.liferayrepository.model.LiferayFileVersion;
059 import com.liferay.portal.security.auth.PrincipalThreadLocal;
060 import com.liferay.portal.service.ServiceContext;
061 import com.liferay.portal.util.PortalUtil;
062 import com.liferay.portal.util.PrefsPropsUtil;
063 import com.liferay.portal.util.PropsValues;
064 import com.liferay.portlet.asset.model.AssetEntry;
065 import com.liferay.portlet.asset.model.AssetLink;
066 import com.liferay.portlet.asset.model.AssetLinkConstants;
067 import com.liferay.portlet.documentlibrary.DuplicateFileException;
068 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
069 import com.liferay.portlet.documentlibrary.FileExtensionException;
070 import com.liferay.portlet.documentlibrary.FileNameException;
071 import com.liferay.portlet.documentlibrary.ImageSizeException;
072 import com.liferay.portlet.documentlibrary.InvalidFileEntryTypeException;
073 import com.liferay.portlet.documentlibrary.InvalidFileVersionException;
074 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
075 import com.liferay.portlet.documentlibrary.NoSuchFileEntryMetadataException;
076 import com.liferay.portlet.documentlibrary.NoSuchFileVersionException;
077 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
078 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
079 import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
080 import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
081 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
082 import com.liferay.portlet.documentlibrary.model.DLFolder;
083 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
084 import com.liferay.portlet.documentlibrary.model.DLSyncConstants;
085 import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryImpl;
086 import com.liferay.portlet.documentlibrary.service.base.DLFileEntryLocalServiceBaseImpl;
087 import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
088 import com.liferay.portlet.documentlibrary.util.DL;
089 import com.liferay.portlet.documentlibrary.util.DLAppUtil;
090 import com.liferay.portlet.documentlibrary.util.DLUtil;
091 import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelModifiedDateComparator;
092 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
093 import com.liferay.portlet.dynamicdatamapping.storage.Fields;
094 import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
095 import com.liferay.portlet.expando.NoSuchRowException;
096 import com.liferay.portlet.expando.NoSuchTableException;
097 import com.liferay.portlet.expando.model.ExpandoBridge;
098 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
099 import com.liferay.portlet.expando.model.ExpandoRow;
100 import com.liferay.portlet.expando.model.ExpandoTable;
101 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
102 import com.liferay.portlet.trash.model.TrashVersion;
103
104 import java.awt.image.RenderedImage;
105
106 import java.io.File;
107 import java.io.IOException;
108 import java.io.InputStream;
109 import java.io.Serializable;
110
111 import java.util.ArrayList;
112 import java.util.Date;
113 import java.util.HashMap;
114 import java.util.List;
115 import java.util.Map;
116
117
135 public class DLFileEntryLocalServiceImpl
136 extends DLFileEntryLocalServiceBaseImpl {
137
138 public DLFileEntry addFileEntry(
139 long userId, long groupId, long repositoryId, long folderId,
140 String sourceFileName, String mimeType, String title,
141 String description, String changeLog, long fileEntryTypeId,
142 Map<String, Fields> fieldsMap, File file, InputStream is, long size,
143 ServiceContext serviceContext)
144 throws PortalException, SystemException {
145
146 if (Validator.isNull(title)) {
147 if (size == 0) {
148 throw new FileNameException();
149 }
150 else {
151 title = sourceFileName;
152 }
153 }
154
155
156
157 User user = userPersistence.findByPrimaryKey(userId);
158 folderId = dlFolderLocalService.getFolderId(
159 user.getCompanyId(), folderId);
160 String name = String.valueOf(
161 counterLocalService.increment(DLFileEntry.class.getName()));
162 String extension = DLAppUtil.getExtension(title, sourceFileName);
163
164 if (fileEntryTypeId == -1) {
165 fileEntryTypeId =
166 dlFileEntryTypeLocalService.getDefaultFileEntryTypeId(folderId);
167 }
168
169 Date now = new Date();
170
171 validateFileEntryTypeId(
172 PortalUtil.getSiteAndCompanyGroupIds(groupId), folderId,
173 fileEntryTypeId);
174
175 validateFile(
176 groupId, folderId, 0, title, extension, sourceFileName, file, is);
177
178 long fileEntryId = counterLocalService.increment();
179
180 DLFileEntry dlFileEntry = dlFileEntryPersistence.create(fileEntryId);
181
182 dlFileEntry.setUuid(serviceContext.getUuid());
183 dlFileEntry.setGroupId(groupId);
184 dlFileEntry.setCompanyId(user.getCompanyId());
185 dlFileEntry.setUserId(user.getUserId());
186 dlFileEntry.setUserName(user.getFullName());
187 dlFileEntry.setVersionUserId(user.getUserId());
188 dlFileEntry.setVersionUserName(user.getFullName());
189 dlFileEntry.setCreateDate(serviceContext.getCreateDate(now));
190 dlFileEntry.setModifiedDate(serviceContext.getModifiedDate(now));
191
192 DLFolder repositoryDLFolder = null;
193
194 if (repositoryId != groupId) {
195 Repository repository = repositoryLocalService.getRepository(
196 repositoryId);
197
198 repositoryDLFolder = dlFolderPersistence.findByPrimaryKey(
199 repository.getDlFolderId());
200 }
201
202 if ((repositoryDLFolder != null) && repositoryDLFolder.isHidden()) {
203 long classNameId = PortalUtil.getClassNameId(
204 (String)serviceContext.getAttribute("className"));
205 long classPK = ParamUtil.getLong(serviceContext, "classPK");
206
207 if (Validator.isNotNull(classNameId) &&
208 Validator.isNotNull(classPK)) {
209
210 dlFileEntry.setClassNameId(classNameId);
211 dlFileEntry.setClassPK(classPK);
212 }
213 }
214
215 dlFileEntry.setRepositoryId(repositoryId);
216 dlFileEntry.setFolderId(folderId);
217 dlFileEntry.setName(name);
218 dlFileEntry.setExtension(extension);
219 dlFileEntry.setMimeType(mimeType);
220 dlFileEntry.setTitle(title);
221 dlFileEntry.setDescription(description);
222 dlFileEntry.setFileEntryTypeId(fileEntryTypeId);
223 dlFileEntry.setVersion(DLFileEntryConstants.VERSION_DEFAULT);
224 dlFileEntry.setSize(size);
225 dlFileEntry.setReadCount(DLFileEntryConstants.DEFAULT_READ_COUNT);
226
227 dlFileEntryPersistence.update(dlFileEntry);
228
229
230
231 addFileVersion(
232 user, dlFileEntry, serviceContext.getModifiedDate(now), extension,
233 mimeType, title, description, null, StringPool.BLANK,
234 fileEntryTypeId, fieldsMap, DLFileEntryConstants.VERSION_DEFAULT,
235 size, WorkflowConstants.STATUS_DRAFT, serviceContext);
236
237
238
239 if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
240 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
241 dlFileEntry.getFolderId());
242
243 dlFolder.setLastPostDate(dlFileEntry.getModifiedDate());
244
245 dlFolderPersistence.update(dlFolder);
246 }
247
248
249
250 if (file != null) {
251 DLStoreUtil.addFile(
252 user.getCompanyId(), dlFileEntry.getDataRepositoryId(), name,
253 false, file);
254 }
255 else {
256 DLStoreUtil.addFile(
257 user.getCompanyId(), dlFileEntry.getDataRepositoryId(), name,
258 false, is);
259 }
260
261 return dlFileEntry;
262 }
263
264 public DLFileVersion cancelCheckOut(long userId, long fileEntryId)
265 throws PortalException, SystemException {
266
267 if (!isFileEntryCheckedOut(fileEntryId)) {
268 return null;
269 }
270
271 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
272 fileEntryId);
273
274 DLFileVersion dlFileVersion =
275 dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
276
277 removeFileVersion(dlFileEntry, dlFileVersion);
278
279 if (dlFileEntry.getFolderId() !=
280 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
281
282 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
283 dlFileEntry.getFolderId());
284
285 dlFolder.setLastPostDate(new Date());
286
287 dlFolderPersistence.update(dlFolder);
288 }
289
290 return dlFileVersion;
291 }
292
293 public void checkInFileEntry(
294 long userId, long fileEntryId, boolean majorVersion,
295 String changeLog, ServiceContext serviceContext)
296 throws PortalException, SystemException {
297
298 if (!isFileEntryCheckedOut(fileEntryId)) {
299 return;
300 }
301
302 User user = userPersistence.findByPrimaryKey(userId);
303
304 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
305 fileEntryId);
306
307 boolean webDAVCheckInMode = GetterUtil.getBoolean(
308 serviceContext.getAttribute(DL.WEBDAV_CHECK_IN_MODE));
309
310 boolean manualCheckInRequired = dlFileEntry.getManualCheckInRequired();
311
312 if (!webDAVCheckInMode && manualCheckInRequired) {
313 dlFileEntry.setManualCheckInRequired(false);
314
315 dlFileEntryPersistence.update(dlFileEntry);
316 }
317
318 DLFileVersion lastDLFileVersion =
319 dlFileVersionLocalService.getFileVersion(
320 dlFileEntry.getFileEntryId(), dlFileEntry.getVersion());
321
322 DLFileVersion latestDLFileVersion =
323 dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
324
325 if (isKeepFileVersionLabel(
326 dlFileEntry, lastDLFileVersion, latestDLFileVersion,
327 serviceContext.getWorkflowAction())) {
328
329 if (lastDLFileVersion.getSize() != latestDLFileVersion.getSize()) {
330
331
332
333 lastDLFileVersion.setExtension(
334 latestDLFileVersion.getExtension());
335 lastDLFileVersion.setMimeType(
336 latestDLFileVersion.getMimeType());
337 lastDLFileVersion.setSize(latestDLFileVersion.getSize());
338
339 dlFileVersionPersistence.update(lastDLFileVersion);
340
341
342
343 try {
344 DLStoreUtil.deleteFile(
345 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
346 dlFileEntry.getName(), lastDLFileVersion.getVersion());
347 }
348 catch (NoSuchModelException nsme) {
349 }
350
351 DLStoreUtil.copyFileVersion(
352 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
353 dlFileEntry.getName(),
354 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
355 lastDLFileVersion.getVersion());
356 }
357
358
359
360 removeFileVersion(dlFileEntry, latestDLFileVersion);
361
362 latestDLFileVersion = lastDLFileVersion;
363 }
364 else {
365
366
367
368 String version = getNextVersion(
369 dlFileEntry, majorVersion, serviceContext.getWorkflowAction());
370
371 latestDLFileVersion.setVersion(version);
372 latestDLFileVersion.setChangeLog(changeLog);
373
374 dlFileVersionPersistence.update(latestDLFileVersion);
375
376
377
378 DLStoreUtil.updateFileVersion(
379 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
380 dlFileEntry.getName(),
381 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION, version);
382 }
383
384
385
386 if (dlFileEntry.getFolderId() !=
387 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
388
389 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
390 dlFileEntry.getFolderId());
391
392 dlFolder.setLastPostDate(dlFileEntry.getModifiedDate());
393
394 dlFolderPersistence.update(dlFolder);
395 }
396
397
398
399 if (serviceContext.getWorkflowAction() ==
400 WorkflowConstants.ACTION_PUBLISH) {
401
402 startWorkflowInstance(
403 userId, serviceContext, latestDLFileVersion,
404 DLSyncConstants.EVENT_UPDATE);
405 }
406
407 unlockFileEntry(fileEntryId);
408 }
409
410
414 public void checkInFileEntry(long userId, long fileEntryId, String lockUuid)
415 throws PortalException, SystemException {
416
417 checkInFileEntry(userId, fileEntryId, lockUuid, new ServiceContext());
418 }
419
420 public void checkInFileEntry(
421 long userId, long fileEntryId, String lockUuid,
422 ServiceContext serviceContext)
423 throws PortalException, SystemException {
424
425 if (Validator.isNotNull(lockUuid)) {
426 try {
427 Lock lock = lockLocalService.getLock(
428 DLFileEntry.class.getName(), fileEntryId);
429
430 if (!lock.getUuid().equals(lockUuid)) {
431 throw new InvalidLockException("UUIDs do not match");
432 }
433 }
434 catch (PortalException pe) {
435 if ((pe instanceof ExpiredLockException) ||
436 (pe instanceof NoSuchLockException)) {
437 }
438 else {
439 throw pe;
440 }
441 }
442 }
443
444 checkInFileEntry(
445 userId, fileEntryId, false, StringPool.BLANK, serviceContext);
446 }
447
448
452 public DLFileEntry checkOutFileEntry(long userId, long fileEntryId)
453 throws PortalException, SystemException {
454
455 return checkOutFileEntry(userId, fileEntryId, new ServiceContext());
456 }
457
458 public DLFileEntry checkOutFileEntry(
459 long userId, long fileEntryId, ServiceContext serviceContext)
460 throws PortalException, SystemException {
461
462 return checkOutFileEntry(
463 userId, fileEntryId, StringPool.BLANK,
464 DLFileEntryImpl.LOCK_EXPIRATION_TIME, serviceContext);
465 }
466
467
471 public DLFileEntry checkOutFileEntry(
472 long userId, long fileEntryId, String owner, long expirationTime)
473 throws PortalException, SystemException {
474
475 return checkOutFileEntry(
476 userId, fileEntryId, owner, expirationTime, new ServiceContext());
477 }
478
479 public DLFileEntry checkOutFileEntry(
480 long userId, long fileEntryId, String owner, long expirationTime,
481 ServiceContext serviceContext)
482 throws PortalException, SystemException {
483
484 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
485 fileEntryId);
486
487 boolean hasLock = hasFileEntryLock(userId, fileEntryId);
488
489 if (!hasLock) {
490 if ((expirationTime <= 0) ||
491 (expirationTime > DLFileEntryImpl.LOCK_EXPIRATION_TIME)) {
492
493 expirationTime = DLFileEntryImpl.LOCK_EXPIRATION_TIME;
494 }
495
496 lockLocalService.lock(
497 userId, DLFileEntry.class.getName(), fileEntryId, owner, false,
498 expirationTime);
499 }
500
501 User user = userPersistence.findByPrimaryKey(userId);
502
503 serviceContext.setCompanyId(user.getCompanyId());
504
505 DLFileVersion dlFileVersion =
506 dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
507
508 long dlFileVersionId = dlFileVersion.getFileVersionId();
509
510 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
511 serviceContext.getCompanyId(), DLFileEntry.class.getName(),
512 dlFileVersionId);
513
514 serviceContext.setExpandoBridgeAttributes(
515 expandoBridge.getAttributes());
516 serviceContext.setUserId(userId);
517
518 boolean manualCheckinRequired = GetterUtil.getBoolean(
519 serviceContext.getAttribute(DL.MANUAL_CHECK_IN_REQUIRED));
520
521 dlFileEntry.setManualCheckInRequired(manualCheckinRequired);
522
523 dlFileEntryPersistence.update(dlFileEntry);
524
525 String version = dlFileVersion.getVersion();
526
527 if (!version.equals(
528 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
529
530 long existingDLFileVersionId = ParamUtil.getLong(
531 serviceContext, "existingDLFileVersionId");
532
533 if (existingDLFileVersionId > 0) {
534 DLFileVersion existingDLFileVersion =
535 dlFileVersionPersistence.findByPrimaryKey(
536 existingDLFileVersionId);
537
538 dlFileVersion = updateFileVersion(
539 user, existingDLFileVersion, null,
540 existingDLFileVersion.getExtension(),
541 existingDLFileVersion.getMimeType(),
542 existingDLFileVersion.getTitle(),
543 existingDLFileVersion.getDescription(),
544 existingDLFileVersion.getChangeLog(),
545 existingDLFileVersion.getExtraSettings(),
546 existingDLFileVersion.getFileEntryTypeId(), null,
547 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
548 existingDLFileVersion.getSize(),
549 WorkflowConstants.STATUS_DRAFT, new Date(), serviceContext);
550 }
551 else {
552 long oldDLFileVersionId = dlFileVersion.getFileVersionId();
553
554 dlFileVersion = addFileVersion(
555 user, dlFileEntry, new Date(), dlFileVersion.getExtension(),
556 dlFileVersion.getMimeType(), dlFileVersion.getTitle(),
557 dlFileVersion.getDescription(),
558 dlFileVersion.getChangeLog(),
559 dlFileVersion.getExtraSettings(),
560 dlFileVersion.getFileEntryTypeId(), null,
561 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION,
562 dlFileVersion.getSize(), WorkflowConstants.STATUS_DRAFT,
563 serviceContext);
564
565 copyExpandoRowModifiedDate(
566 dlFileEntry.getCompanyId(), oldDLFileVersionId,
567 dlFileVersion.getFileVersionId());
568 }
569
570 try {
571 DLStoreUtil.deleteFile(
572 dlFileEntry.getCompanyId(),
573 dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
574 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
575 }
576 catch (NoSuchModelException nsme) {
577 }
578
579 DLStoreUtil.copyFileVersion(
580 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
581 dlFileEntry.getName(), version,
582 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
583
584 copyFileEntryMetadata(
585 dlFileEntry.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
586 fileEntryId, dlFileVersionId, dlFileVersion.getFileVersionId(),
587 serviceContext);
588 }
589
590 if (dlFileEntry.getFolderId() !=
591 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
592
593 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
594 dlFileEntry.getFolderId());
595
596 dlFolder.setLastPostDate(dlFileVersion.getModifiedDate());
597
598 dlFolderPersistence.update(dlFolder);
599 }
600
601 return dlFileEntry;
602 }
603
604 public void convertExtraSettings(String[] keys)
605 throws PortalException, SystemException {
606
607 int count = dlFileEntryFinder.countByExtraSettings();
608
609 int pages = count / Indexer.DEFAULT_INTERVAL;
610
611 for (int i = 0; i <= pages; i++) {
612 int start = (i * Indexer.DEFAULT_INTERVAL);
613 int end = start + Indexer.DEFAULT_INTERVAL;
614
615 List<DLFileEntry> dlFileEntries =
616 dlFileEntryFinder.findByExtraSettings(start, end);
617
618 for (DLFileEntry dlFileEntry : dlFileEntries) {
619 convertExtraSettings(dlFileEntry, keys);
620 }
621 }
622 }
623
624 public void copyFileEntryMetadata(
625 long companyId, long fileEntryTypeId, long fileEntryId,
626 long fromFileVersionId, long toFileVersionId,
627 ServiceContext serviceContext)
628 throws PortalException, SystemException {
629
630 Map<String, Fields> fieldsMap = new HashMap<String, Fields>();
631
632 List<DDMStructure> ddmStructures = null;
633
634 if (fileEntryTypeId > 0) {
635 DLFileEntryType dlFileEntryType =
636 dlFileEntryTypeLocalService.getFileEntryType(fileEntryTypeId);
637
638 ddmStructures = dlFileEntryType.getDDMStructures();
639
640 for (DDMStructure ddmStructure : ddmStructures) {
641 try {
642 DLFileEntryMetadata dlFileEntryMetadata =
643 dlFileEntryMetadataLocalService.getFileEntryMetadata(
644 ddmStructure.getStructureId(), fromFileVersionId);
645
646 Fields fields = StorageEngineUtil.getFields(
647 dlFileEntryMetadata.getDDMStorageId());
648
649 fieldsMap.put(ddmStructure.getStructureKey(), fields);
650 }
651 catch (NoSuchFileEntryMetadataException nsfeme) {
652 }
653 }
654
655 dlFileEntryMetadataLocalService.updateFileEntryMetadata(
656 companyId, ddmStructures, fileEntryTypeId, fileEntryId,
657 toFileVersionId, fieldsMap, serviceContext);
658 }
659
660 ddmStructures = ddmStructureLocalService.getClassStructures(
661 companyId, PortalUtil.getClassNameId(DLFileEntryMetadata.class));
662
663 for (DDMStructure ddmStructure : ddmStructures) {
664 try {
665 DLFileEntryMetadata fileEntryMetadata =
666 dlFileEntryMetadataLocalService.getFileEntryMetadata(
667 ddmStructure.getStructureId(), fromFileVersionId);
668
669 Fields fields = StorageEngineUtil.getFields(
670 fileEntryMetadata.getDDMStorageId());
671
672 fieldsMap.put(ddmStructure.getStructureKey(), fields);
673 }
674 catch (NoSuchFileEntryMetadataException nsfeme) {
675 }
676 }
677
678 dlFileEntryMetadataLocalService.updateFileEntryMetadata(
679 companyId, ddmStructures, fileEntryTypeId, fileEntryId,
680 toFileVersionId, fieldsMap, serviceContext);
681 }
682
683 public void deleteFileEntries(long groupId, long folderId)
684 throws PortalException, SystemException {
685
686 deleteFileEntries(groupId, folderId, true);
687 }
688
689 public void deleteFileEntries(
690 long groupId, long folderId, boolean includeTrashedEntries)
691 throws PortalException, SystemException {
692
693 int count = dlFileEntryPersistence.countByG_F(groupId, folderId);
694
695 int pages = count / _DELETE_INTERVAL;
696
697 for (int i = 0; i <= pages; i++) {
698 int start = (i * _DELETE_INTERVAL);
699 int end = start + _DELETE_INTERVAL;
700
701 List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(
702 groupId, folderId, start, end);
703
704 for (DLFileEntry dlFileEntry : dlFileEntries) {
705 DLFileVersion dlFileVersion = dlFileEntry.getLatestFileVersion(
706 true);
707
708 if (includeTrashedEntries || !dlFileVersion.isInTrash()) {
709 dlAppHelperLocalService.deleteFileEntry(
710 new LiferayFileEntry(dlFileEntry));
711
712 dlFileEntryLocalService.deleteFileEntry(dlFileEntry);
713 }
714 }
715 }
716 }
717
718 @Indexable(type = IndexableType.DELETE)
719 public DLFileEntry deleteFileEntry(DLFileEntry dlFileEntry)
720 throws PortalException, SystemException {
721
722
723
724 dlFileEntryPersistence.remove(dlFileEntry);
725
726
727
728 resourceLocalService.deleteResource(
729 dlFileEntry.getCompanyId(), DLFileEntry.class.getName(),
730 ResourceConstants.SCOPE_INDIVIDUAL, dlFileEntry.getFileEntryId());
731
732
733
734 webDAVPropsLocalService.deleteWebDAVProps(
735 DLFileEntry.class.getName(), dlFileEntry.getFileEntryId());
736
737
738
739 dlFileEntryMetadataLocalService.deleteFileEntryMetadata(
740 dlFileEntry.getFileEntryId());
741
742
743
744 List<DLFileVersion> dlFileVersions =
745 dlFileVersionPersistence.findByFileEntryId(
746 dlFileEntry.getFileEntryId());
747
748 for (DLFileVersion dlFileVersion : dlFileVersions) {
749 dlFileVersionPersistence.remove(dlFileVersion);
750
751 expandoValueLocalService.deleteValues(
752 DLFileVersion.class.getName(),
753 dlFileVersion.getFileVersionId());
754
755 workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks(
756 dlFileEntry.getCompanyId(), dlFileEntry.getGroupId(),
757 DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
758 }
759
760
761
762 expandoValueLocalService.deleteValues(
763 DLFileEntry.class.getName(), dlFileEntry.getFileEntryId());
764
765
766
767 unlockFileEntry(dlFileEntry.getFileEntryId());
768
769
770
771 try {
772 DLStoreUtil.deleteFile(
773 dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
774 dlFileEntry.getName());
775 }
776 catch (Exception e) {
777 if (_log.isWarnEnabled()) {
778 _log.warn(e, e);
779 }
780 }
781
782 return dlFileEntry;
783 }
784
785 @Indexable(type = IndexableType.DELETE)
786 public DLFileEntry deleteFileEntry(long fileEntryId)
787 throws PortalException, SystemException {
788
789 DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
790
791 return deleteFileEntry(dlFileEntry);
792 }
793
794 @Indexable(type = IndexableType.DELETE)
795 public DLFileEntry deleteFileEntry(long userId, long fileEntryId)
796 throws PortalException, SystemException {
797
798 if (!hasFileEntryLock(userId, fileEntryId)) {
799 lockFileEntry(userId, fileEntryId);
800 }
801
802 try {
803 return deleteFileEntry(fileEntryId);
804 }
805 finally {
806 unlockFileEntry(fileEntryId);
807 }
808 }
809
810 @Indexable(type = IndexableType.REINDEX)
811 public DLFileEntry deleteFileVersion(
812 long userId, long fileEntryId, String version)
813 throws PortalException, SystemException {
814
815 if (Validator.isNull(version) ||
816 version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
817
818 throw new InvalidFileVersionException();
819 }
820
821 if (!hasFileEntryLock(userId, fileEntryId)) {
822 lockFileEntry(userId, fileEntryId);
823 }
824
825 boolean latestVersion = false;
826
827 DLFileEntry dlFileEntry = null;
828
829 try {
830 DLFileVersion dlFileVersion = dlFileVersionPersistence.findByF_V(
831 fileEntryId, version);
832
833 if (!dlFileVersion.isApproved()) {
834 throw new InvalidFileVersionException(
835 "Cannot delete an unapproved file version");
836 }
837 else {
838 int count = dlFileVersionPersistence.countByF_S(
839 fileEntryId, WorkflowConstants.STATUS_APPROVED);
840
841 if (count <= 1) {
842 throw new InvalidFileVersionException(
843 "Cannot delete the only approved file version");
844 }
845 }
846
847 dlFileVersionPersistence.remove(dlFileVersion);
848
849 expandoValueLocalService.deleteValues(
850 DLFileVersion.class.getName(),
851 dlFileVersion.getFileVersionId());
852
853 dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
854
855 latestVersion = version.equals(dlFileEntry.getVersion());
856
857 if (latestVersion) {
858 try {
859 DLFileVersion dlLatestFileVersion =
860 dlFileVersionLocalService.getLatestFileVersion(
861 dlFileEntry.getFileEntryId(), true);
862
863 dlFileEntry.setVersionUserId(
864 dlLatestFileVersion.getUserId());
865 dlFileEntry.setVersionUserName(
866 dlLatestFileVersion.getUserName());
867 dlFileEntry.setModifiedDate(
868 dlLatestFileVersion.getCreateDate());
869 dlFileEntry.setExtension(
870 dlLatestFileVersion.getExtension());
871 dlFileEntry.setMimeType(dlLatestFileVersion.getMimeType());
872 dlFileEntry.setTitle(dlLatestFileVersion.getTitle());
873 dlFileEntry.setDescription(
874 dlLatestFileVersion.getDescription());
875 dlFileEntry.setExtraSettings(
876 dlLatestFileVersion.getExtraSettings());
877 dlFileEntry.setFileEntryTypeId(
878 dlLatestFileVersion.getFileEntryTypeId());
879 dlFileEntry.setVersion(dlLatestFileVersion.getVersion());
880 dlFileEntry.setSize(dlLatestFileVersion.getSize());
881
882 dlFileEntry = dlFileEntryPersistence.update(dlFileEntry);
883 }
884 catch (NoSuchFileVersionException nsfve) {
885 }
886 }
887
888 try {
889 DLStoreUtil.deleteFile(
890 dlFileEntry.getCompanyId(),
891 dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
892 version);
893 }
894 catch (NoSuchModelException nsme) {
895 }
896 }
897 finally {
898 unlockFileEntry(fileEntryId);
899 }
900
901 if (latestVersion) {
902 return dlFileEntry;
903 }
904
905 return null;
906 }
907
908 public DLFileEntry fetchFileEntry(long groupId, long folderId, String title)
909 throws SystemException {
910
911 return dlFileEntryPersistence.fetchByG_F_T(groupId, folderId, title);
912 }
913
914 public DLFileEntry fetchFileEntryByAnyImageId(long imageId)
915 throws SystemException {
916
917 return dlFileEntryFinder.fetchByAnyImageId(imageId);
918 }
919
920 public DLFileEntry fetchFileEntryByName(
921 long groupId, long folderId, String name)
922 throws SystemException {
923
924 return dlFileEntryPersistence.fetchByG_F_N(groupId, folderId, name);
925 }
926
927 public List<DLFileEntry> getDDMStructureFileEntries(long[] ddmStructureIds)
928 throws SystemException {
929
930 return dlFileEntryFinder.findByDDMStructureIds(
931 ddmStructureIds, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
932 }
933
934 public List<DLFileEntry> getExtraSettingsFileEntries(int start, int end)
935 throws SystemException {
936
937 return dlFileEntryFinder.findByExtraSettings(start, end);
938 }
939
940 public File getFile(
941 long userId, long fileEntryId, String version,
942 boolean incrementCounter)
943 throws PortalException, SystemException {
944
945 return getFile(userId, fileEntryId, version, incrementCounter, 1);
946 }
947
948 public File getFile(
949 long userId, long fileEntryId, String version,
950 boolean incrementCounter, int increment)
951 throws PortalException, SystemException {
952
953 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
954 fileEntryId);
955
956 if (incrementCounter) {
957 dlFileEntryLocalService.incrementViewCounter(
958 dlFileEntry, increment);
959 }
960
961 dlAppHelperLocalService.getFileAsStream(
962 userId, new LiferayFileEntry(dlFileEntry), incrementCounter);
963
964 return DLStoreUtil.getFile(
965 dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
966 dlFileEntry.getName(), version);
967 }
968
969 public InputStream getFileAsStream(
970 long userId, long fileEntryId, String version)
971 throws PortalException, SystemException {
972
973 return getFileAsStream(userId, fileEntryId, version, true, 1);
974 }
975
976 public InputStream getFileAsStream(
977 long userId, long fileEntryId, String version,
978 boolean incrementCounter)
979 throws PortalException, SystemException {
980
981 return getFileAsStream(
982 userId, fileEntryId, version, incrementCounter, 1);
983 }
984
985 public InputStream getFileAsStream(
986 long userId, long fileEntryId, String version,
987 boolean incrementCounter, int increment)
988 throws PortalException, SystemException {
989
990 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
991 fileEntryId);
992
993 if (incrementCounter) {
994 dlFileEntryLocalService.incrementViewCounter(
995 dlFileEntry, increment);
996 }
997
998 dlAppHelperLocalService.getFileAsStream(
999 userId, new LiferayFileEntry(dlFileEntry), incrementCounter);
1000
1001 return DLStoreUtil.getFileAsStream(
1002 dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
1003 dlFileEntry.getName(), version);
1004 }
1005
1006 public List<DLFileEntry> getFileEntries(int start, int end)
1007 throws SystemException {
1008
1009 return dlFileEntryPersistence.findAll(start, end);
1010 }
1011
1012 public List<DLFileEntry> getFileEntries(long groupId, long folderId)
1013 throws SystemException {
1014
1015 return dlFileEntryPersistence.findByG_F(groupId, folderId);
1016 }
1017
1018 public List<DLFileEntry> getFileEntries(
1019 long groupId, long folderId, int status, int start, int end,
1020 OrderByComparator obc)
1021 throws SystemException {
1022
1023 List<Long> folderIds = new ArrayList<Long>();
1024
1025 folderIds.add(folderId);
1026
1027 QueryDefinition queryDefinition = new QueryDefinition(
1028 status, false, start, end, obc);
1029
1030 return dlFileEntryFinder.findByG_F(groupId, folderIds, queryDefinition);
1031 }
1032
1033 public List<DLFileEntry> getFileEntries(
1034 long groupId, long folderId, int start, int end,
1035 OrderByComparator obc)
1036 throws SystemException {
1037
1038 return dlFileEntryPersistence.findByG_F(
1039 groupId, folderId, start, end, obc);
1040 }
1041
1042 public List<DLFileEntry> getFileEntries(long folderId, String name)
1043 throws SystemException {
1044
1045 return dlFileEntryPersistence.findByF_N(folderId, name);
1046 }
1047
1048 public int getFileEntriesCount() throws SystemException {
1049 return dlFileEntryPersistence.countAll();
1050 }
1051
1052 public int getFileEntriesCount(long groupId, long folderId)
1053 throws SystemException {
1054
1055 return dlFileEntryPersistence.countByG_F(groupId, folderId);
1056 }
1057
1058 public int getFileEntriesCount(long groupId, long folderId, int status)
1059 throws SystemException {
1060
1061 List<Long> folderIds = new ArrayList<Long>();
1062
1063 folderIds.add(folderId);
1064
1065 return dlFileEntryFinder.countByG_F(
1066 groupId, folderIds, new QueryDefinition(status));
1067 }
1068
1069 public DLFileEntry getFileEntry(long fileEntryId)
1070 throws PortalException, SystemException {
1071
1072 return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
1073 }
1074
1075 public DLFileEntry getFileEntry(long groupId, long folderId, String title)
1076 throws PortalException, SystemException {
1077
1078 DLFileEntry dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
1079 groupId, folderId, title);
1080
1081 if (dlFileEntry != null) {
1082 return dlFileEntry;
1083 }
1084
1085 List<DLFileVersion> dlFileVersions =
1086 dlFileVersionPersistence.findByG_F_T_V(
1087 groupId, folderId, title,
1088 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
1089
1090 long userId = PrincipalThreadLocal.getUserId();
1091
1092 for (DLFileVersion dlFileVersion : dlFileVersions) {
1093 if (hasFileEntryLock(userId, dlFileVersion.getFileEntryId())) {
1094 return dlFileVersion.getFileEntry();
1095 }
1096 }
1097
1098 StringBundler sb = new StringBundler(8);
1099
1100 sb.append("No DLFileEntry exists with the key {");
1101 sb.append("groupId=");
1102 sb.append(groupId);
1103 sb.append(", folderId=");
1104 sb.append(folderId);
1105 sb.append(", title=");
1106 sb.append(title);
1107 sb.append(StringPool.CLOSE_CURLY_BRACE);
1108
1109 throw new NoSuchFileEntryException(sb.toString());
1110 }
1111
1112 public DLFileEntry getFileEntryByName(
1113 long groupId, long folderId, String name)
1114 throws PortalException, SystemException {
1115
1116 return dlFileEntryPersistence.findByG_F_N(groupId, folderId, name);
1117 }
1118
1119 public DLFileEntry getFileEntryByUuidAndGroupId(String uuid, long groupId)
1120 throws PortalException, SystemException {
1121
1122 return dlFileEntryPersistence.findByUUID_G(uuid, groupId);
1123 }
1124
1125 public List<DLFileEntry> getGroupFileEntries(
1126 long groupId, int start, int end)
1127 throws SystemException {
1128
1129 return getGroupFileEntries(
1130 groupId, start, end, new RepositoryModelModifiedDateComparator());
1131 }
1132
1133 public List<DLFileEntry> getGroupFileEntries(
1134 long groupId, int start, int end, OrderByComparator obc)
1135 throws SystemException {
1136
1137 return dlFileEntryPersistence.findByGroupId(groupId, start, end, obc);
1138 }
1139
1140 public List<DLFileEntry> getGroupFileEntries(
1141 long groupId, long userId, int start, int end)
1142 throws SystemException {
1143
1144 return getGroupFileEntries(
1145 groupId, userId, start, end,
1146 new RepositoryModelModifiedDateComparator());
1147 }
1148
1149 public List<DLFileEntry> getGroupFileEntries(
1150 long groupId, long userId, int start, int end,
1151 OrderByComparator obc)
1152 throws SystemException {
1153
1154 if (userId <= 0) {
1155 return dlFileEntryPersistence.findByGroupId(
1156 groupId, start, end, obc);
1157 }
1158 else {
1159 return dlFileEntryPersistence.findByG_U(
1160 groupId, userId, start, end, obc);
1161 }
1162 }
1163
1164 public int getGroupFileEntriesCount(long groupId) throws SystemException {
1165 return dlFileEntryPersistence.countByGroupId(groupId);
1166 }
1167
1168 public int getGroupFileEntriesCount(long groupId, long userId)
1169 throws SystemException {
1170
1171 if (userId <= 0) {
1172 return dlFileEntryPersistence.countByGroupId(groupId);
1173 }
1174 else {
1175 return dlFileEntryPersistence.countByG_U(groupId, userId);
1176 }
1177 }
1178
1179 public List<DLFileEntry> getMisversionedFileEntries()
1180 throws SystemException {
1181
1182 return dlFileEntryFinder.findByMisversioned();
1183 }
1184
1185 public List<DLFileEntry> getNoAssetFileEntries() throws SystemException {
1186 return dlFileEntryFinder.findByNoAssets();
1187 }
1188
1189 public List<DLFileEntry> getOrphanedFileEntries() throws SystemException {
1190 return dlFileEntryFinder.findByOrphanedFileEntries();
1191 }
1192
1193 public boolean hasExtraSettings() throws SystemException {
1194 if (dlFileEntryFinder.countByExtraSettings() > 0) {
1195 return true;
1196 }
1197 else {
1198 return false;
1199 }
1200 }
1201
1202 public boolean hasFileEntryLock(long userId, long fileEntryId)
1203 throws PortalException, SystemException {
1204
1205 DLFileEntry dlFileEntry = getFileEntry(fileEntryId);
1206
1207 long folderId = dlFileEntry.getFolderId();
1208
1209 boolean hasLock = lockLocalService.hasLock(
1210 userId, DLFileEntry.class.getName(), fileEntryId);
1211
1212 if (!hasLock &&
1213 (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
1214
1215 hasLock = dlFolderService.hasInheritableLock(folderId);
1216 }
1217
1218 return hasLock;
1219 }
1220
1221 @BufferedIncrement(
1222 configuration = "DLFileEntry", incrementClass = NumberIncrement.class)
1223 public void incrementViewCounter(DLFileEntry dlFileEntry, int increment)
1224 throws SystemException {
1225
1226 dlFileEntry.setReadCount(dlFileEntry.getReadCount() + increment);
1227
1228 dlFileEntryPersistence.update(dlFileEntry);
1229 }
1230
1231 public boolean isFileEntryCheckedOut(long fileEntryId)
1232 throws PortalException, SystemException {
1233
1234 DLFileVersion dlFileVersion =
1235 dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
1236
1237 String version = dlFileVersion.getVersion();
1238
1239 if (version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
1240 return true;
1241 }
1242 else {
1243 return false;
1244 }
1245 }
1246
1247 public Lock lockFileEntry(long userId, long fileEntryId)
1248 throws PortalException, SystemException {
1249
1250 if (hasFileEntryLock(userId, fileEntryId)) {
1251 return lockLocalService.getLock(
1252 DLFileEntry.class.getName(), fileEntryId);
1253 }
1254
1255 return lockLocalService.lock(
1256 userId, DLFileEntry.class.getName(), fileEntryId, null, false,
1257 DLFileEntryImpl.LOCK_EXPIRATION_TIME);
1258 }
1259
1260 @Indexable(type = IndexableType.REINDEX)
1261 public DLFileEntry moveFileEntry(
1262 long userId, long fileEntryId, long newFolderId,
1263 ServiceContext serviceContext)
1264 throws PortalException, SystemException {
1265
1266 if (!hasFileEntryLock(userId, fileEntryId)) {
1267 lockFileEntry(userId, fileEntryId);
1268 }
1269
1270 try {
1271 DLFileEntry dlFileEntry = moveFileEntryImpl(
1272 userId, fileEntryId, newFolderId, serviceContext);
1273
1274 dlAppHelperLocalService.moveFileEntry(
1275 new LiferayFileEntry(dlFileEntry));
1276
1277 return dlFileEntryTypeLocalService.updateFileEntryFileEntryType(
1278 dlFileEntry, serviceContext);
1279 }
1280 finally {
1281 if (!isFileEntryCheckedOut(fileEntryId)) {
1282 unlockFileEntry(fileEntryId);
1283 }
1284 }
1285 }
1286
1287 public void revertFileEntry(
1288 long userId, long fileEntryId, String version,
1289 ServiceContext serviceContext)
1290 throws PortalException, SystemException {
1291
1292 if (Validator.isNull(version) ||
1293 version.equals(DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION)) {
1294
1295 throw new InvalidFileVersionException();
1296 }
1297
1298 DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(
1299 fileEntryId, version);
1300
1301 if (!dlFileVersion.isApproved()) {
1302 throw new InvalidFileVersionException(
1303 "Cannot revert from an unapproved file version");
1304 }
1305
1306 DLFileVersion latestDLFileVersion =
1307 dlFileVersionLocalService.getLatestFileVersion(fileEntryId, false);
1308
1309 if (version.equals(latestDLFileVersion.getVersion())) {
1310 throw new InvalidFileVersionException(
1311 "Cannot revert from the latest file version");
1312 }
1313
1314 String sourceFileName = dlFileVersion.getTitle();
1315 String extension = dlFileVersion.getExtension();
1316 String mimeType = dlFileVersion.getMimeType();
1317 String title = dlFileVersion.getTitle();
1318 String description = dlFileVersion.getDescription();
1319 String changeLog = "Reverted to " + version;
1320 boolean majorVersion = true;
1321 String extraSettings = dlFileVersion.getExtraSettings();
1322 long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();
1323 Map<String, Fields> fieldsMap = null;
1324 InputStream is = getFileAsStream(userId, fileEntryId, version);
1325 long size = dlFileVersion.getSize();
1326
1327 DLFileEntry dlFileEntry = updateFileEntry(
1328 userId, fileEntryId, sourceFileName, extension, mimeType, title,
1329 description, changeLog, majorVersion, extraSettings,
1330 fileEntryTypeId, fieldsMap, null, is, size, serviceContext);
1331
1332 DLFileVersion newDlFileVersion =
1333 dlFileVersionLocalService.getFileVersion(
1334 fileEntryId, dlFileEntry.getVersion());
1335
1336 copyFileEntryMetadata(
1337 dlFileVersion.getCompanyId(), dlFileVersion.getFileEntryTypeId(),
1338 fileEntryId, newDlFileVersion.getFileVersionId(),
1339 dlFileVersion.getFileVersionId(), serviceContext);
1340 }
1341
1342 public void unlockFileEntry(long fileEntryId) throws SystemException {
1343 lockLocalService.unlock(DLFileEntry.class.getName(), fileEntryId);
1344 }
1345
1346 public DLFileEntry updateFileEntry(
1347 long userId, long fileEntryId, String sourceFileName,
1348 String mimeType, String title, String description, String changeLog,
1349 boolean majorVersion, long fileEntryTypeId,
1350 Map<String, Fields> fieldsMap, File file, InputStream is, long size,
1351 ServiceContext serviceContext)
1352 throws PortalException, SystemException {
1353
1354 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1355 fileEntryId);
1356
1357 String extension = DLAppUtil.getExtension(title, sourceFileName);
1358
1359 String extraSettings = StringPool.BLANK;
1360
1361 if (fileEntryTypeId == -1) {
1362 fileEntryTypeId = dlFileEntry.getFileEntryTypeId();
1363 }
1364
1365 validateFileEntryTypeId(
1366 PortalUtil.getSiteAndCompanyGroupIds(dlFileEntry.getGroupId()),
1367 dlFileEntry.getFolderId(), fileEntryTypeId);
1368
1369 return updateFileEntry(
1370 userId, fileEntryId, sourceFileName, extension, mimeType, title,
1371 description, changeLog, majorVersion, extraSettings,
1372 fileEntryTypeId, fieldsMap, file, is, size, serviceContext);
1373 }
1374
1375 public void updateSmallImage(long smallImageId, long largeImageId)
1376 throws PortalException, SystemException {
1377
1378 try {
1379 RenderedImage renderedImage = null;
1380
1381 Image largeImage = imageLocalService.getImage(largeImageId);
1382
1383 byte[] bytes = largeImage.getTextObj();
1384 String contentType = largeImage.getType();
1385
1386 if (bytes != null) {
1387 ImageBag imageBag = ImageToolUtil.read(bytes);
1388
1389 renderedImage = imageBag.getRenderedImage();
1390
1391
1392 }
1393
1394 if (renderedImage != null) {
1395 int height = PrefsPropsUtil.getInteger(
1396 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_HEIGHT);
1397 int width = PrefsPropsUtil.getInteger(
1398 PropsKeys.DL_FILE_ENTRY_THUMBNAIL_MAX_WIDTH);
1399
1400 RenderedImage thumbnailRenderedImage = ImageToolUtil.scale(
1401 renderedImage, height, width);
1402
1403 imageLocalService.updateImage(
1404 smallImageId,
1405 ImageToolUtil.getBytes(
1406 thumbnailRenderedImage, contentType));
1407 }
1408 }
1409 catch (IOException ioe) {
1410 throw new ImageSizeException(ioe);
1411 }
1412 }
1413
1414 public DLFileEntry updateStatus(
1415 long userId, long fileVersionId, int status,
1416 Map<String, Serializable> workflowContext,
1417 ServiceContext serviceContext)
1418 throws PortalException, SystemException {
1419
1420
1421
1422 User user = userPersistence.findByPrimaryKey(userId);
1423
1424 DLFileVersion dlFileVersion = dlFileVersionPersistence.findByPrimaryKey(
1425 fileVersionId);
1426
1427 int oldStatus = dlFileVersion.getStatus();
1428
1429 int oldDLFileVersionStatus = WorkflowConstants.STATUS_ANY;
1430
1431 List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1432 new ArrayList<ObjectValuePair<Long, Integer>>();
1433
1434 List<DLFileVersion> dlFileVersions =
1435 (List<DLFileVersion>)workflowContext.get("dlFileVersions");
1436
1437 if ((dlFileVersions != null) && !dlFileVersions.isEmpty()) {
1438 DLFileVersion oldDLFileVersion = dlFileVersions.get(0);
1439
1440 oldDLFileVersionStatus = oldDLFileVersion.getStatus();
1441
1442 dlFileVersionStatusOVPs = getDlFileVersionStatuses(dlFileVersions);
1443 }
1444
1445 dlFileVersion.setStatus(status);
1446 dlFileVersion.setStatusByUserId(user.getUserId());
1447 dlFileVersion.setStatusByUserName(user.getFullName());
1448 dlFileVersion.setStatusDate(new Date());
1449
1450 dlFileVersionPersistence.update(dlFileVersion);
1451
1452
1453
1454 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
1455 dlFileVersion.getFileEntryId());
1456
1457 if (status == WorkflowConstants.STATUS_APPROVED) {
1458 if (DLUtil.compareVersions(
1459 dlFileEntry.getVersion(),
1460 dlFileVersion.getVersion()) <= 0) {
1461
1462 dlFileEntry.setExtension(dlFileVersion.getExtension());
1463 dlFileEntry.setMimeType(dlFileVersion.getMimeType());
1464 dlFileEntry.setTitle(dlFileVersion.getTitle());
1465 dlFileEntry.setDescription(dlFileVersion.getDescription());
1466 dlFileEntry.setExtraSettings(dlFileVersion.getExtraSettings());
1467 dlFileEntry.setFileEntryTypeId(
1468 dlFileVersion.getFileEntryTypeId());
1469 dlFileEntry.setVersion(dlFileVersion.getVersion());
1470 dlFileEntry.setVersionUserId(dlFileVersion.getUserId());
1471 dlFileEntry.setVersionUserName(dlFileVersion.getUserName());
1472 dlFileEntry.setModifiedDate(dlFileVersion.getCreateDate());
1473 dlFileEntry.setSize(dlFileVersion.getSize());
1474
1475 dlFileEntryPersistence.update(dlFileEntry);
1476 }
1477 }
1478 else {
1479
1480
1481
1482 if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
1483 dlFileEntry.getVersion().equals(dlFileVersion.getVersion())) {
1484
1485 String newVersion = DLFileEntryConstants.VERSION_DEFAULT;
1486
1487 List<DLFileVersion> approvedFileVersions =
1488 dlFileVersionPersistence.findByF_S(
1489 dlFileEntry.getFileEntryId(),
1490 WorkflowConstants.STATUS_APPROVED);
1491
1492 if (!approvedFileVersions.isEmpty()) {
1493 newVersion = approvedFileVersions.get(0).getVersion();
1494 }
1495
1496 dlFileEntry.setVersion(newVersion);
1497
1498 dlFileEntryPersistence.update(dlFileEntry);
1499 }
1500
1501
1502
1503 if (dlFileVersion.getVersion().equals(
1504 DLFileEntryConstants.VERSION_DEFAULT)) {
1505
1506 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1507 DLFileEntry.class);
1508
1509 indexer.delete(dlFileEntry);
1510 }
1511 }
1512
1513
1514
1515 if (oldStatus == WorkflowConstants.STATUS_IN_TRASH) {
1516
1517
1518
1519 List<TrashVersion> trashVersions =
1520 (List<TrashVersion>)workflowContext.get("trashVersions");
1521
1522 for (TrashVersion trashVersion : trashVersions) {
1523 DLFileVersion trashDLFileVersion =
1524 dlFileVersionPersistence.findByPrimaryKey(
1525 trashVersion.getClassPK());
1526
1527 trashDLFileVersion.setStatus(trashVersion.getStatus());
1528
1529 dlFileVersionPersistence.update(trashDLFileVersion);
1530 }
1531
1532 trashEntryLocalService.deleteEntry(
1533 DLFileEntryConstants.getClassName(),
1534 dlFileEntry.getFileEntryId());
1535
1536
1537
1538 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
1539 DLFileEntry.class);
1540
1541 indexer.delete(dlFileEntry);
1542 }
1543 else if (status == WorkflowConstants.STATUS_IN_TRASH) {
1544
1545
1546
1547 for (DLFileVersion curDLFileVersion : dlFileVersions) {
1548 curDLFileVersion.setStatus(WorkflowConstants.STATUS_IN_TRASH);
1549
1550 dlFileVersionPersistence.update(curDLFileVersion);
1551 }
1552
1553 UnicodeProperties typeSettingsProperties = new UnicodeProperties();
1554
1555 typeSettingsProperties.put("title", dlFileEntry.getTitle());
1556
1557 trashEntryLocalService.addTrashEntry(
1558 userId, dlFileEntry.getGroupId(),
1559 DLFileEntryConstants.getClassName(),
1560 dlFileEntry.getFileEntryId(), oldDLFileVersionStatus,
1561 dlFileVersionStatusOVPs, typeSettingsProperties);
1562 }
1563
1564
1565
1566 dlAppHelperLocalService.updateStatus(
1567 userId, new LiferayFileEntry(dlFileEntry),
1568 new LiferayFileVersion(dlFileVersion), oldStatus, status,
1569 workflowContext, serviceContext);
1570
1571
1572
1573 if (((status == WorkflowConstants.STATUS_APPROVED) ||
1574 (status == WorkflowConstants.STATUS_IN_TRASH) ||
1575 (oldStatus == WorkflowConstants.STATUS_IN_TRASH)) &&
1576 ((serviceContext == null) || serviceContext.isIndexingEnabled())) {
1577
1578 reindex(dlFileEntry);
1579 }
1580
1581 return dlFileEntry;
1582 }
1583
1584 public boolean verifyFileEntryCheckOut(long fileEntryId, String lockUuid)
1585 throws PortalException, SystemException {
1586
1587 if (verifyFileEntryLock(fileEntryId, lockUuid) &&
1588 isFileEntryCheckedOut(fileEntryId)) {
1589
1590 return true;
1591 }
1592 else {
1593 return false;
1594 }
1595 }
1596
1597 public boolean verifyFileEntryLock(long fileEntryId, String lockUuid)
1598 throws PortalException, SystemException {
1599
1600 boolean lockVerified = false;
1601
1602 try {
1603 Lock lock = lockLocalService.getLock(
1604 DLFileEntry.class.getName(), fileEntryId);
1605
1606 if (lock.getUuid().equals(lockUuid)) {
1607 lockVerified = true;
1608 }
1609 }
1610 catch (PortalException pe) {
1611 if ((pe instanceof ExpiredLockException) ||
1612 (pe instanceof NoSuchLockException)) {
1613
1614 DLFileEntry dlFileEntry = dlFileEntryLocalService.getFileEntry(
1615 fileEntryId);
1616
1617 lockVerified = dlFolderService.verifyInheritableLock(
1618 dlFileEntry.getFolderId(), lockUuid);
1619 }
1620 else {
1621 throw pe;
1622 }
1623 }
1624
1625 return lockVerified;
1626 }
1627
1628 protected DLFileVersion addFileVersion(
1629 User user, DLFileEntry dlFileEntry, Date modifiedDate,
1630 String extension, String mimeType, String title, String description,
1631 String changeLog, String extraSettings, long fileEntryTypeId,
1632 Map<String, Fields> fieldsMap, String version, long size,
1633 int status, ServiceContext serviceContext)
1634 throws PortalException, SystemException {
1635
1636 long fileVersionId = counterLocalService.increment();
1637
1638 DLFileVersion dlFileVersion = dlFileVersionPersistence.create(
1639 fileVersionId);
1640
1641 String uuid = ParamUtil.getString(
1642 serviceContext, "fileVersionUuid", serviceContext.getUuid());
1643
1644 dlFileVersion.setUuid(uuid);
1645
1646 dlFileVersion.setGroupId(dlFileEntry.getGroupId());
1647 dlFileVersion.setCompanyId(dlFileEntry.getCompanyId());
1648
1649 long versionUserId = dlFileEntry.getVersionUserId();
1650
1651 if (versionUserId <= 0) {
1652 versionUserId = dlFileEntry.getUserId();
1653 }
1654
1655 dlFileVersion.setUserId(versionUserId);
1656
1657 String versionUserName = GetterUtil.getString(
1658 dlFileEntry.getVersionUserName(), dlFileEntry.getUserName());
1659
1660 dlFileVersion.setUserName(versionUserName);
1661
1662 dlFileVersion.setCreateDate(modifiedDate);
1663 dlFileVersion.setModifiedDate(modifiedDate);
1664 dlFileVersion.setRepositoryId(dlFileEntry.getRepositoryId());
1665 dlFileVersion.setFolderId(dlFileEntry.getFolderId());
1666 dlFileVersion.setFileEntryId(dlFileEntry.getFileEntryId());
1667 dlFileVersion.setExtension(extension);
1668 dlFileVersion.setMimeType(mimeType);
1669 dlFileVersion.setTitle(title);
1670 dlFileVersion.setDescription(description);
1671 dlFileVersion.setChangeLog(changeLog);
1672 dlFileVersion.setExtraSettings(extraSettings);
1673 dlFileVersion.setFileEntryTypeId(fileEntryTypeId);
1674 dlFileVersion.setVersion(version);
1675 dlFileVersion.setSize(size);
1676 dlFileVersion.setStatus(status);
1677 dlFileVersion.setStatusByUserId(user.getUserId());
1678 dlFileVersion.setStatusByUserName(user.getFullName());
1679 dlFileVersion.setStatusDate(dlFileEntry.getModifiedDate());
1680 dlFileVersion.setExpandoBridgeAttributes(serviceContext);
1681
1682 dlFileVersionPersistence.update(dlFileVersion);
1683
1684 if ((fileEntryTypeId > 0) && (fieldsMap != null)) {
1685 dlFileEntryMetadataLocalService.updateFileEntryMetadata(
1686 fileEntryTypeId, dlFileEntry.getFileEntryId(), fileVersionId,
1687 fieldsMap, serviceContext);
1688 }
1689
1690 return dlFileVersion;
1691 }
1692
1693 protected void convertExtraSettings(
1694 DLFileEntry dlFileEntry, DLFileVersion dlFileVersion, String[] keys)
1695 throws PortalException, SystemException {
1696
1697 UnicodeProperties extraSettingsProperties =
1698 dlFileVersion.getExtraSettingsProperties();
1699
1700 ExpandoBridge expandoBridge = dlFileVersion.getExpandoBridge();
1701
1702 convertExtraSettings(extraSettingsProperties, expandoBridge, keys);
1703
1704 dlFileVersion.setExtraSettingsProperties(extraSettingsProperties);
1705
1706 dlFileVersionPersistence.update(dlFileVersion);
1707
1708 int status = dlFileVersion.getStatus();
1709
1710 if ((status == WorkflowConstants.STATUS_APPROVED) &&
1711 (DLUtil.compareVersions(
1712 dlFileEntry.getVersion(), dlFileVersion.getVersion()) <= 0)) {
1713
1714 reindex(dlFileEntry);
1715 }
1716 }
1717
1718 protected void convertExtraSettings(DLFileEntry dlFileEntry, String[] keys)
1719 throws PortalException, SystemException {
1720
1721 UnicodeProperties extraSettingsProperties =
1722 dlFileEntry.getExtraSettingsProperties();
1723
1724 ExpandoBridge expandoBridge = dlFileEntry.getExpandoBridge();
1725
1726 convertExtraSettings(extraSettingsProperties, expandoBridge, keys);
1727
1728 dlFileEntry.setExtraSettingsProperties(extraSettingsProperties);
1729
1730 dlFileEntryPersistence.update(dlFileEntry);
1731
1732 List<DLFileVersion> dlFileVersions =
1733 dlFileVersionLocalService.getFileVersions(
1734 dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);
1735
1736 for (DLFileVersion dlFileVersion : dlFileVersions) {
1737 convertExtraSettings(dlFileEntry, dlFileVersion, keys);
1738 }
1739 }
1740
1741 protected void convertExtraSettings(
1742 UnicodeProperties extraSettingsProperties, ExpandoBridge expandoBridge,
1743 String[] keys) {
1744
1745 for (String key : keys) {
1746 String value = extraSettingsProperties.remove(key);
1747
1748 if (Validator.isNull(value)) {
1749 continue;
1750 }
1751
1752 int type = expandoBridge.getAttributeType(key);
1753
1754 Serializable serializable = ExpandoColumnConstants.getSerializable(
1755 type, value);
1756
1757 expandoBridge.setAttribute(key, serializable);
1758 }
1759 }
1760
1761 protected void copyExpandoRowModifiedDate(
1762 long companyId, long sourceFileVersionId,
1763 long destinationFileVersionId)
1764 throws PortalException, SystemException {
1765
1766 ExpandoTable expandoTable = null;
1767
1768 try {
1769 expandoTable = expandoTableLocalService.getDefaultTable(
1770 companyId, DLFileEntry.class.getName());
1771 }
1772 catch (NoSuchTableException nste) {
1773 return;
1774 }
1775
1776 Date sourceModifiedDate = null;
1777
1778 try {
1779 ExpandoRow sourceExpandoRow = expandoRowLocalService.getRow(
1780 expandoTable.getTableId(), sourceFileVersionId);
1781
1782 sourceModifiedDate = sourceExpandoRow.getModifiedDate();
1783 }
1784 catch (NoSuchRowException nsre) {
1785 return;
1786 }
1787
1788 try {
1789 ExpandoRow destinationExpandoRow = expandoRowLocalService.getRow(
1790 expandoTable.getTableId(), destinationFileVersionId);
1791
1792 destinationExpandoRow.setModifiedDate(sourceModifiedDate);
1793
1794 expandoRowLocalService.updateExpandoRow(destinationExpandoRow);
1795 }
1796 catch (NoSuchRowException nsre) {
1797 }
1798 }
1799
1800 protected List<ObjectValuePair<Long, Integer>> getDlFileVersionStatuses(
1801 List<DLFileVersion> dlFileVersions) {
1802
1803 List<ObjectValuePair<Long, Integer>> dlFileVersionStatusOVPs =
1804 new ArrayList<ObjectValuePair<Long, Integer>>(
1805 dlFileVersions.size());
1806
1807 for (DLFileVersion dlFileVersion : dlFileVersions) {
1808 int status = dlFileVersion.getStatus();
1809
1810 if (status == WorkflowConstants.STATUS_PENDING) {
1811 status = WorkflowConstants.STATUS_DRAFT;
1812 }
1813
1814 ObjectValuePair<Long, Integer> dlFileVersionStatusOVP =
1815 new ObjectValuePair<Long, Integer>(
1816 dlFileVersion.getFileVersionId(), status);
1817
1818 dlFileVersionStatusOVPs.add(dlFileVersionStatusOVP);
1819 }
1820
1821 return dlFileVersionStatusOVPs;
1822 }
1823
1824 protected String getNextVersion(
1825 DLFileEntry dlFileEntry, boolean majorVersion, int workflowAction)
1826 throws PortalException, SystemException {
1827
1828 String version = dlFileEntry.getVersion();
1829
1830 try {
1831 DLFileVersion dlFileVersion =
1832 dlFileVersionLocalService.getLatestFileVersion(
1833 dlFileEntry.getFileEntryId(), true);
1834
1835 version = dlFileVersion.getVersion();
1836 }
1837 catch (NoSuchFileVersionException nsfve) {
1838 }
1839
1840 if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1841 majorVersion = false;
1842 }
1843
1844 int[] versionParts = StringUtil.split(version, StringPool.PERIOD, 0);
1845
1846 if (majorVersion) {
1847 versionParts[0]++;
1848 versionParts[1] = 0;
1849 }
1850 else {
1851 versionParts[1]++;
1852 }
1853
1854 return versionParts[0] + StringPool.PERIOD + versionParts[1];
1855 }
1856
1857 protected boolean isKeepFileVersionLabel(
1858 DLFileEntry dlFileEntry, DLFileVersion lastDLFileVersion,
1859 DLFileVersion latestDLFileVersion, int workflowAction)
1860 throws PortalException, SystemException {
1861
1862 if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
1863 return false;
1864 }
1865
1866 if (PropsValues.DL_FILE_ENTRY_VERSION_POLICY != 1) {
1867 return false;
1868 }
1869
1870 if ((lastDLFileVersion.getFolderId() ==
1871 latestDLFileVersion.getFolderId()) &&
1872 Validator.equals(
1873 lastDLFileVersion.getTitle(), latestDLFileVersion.getTitle()) &&
1874 Validator.equals(
1875 lastDLFileVersion.getDescription(),
1876 latestDLFileVersion.getDescription()) &&
1877 (lastDLFileVersion.getFileEntryTypeId() ==
1878 latestDLFileVersion.getFileEntryTypeId())) {
1879
1880
1881
1882 AssetEntry lastAssetEntry = assetEntryLocalService.getEntry(
1883 DLFileEntryConstants.getClassName(),
1884 dlFileEntry.getFileEntryId());
1885 AssetEntry latestAssetEntry = assetEntryLocalService.getEntry(
1886 DLFileEntryConstants.getClassName(),
1887 latestDLFileVersion.getFileVersionId());
1888
1889 if (!Validator.equalsSorted(
1890 lastAssetEntry.getCategoryIds(),
1891 latestAssetEntry.getCategoryIds())) {
1892
1893 return false;
1894 }
1895
1896 if (!Validator.equalsSorted(
1897 lastAssetEntry.getTagNames(),
1898 latestAssetEntry.getTagNames())) {
1899
1900 return false;
1901 }
1902
1903 List<AssetLink> lastAssetLinks =
1904 assetLinkLocalService.getDirectLinks(
1905 lastAssetEntry.getEntryId(),
1906 AssetLinkConstants.TYPE_RELATED);
1907 List<AssetLink> latestAssetLinks =
1908 assetLinkLocalService.getDirectLinks(
1909 latestAssetEntry.getEntryId(),
1910 AssetLinkConstants.TYPE_RELATED);
1911
1912 if (!Validator.equalsSorted(
1913 StringUtil.split(
1914 ListUtil.toString(
1915 lastAssetLinks, AssetLink.ENTRY_ID2_ACCESSOR), 0L),
1916 StringUtil.split(
1917 ListUtil.toString(
1918 latestAssetLinks, AssetLink.ENTRY_ID2_ACCESSOR),
1919 0L))) {
1920
1921 return false;
1922 }
1923
1924
1925
1926 ExpandoTable expandoTable = null;
1927
1928 try {
1929 expandoTable = expandoTableLocalService.getDefaultTable(
1930 lastDLFileVersion.getCompanyId(),
1931 DLFileEntry.class.getName());
1932 }
1933 catch (NoSuchTableException nste) {
1934 }
1935
1936 if (expandoTable != null) {
1937 Date lastModifiedDate = null;
1938
1939 try {
1940 ExpandoRow lastExpandoRow = expandoRowLocalService.getRow(
1941 expandoTable.getTableId(),
1942 lastDLFileVersion.getPrimaryKey());
1943
1944 lastModifiedDate = lastExpandoRow.getModifiedDate();
1945 }
1946 catch (NoSuchRowException nsre) {
1947 }
1948
1949 Date latestModifiedDate = null;
1950
1951 try {
1952 ExpandoRow latestExpandoRow = expandoRowLocalService.getRow(
1953 expandoTable.getTableId(),
1954 latestDLFileVersion.getPrimaryKey());
1955
1956 latestModifiedDate = latestExpandoRow.getModifiedDate();
1957 }
1958 catch (NoSuchRowException nsre) {
1959 }
1960
1961 if (!Validator.equals(lastModifiedDate, latestModifiedDate)) {
1962 return false;
1963 }
1964 }
1965
1966
1967
1968 List<DLFileEntryMetadata> lastFileEntryMetadatas =
1969 dlFileEntryMetadataLocalService.
1970 getFileVersionFileEntryMetadatas(
1971 lastDLFileVersion.getFileVersionId());
1972 List<DLFileEntryMetadata> latestFileEntryMetadatas =
1973 dlFileEntryMetadataLocalService.
1974 getFileVersionFileEntryMetadatas(
1975 latestDLFileVersion.getFileVersionId());
1976
1977 for (DLFileEntryMetadata lastFileEntryMetadata :
1978 lastFileEntryMetadatas) {
1979
1980 Fields lastFields = StorageEngineUtil.getFields(
1981 lastFileEntryMetadata.getDDMStorageId());
1982
1983 boolean found = false;
1984
1985 for (DLFileEntryMetadata latestEntryMetadata :
1986 latestFileEntryMetadatas) {
1987
1988 Fields latestFields = StorageEngineUtil.getFields(
1989 latestEntryMetadata.getDDMStorageId());
1990
1991 if (lastFields.equals(latestFields)) {
1992 found = true;
1993
1994 break;
1995 }
1996 }
1997
1998 if (!found) {
1999 return false;
2000 }
2001 }
2002
2003
2004
2005 long lastSize = lastDLFileVersion.getSize();
2006 long latestSize = latestDLFileVersion.getSize();
2007
2008 if ((lastSize == 0) && ((latestSize == 0) || (latestSize > 0))) {
2009 return true;
2010 }
2011
2012 if (lastSize != latestSize) {
2013 return false;
2014 }
2015
2016
2017
2018 InputStream lastInputStream = null;
2019 InputStream latestInputStream = null;
2020
2021 try {
2022 String lastChecksum = lastDLFileVersion.getChecksum();
2023
2024 if (Validator.isNull(lastChecksum)) {
2025 lastInputStream = DLStoreUtil.getFileAsStream(
2026 dlFileEntry.getCompanyId(),
2027 dlFileEntry.getDataRepositoryId(),
2028 dlFileEntry.getName(), lastDLFileVersion.getVersion());
2029
2030 lastChecksum = DigesterUtil.digestBase64(lastInputStream);
2031
2032 lastDLFileVersion.setChecksum(lastChecksum);
2033
2034 dlFileVersionPersistence.update(lastDLFileVersion);
2035 }
2036
2037 latestInputStream = DLStoreUtil.getFileAsStream(
2038 dlFileEntry.getCompanyId(),
2039 dlFileEntry.getDataRepositoryId(), dlFileEntry.getName(),
2040 latestDLFileVersion.getVersion());
2041
2042 String latestChecksum = DigesterUtil.digestBase64(
2043 latestInputStream);
2044
2045 if (lastChecksum.equals(latestChecksum)) {
2046 return true;
2047 }
2048
2049 latestDLFileVersion.setChecksum(latestChecksum);
2050
2051 dlFileVersionPersistence.update(latestDLFileVersion);
2052 }
2053 catch (Exception e) {
2054 if (_log.isWarnEnabled()) {
2055 _log.warn(e, e);
2056 }
2057 }
2058 finally {
2059 StreamUtil.cleanUp(lastInputStream);
2060 StreamUtil.cleanUp(latestInputStream);
2061 }
2062 }
2063
2064 return false;
2065 }
2066
2067 protected DLFileEntry moveFileEntryImpl(
2068 long userId, long fileEntryId, long newFolderId,
2069 ServiceContext serviceContext)
2070 throws PortalException, SystemException {
2071
2072
2073
2074 User user = userPersistence.findByPrimaryKey(userId);
2075 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
2076 fileEntryId);
2077
2078 long oldDataRepositoryId = dlFileEntry.getDataRepositoryId();
2079
2080 validateFile(
2081 dlFileEntry.getGroupId(), newFolderId, dlFileEntry.getFileEntryId(),
2082 dlFileEntry.getTitle(), dlFileEntry.getExtension());
2083
2084 if (DLStoreUtil.hasFile(
2085 user.getCompanyId(),
2086 DLFolderConstants.getDataRepositoryId(
2087 dlFileEntry.getGroupId(), newFolderId),
2088 dlFileEntry.getName(), StringPool.BLANK)) {
2089
2090 throw new DuplicateFileException(dlFileEntry.getName());
2091 }
2092
2093 dlFileEntry.setModifiedDate(serviceContext.getModifiedDate(null));
2094 dlFileEntry.setFolderId(newFolderId);
2095
2096 dlFileEntryPersistence.update(dlFileEntry);
2097
2098
2099
2100 List<DLFileVersion> dlFileVersions =
2101 dlFileVersionPersistence.findByFileEntryId(fileEntryId);
2102
2103 for (DLFileVersion dlFileVersion : dlFileVersions) {
2104 dlFileVersion.setFolderId(newFolderId);
2105
2106 dlFileVersionPersistence.update(dlFileVersion);
2107 }
2108
2109
2110
2111 if (newFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
2112 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
2113 newFolderId);
2114
2115 dlFolder.setModifiedDate(serviceContext.getModifiedDate(null));
2116
2117 dlFolderPersistence.update(dlFolder);
2118 }
2119
2120
2121
2122 DLStoreUtil.updateFile(
2123 user.getCompanyId(), oldDataRepositoryId,
2124 dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
2125
2126 return dlFileEntry;
2127 }
2128
2129 protected void reindex(DLFileEntry dlFileEntry) throws SearchException {
2130 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
2131 DLFileEntry.class);
2132
2133 indexer.reindex(dlFileEntry);
2134 }
2135
2136 protected void removeFileVersion(
2137 DLFileEntry dlFileEntry, DLFileVersion dlFileVersion)
2138 throws PortalException, SystemException {
2139
2140 dlFileVersionPersistence.remove(dlFileVersion);
2141
2142 expandoValueLocalService.deleteValues(
2143 DLFileVersion.class.getName(), dlFileVersion.getFileVersionId());
2144
2145 dlFileEntryMetadataLocalService.deleteFileVersionFileEntryMetadata(
2146 dlFileVersion.getFileVersionId());
2147
2148 try {
2149 DLStoreUtil.deleteFile(
2150 dlFileEntry.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2151 dlFileEntry.getName(),
2152 DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);
2153 }
2154 catch (NoSuchModelException nsme) {
2155 }
2156
2157 unlockFileEntry(dlFileEntry.getFileEntryId());
2158 }
2159
2160 protected void startWorkflowInstance(
2161 long userId, ServiceContext serviceContext,
2162 DLFileVersion dlFileVersion, String syncEventType)
2163 throws PortalException, SystemException {
2164
2165 Map<String, Serializable> workflowContext =
2166 new HashMap<String, Serializable>();
2167
2168 workflowContext.put("event", syncEventType);
2169
2170 WorkflowHandlerRegistryUtil.startWorkflowInstance(
2171 dlFileVersion.getCompanyId(), dlFileVersion.getGroupId(), userId,
2172 DLFileEntry.class.getName(), dlFileVersion.getFileVersionId(),
2173 dlFileVersion, serviceContext, workflowContext);
2174 }
2175
2176 protected DLFileEntry updateFileEntry(
2177 long userId, long fileEntryId, String sourceFileName,
2178 String extension, String mimeType, String title, String description,
2179 String changeLog, boolean majorVersion, String extraSettings,
2180 long fileEntryTypeId, Map<String, Fields> fieldsMap, File file,
2181 InputStream is, long size, ServiceContext serviceContext)
2182 throws PortalException, SystemException {
2183
2184 User user = userPersistence.findByPrimaryKey(userId);
2185 DLFileEntry dlFileEntry = dlFileEntryPersistence.findByPrimaryKey(
2186 fileEntryId);
2187
2188 boolean checkedOut = dlFileEntry.isCheckedOut();
2189
2190 DLFileVersion dlFileVersion =
2191 dlFileVersionLocalService.getLatestFileVersion(
2192 fileEntryId, !checkedOut);
2193
2194 boolean autoCheckIn = !checkedOut && dlFileVersion.isApproved();
2195
2196 if (autoCheckIn) {
2197 dlFileEntry = checkOutFileEntry(
2198 userId, fileEntryId, serviceContext);
2199 }
2200 else if (!checkedOut) {
2201 lockFileEntry(userId, fileEntryId);
2202 }
2203
2204 if (!hasFileEntryLock(userId, fileEntryId)) {
2205 lockFileEntry(userId, fileEntryId);
2206 }
2207
2208 if (checkedOut || autoCheckIn) {
2209 dlFileVersion = dlFileVersionLocalService.getLatestFileVersion(
2210 fileEntryId, false);
2211 }
2212
2213 try {
2214 if (Validator.isNull(extension)) {
2215 extension = dlFileEntry.getExtension();
2216 }
2217
2218 if (Validator.isNull(mimeType)) {
2219 mimeType = dlFileEntry.getMimeType();
2220 }
2221
2222 if (Validator.isNull(title)) {
2223 title = sourceFileName;
2224
2225 if (Validator.isNull(title)) {
2226 title = dlFileEntry.getTitle();
2227 }
2228 }
2229
2230 Date now = new Date();
2231
2232 validateFile(
2233 dlFileEntry.getGroupId(), dlFileEntry.getFolderId(),
2234 dlFileEntry.getFileEntryId(), title, extension, sourceFileName,
2235 file, is);
2236
2237
2238
2239 String version = dlFileVersion.getVersion();
2240
2241 if (size == 0) {
2242 size = dlFileVersion.getSize();
2243 }
2244
2245 updateFileVersion(
2246 user, dlFileVersion, sourceFileName, extension, mimeType, title,
2247 description, changeLog, extraSettings, fileEntryTypeId,
2248 fieldsMap, version, size, dlFileVersion.getStatus(),
2249 serviceContext.getModifiedDate(now), serviceContext);
2250
2251
2252
2253 if (dlFileEntry.getFolderId() !=
2254 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
2255
2256 DLFolder dlFolder = dlFolderPersistence.findByPrimaryKey(
2257 dlFileEntry.getFolderId());
2258
2259 dlFolder.setLastPostDate(serviceContext.getModifiedDate(now));
2260
2261 dlFolderPersistence.update(dlFolder);
2262 }
2263
2264
2265
2266 dlAppHelperLocalService.updateAsset(
2267 userId, new LiferayFileEntry(dlFileEntry),
2268 new LiferayFileVersion(dlFileVersion),
2269 serviceContext.getAssetCategoryIds(),
2270 serviceContext.getAssetTagNames(),
2271 serviceContext.getAssetLinkEntryIds());
2272
2273
2274
2275 if ((file != null) || (is != null)) {
2276 try {
2277 DLStoreUtil.deleteFile(
2278 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2279 dlFileEntry.getName(), version);
2280 }
2281 catch (NoSuchModelException nsme) {
2282 }
2283
2284 if (file != null) {
2285 DLStoreUtil.updateFile(
2286 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2287 dlFileEntry.getName(), dlFileEntry.getExtension(),
2288 false, version, sourceFileName, file);
2289 }
2290 else {
2291 DLStoreUtil.updateFile(
2292 user.getCompanyId(), dlFileEntry.getDataRepositoryId(),
2293 dlFileEntry.getName(), dlFileEntry.getExtension(),
2294 false, version, sourceFileName, is);
2295 }
2296 }
2297
2298 if (autoCheckIn) {
2299 dlFileEntryService.checkInFileEntry(
2300 fileEntryId, majorVersion, changeLog, serviceContext);
2301 }
2302 else if (!checkedOut &&
2303 (serviceContext.getWorkflowAction() ==
2304 WorkflowConstants.ACTION_PUBLISH)) {
2305
2306 String syncEvent = DLSyncConstants.EVENT_UPDATE;
2307
2308 if (dlFileVersion.getVersion().equals(
2309 DLFileEntryConstants.VERSION_DEFAULT)) {
2310
2311 syncEvent = DLSyncConstants.EVENT_ADD;
2312 }
2313
2314 startWorkflowInstance(
2315 userId, serviceContext, dlFileVersion, syncEvent);
2316 }
2317 }
2318 catch (PortalException pe) {
2319 if (autoCheckIn) {
2320 dlFileEntryService.cancelCheckOut(fileEntryId);
2321 }
2322
2323 throw pe;
2324 }
2325 catch (SystemException se) {
2326 if (autoCheckIn) {
2327 dlFileEntryService.cancelCheckOut(fileEntryId);
2328 }
2329
2330 throw se;
2331 }
2332 finally {
2333 if (!autoCheckIn && !checkedOut) {
2334 unlockFileEntry(fileEntryId);
2335 }
2336 }
2337
2338 return dlFileEntryPersistence.findByPrimaryKey(fileEntryId);
2339 }
2340
2341 protected DLFileVersion updateFileVersion(
2342 User user, DLFileVersion dlFileVersion, String sourceFileName,
2343 String extension, String mimeType, String title, String description,
2344 String changeLog, String extraSettings, long fileEntryTypeId,
2345 Map<String, Fields> fieldsMap, String version, long size,
2346 int status, Date statusDate, ServiceContext serviceContext)
2347 throws PortalException, SystemException {
2348
2349 dlFileVersion.setModifiedDate(statusDate);
2350
2351 if (Validator.isNotNull(sourceFileName)) {
2352 dlFileVersion.setExtension(extension);
2353 dlFileVersion.setMimeType(mimeType);
2354 }
2355
2356 dlFileVersion.setTitle(title);
2357 dlFileVersion.setDescription(description);
2358 dlFileVersion.setChangeLog(changeLog);
2359 dlFileVersion.setExtraSettings(extraSettings);
2360 dlFileVersion.setFileEntryTypeId(fileEntryTypeId);
2361 dlFileVersion.setVersion(version);
2362 dlFileVersion.setSize(size);
2363 dlFileVersion.setStatus(status);
2364 dlFileVersion.setStatusByUserId(user.getUserId());
2365 dlFileVersion.setStatusByUserName(user.getFullName());
2366 dlFileVersion.setStatusDate(statusDate);
2367 dlFileVersion.setExpandoBridgeAttributes(serviceContext);
2368
2369 dlFileVersion = dlFileVersionPersistence.update(dlFileVersion);
2370
2371 if ((fileEntryTypeId > 0) && (fieldsMap != null)) {
2372 dlFileEntryMetadataLocalService.updateFileEntryMetadata(
2373 fileEntryTypeId, dlFileVersion.getFileEntryId(),
2374 dlFileVersion.getFileVersionId(), fieldsMap, serviceContext);
2375 }
2376
2377 return dlFileVersion;
2378 }
2379
2380 protected void validateFile(
2381 long groupId, long folderId, long fileEntryId, String title,
2382 String extension)
2383 throws PortalException, SystemException {
2384
2385 DLFolder dlFolder = dlFolderPersistence.fetchByG_P_N(
2386 groupId, folderId, title);
2387
2388 if (dlFolder != null) {
2389 throw new DuplicateFolderNameException(title);
2390 }
2391
2392 DLFileEntry dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
2393 groupId, folderId, title);
2394
2395 if ((dlFileEntry != null) &&
2396 (dlFileEntry.getFileEntryId() != fileEntryId)) {
2397
2398 throw new DuplicateFileException(title);
2399 }
2400
2401 String periodAndExtension = StringPool.PERIOD + extension;
2402
2403 if (!title.endsWith(periodAndExtension)) {
2404 title += periodAndExtension;
2405
2406 dlFileEntry = dlFileEntryPersistence.fetchByG_F_T(
2407 groupId, folderId, title);
2408
2409 if ((dlFileEntry != null) &&
2410 (dlFileEntry.getFileEntryId() != fileEntryId)) {
2411
2412 throw new DuplicateFileException(title);
2413 }
2414 }
2415 }
2416
2417 protected void validateFile(
2418 long groupId, long folderId, long fileEntryId, String title,
2419 String extension, String sourceFileName, File file, InputStream is)
2420 throws PortalException, SystemException {
2421
2422 if (Validator.isNotNull(sourceFileName)) {
2423 if (file != null) {
2424 DLStoreUtil.validate(
2425 sourceFileName, extension, sourceFileName, true, file);
2426 }
2427 else {
2428 DLStoreUtil.validate(
2429 sourceFileName, extension, sourceFileName, true, is);
2430 }
2431 }
2432
2433 validateFileExtension(extension);
2434 validateFileName(title);
2435
2436 DLStoreUtil.validate(title, false);
2437
2438 validateFile(groupId, folderId, fileEntryId, title, extension);
2439 }
2440
2441 protected void validateFileEntryTypeId(
2442 long[] groupIds, long folderId, long fileEntryTypeId)
2443 throws PortalException, SystemException {
2444
2445 List<DLFileEntryType> dlFileEntryTypes =
2446 dlFileEntryTypeLocalService.getFolderFileEntryTypes(
2447 groupIds, folderId, true);
2448
2449 for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
2450 if (dlFileEntryType.getFileEntryTypeId() == fileEntryTypeId) {
2451 return;
2452 }
2453 }
2454
2455 throw new InvalidFileEntryTypeException(
2456 "Invalid file entry type " + fileEntryTypeId + " for folder " +
2457 folderId);
2458 }
2459
2460 protected void validateFileExtension(String extension)
2461 throws PortalException {
2462
2463 if (Validator.isNotNull(extension)) {
2464 int maxLength = ModelHintsUtil.getMaxLength(
2465 DLFileEntry.class.getName(), "extension");
2466
2467 if (extension.length() > maxLength) {
2468 throw new FileExtensionException();
2469 }
2470 }
2471 }
2472
2473 protected void validateFileName(String fileName) throws PortalException {
2474 if (fileName.contains(StringPool.SLASH)) {
2475 throw new FileNameException(fileName);
2476 }
2477 }
2478
2479 private static final int _DELETE_INTERVAL = 100;
2480
2481 private static Log _log = LogFactoryUtil.getLog(
2482 DLFileEntryLocalServiceImpl.class);
2483
2484 }