@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 public FileEntry addPortletFileEntry( long groupId, long userId, String className, long classPK, String portletId, long folderId, File file, String fileName, String mimeType, boolean indexingEnabled) throws PortalException, SystemException { if (Validator.isNull(fileName)) { return null; } ServiceContext serviceContext = new ServiceContext(); serviceContext.setAddGroupPermissions(true); serviceContext.setAddGuestPermissions(true); Repository repository = addPortletRepository(groupId, portletId, serviceContext); serviceContext.setAttribute("className", className); serviceContext.setAttribute("classPK", String.valueOf(classPK)); serviceContext.setIndexingEnabled(indexingEnabled); if (Validator.isNull(mimeType) || mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) { mimeType = MimeTypesUtil.getContentType(file, fileName); } boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled(); try { DLAppHelperThreadLocal.setEnabled(false); return DLAppLocalServiceUtil.addFileEntry( userId, repository.getRepositoryId(), folderId, fileName, mimeType, fileName, StringPool.BLANK, StringPool.BLANK, file, serviceContext); } finally { DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled); } }
@Test public void testGetFieldsFromContentWithDocumentLibraryElement() throws Exception { Fields expectedFields = new Fields(); ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(), TestPropsValues.getUserId()); FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), group.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "Test 1.txt", ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(), serviceContext); Field documentLibraryField = getDocumentLibraryField(fileEntry, _ddmStructure.getStructureId()); expectedFields.put(documentLibraryField); Field fieldsDisplayField = getFieldsDisplayField(_ddmStructure.getStructureId(), "document_library_INSTANCE_4aGOvP3N"); expectedFields.put(fieldsDisplayField); String content = read("test-journal-content-doc-library-field.xml"); XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-content"); Document document = SAXReaderUtil.read(content); Element element = (Element) xPathSelector.selectSingleNode(document); String[] previewURLs = new String[2]; previewURLs[0] = DLUtil.getPreviewURL( fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, true, true); previewURLs[1] = DLUtil.getPreviewURL( fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, false, false); for (int i = 0; i < previewURLs.length; i++) { element.addCDATA(previewURLs[i]); Fields actualFields = JournalConverterUtil.getDDMFields(_ddmStructure, document.asXML()); Assert.assertEquals(expectedFields, actualFields); } }
protected FileEntry addFileEntry(long folderId, String sourceFileName) throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(), TestPropsValues.getUserId()); return DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), group.getGroupId(), folderId, sourceFileName, ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE), serviceContext); }
protected void doAddDLFileEntries(String name, InputStream inputStream, long length) throws Exception { String mimeType = MimeTypesUtil.getContentType(name); FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry( userId, groupId, 0, name, mimeType, name, StringPool.BLANK, StringPool.BLANK, inputStream, length, serviceContext); _fileEntries.put(name, fileEntry); }
@Test public void testRequestFileInTrash() throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(), TestPropsValues.getUserId()); FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), group.getGroupId(), parentFolder.getFolderId(), "Test Trash.txt", ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(), serviceContext); MockHttpServletResponse mockHttpServletResponse = testRequestFile(fileEntry, _user, false); Assert.assertEquals(MockHttpServletResponse.SC_OK, mockHttpServletResponse.getStatus()); DLAppServiceUtil.moveFileEntryToTrash(fileEntry.getFileEntryId()); mockHttpServletResponse = testRequestFile(fileEntry, _user, false); Assert.assertEquals(MockHttpServletResponse.SC_NOT_FOUND, mockHttpServletResponse.getStatus()); mockHttpServletResponse = testRequestFile(fileEntry, _user, true); Assert.assertEquals( MockHttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus()); RoleLocalServiceUtil.addUserRoles(_user.getUserId(), new long[] {_role.getRoleId()}); mockHttpServletResponse = testRequestFile(fileEntry, _user, true); Assert.assertEquals(MockHttpServletResponse.SC_OK, mockHttpServletResponse.getStatus()); }
@Test public void testDocLibraryField() throws Exception { String definition = read("ddm-structure-doc-lib-field.xsd"); DDMStructure structure = addStructure( _CLASS_NAME_ID, null, "Documents and Media Field Structure", definition, StorageType.JSON.getValue(), DDMStructureConstants.TYPE_DEFAULT); Fields fields = new Fields(); Map<Locale, List<Serializable>> dataMap = new HashMap<>(); ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext( TestPropsValues.getGroupId(), TestPropsValues.getUserId()); FileEntry file1 = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), TestPropsValues.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "Test 1.txt", ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE), serviceContext); String file1Value = getDocLibraryFieldValue(file1); FileEntry file2 = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), TestPropsValues.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "Test 2.txt", ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE), serviceContext); String file2Value = getDocLibraryFieldValue(file2); List<Serializable> enValues = ListUtil.fromArray(new Serializable[] {file1Value, file2Value}); dataMap.put(_enLocale, enValues); List<Serializable> ptValues = ListUtil.fromArray(new Serializable[] {file1Value, file2Value}); dataMap.put(_ptLocale, ptValues); Field documentLibraryField = new Field(structure.getStructureId(), "doc_library", dataMap, _enLocale); fields.put(documentLibraryField); Field fieldsDisplayField = createFieldsDisplayField( structure.getStructureId(), "doc_library_INSTANCE_rztm,doc_library_INSTANCE_ovho"); fields.put(fieldsDisplayField); validate(structure.getStructureId(), fields); }
@Before public void setUp() throws Exception { _liveGroup = GroupTestUtil.addGroup(); _stagingGroup = GroupTestUtil.addGroup(); ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext( _stagingGroup.getGroupId(), TestPropsValues.getUserId()); _fileEntry = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), _stagingGroup.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, RandomTestUtil.randomString() + ".txt", ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE), serviceContext); ThumbnailCapability thumbnailCapability = _fileEntry.getRepositoryCapability(ThumbnailCapability.class); _fileEntry = thumbnailCapability.setLargeImageId(_fileEntry, _fileEntry.getFileEntryId()); TestReaderWriter testReaderWriter = new TestReaderWriter(); _portletDataContextExport = PortletDataContextFactoryUtil.createExportPortletDataContext( _stagingGroup.getCompanyId(), _stagingGroup.getGroupId(), new HashMap<String, String[]>(), new Date(System.currentTimeMillis() - Time.HOUR), new Date(), testReaderWriter); Document document = SAXReaderUtil.createDocument(); Element manifestRootElement = document.addElement("root"); manifestRootElement.addElement("header"); testReaderWriter.addEntry("/manifest.xml", document.asXML()); Element rootElement = SAXReaderUtil.createElement("root"); _portletDataContextExport.setExportDataRootElement(rootElement); _stagingPrivateLayout = LayoutTestUtil.addLayout(_stagingGroup, true); _stagingPublicLayout = LayoutTestUtil.addLayout(_stagingGroup, false); _portletDataContextExport.setPlid(_stagingPublicLayout.getPlid()); _portletDataContextImport = PortletDataContextFactoryUtil.createImportPortletDataContext( _liveGroup.getCompanyId(), _liveGroup.getGroupId(), new HashMap<String, String[]>(), new TestUserIdStrategy(), testReaderWriter); _portletDataContextImport.setImportDataRootElement(rootElement); Element missingReferencesElement = rootElement.addElement("missing-references"); _portletDataContextImport.setMissingReferencesElement(missingReferencesElement); _livePublicLayout = LayoutTestUtil.addLayout(_liveGroup, false); _portletDataContextImport.setPlid(_livePublicLayout.getPlid()); _portletDataContextImport.setSourceGroupId(_stagingGroup.getGroupId()); rootElement.addElement("entry"); _referrerStagedModel = JournalTestUtil.addArticle( _stagingGroup.getGroupId(), RandomTestUtil.randomString(), RandomTestUtil.randomString()); }
@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()); }