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