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.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            @Override
064            public long getClassPK() {
065                    return _record.getRecordId();
066            }
067    
068            @Override
069            public long getGroupId() {
070                    return _record.getGroupId();
071            }
072    
073            @Override
074            public String getSummary(Locale locale) {
075                    return StringPool.BLANK;
076            }
077    
078            @Override
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            @Override
104            public long getUserId() {
105                    return _record.getUserId();
106            }
107    
108            @Override
109            public String getUserName() {
110                    return _record.getUserName();
111            }
112    
113            @Override
114            public String getUuid() {
115                    return _record.getUuid();
116            }
117    
118            @Override
119            public boolean hasEditPermission(PermissionChecker permissionChecker) {
120                    return DDLRecordSetPermission.contains(
121                            permissionChecker, _recordSet, ActionKeys.UPDATE);
122            }
123    
124            @Override
125            public boolean hasViewPermission(PermissionChecker permissionChecker) {
126                    return DDLRecordSetPermission.contains(
127                            permissionChecker, _recordSet, ActionKeys.VIEW);
128            }
129    
130            @Override
131            public String render(
132                            RenderRequest renderRequest, RenderResponse renderResponse,
133                            String template)
134                    throws Exception {
135    
136                    if (template.equals(TEMPLATE_ABSTRACT) ||
137                            template.equals(TEMPLATE_FULL_CONTENT)) {
138    
139                            renderRequest.setAttribute(
140                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD, _record);
141                            renderRequest.setAttribute(
142                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD_VERSION, _recordVersion);
143    
144                            String path =
145                                    "/html/portlet/dynamic_data_lists/asset/full_content.jsp";
146    
147                            return path;
148                    }
149                    else {
150                            return null;
151                    }
152            }
153    
154            @Override
155            protected String getIconPath(ThemeDisplay themeDisplay) {
156                    return themeDisplay.getPathThemeImages() + "/common/history.png";
157            }
158    
159            private static Log _log = LogFactoryUtil.getLog(
160                    DDLRecordAssetRenderer.class);
161    
162            private DDLRecord _record;
163            private DDLRecordSet _recordSet;
164            private DDLRecordVersion _recordVersion;
165    
166    }