001    /**
002     * Copyright (c) 2000-2012 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.bookmarks.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.HtmlUtil;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.asset.model.BaseAssetRenderer;
026    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
027    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
028    
029    import java.util.Locale;
030    
031    import javax.portlet.PortletRequest;
032    import javax.portlet.PortletURL;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    /**
037     * @author Julio Camarero
038     * @author Juan Fernández
039     * @author Sergio González
040     */
041    public class BookmarksEntryAssetRenderer extends BaseAssetRenderer {
042    
043            public BookmarksEntryAssetRenderer(BookmarksEntry entry) {
044                    _entry = entry;
045            }
046    
047            public long getClassPK() {
048                    return _entry.getEntryId();
049            }
050    
051            public long getGroupId() {
052                    return _entry.getGroupId();
053            }
054    
055            public String getSummary(Locale locale) {
056                    return HtmlUtil.stripHtml(_entry.getDescription());
057            }
058    
059            public String getTitle(Locale locale) {
060                    return _entry.getName();
061            }
062    
063            @Override
064            public PortletURL getURLEdit(
065                            LiferayPortletRequest liferayPortletRequest,
066                            LiferayPortletResponse liferayPortletResponse)
067                    throws Exception {
068    
069                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
070                            getControlPanelPlid(liferayPortletRequest), PortletKeys.BOOKMARKS,
071                            PortletRequest.RENDER_PHASE);
072    
073                    portletURL.setParameter("struts_action", "/bookmarks/edit_entry");
074                    portletURL.setParameter(
075                            "folderId", String.valueOf(_entry.getFolderId()));
076                    portletURL.setParameter("entryId", String.valueOf(_entry.getEntryId()));
077    
078                    return portletURL;
079            }
080    
081            @Override
082            public String getURLViewInContext(
083                    LiferayPortletRequest liferayPortletRequest,
084                    LiferayPortletResponse liferayPortletResponse,
085                    String noSuchEntryRedirect) {
086    
087                    return getURLViewInContext(
088                            liferayPortletRequest, noSuchEntryRedirect, "/bookmarks/find_entry",
089                            "entryId", _entry.getEntryId());
090            }
091    
092            public long getUserId() {
093                    return _entry.getUserId();
094            }
095    
096            public String getUuid() {
097                    return _entry.getUuid();
098            }
099    
100            @Override
101            public boolean hasEditPermission(PermissionChecker permissionChecker) {
102                    try {
103                            return BookmarksEntryPermission.contains(
104                                    permissionChecker, _entry, ActionKeys.UPDATE);
105                    }
106                    catch (Exception e) {
107                    }
108    
109                    return false;
110            }
111    
112            @Override
113            public boolean hasViewPermission(PermissionChecker permissionChecker) {
114                    try {
115                            return BookmarksEntryPermission.contains(
116                                    permissionChecker, _entry, ActionKeys.VIEW);
117                    }
118                    catch (Exception e) {
119                    }
120    
121                    return true;
122            }
123    
124            @Override
125            public boolean isPrintable() {
126                    return true;
127            }
128    
129            public String render(
130                            RenderRequest renderRequest, RenderResponse renderResponse,
131                            String template)
132                    throws Exception {
133    
134                    if (template.equals(TEMPLATE_FULL_CONTENT)) {
135                            renderRequest.setAttribute(WebKeys.BOOKMARKS_ENTRY, _entry);
136    
137                            return "/html/portlet/bookmarks/asset/" + template + ".jsp";
138                    }
139                    else {
140                            return null;
141                    }
142            }
143    
144            @Override
145            protected String getIconPath(ThemeDisplay themeDisplay) {
146                    return themeDisplay.getPathThemeImages() + "/ratings/star_hover.png";
147            }
148    
149            private BookmarksEntry _entry;
150    
151    }