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