001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 int getStatus() {
083                    return _recordVersion.getStatus();
084            }
085    
086            @Override
087            public String getSummary(Locale locale) {
088                    return StringPool.BLANK;
089            }
090    
091            @Override
092            public String getTitle(Locale locale) {
093                    String ddmStructureName = _ddmStructure.getName(locale);
094    
095                    String recordSetName = _recordSet.getName(locale);
096    
097                    return LanguageUtil.format(
098                            locale, "new-x-for-list-x",
099                            new Object[] {ddmStructureName, recordSetName}, false);
100            }
101    
102            @Override
103            public PortletURL getURLEdit(
104                            LiferayPortletRequest liferayPortletRequest,
105                            LiferayPortletResponse liferayPortletResponse)
106                    throws Exception {
107    
108                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
109                            getControlPanelPlid(liferayPortletRequest),
110                            PortletKeys.DYNAMIC_DATA_LISTS, PortletRequest.RENDER_PHASE);
111    
112                    portletURL.setParameter(
113                            "struts_action", "/dynamic_data_lists/edit_record");
114                    portletURL.setParameter(
115                            "recordId", String.valueOf(_record.getRecordId()));
116                    portletURL.setParameter("version", _recordVersion.getVersion());
117    
118                    return portletURL;
119            }
120    
121            @Override
122            public String getURLViewInContext(
123                            LiferayPortletRequest liferayPortletRequest,
124                            LiferayPortletResponse liferayPortletResponse,
125                            String noSuchEntryRedirect)
126                    throws Exception {
127    
128                    return getURLViewInContext(
129                            liferayPortletRequest, noSuchEntryRedirect,
130                            "/dynamic_data_lists/find_record", "recordId",
131                            _record.getRecordId());
132            }
133    
134            @Override
135            public long getUserId() {
136                    return _recordVersion.getUserId();
137            }
138    
139            @Override
140            public String getUserName() {
141                    return _recordVersion.getUserName();
142            }
143    
144            @Override
145            public String getUuid() {
146                    return _record.getUuid();
147            }
148    
149            @Override
150            public boolean hasEditPermission(PermissionChecker permissionChecker) {
151                    return DDLRecordSetPermission.contains(
152                            permissionChecker, _recordSet, ActionKeys.UPDATE);
153            }
154    
155            @Override
156            public boolean hasViewPermission(PermissionChecker permissionChecker) {
157                    return DDLRecordSetPermission.contains(
158                            permissionChecker, _recordSet, ActionKeys.VIEW);
159            }
160    
161            @Override
162            public String render(
163                            RenderRequest renderRequest, RenderResponse renderResponse,
164                            String template)
165                    throws Exception {
166    
167                    if (template.equals(TEMPLATE_ABSTRACT) ||
168                            template.equals(TEMPLATE_FULL_CONTENT)) {
169    
170                            renderRequest.setAttribute(
171                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD, _record);
172                            renderRequest.setAttribute(
173                                    WebKeys.DYNAMIC_DATA_LISTS_RECORD_VERSION, _recordVersion);
174    
175                            String path =
176                                    "/html/portlet/dynamic_data_lists/asset/full_content.jsp";
177    
178                            return path;
179                    }
180                    else {
181                            return null;
182                    }
183            }
184    
185            @Override
186            protected String getIconPath(ThemeDisplay themeDisplay) {
187                    return themeDisplay.getPathThemeImages() + "/common/history.png";
188            }
189    
190            private static Log _log = LogFactoryUtil.getLog(
191                    DDLRecordAssetRenderer.class);
192    
193            private DDMStructure _ddmStructure;
194            private DDLRecord _record;
195            private DDLRecordSet _recordSet;
196            private DDLRecordVersion _recordVersion;
197    
198    }