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