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