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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.repository.model.Folder;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.Image;
021    import com.liferay.portal.model.Repository;
022    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
023    import com.liferay.portal.service.ImageLocalServiceUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.webserver.WebServerServletTokenUtil;
028    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
029    
030    import java.util.Date;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Juan Fern??ndez
035     */
036    public class BlogsEntryImpl extends BlogsEntryBaseImpl {
037    
038            public BlogsEntryImpl() {
039            }
040    
041            @Override
042            public String getEntryImageURL(ThemeDisplay themeDisplay) {
043                    if (!isSmallImage()) {
044                            return null;
045                    }
046    
047                    if (Validator.isNotNull(getSmallImageURL())) {
048                            return getSmallImageURL();
049                    }
050    
051                    return
052                            themeDisplay.getPathImage() + "/blogs/entry?img_id=" +
053                                    getSmallImageId() + "&t=" +
054                                            WebServerServletTokenUtil.getToken(getSmallImageId());
055            }
056    
057            @Override
058            public long getSmallImageFolderId() {
059                    if (_smallImageFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
060                            return _smallImageFolderId;
061                    }
062    
063                    Repository repository =
064                            PortletFileRepositoryUtil.fetchPortletRepository(
065                                    getGroupId(), PortletKeys.BLOGS);
066    
067                    if (repository == null) {
068                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
069                    }
070    
071                    ServiceContext serviceContext = new ServiceContext();
072    
073                    serviceContext.setAddGroupPermissions(true);
074                    serviceContext.setAddGuestPermissions(true);
075    
076                    try {
077                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
078                                    getUserId(), repository.getRepositoryId(),
079                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
080                                    String.valueOf(getEntryId()), serviceContext);
081    
082                            _smallImageFolderId = folder.getFolderId();
083                    }
084                    catch (Exception e) {
085                    }
086    
087                    return _smallImageFolderId;
088            }
089    
090            @Override
091            public String getSmallImageType() throws PortalException {
092                    if ((_smallImageType == null) && isSmallImage()) {
093                            Image smallImage = ImageLocalServiceUtil.getImage(
094                                    getSmallImageId());
095    
096                            _smallImageType = smallImage.getType();
097                    }
098    
099                    return _smallImageType;
100            }
101    
102            @Override
103            public boolean isVisible() {
104                    Date displayDate = getDisplayDate();
105    
106                    if (isApproved() && displayDate.before(new Date())) {
107                            return true;
108                    }
109                    else {
110                            return false;
111                    }
112            }
113    
114            @Override
115            public void setSmallImageType(String smallImageType) {
116                    _smallImageType = smallImageType;
117            }
118    
119            private long _smallImageFolderId;
120            private String _smallImageType;
121    
122    }