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