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