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