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.dynamicdatalists.asset;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.asset.model.BaseAssetRenderer;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
030    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
031    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
032    import com.liferay.portlet.dynamicdatalists.service.permission.DDLRecordSetPermission;
033    
034    import java.util.Locale;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    /**
042     * @author Marcellus Tavares
043     * @author Sergio González
044     */
045    public class DDLRecordAssetRenderer extends BaseAssetRenderer {
046    
047            public DDLRecordAssetRenderer(
048                    DDLRecord record, DDLRecordVersion recordVersion) {
049    
050                    _record = record;
051                    _recordVersion = recordVersion;
052    
053                    try {
054                            _recordSet = record.getRecordSet();
055                    }
056                    catch (Exception e) {
057                            if (_log.isWarnEnabled()) {
058                                    _log.warn(e, e);
059                            }
060                    }
061            }
062    
063            public String getAssetRendererFactoryClassName() {
064                    return DDLRecordAssetRendererFactory.CLASS_NAME;
065            }
066    
067            public long getClassPK() {
068                    return _record.getRecordId();
069            }
070    
071            public long getGroupId() {
072                    return _record.getGroupId();
073            }
074    
075            public String getSummary(Locale locale) {
076                    return StringPool.BLANK;
077            }
078    
079            public String getTitle(Locale locale) {
080                    String name = _recordSet.getName(locale);
081    
082                    return LanguageUtil.format(locale, "new-record-for-list-x", name);
083            }
084    
085            @Override
086            public PortletURL getURLEdit(
087                            LiferayPortletRequest liferayPortletRequest,
088                            LiferayPortletResponse liferayPortletResponse)
089                    throws Exception {
090    
091                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
092                            getControlPanelPlid(liferayPortletRequest),
093                            PortletKeys.DYNAMIC_DATA_LISTS, PortletRequest.RENDER_PHASE);
094    
095                    portletURL.setParameter(
096                            "struts_action", "/dynamic_data_lists/edit_record");
097                    portletURL.setParameter(
098                            "recordId", String.valueOf(_record.getRecordId()));
099    
100                    return portletURL;
101            }
102    
103            public long getUserId() {
104                    return _record.getUserId();
105            }
106    
107            public String getUserName() {
108                    return _record.getUserName();
109            }
110    
111            public String getUuid() {
112                    return _record.getUuid();
113            }
114    
115            @Override
116            public boolean hasEditPermission(PermissionChecker permissionChecker) {
117                    return DDLRecordSetPermission.contains(
118                            permissionChecker, _recordSet, ActionKeys.UPDATE);
119            }
120    
121            @Override
122            public boolean hasViewPermission(PermissionChecker permissionChecker) {
123                    return DDLRecordSetPermission.contains(
124                            permissionChecker, _recordSet, ActionKeys.VIEW);
125            }
126    
127            public String render(
128                            RenderRequest renderRequest, RenderResponse renderResponse,
129                            String template)
130                    throws Exception {
131    
132                    if (template.equals(TEMPLATE_ABSTRACT) ||
133                            template.equals(TEMPLATE_FULL_CONTENT)) {
134    
135                            renderRequest.setAttribute(
136                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD, _record);
137                            renderRequest.setAttribute(
138                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD_VERSION, _recordVersion);
139    
140                            String path =
141                                    "/html/portlet/dynamic_data_lists/asset/full_content.jsp";
142    
143                            return path;
144                    }
145                    else {
146                            return null;
147                    }
148            }
149    
150            @Override
151            protected String getIconPath(ThemeDisplay themeDisplay) {
152                    return themeDisplay.getPathThemeImages() + "/common/history.png";
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(
156                    DDLRecordAssetRenderer.class);
157    
158            private DDLRecord _record;
159            private DDLRecordSet _recordSet;
160            private DDLRecordVersion _recordVersion;
161    
162    }