@Test public void testUpdateAssetWhenUpdatingFileEntry() throws Throwable { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group.getGroupId()); FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), _group.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString(), ContentTypes.TEXT_PLAIN, "Old Title", RandomTestUtil.randomString(), null, RandomTestUtil.randomBytes(), serviceContext); DLAppLocalServiceUtil.updateFileEntry( TestPropsValues.getUserId(), fileEntry.getFileEntryId(), RandomTestUtil.randomString(), ContentTypes.TEXT_PLAIN, "New Title", RandomTestUtil.randomString(), null, true, RandomTestUtil.randomBytes(), serviceContext); AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry( DLFileEntryConstants.getClassName(), fileEntry.getFileEntryId()); Assert.assertEquals("New Title", assetEntry.getTitle()); }
@Override protected void doImportStagedModel(PortletDataContext portletDataContext, FileEntry fileEntry) throws Exception { long userId = portletDataContext.getUserId(fileEntry.getUserUuid()); if (!fileEntry.isDefaultRepository()) { // References has been automatically imported, nothing to do here return; } Map<Long, Long> folderIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Folder.class); long folderId = MapUtil.getLong(folderIds, fileEntry.getFolderId(), fileEntry.getFolderId()); long[] assetCategoryIds = portletDataContext.getAssetCategoryIds(DLFileEntry.class, fileEntry.getFileEntryId()); String[] assetTagNames = portletDataContext.getAssetTagNames(DLFileEntry.class, fileEntry.getFileEntryId()); ServiceContext serviceContext = portletDataContext.createServiceContext(fileEntry, DLFileEntry.class); serviceContext.setAttribute("sourceFileName", "A." + fileEntry.getExtension()); serviceContext.setUserId(userId); Element fileEntryElement = portletDataContext.getImportDataElement(fileEntry); String binPath = fileEntryElement.attributeValue("bin-path"); InputStream is = null; if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) { try { is = FileEntryUtil.getContentStream(fileEntry); } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn("Unable to retrieve content for file entry " + fileEntry.getFileEntryId(), e); } return; } } else { is = portletDataContext.getZipEntryAsInputStream(binPath); } if (is == null) { if (_log.isWarnEnabled()) { _log.warn("No file found for file entry " + fileEntry.getFileEntryId()); } return; } importMetaData(portletDataContext, fileEntryElement, fileEntry, serviceContext); FileEntry importedFileEntry = null; String titleWithExtension = DLUtil.getTitleWithExtension(fileEntry); String extension = fileEntry.getExtension(); String periodAndExtension = StringPool.PERIOD.concat(extension); if (portletDataContext.isDataStrategyMirror()) { FileEntry existingFileEntry = fetchStagedModelByUuidAndGroupId( fileEntry.getUuid(), portletDataContext.getScopeGroupId()); FileVersion fileVersion = fileEntry.getFileVersion(); if (existingFileEntry == null) { String fileEntryTitle = fileEntry.getTitle(); FileEntry existingTitleFileEntry = FileEntryUtil.fetchByR_F_T( portletDataContext.getScopeGroupId(), folderId, fileEntryTitle); if (existingTitleFileEntry != null) { if ((fileEntry.getGroupId() == portletDataContext.getSourceGroupId()) && portletDataContext.isDataStrategyMirrorWithOverwriting()) { DLAppLocalServiceUtil.deleteFileEntry(existingTitleFileEntry.getFileEntryId()); } else { boolean titleHasExtension = false; if (fileEntryTitle.endsWith(periodAndExtension)) { fileEntryTitle = FileUtil.stripExtension(fileEntryTitle); titleHasExtension = true; } for (int i = 1; ; i++) { fileEntryTitle += StringPool.SPACE + i; titleWithExtension = fileEntryTitle + periodAndExtension; existingTitleFileEntry = FileEntryUtil.fetchByR_F_T( portletDataContext.getScopeGroupId(), folderId, titleWithExtension); if (existingTitleFileEntry == null) { if (titleHasExtension) { fileEntryTitle += periodAndExtension; } break; } } } } serviceContext.setAttribute("fileVersionUuid", fileVersion.getUuid()); serviceContext.setUuid(fileEntry.getUuid()); importedFileEntry = DLAppLocalServiceUtil.addFileEntry( userId, portletDataContext.getScopeGroupId(), folderId, titleWithExtension, fileEntry.getMimeType(), fileEntryTitle, fileEntry.getDescription(), null, is, fileEntry.getSize(), serviceContext); if (fileEntry.isInTrash()) { importedFileEntry = DLAppServiceUtil.moveFileEntryToTrash(importedFileEntry.getFileEntryId()); } } else { FileVersion latestExistingFileVersion = existingFileEntry.getLatestFileVersion(true); boolean indexEnabled = serviceContext.isIndexingEnabled(); boolean deleteFileEntry = false; boolean updateFileEntry = false; if (!Validator.equals(fileVersion.getUuid(), latestExistingFileVersion.getUuid())) { deleteFileEntry = true; updateFileEntry = true; } else { InputStream existingFileVersionInputStream = null; try { existingFileVersionInputStream = latestExistingFileVersion.getContentStream(false); } catch (Exception e) { if (_log.isDebugEnabled()) { _log.debug(e, e); } } finally { if (existingFileVersionInputStream != null) { existingFileVersionInputStream.close(); } } if (existingFileVersionInputStream == null) { updateFileEntry = true; } } try { serviceContext.setIndexingEnabled(false); if (updateFileEntry) { DLFileVersion alreadyExistingFileVersion = DLFileVersionLocalServiceUtil.getFileVersionByUuidAndGroupId( fileVersion.getUuid(), existingFileEntry.getGroupId()); if (alreadyExistingFileVersion != null) { serviceContext.setAttribute( "existingDLFileVersionId", alreadyExistingFileVersion.getFileVersionId()); } serviceContext.setUuid(fileVersion.getUuid()); importedFileEntry = DLAppLocalServiceUtil.updateFileEntry( userId, existingFileEntry.getFileEntryId(), titleWithExtension, fileEntry.getMimeType(), fileEntry.getTitle(), fileEntry.getDescription(), null, false, is, fileEntry.getSize(), serviceContext); } else { DLAppLocalServiceUtil.updateAsset( userId, existingFileEntry, latestExistingFileVersion, assetCategoryIds, assetTagNames, null); importedFileEntry = existingFileEntry; } if (importedFileEntry.getFolderId() != folderId) { importedFileEntry = DLAppLocalServiceUtil.moveFileEntry( userId, importedFileEntry.getFileEntryId(), folderId, serviceContext); } if (importedFileEntry instanceof LiferayFileEntry) { LiferayFileEntry liferayFileEntry = (LiferayFileEntry) importedFileEntry; Indexer<DLFileEntry> indexer = IndexerRegistryUtil.nullSafeGetIndexer(DLFileEntry.class); indexer.reindex((DLFileEntry) liferayFileEntry.getModel()); } if (deleteFileEntry && ExportImportThreadLocal.isStagingInProcess()) { DLAppServiceUtil.deleteFileVersion( latestExistingFileVersion.getFileEntryId(), latestExistingFileVersion.getVersion()); } } finally { serviceContext.setIndexingEnabled(indexEnabled); } } } else { try { importedFileEntry = DLAppLocalServiceUtil.addFileEntry( userId, portletDataContext.getScopeGroupId(), folderId, titleWithExtension, fileEntry.getMimeType(), fileEntry.getTitle(), fileEntry.getDescription(), null, is, fileEntry.getSize(), serviceContext); } catch (DuplicateFileException dfe) { String title = fileEntry.getTitle(); String[] titleParts = title.split("\\.", 2); title = titleParts[0] + StringUtil.randomString(); if (titleParts.length > 1) { title += StringPool.PERIOD + titleParts[1]; } if (!title.endsWith(periodAndExtension)) { title += periodAndExtension; } importedFileEntry = DLAppLocalServiceUtil.addFileEntry( userId, portletDataContext.getScopeGroupId(), folderId, title, fileEntry.getMimeType(), title, fileEntry.getDescription(), null, is, fileEntry.getSize(), serviceContext); } } if (portletDataContext.getBooleanParameter( DLPortletDataHandler.NAMESPACE, "previews-and-thumbnails")) { DLProcessorRegistryUtil.importGeneratedFiles( portletDataContext, fileEntry, importedFileEntry, fileEntryElement); } portletDataContext.importClassedModel(fileEntry, importedFileEntry, DLFileEntry.class); Map<Long, Long> fileEntryIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(FileEntry.class); fileEntryIds.put(fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId()); }