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