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            @Override
067            public String getClassName() {
068                    return DDLRecord.class.getName();
069            }
070    
071            @Override
072            public long getClassPK() {
073                    return _record.getRecordId();
074            }
075    
076            @Override
077            public long getGroupId() {
078                    return _record.getGroupId();
079            }
080    
081            @Override
082            public String getSummary(Locale locale) {
083                    return StringPool.BLANK;
084            }
085    
086            @Override
087            public String getTitle(Locale locale) {
088                    String ddmStructureName = _ddmStructure.getName(locale);
089    
090                    String recordSetName = _recordSet.getName(locale);
091    
092                    return LanguageUtil.format(
093                            locale, "new-x-for-list-x",
094                            new Object[] {ddmStructureName, recordSetName});
095            }
096    
097            @Override
098            public PortletURL getURLEdit(
099                            LiferayPortletRequest liferayPortletRequest,
100                            LiferayPortletResponse liferayPortletResponse)
101                    throws Exception {
102    
103                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
104                            getControlPanelPlid(liferayPortletRequest),
105                            PortletKeys.DYNAMIC_DATA_LISTS, PortletRequest.RENDER_PHASE);
106    
107                    portletURL.setParameter(
108                            "struts_action", "/dynamic_data_lists/edit_record");
109                    portletURL.setParameter(
110                            "recordId", String.valueOf(_record.getRecordId()));
111                    portletURL.setParameter("version", _recordVersion.getVersion());
112    
113                    return portletURL;
114            }
115    
116            @Override
117            public String getURLViewInContext(
118                            LiferayPortletRequest liferayPortletRequest,
119                            LiferayPortletResponse liferayPortletResponse,
120                            String noSuchEntryRedirect)
121                    throws Exception {
122    
123                    return getURLViewInContext(
124                            liferayPortletRequest, noSuchEntryRedirect,
125                            "/dynamic_data_lists/find_record", "recordId",
126                            _record.getRecordId());
127            }
128    
129            @Override
130            public long getUserId() {
131                    return _record.getUserId();
132            }
133    
134            @Override
135            public String getUserName() {
136                    return _record.getUserName();
137            }
138    
139            @Override
140            public String getUuid() {
141                    return _record.getUuid();
142            }
143    
144            @Override
145            public boolean hasEditPermission(PermissionChecker permissionChecker) {
146                    return DDLRecordSetPermission.contains(
147                            permissionChecker, _recordSet, ActionKeys.UPDATE);
148            }
149    
150            @Override
151            public boolean hasViewPermission(PermissionChecker permissionChecker) {
152                    return DDLRecordSetPermission.contains(
153                            permissionChecker, _recordSet, ActionKeys.VIEW);
154            }
155    
156            @Override
157            public String render(
158                            RenderRequest renderRequest, RenderResponse renderResponse,
159                            String template)
160                    throws Exception {
161    
162                    if (template.equals(TEMPLATE_ABSTRACT) ||
163                            template.equals(TEMPLATE_FULL_CONTENT)) {
164    
165                            renderRequest.setAttribute(
166                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD, _record);
167                            renderRequest.setAttribute(
168                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD_VERSION, _recordVersion);
169    
170                            String path =
171                                    "/html/portlet/dynamic_data_lists/asset/full_content.jsp";
172    
173                            return path;
174                    }
175                    else {
176                            return null;
177                    }
178            }
179    
180            @Override
181            protected String getIconPath(ThemeDisplay themeDisplay) {
182                    return themeDisplay.getPathThemeImages() + "/common/history.png";
183            }
184    
185            private static Log _log = LogFactoryUtil.getLog(
186                    DDLRecordAssetRenderer.class);
187    
188            private DDMStructure _ddmStructure;
189            private DDLRecord _record;
190            private DDLRecordSet _recordSet;
191            private DDLRecordVersion _recordVersion;
192    
193    }