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 long getClassPK() {
064                    return _record.getRecordId();
065            }
066    
067            public long getGroupId() {
068                    return _record.getGroupId();
069            }
070    
071            public String getSummary(Locale locale) {
072                    return StringPool.BLANK;
073            }
074    
075            public String getTitle(Locale locale) {
076                    String name = _recordSet.getName(locale);
077    
078                    return LanguageUtil.format(locale, "new-record-for-list-x", name);
079            }
080    
081            @Override
082            public PortletURL getURLEdit(
083                            LiferayPortletRequest liferayPortletRequest,
084                            LiferayPortletResponse liferayPortletResponse)
085                    throws Exception {
086    
087                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
088                            getControlPanelPlid(liferayPortletRequest),
089                            PortletKeys.DYNAMIC_DATA_LISTS, PortletRequest.RENDER_PHASE);
090    
091                    portletURL.setParameter(
092                            "struts_action", "/dynamic_data_lists/edit_record");
093                    portletURL.setParameter(
094                            "recordId", String.valueOf(_record.getRecordId()));
095    
096                    return portletURL;
097            }
098    
099            public long getUserId() {
100                    return _record.getUserId();
101            }
102    
103            public String getUuid() {
104                    return _record.getUuid();
105            }
106    
107            @Override
108            public boolean hasEditPermission(PermissionChecker permissionChecker) {
109                    return DDLRecordSetPermission.contains(
110                            permissionChecker, _recordSet, ActionKeys.UPDATE);
111            }
112    
113            @Override
114            public boolean hasViewPermission(PermissionChecker permissionChecker) {
115                    return DDLRecordSetPermission.contains(
116                            permissionChecker, _recordSet, ActionKeys.VIEW);
117            }
118    
119            public String render(
120                            RenderRequest renderRequest, RenderResponse renderResponse,
121                            String template)
122                    throws Exception {
123    
124                    if (template.equals(TEMPLATE_ABSTRACT) ||
125                            template.equals(TEMPLATE_FULL_CONTENT)) {
126    
127                            renderRequest.setAttribute(
128                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD, _record);
129                            renderRequest.setAttribute(
130                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD_VERSION, _recordVersion);
131    
132                            String path =
133                                    "/html/portlet/dynamic_data_lists/asset/full_content.jsp";
134    
135                            return path;
136                    }
137                    else {
138                            return null;
139                    }
140            }
141    
142            @Override
143            protected String getIconPath(ThemeDisplay themeDisplay) {
144                    return themeDisplay.getPathThemeImages() + "/common/history.png";
145            }
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    DDLRecordAssetRenderer.class);
149    
150            private DDLRecord _record;
151            private DDLRecordSet _recordSet;
152            private DDLRecordVersion _recordVersion;
153    
154    }