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    
112                    return portletURL;
113            }
114    
115            @Override
116            public long getUserId() {
117                    return _record.getUserId();
118            }
119    
120            @Override
121            public String getUserName() {
122                    return _record.getUserName();
123            }
124    
125            @Override
126            public String getUuid() {
127                    return _record.getUuid();
128            }
129    
130            @Override
131            public boolean hasEditPermission(PermissionChecker permissionChecker) {
132                    return DDLRecordSetPermission.contains(
133                            permissionChecker, _recordSet, ActionKeys.UPDATE);
134            }
135    
136            @Override
137            public boolean hasViewPermission(PermissionChecker permissionChecker) {
138                    return DDLRecordSetPermission.contains(
139                            permissionChecker, _recordSet, ActionKeys.VIEW);
140            }
141    
142            @Override
143            public String render(
144                            RenderRequest renderRequest, RenderResponse renderResponse,
145                            String template)
146                    throws Exception {
147    
148                    if (template.equals(TEMPLATE_ABSTRACT) ||
149                            template.equals(TEMPLATE_FULL_CONTENT)) {
150    
151                            renderRequest.setAttribute(
152                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD, _record);
153                            renderRequest.setAttribute(
154                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD_VERSION, _recordVersion);
155    
156                            String path =
157                                    "/html/portlet/dynamic_data_lists/asset/full_content.jsp";
158    
159                            return path;
160                    }
161                    else {
162                            return null;
163                    }
164            }
165    
166            @Override
167            protected String getIconPath(ThemeDisplay themeDisplay) {
168                    return themeDisplay.getPathThemeImages() + "/common/history.png";
169            }
170    
171            private static Log _log = LogFactoryUtil.getLog(
172                    DDLRecordAssetRenderer.class);
173    
174            private DDMStructure _ddmStructure;
175            private DDLRecord _record;
176            private DDLRecordSet _recordSet;
177            private DDLRecordVersion _recordVersion;
178    
179    }