001    /**
002     * Copyright (c) 2000-2013 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.calendar.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.PropsValues;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.BaseAssetRenderer;
027    import com.liferay.portlet.calendar.model.CalEvent;
028    import com.liferay.portlet.calendar.service.permission.CalEventPermission;
029    
030    import java.util.Locale;
031    
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    import javax.portlet.WindowState;
037    
038    /**
039     * @author Juan Fern??ndez
040     * @author Sergio Gonz??lez
041     */
042    public class CalEventAssetRenderer extends BaseAssetRenderer {
043    
044            public CalEventAssetRenderer(CalEvent event) {
045                    _event = event;
046            }
047    
048            @Override
049            public long getClassPK() {
050                    return _event.getEventId();
051            }
052    
053            @Override
054            public String getDiscussionPath() {
055                    if (PropsValues.CALENDAR_EVENT_COMMENTS_ENABLED) {
056                            return "edit_event_discussion";
057                    }
058                    else {
059                            return null;
060                    }
061            }
062    
063            @Override
064            public long getGroupId() {
065                    return _event.getGroupId();
066            }
067    
068            @Override
069            public String getSummary(Locale locale) {
070                    return HtmlUtil.extractText(_event.getDescription());
071            }
072    
073            @Override
074            public String getTitle(Locale locale) {
075                    return _event.getTitle();
076            }
077    
078            @Override
079            public PortletURL getURLEdit(
080                            LiferayPortletRequest liferayPortletRequest,
081                            LiferayPortletResponse liferayPortletResponse)
082                    throws Exception {
083    
084                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
085                            getControlPanelPlid(liferayPortletRequest), PortletKeys.CALENDAR,
086                            PortletRequest.RENDER_PHASE);
087    
088                    portletURL.setParameter("struts_action", "/calendar/edit_event");
089                    portletURL.setParameter("eventId", String.valueOf(_event.getEventId()));
090    
091                    return portletURL;
092            }
093    
094            @Override
095            public PortletURL getURLView(
096                            LiferayPortletResponse liferayPortletResponse,
097                            WindowState windowState)
098                    throws Exception {
099    
100                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
101                            PortletKeys.CALENDAR, PortletRequest.RENDER_PHASE);
102    
103                    portletURL.setParameter("struts_action", "/calendar/view_event");
104                    portletURL.setParameter("eventId", String.valueOf(_event.getEventId()));
105                    portletURL.setWindowState(windowState);
106    
107                    return portletURL;
108            }
109    
110            @Override
111            public String getURLViewInContext(
112                    LiferayPortletRequest liferayPortletRequest,
113                    LiferayPortletResponse liferayPortletResponse,
114                    String noSuchEntryRedirect) {
115    
116                    return getURLViewInContext(
117                            liferayPortletRequest, noSuchEntryRedirect, "/calendar/find_event",
118                            "eventId", _event.getEventId());
119            }
120    
121            @Override
122            public long getUserId() {
123                    return _event.getUserId();
124            }
125    
126            @Override
127            public String getUserName() {
128                    return _event.getUserName();
129            }
130    
131            @Override
132            public String getUuid() {
133                    return _event.getUuid();
134            }
135    
136            @Override
137            public boolean hasEditPermission(PermissionChecker permissionChecker) {
138                    return CalEventPermission.contains(
139                            permissionChecker, _event, ActionKeys.UPDATE);
140            }
141    
142            @Override
143            public boolean hasViewPermission(PermissionChecker permissionChecker) {
144                    return CalEventPermission.contains(
145                            permissionChecker, _event, ActionKeys.VIEW);
146            }
147    
148            @Override
149            public boolean isPrintable() {
150                    return true;
151            }
152    
153            @Override
154            public String render(
155                            RenderRequest renderRequest, RenderResponse renderResponse,
156                            String template)
157                    throws Exception {
158    
159                    if (template.equals(TEMPLATE_ABSTRACT) ||
160                            template.equals(TEMPLATE_FULL_CONTENT)) {
161    
162                            renderRequest.setAttribute(WebKeys.CALENDAR_EVENT, _event);
163    
164                            return "/html/portlet/calendar/asset/" + template + ".jsp";
165                    }
166                    else {
167                            return null;
168                    }
169            }
170    
171            @Override
172            protected String getIconPath(ThemeDisplay themeDisplay) {
173                    return themeDisplay.getPathThemeImages() + "/common/date.png";
174            }
175    
176            private CalEvent _event;
177    
178    }