001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.journal.asset;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.service.GroupLocalServiceUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.asset.model.BaseAssetRenderer;
030    import com.liferay.portlet.journal.model.JournalArticle;
031    import com.liferay.portlet.journal.model.JournalArticleConstants;
032    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
033    
034    import java.util.Date;
035    import java.util.Locale;
036    
037    import javax.portlet.PortletRequest;
038    import javax.portlet.PortletURL;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    /**
043     * @author Julio Camarero
044     * @author Juan Fernández
045     * @author Sergio González
046     */
047    public class JournalArticleAssetRenderer extends BaseAssetRenderer {
048    
049            public JournalArticleAssetRenderer(JournalArticle article) {
050                    _article = article;
051            }
052    
053            public JournalArticle getArticle() {
054                    return _article;
055            }
056    
057            @Override
058            public String[] getAvailableLocales() {
059                    return _article.getAvailableLocales();
060            }
061    
062            public long getClassPK() {
063                    if ((_article.isDraft() || _article.isPending()) &&
064                            (_article.getVersion() !=
065                                    JournalArticleConstants.VERSION_DEFAULT)) {
066    
067                            return _article.getPrimaryKey();
068                    }
069                    else {
070                            return _article.getResourcePrimKey();
071                    }
072            }
073    
074            @Override
075            public String getDiscussionPath() {
076                    if (PropsValues.JOURNAL_ARTICLE_COMMENTS_ENABLED) {
077                            return "edit_article_discussion";
078                    }
079                    else {
080                            return null;
081                    }
082            }
083    
084            public long getGroupId() {
085                    return _article.getGroupId();
086            }
087    
088            public String getSummary(Locale locale) {
089                    return _article.getDescription(locale);
090            }
091    
092            public String getTitle(Locale locale) {
093                    return _article.getTitle(locale);
094            }
095    
096            @Override
097            public PortletURL getURLEdit(
098                            LiferayPortletRequest liferayPortletRequest,
099                            LiferayPortletResponse liferayPortletResponse)
100                    throws Exception {
101    
102                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
103                            getControlPanelPlid(liferayPortletRequest), PortletKeys.JOURNAL,
104                            PortletRequest.RENDER_PHASE);
105    
106                    portletURL.setParameter("struts_action", "/journal/edit_article");
107                    portletURL.setParameter(
108                            "groupId", String.valueOf(_article.getGroupId()));
109                    portletURL.setParameter("articleId", _article.getArticleId());
110                    portletURL.setParameter(
111                            "version", String.valueOf(_article.getVersion()));
112    
113                    return portletURL;
114            }
115    
116            @Override
117            public PortletURL getURLExport(
118                    LiferayPortletRequest liferayPortletRequest,
119                    LiferayPortletResponse liferayPortletResponse) {
120    
121                    PortletURL portletURL = liferayPortletResponse.createActionURL();
122    
123                    portletURL.setParameter(
124                            "struts_action", "/asset_publisher/export_journal_article");
125                    portletURL.setParameter(
126                            "groupId", String.valueOf(_article.getGroupId()));
127                    portletURL.setParameter("articleId", _article.getArticleId());
128    
129                    return portletURL;
130            }
131    
132            @Override
133            public String getUrlTitle() {
134                    return _article.getUrlTitle();
135            }
136    
137            @Override
138            public String getURLViewInContext(
139                            LiferayPortletRequest liferayPortletRequest,
140                            LiferayPortletResponse liferayPortletResponse,
141                            String noSuchEntryRedirect)
142                    throws Exception {
143    
144                    if (Validator.isNull(_article.getLayoutUuid())) {
145                            return null;
146                    }
147    
148                    ThemeDisplay themeDisplay =
149                            (ThemeDisplay)liferayPortletRequest.getAttribute(
150                                    WebKeys.THEME_DISPLAY);
151    
152                    Group group = themeDisplay.getScopeGroup();
153    
154                    if (group.getGroupId() != _article.getGroupId()) {
155                            group = GroupLocalServiceUtil.getGroup(_article.getGroupId());
156                    }
157    
158                    String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
159                            group, false, themeDisplay);
160    
161                    return groupFriendlyURL.concat(
162                            JournalArticleConstants.CANONICAL_URL_SEPARATOR).concat(
163                                    _article.getUrlTitle());
164            }
165    
166            public long getUserId() {
167                    return _article.getUserId();
168            }
169    
170            public String getUuid() {
171                    return _article.getUuid();
172            }
173    
174            @Override
175            public String getViewInContextMessage() {
176                    return "view";
177            }
178    
179            @Override
180            public boolean hasEditPermission(PermissionChecker permissionChecker) {
181                    return JournalArticlePermission.contains(
182                            permissionChecker,_article, ActionKeys.UPDATE);
183            }
184    
185            @Override
186            public boolean hasViewPermission(PermissionChecker permissionChecker) {
187                    return JournalArticlePermission.contains(
188                            permissionChecker,_article, ActionKeys.VIEW);
189            }
190    
191            @Override
192            public boolean isConvertible() {
193                    return true;
194            }
195    
196            @Override
197            public boolean isDisplayable() {
198                    Date now = new Date();
199    
200                    Date displayDate = _article.getDisplayDate();
201    
202                    if ((displayDate != null) && displayDate.after(now)) {
203                            return false;
204                    }
205    
206                    Date expirationDate = _article.getExpirationDate();
207    
208                    if ((expirationDate != null) && expirationDate.before(now)) {
209                            return false;
210                    }
211    
212                    return true;
213            }
214    
215            @Override
216            public boolean isLocalizable() {
217                    return true;
218            }
219    
220            @Override
221            public boolean isPrintable() {
222                    return true;
223            }
224    
225            public String render(
226                            RenderRequest renderRequest, RenderResponse renderResponse,
227                            String template)
228                    throws Exception {
229    
230                    if (template.equals(TEMPLATE_ABSTRACT) ||
231                            template.equals(TEMPLATE_FULL_CONTENT)) {
232    
233                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, _article);
234    
235                            return "/html/portlet/journal/asset/" + template + ".jsp";
236                    }
237                    else {
238                            return null;
239                    }
240            }
241    
242            @Override
243            protected String getIconPath(ThemeDisplay themeDisplay) {
244                    return themeDisplay.getPathThemeImages() + "/common/history.png";
245            }
246    
247            private JournalArticle _article;
248    
249    }