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