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