Example usage for com.liferay.portal.kernel.repository.model FileEntry getLatestFileVersion

List of usage examples for com.liferay.portal.kernel.repository.model FileEntry getLatestFileVersion

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.repository.model FileEntry getLatestFileVersion.

Prototype

public FileVersion getLatestFileVersion(boolean trusted) throws PortalException;

Source Link

Document

Returns the latest file version, optionally bypassing security checks.

Usage

From source file:com.liferay.adaptive.media.document.library.internal.repository.BaseOverridingRepositoryDefiner.java

License:Open Source License

private void _updateAdaptiveMedia(FileEntry fileEntry) {
    try {/*from  w ww .j a  v  a2 s.  c  o m*/
        AdaptiveMediaAsyncProcessor<FileVersion, ?> asyncProcessor = _asyncProcessorLocator
                .locateForClass(FileVersion.class);

        FileVersion latestFileVersion = fileEntry.getLatestFileVersion(true);

        asyncProcessor.triggerProcess(latestFileVersion, String.valueOf(latestFileVersion.getFileVersionId()));
    } catch (PortalException pe) {
        throw new RuntimeException(pe);
    }
}

From source file:com.liferay.adaptive.media.document.library.internal.repository.registry.BaseOverridingRepositoryDefiner.java

License:Open Source License

private void _updateAdaptiveMedia(FileEntry fileEntry) {
    if (ExportImportThreadLocal.isImportInProcess()) {
        return;//from  ww  w  .j ava 2s  .  c o m
    }

    try {
        AMAsyncProcessor<FileVersion, ?> amAsyncProcessor = _amAsyncProcessorLocator
                .locateForClass(FileVersion.class);

        FileVersion latestFileVersion = fileEntry.getLatestFileVersion(true);

        amAsyncProcessor.triggerProcess(latestFileVersion,
                String.valueOf(latestFileVersion.getFileVersionId()));
    } catch (PortalException pe) {
        throw new RuntimeException(pe);
    }
}

From source file:com.liferay.adaptive.media.document.library.repository.internal.BaseOverridingRepositoryDefiner.java

License:Open Source License

private void _updateAdaptiveMedia(FileEntry fileEntry) {
    try {//w ww.j a  va  2  s  .co m
        AdaptiveMediaAsyncProcessor<FileVersion, ?> asyncProcessor = _asyncProcessorLocator
                .locateForClass(FileVersion.class);

        FileVersion latestFileVersion = fileEntry.getLatestFileVersion(true);

        asyncProcessor.triggerProcess(latestFileVersion, String.valueOf(latestFileVersion.getFileVersionId()));
    } catch (AdaptiveMediaException | PortalException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.liferay.adaptive.media.image.processor.test.AdaptiveMediaImageProcessorTest.java

License:Open Source License

@Test
public void testCleaningFileEntryWithImageRemovesMedia() throws Exception {
    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addImageFileEntry(serviceContext);

        _adaptiveMediaImageProcessor.cleanUp(fileEntry.getLatestFileVersion(true));

        Stream<AdaptiveMedia<AdaptiveMediaImageProcessor>> adaptiveMediaStream = _adaptiveMediaImageFinder
                .getAdaptiveMediaStream(queryBuilder -> queryBuilder.allForFileEntry(fileEntry).done());

        Assert.assertEquals(0, adaptiveMediaStream.count());
    }//from   w  w  w . j  av a 2  s.com
}

From source file:com.liferay.adaptive.media.image.processor.test.AdaptiveMediaImageProcessorTest.java

License:Open Source License

@Test
public void testCleaningFileEntryWithNoImageDoesNothing() throws Exception {
    try (DestinationReplacer destinationReplacer = new DestinationReplacer(
            "liferay/adaptive_media_processor")) {

        ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
                TestPropsValues.getUserId());

        final FileEntry fileEntry = _addNonImageFileEntry(serviceContext);

        _adaptiveMediaImageProcessor.cleanUp(fileEntry.getLatestFileVersion(true));

        Stream<AdaptiveMedia<AdaptiveMediaImageProcessor>> adaptiveMediaStream = _adaptiveMediaImageFinder
                .getAdaptiveMediaStream(queryBuilder -> queryBuilder.allForFileEntry(fileEntry).done());

        Assert.assertEquals(0, adaptiveMediaStream.count());
    }/*from   w  w  w.  ja  v  a  2  s. co  m*/
}

From source file:com.liferay.adaptive.media.image.processor.test.AMImageProcessorTest.java

License:Open Source License

@Test
public void testCleaningFileEntryWithImageRemovesMedia() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
            TestPropsValues.getUserId());

    final FileEntry fileEntry = _addImageFileEntry(serviceContext);

    _amImageProcessor.cleanUp(fileEntry.getLatestFileVersion(true));

    Stream<AdaptiveMedia<AMImageProcessor>> adaptiveMediaStream = _amImageFinder
            .getAdaptiveMediaStream(amImageQueryBuilder -> amImageQueryBuilder.forFileEntry(fileEntry).done());

    Assert.assertEquals(0, adaptiveMediaStream.count());
}

From source file:com.liferay.adaptive.media.image.processor.test.AMImageProcessorTest.java

License:Open Source License

@Test
public void testCleaningFileEntryWithNoImageDoesNothing() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(_group,
            TestPropsValues.getUserId());

    final FileEntry fileEntry = _addNonImageFileEntry(serviceContext);

    _amImageProcessor.cleanUp(fileEntry.getLatestFileVersion(true));

    Stream<AdaptiveMedia<AMImageProcessor>> adaptiveMediaStream = _amImageFinder
            .getAdaptiveMediaStream(amImageQueryBuilder -> amImageQueryBuilder.forFileEntry(fileEntry).done());

    Assert.assertEquals(0, adaptiveMediaStream.count());
}

From source file:com.liferay.document.library.internal.exportimport.data.handler.FileEntryStagedModelDataHandler.java

License:Open Source License

@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;//from w w w  .j a  va 2  s. co m
    }

    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;

    try {
        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;

        if (portletDataContext.isDataStrategyMirror()) {
            FileEntry existingFileEntry = fetchStagedModelByUuidAndGroupId(fileEntry.getUuid(),
                    portletDataContext.getScopeGroupId());

            FileVersion fileVersion = fileEntry.getFileVersion();

            if (existingFileEntry == null) {
                if (portletDataContext.isDataStrategyMirrorWithOverwriting()) {

                    FileEntry existingTitleFileEntry = FileEntryUtil
                            .fetchByR_F_T(portletDataContext.getScopeGroupId(), folderId, fileEntry.getTitle());

                    if (existingTitleFileEntry == null) {
                        existingTitleFileEntry = FileEntryUtil.fetchByR_F_FN(
                                portletDataContext.getScopeGroupId(), folderId, fileEntry.getFileName());
                    }

                    if (existingTitleFileEntry != null) {
                        _dlAppLocalService.deleteFileEntry(existingTitleFileEntry.getFileEntryId());
                    }
                }

                serviceContext.setAttribute("fileVersionUuid", fileVersion.getUuid());
                serviceContext.setUuid(fileEntry.getUuid());

                String fileEntryTitle = _dlFileEntryLocalService.getUniqueTitle(
                        portletDataContext.getScopeGroupId(), folderId, 0, fileEntry.getTitle(),
                        fileEntry.getExtension());

                importedFileEntry = _dlAppLocalService.addFileEntry(userId,
                        portletDataContext.getScopeGroupId(), folderId, fileEntry.getFileName(),
                        fileEntry.getMimeType(), fileEntryTitle, fileEntry.getDescription(), null, is,
                        fileEntry.getSize(), serviceContext);

                if (fileEntry.isInTrash()) {
                    importedFileEntry = _dlTrashService
                            .moveFileEntryToTrash(importedFileEntry.getFileEntryId());
                }
            } else {
                FileVersion latestExistingFileVersion = existingFileEntry.getLatestFileVersion(true);

                boolean indexEnabled = serviceContext.isIndexingEnabled();

                boolean deleteFileEntry = false;
                boolean updateFileEntry = false;

                if (!Objects.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 = _dlFileVersionLocalService
                                .getFileVersionByUuidAndGroupId(fileVersion.getUuid(),
                                        existingFileEntry.getGroupId());

                        if (alreadyExistingFileVersion != null) {
                            serviceContext.setAttribute("existingDLFileVersionId",
                                    alreadyExistingFileVersion.getFileVersionId());
                        }

                        serviceContext.setUuid(fileVersion.getUuid());

                        String fileEntryTitle = _dlFileEntryLocalService.getUniqueTitle(
                                portletDataContext.getScopeGroupId(), existingFileEntry.getFolderId(),
                                existingFileEntry.getFileEntryId(), fileEntry.getTitle(),
                                fileEntry.getExtension());

                        importedFileEntry = _dlAppLocalService.updateFileEntry(userId,
                                existingFileEntry.getFileEntryId(), fileEntry.getFileName(),
                                fileEntry.getMimeType(), fileEntryTitle, fileEntry.getDescription(), null,
                                false, is, fileEntry.getSize(), serviceContext);
                    } else {
                        _dlAppLocalService.updateAsset(userId, existingFileEntry, latestExistingFileVersion,
                                assetCategoryIds, assetTagNames, null);

                        importedFileEntry = existingFileEntry;
                    }

                    if (importedFileEntry.getFolderId() != folderId) {
                        importedFileEntry = _dlAppLocalService.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()) {

                        _dlAppService.deleteFileVersion(latestExistingFileVersion.getFileEntryId(),
                                latestExistingFileVersion.getVersion());
                    }
                } finally {
                    serviceContext.setIndexingEnabled(indexEnabled);
                }
            }
        } else {
            String fileEntryTitle = _dlFileEntryLocalService.getUniqueTitle(
                    portletDataContext.getScopeGroupId(), folderId, 0, fileEntry.getTitle(),
                    fileEntry.getExtension());

            importedFileEntry = _dlAppLocalService.addFileEntry(userId, portletDataContext.getScopeGroupId(),
                    folderId, fileEntry.getFileName(), fileEntry.getMimeType(), fileEntryTitle,
                    fileEntry.getDescription(), null, is, fileEntry.getSize(), serviceContext);
        }

        for (DLPluggableContentDataHandler dlPluggableContentDataHandler : _serviceTrackerList) {

            dlPluggableContentDataHandler.importContent(portletDataContext, fileEntryElement, fileEntry,
                    importedFileEntry);
        }

        portletDataContext.importClassedModel(fileEntry, importedFileEntry, DLFileEntry.class);

        Map<Long, Long> fileEntryIds = (Map<Long, Long>) portletDataContext
                .getNewPrimaryKeysMap(FileEntry.class);

        fileEntryIds.put(fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId());
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
            _log.error(ioe, ioe);
        }
    }
}

From source file:com.liferay.document.library.internal.util.DLProcessorRegistryImpl.java

License:Open Source License

private FileVersion _getLatestFileVersion(FileEntry fileEntry, boolean trusted) {

    try {/*from w  w  w.j a va 2  s  .  c o m*/
        return fileEntry.getLatestFileVersion(trusted);
    } catch (Exception e) {
        _log.error(e, e);

        return null;
    }
}