001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.capabilities.TemporaryFileEntriesCapability;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.servlet.taglib.ui.ImageSelector;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
023    
024    /**
025     * @author Roberto D??az
026     */
027    public class BlogsEntryImageSelectorHelper {
028    
029            public BlogsEntryImageSelectorHelper(
030                    long imageFileEntryId, long oldImageFileEntryId, String imageCropRegion,
031                    String imageURL, String oldImageURL) {
032    
033                    _imageFileEntryId = imageFileEntryId;
034                    _oldImageFileEntryId = oldImageFileEntryId;
035                    _imageCropRegion = imageCropRegion;
036                    _imageURL = imageURL;
037                    _oldImageURL = oldImageURL;
038            }
039    
040            public ImageSelector getImageSelector() throws Exception {
041                    if (_imageFileEntryId != _oldImageFileEntryId) {
042                            if (_imageFileEntryId != 0) {
043                                    FileEntry fileEntry =
044                                            PortletFileRepositoryUtil.getPortletFileEntry(
045                                                    _imageFileEntryId);
046    
047                                    _fileEntryTempFile = fileEntry.isRepositoryCapabilityProvided(
048                                            TemporaryFileEntriesCapability.class);
049    
050                                    return new ImageSelector(
051                                            FileUtil.getBytes(fileEntry.getContentStream()),
052                                            fileEntry.getFileName(), fileEntry.getMimeType(),
053                                            _imageCropRegion);
054                            }
055                            else {
056                                    return new ImageSelector();
057                            }
058                    }
059                    else if (!_imageURL.equals(_oldImageURL)) {
060                            return new ImageSelector(_imageURL);
061                    }
062    
063                    return null;
064            }
065    
066            public boolean isFileEntryTempFile() {
067                    if (_fileEntryTempFile == null) {
068                            if ((_imageFileEntryId == 0) ||
069                                    (_imageFileEntryId == _oldImageFileEntryId)) {
070    
071                                    _fileEntryTempFile = false;
072                            }
073                            else {
074                                    try {
075                                            FileEntry fileEntry =
076                                                    PortletFileRepositoryUtil.getPortletFileEntry(
077                                                            _imageFileEntryId);
078    
079                                            _fileEntryTempFile =
080                                                    fileEntry.isRepositoryCapabilityProvided(
081                                                            TemporaryFileEntriesCapability.class);
082                                    }
083                                    catch (PortalException pe) {
084                                            return false;
085                                    }
086                            }
087                    }
088    
089                    return _fileEntryTempFile;
090            }
091    
092            private Boolean _fileEntryTempFile;
093            private final String _imageCropRegion;
094            private final long _imageFileEntryId;
095            private final String _imageURL;
096            private final long _oldImageFileEntryId;
097            private final String _oldImageURL;
098    
099    }