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

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

Introduction

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

Prototype

public String getDescription();

Source Link

Usage

From source file:com.liferay.document.library.internal.atom.FileEntryAtomCollectionAdapter.java

License:Open Source License

@Override
public String getEntrySummary(FileEntry fileEntry) {
    return fileEntry.getDescription();
}

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 ww.  jav a  2  s . c  o 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.exportimport.data.handler.test.FileEntryStagedModelDataHandlerTest.java

License:Open Source License

@Override
protected void validateImportedStagedModel(StagedModel stagedModel, StagedModel importedStagedModel)
        throws Exception {

    Assert.assertTrue(/*from ww  w.j a v  a2  s. c  o  m*/
            String.valueOf(stagedModel.getCreateDate()) + StringPool.SPACE
                    + importedStagedModel.getCreateDate(),
            DateUtil.equals(stagedModel.getCreateDate(), importedStagedModel.getCreateDate()));

    Assert.assertEquals(stagedModel.getUuid(), importedStagedModel.getUuid());

    FileEntry fileEntry = (FileEntry) stagedModel;
    FileEntry importedFileEntry = (FileEntry) importedStagedModel;

    Assert.assertEquals(fileEntry.getFileName(), importedFileEntry.getFileName());
    Assert.assertEquals(fileEntry.getExtension(), importedFileEntry.getExtension());
    Assert.assertEquals(fileEntry.getMimeType(), importedFileEntry.getMimeType());
    Assert.assertEquals(fileEntry.getTitle(), importedFileEntry.getTitle());
    Assert.assertEquals(fileEntry.getDescription(), importedFileEntry.getDescription());
    Assert.assertEquals(fileEntry.getSize(), importedFileEntry.getSize());

    FileVersion latestFileVersion = fileEntry.getLatestFileVersion();
    FileVersion importedLatestFileVersion = importedFileEntry.getLatestFileVersion();

    Assert.assertEquals(latestFileVersion.getUuid(), importedLatestFileVersion.getUuid());
    Assert.assertEquals(latestFileVersion.getFileName(), importedLatestFileVersion.getFileName());
    Assert.assertEquals(latestFileVersion.getExtension(), importedLatestFileVersion.getExtension());
    Assert.assertEquals(latestFileVersion.getMimeType(), importedLatestFileVersion.getMimeType());
    Assert.assertEquals(latestFileVersion.getTitle(), importedLatestFileVersion.getTitle());
    Assert.assertEquals(latestFileVersion.getDescription(), importedLatestFileVersion.getDescription());
    Assert.assertEquals(latestFileVersion.getSize(), importedLatestFileVersion.getSize());
    Assert.assertEquals(latestFileVersion.getStatus(), importedLatestFileVersion.getStatus());
}

From source file:com.liferay.document.library.jaxrs.FileRepr.java

License:Open Source License

public static FileRepr fromFileEntry(FileEntry fileEntry, UriBuilder uriBuilder) {

    return new FileRepr(fileEntry.getFileEntryId(), fileEntry.getDescription(), fileEntry.getFileName(),
            fileEntry.getTitle(), fileEntry.getSize(), uriBuilder.build(fileEntry.getFileEntryId()).toString(),
            uriBuilder.path("content").build(fileEntry.getUuid()).toString());
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int copySimpleResource(WebDAVRequest webDAVRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

    File file = null;/*from  w w  w.  j  a v a  2  s  . c  o  m*/

    try {
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        long companyId = webDAVRequest.getCompanyId();

        long parentFolderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

        try {
            parentFolderId = getParentFolderId(companyId, destinationArray);
        } catch (NoSuchFolderException nsfe) {
            if (_log.isDebugEnabled()) {
                _log.debug(nsfe, nsfe);
            }

            return HttpServletResponse.SC_CONFLICT;
        }

        FileEntry fileEntry = (FileEntry) resource.getModel();

        long groupId = WebDAVUtil.getGroupId(companyId, destination);
        String mimeType = fileEntry.getMimeType();
        String title = getTitle(destinationArray);
        String description = fileEntry.getDescription();
        String changeLog = StringPool.BLANK;

        InputStream is = fileEntry.getContentStream();

        file = FileUtil.createTempFile(is);

        ServiceContext serviceContext = new ServiceContext();

        serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
        serviceContext.setAddGuestPermissions(true);

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, parentFolderId, title, webDAVRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        _dlAppService.addFileEntry(groupId, parentFolderId, title, mimeType, title, description, changeLog,
                file, serviceContext);

        return status;
    } catch (DuplicateFileEntryException dfee) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfee, dfee);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFolderNameException dfne) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfne, dfne);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (LockException le) {
        if (_log.isDebugEnabled()) {
            _log.debug(le, le);
        }

        return WebDAVUtil.SC_LOCKED;
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug(pe, pe);
        }

        return HttpServletResponse.SC_FORBIDDEN;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int moveSimpleResource(WebDAVRequest webDAVRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

    File file = null;/*from   ww w.j a va2  s . c om*/

    try {
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        FileEntry fileEntry = (FileEntry) resource.getModel();

        if (!hasLock(fileEntry, webDAVRequest.getLockUuid()) && (fileEntry.getLock() != null)) {

            return WebDAVUtil.SC_LOCKED;
        }

        long companyId = webDAVRequest.getCompanyId();

        long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
        long newParentFolderId = getParentFolderId(companyId, destinationArray);

        String title = getTitle(destinationArray);
        String description = fileEntry.getDescription();
        String changeLog = StringPool.BLANK;

        ServiceContext serviceContext = new ServiceContext();

        populateServiceContext(serviceContext, fileEntry);

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, newParentFolderId, title, webDAVRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        // LPS-5415

        if (webDAVRequest.isMac()) {
            try {
                FileEntry destFileEntry = _dlAppService.getFileEntry(groupId, newParentFolderId, title);

                InputStream is = fileEntry.getContentStream();

                file = FileUtil.createTempFile(is);

                populateServiceContext(serviceContext, destFileEntry);

                _dlAppService.updateFileEntry(destFileEntry.getFileEntryId(), destFileEntry.getTitle(),
                        destFileEntry.getMimeType(), destFileEntry.getTitle(), destFileEntry.getDescription(),
                        changeLog, false, file, serviceContext);

                _dlAppService.deleteFileEntry(fileEntry.getFileEntryId());

                return status;
            } catch (NoSuchFileEntryException nsfee) {
                if (_log.isDebugEnabled()) {
                    _log.debug(nsfee, nsfee);
                }
            }
        }

        populateServiceContext(serviceContext, fileEntry);

        _dlAppService.updateFileEntry(fileEntry.getFileEntryId(), title, fileEntry.getMimeType(), title,
                description, changeLog, false, file, serviceContext);

        if (fileEntry.getFolderId() != newParentFolderId) {
            fileEntry = _dlAppService.moveFileEntry(fileEntry.getFileEntryId(), newParentFolderId,
                    serviceContext);
        }

        return status;
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug(pe, pe);
        }

        return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFileEntryException dfee) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfee, dfee);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFolderNameException dfne) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfne, dfne);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (LockException le) {
        if (_log.isDebugEnabled()) {
            _log.debug(le, le);
        }

        return WebDAVUtil.SC_LOCKED;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.document.library.web.internal.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int putResource(WebDAVRequest webDAVRequest) throws WebDAVException {
    File file = null;// ww w . j  ava2s .  c om

    try {
        HttpServletRequest request = webDAVRequest.getHttpServletRequest();

        String[] pathArray = webDAVRequest.getPathArray();

        long companyId = webDAVRequest.getCompanyId();
        long groupId = webDAVRequest.getGroupId();
        long parentFolderId = getParentFolderId(companyId, pathArray);
        String title = getTitle(pathArray);
        String description = StringPool.BLANK;
        String changeLog = StringPool.BLANK;

        ServiceContext serviceContext = ServiceContextFactory.getInstance(request);

        serviceContext.setAddGroupPermissions(isAddGroupPermissions(groupId));
        serviceContext.setAddGuestPermissions(true);

        String extension = FileUtil.getExtension(title);

        file = FileUtil.createTempFile(extension);

        FileUtil.write(file, request.getInputStream());

        String contentType = getContentType(request, file, title, extension);

        try {
            FileEntry fileEntry = _dlAppService.getFileEntry(groupId, parentFolderId, title);

            if (!hasLock(fileEntry, webDAVRequest.getLockUuid()) && (fileEntry.getLock() != null)) {

                return WebDAVUtil.SC_LOCKED;
            }

            long fileEntryId = fileEntry.getFileEntryId();

            description = fileEntry.getDescription();

            populateServiceContext(serviceContext, fileEntry);

            serviceContext.setCommand(Constants.UPDATE_WEBDAV);

            _dlAppService.updateFileEntry(fileEntryId, title, contentType, title, description, changeLog, false,
                    file, serviceContext);
        } catch (NoSuchFileEntryException nsfee) {
            if (_log.isDebugEnabled()) {
                _log.debug(nsfee, nsfee);
            }

            serviceContext.setCommand(Constants.ADD_WEBDAV);

            _dlAppService.addFileEntry(groupId, parentFolderId, title, contentType, title, description,
                    changeLog, file, serviceContext);
        }

        if (_log.isInfoEnabled()) {
            _log.info("Added " + StringUtil.merge(pathArray, StringPool.SLASH));
        }

        return HttpServletResponse.SC_CREATED;
    } catch (FileSizeException fse) {
        if (_log.isDebugEnabled()) {
            _log.debug(fse, fse);
        }

        return HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE;
    } catch (NoSuchFolderException nsfe) {
        if (_log.isDebugEnabled()) {
            _log.debug(nsfe, nsfe);
        }

        return HttpServletResponse.SC_CONFLICT;
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug(pe, pe);
        }

        return HttpServletResponse.SC_FORBIDDEN;
    } catch (PortalException pe) {
        if (_log.isWarnEnabled()) {
            _log.warn(pe, pe);
        }

        return HttpServletResponse.SC_CONFLICT;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.document.library.web.webdav.DLWebDAVStorageImpl.java

License:Open Source License

@Override
public int moveSimpleResource(WebDAVRequest webDAVRequest, Resource resource, String destination,
        boolean overwrite) throws WebDAVException {

    File file = null;//w w  w.ja va  2 s. c o  m

    try {
        String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

        FileEntry fileEntry = (FileEntry) resource.getModel();

        if (!hasLock(fileEntry, webDAVRequest.getLockUuid()) && (fileEntry.getLock() != null)) {

            return WebDAVUtil.SC_LOCKED;
        }

        long companyId = webDAVRequest.getCompanyId();

        long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
        long newParentFolderId = getParentFolderId(companyId, destinationArray);

        String title = getTitle(destinationArray);
        String description = fileEntry.getDescription();
        String changeLog = StringPool.BLANK;

        ServiceContext serviceContext = new ServiceContext();

        populateServiceContext(serviceContext, fileEntry);

        int status = HttpServletResponse.SC_CREATED;

        if (overwrite) {
            if (deleteResource(groupId, newParentFolderId, title, webDAVRequest.getLockUuid())) {

                status = HttpServletResponse.SC_NO_CONTENT;
            }
        }

        // LPS-5415

        if (webDAVRequest.isMac()) {
            try {
                FileEntry destFileEntry = _dlAppService.getFileEntry(groupId, newParentFolderId, title);

                InputStream is = fileEntry.getContentStream();

                file = FileUtil.createTempFile(is);

                _dlAppService.updateFileEntry(destFileEntry.getFileEntryId(), destFileEntry.getTitle(),
                        destFileEntry.getMimeType(), destFileEntry.getTitle(), destFileEntry.getDescription(),
                        changeLog, false, file, serviceContext);

                _dlAppService.deleteFileEntry(fileEntry.getFileEntryId());

                return status;
            } catch (NoSuchFileEntryException nsfee) {
                if (_log.isDebugEnabled()) {
                    _log.debug(nsfee, nsfee);
                }
            }
        }

        _dlAppService.updateFileEntry(fileEntry.getFileEntryId(), title, fileEntry.getMimeType(), title,
                description, changeLog, false, file, serviceContext);

        if (fileEntry.getFolderId() != newParentFolderId) {
            fileEntry = _dlAppService.moveFileEntry(fileEntry.getFileEntryId(), newParentFolderId,
                    serviceContext);
        }

        return status;
    } catch (PrincipalException pe) {
        if (_log.isDebugEnabled()) {
            _log.debug(pe, pe);
        }

        return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFileEntryException dfee) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfee, dfee);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (DuplicateFolderNameException dfne) {
        if (_log.isDebugEnabled()) {
            _log.debug(dfne, dfne);
        }

        return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (LockException le) {
        if (_log.isDebugEnabled()) {
            _log.debug(le, le);
        }

        return WebDAVUtil.SC_LOCKED;
    } catch (Exception e) {
        throw new WebDAVException(e);
    } finally {
        FileUtil.delete(file);
    }
}

From source file:com.liferay.image.editor.hook.action.EditFileEntryAction.java

License:Open Source License

protected void updateImage(ActionRequest actionRequest) throws Exception {
    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");

    if (fileEntryId == 0) {
        throw new NoSuchFileEntryException();
    }//from   w  w w. j ava2  s .  co  m

    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);

    String blob = ParamUtil.getString(actionRequest, "blob");

    if (Validator.isNull(blob)) {
        throw new NoSuchImageException();
    }

    String mimeType = fileEntry.getMimeType();

    FileVersion latestFileVersion = fileEntry.getLatestFileVersion();

    String extension = latestFileVersion.getExtension();

    File imageFile = ActionUtil.getImageFromBlob(blob, extension);

    FileEntry tempFileEntry = TempFileUtil.addTempFile(fileEntry.getGroupId(), fileEntry.getUserId(),
            fileEntry.getTitle() + fileEntry.getVersion(), _TEMP_FOLDER_NAME, imageFile, mimeType);

    try {
        ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);

        String sourceFileName = fileEntry.getTitle();

        if (!sourceFileName.endsWith(extension)) {
            sourceFileName.concat(extension);
        }

        fileEntry = DLAppServiceUtil.updateFileEntry(fileEntryId, sourceFileName, mimeType,
                fileEntry.getTitle(), fileEntry.getDescription(), ActionUtil.getChangeLog(actionRequest), false,
                imageFile, serviceContext);

        AssetPublisherUtil.addAndStoreSelection(actionRequest, DLFileEntry.class.getName(),
                fileEntry.getFileEntryId(), -1);

        AssetPublisherUtil.addRecentFolderId(actionRequest, DLFileEntry.class.getName(),
                fileEntry.getFolderId());

        return;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (tempFileEntry != null) {
            TempFileUtil.deleteTempFile(tempFileEntry.getFileEntryId());
        }
    }
}

From source file:com.liferay.opensocial.editor.portlet.EditorPortlet.java

License:Open Source License

protected void serveUpdateFileEntryContent(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
        throws Exception {

    long fileEntryId = ParamUtil.getLong(resourceRequest, "fileEntryId");

    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);

    String content = ParamUtil.getString(resourceRequest, "content");

    byte[] bytes = content.getBytes(StringPool.UTF8);

    ServiceContext serviceContext = ServiceContextFactory.getInstance(resourceRequest);

    DLAppServiceUtil.updateFileEntry(fileEntryId, fileEntry.getTitle(), resourceRequest.getContentType(),
            fileEntry.getTitle(), fileEntry.getDescription(), StringPool.BLANK, false, bytes, serviceContext);

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    writeJSON(resourceRequest, resourceResponse, jsonObject);
}