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.portletdisplaytemplate.util;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    import com.liferay.portal.kernel.template.TemplateHandler;
019    import com.liferay.portal.kernel.template.TemplateVariableGroup;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
021    
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    /**
029     * @author Eduardo Garcia
030     */
031    public class PortletDisplayTemplateUtil {
032    
033            /**
034             * Returns the portlet display template's DDM template matching the group
035             * and display style stored in the portlet configuration.
036             *
037             * @param      groupId the primary key of the group
038             * @param      displayStyle the display style stored in the portlet
039             *             configuration
040             * @return     the portlet display template's DDM template
041             * @deprecated As of 7.0.0, replaced by {@link
042             *             #getPortletDisplayTemplateDDMTemplate(long, long, String)}
043             */
044            @Deprecated
045            public static DDMTemplate fetchDDMTemplate(
046                    long groupId, String displayStyle) {
047    
048                    return getPortletDisplayTemplate().fetchDDMTemplate(
049                            groupId, displayStyle);
050            }
051    
052            /**
053             * Returns the group ID of the portlet display template's DDM template.
054             *
055             * @param  groupId the primary key of the group
056             * @return the group ID of the portlet display template's DDM template
057             */
058            public static long getDDMTemplateGroupId(long groupId) {
059                    return getPortletDisplayTemplate().getDDMTemplateGroupId(groupId);
060            }
061    
062            /**
063             * Returns the template key of the portlet display template's DDM template
064             * from the display style stored in the portlet configuration.
065             *
066             * @param  displayStyle the display style stored in the portlet
067             *         configuration
068             * @return the template key of the portlet display template's DDM template
069             */
070            public static String getDDMTemplateKey(String displayStyle) {
071                    return getPortletDisplayTemplate().getDDMTemplateKey(displayStyle);
072            }
073    
074            /**
075             * Returns the UUID of the portlet display template's DDM template from the
076             * display style stored in the portlet configuration.
077             *
078             * @param      displayStyle the display style stored in the portlet
079             *             configuration
080             * @return     the UUID of the portlet display template's DDM template
081             * @deprecated As of 7.0.0, replaced by {@link #getDDMTemplateKey(String)}
082             */
083            @Deprecated
084            public static String getDDMTemplateUuid(String displayStyle) {
085                    return getPortletDisplayTemplate().getDDMTemplateUuid(displayStyle);
086            }
087    
088            /**
089             * Returns the default portlet display template's DDM template that's
090             * associated with the class name ID.
091             *
092             * @param  groupId the primary key of the group
093             * @param  classNameId the primary key of the class name of the model the
094             *         display style is related to
095             * @return the default portlet display template's DDM template
096             */
097            public static DDMTemplate getDefaultPortletDisplayTemplateDDMTemplate(
098                    long groupId, long classNameId) {
099    
100                    return
101                            getPortletDisplayTemplate().
102                                    getDefaultPortletDisplayTemplateDDMTemplate(
103                                            groupId, classNameId);
104            }
105    
106            /**
107             * Returns the display style that's associated with the template key of the
108             * portlet display template's DDM template.
109             *
110             * @param  ddmTemplateKey the template key of the portlet display template's
111             *         DDM template
112             * @return the display style stored in the portlet configuration
113             */
114            public static String getDisplayStyle(String ddmTemplateKey) {
115                    return getPortletDisplayTemplate().getDisplayStyle(ddmTemplateKey);
116            }
117    
118            public static PortletDisplayTemplate getPortletDisplayTemplate() {
119                    PortalRuntimePermission.checkGetBeanProperty(
120                            PortletDisplayTemplate.class);
121    
122                    return _portletDisplayTemplate;
123            }
124    
125            /**
126             * Returns the portlet display template's DDM template that's associated
127             * with the class name ID and that matches the group and display style
128             * stored in the portlet configuration.
129             *
130             * @param  groupId the primary key of the group
131             * @param  classNameId the primary key of the class name of the model the
132             *         display style is related to
133             * @param  displayStyle the display style stored in the portlet
134             *         configuration
135             * @return the portlet display template's DDM template
136             */
137            public static DDMTemplate getPortletDisplayTemplateDDMTemplate(
138                    long groupId, long classNameId, String displayStyle) {
139    
140                    return
141                            getPortletDisplayTemplate().getPortletDisplayTemplateDDMTemplate(
142                                    groupId, classNameId, displayStyle);
143            }
144    
145            /**
146             * Returns the portlet display template's DDM template that's associated
147             * with the class name ID and that matches the group and display style
148             * stored in the portlet configuration. Optionally returns the default
149             * portlet display template's DDM template that's associated with the class
150             * name ID if none are found for the given parameters.
151             *
152             * @param  groupId the primary key of the group
153             * @param  classNameId the primary key of the class name of the model the
154             *         display style is related to
155             * @param  displayStyle the display style stored in the portlet
156             *         configuration
157             * @param  useDefault whether to return the default portlet display
158             *         template's DDM template that's associated with the class name ID
159             *         if none are found for the given parameters.
160             * @return the portlet display template's DDM template
161             */
162            public static DDMTemplate getPortletDisplayTemplateDDMTemplate(
163                    long groupId, long classNameId, String displayStyle,
164                    boolean useDefault) {
165    
166                    return
167                            getPortletDisplayTemplate().getPortletDisplayTemplateDDMTemplate(
168                                    groupId, classNameId, displayStyle, useDefault);
169            }
170    
171            /**
172             * Returns the primary key of the portlet display template's DDM template
173             * matching the group and the display style stored in the portlet
174             * configuration.
175             *
176             * @param      groupId the primary key of the group
177             * @param      displayStyle the display style stored in the portlet
178             *             configuration
179             * @return     the primary key of the portlet display template's DDM
180             *             template
181             * @deprecated As of 7.0.0, replaced by {@link
182             *             #getPortletDisplayTemplateDDMTemplate(long, long, String)}
183             */
184            @Deprecated
185            public static long getPortletDisplayTemplateDDMTemplateId(
186                    long groupId, String displayStyle) {
187    
188                    return
189                            getPortletDisplayTemplate().getPortletDisplayTemplateDDMTemplateId(
190                                    groupId, displayStyle);
191            }
192    
193            /**
194             * Returns the portlet display template handlers.
195             *
196             * @return the portlet display template handlers
197             */
198            public static List<TemplateHandler> getPortletDisplayTemplateHandlers() {
199                    return getPortletDisplayTemplate().getPortletDisplayTemplateHandlers();
200            }
201    
202            /**
203             * Returns the portlet display template's map of script variable groups for
204             * which hints are displayed in the template editor palette.
205             *
206             * @param  language the portlet display template's scripting language.
207             *         Acceptable values for the FreeMarker, Velocity, or XSL languages
208             *         are {@link
209             *         com.liferay.portal.kernel.template.TemplateConstants#LANG_TYPE_FTL},
210             *         {@link
211             *         com.liferay.portal.kernel.template.TemplateConstants#LANG_TYPE_VM},
212             *         or {@link
213             *         com.liferay.portal.kernel.template.TemplateConstants#LANG_TYPE_XSL},
214             *         respectively.
215             * @return the portlet display template's map of script variable groups for
216             *         which hints are displayed in the template editor palette
217             * @see    TemplateHandler#getTemplateVariableGroups(long, String,
218             *         java.util.Locale)
219             */
220            public static Map<String, TemplateVariableGroup>
221                    getTemplateVariableGroups(String language) {
222    
223                    return getPortletDisplayTemplate().getTemplateVariableGroups(language);
224            }
225    
226            /**
227             * Returns the result of rendering the DDM template with the entries in the
228             * page context.
229             *
230             * @param  request the request with which the template is rendered
231             * @param  response the response with which the template is rendered
232             * @param  ddmTemplate the DDM template
233             * @param  entries the entries that are rendered in the template
234             * @return the result of rendering the DDM template
235             * @throws Exception if the DDM template could not be rendered
236             */
237            public static String renderDDMTemplate(
238                            HttpServletRequest request, HttpServletResponse response,
239                            DDMTemplate ddmTemplate, List<?> entries)
240                    throws Exception {
241    
242                    return getPortletDisplayTemplate().renderDDMTemplate(
243                            request, response, ddmTemplate, entries);
244            }
245    
246            /**
247             * Returns the result of rendering the DDM template with the entries in the
248             * page context and template context.
249             *
250             * @param  request the request with which the template is rendered
251             * @param  response the response with which the template is rendered
252             * @param  ddmTemplate the DDM template
253             * @param  entries the entries that are rendered in the template
254             * @param  contextObjects the map of objects defining the context in which
255             *         the template is rendered
256             * @return the result of rendering the DDM template
257             * @throws Exception if the DDM template could not be rendered
258             */
259            public static String renderDDMTemplate(
260                            HttpServletRequest request, HttpServletResponse response,
261                            DDMTemplate ddmTemplate, List<?> entries,
262                            Map<String, Object> contextObjects)
263                    throws Exception {
264    
265                    return getPortletDisplayTemplate().renderDDMTemplate(
266                            request, response, ddmTemplate, entries, contextObjects);
267            }
268    
269            /**
270             * Returns the result of rendering the DDM template with the entries in the
271             * page context.
272             *
273             * @param  request the request with which the template is rendered.
274             * @param  response the response with which the template is rendered.
275             * @param  ddmTemplateId the primary key of the DDM template
276             * @param  entries the entries that are rendered in the template
277             * @return the result of rendering the DDM template
278             * @throws Exception if the DDM template could not be rendered
279             */
280            public static String renderDDMTemplate(
281                            HttpServletRequest request, HttpServletResponse response,
282                            long ddmTemplateId, List<?> entries)
283                    throws Exception {
284    
285                    return getPortletDisplayTemplate().renderDDMTemplate(
286                            request, response, ddmTemplateId, entries);
287            }
288    
289            /**
290             * Returns the result of rendering the DDM template with the entries in the
291             * page context and template context.
292             *
293             * @param  request the request with which the template is rendered.
294             * @param  response the response with which the template is rendered.
295             * @param  ddmTemplateId the primary key of the DDM template
296             * @param  entries the entries that are rendered in the template
297             * @param  contextObjects the map of objects defining the context in which
298             *         the template is rendered
299             * @return the result of rendering the DDM template
300             * @throws Exception if the DDM template could not be rendered
301             */
302            public static String renderDDMTemplate(
303                            HttpServletRequest request, HttpServletResponse response,
304                            long ddmTemplateId, List<?> entries,
305                            Map<String, Object> contextObjects)
306                    throws Exception {
307    
308                    return getPortletDisplayTemplate().renderDDMTemplate(
309                            request, response, ddmTemplateId, entries, contextObjects);
310            }
311    
312            public void setPortletDisplayTemplate(
313                    PortletDisplayTemplate portletDisplayTemplate) {
314    
315                    PortalRuntimePermission.checkSetBeanProperty(getClass());
316    
317                    _portletDisplayTemplate = portletDisplayTemplate;
318            }
319    
320            private static PortletDisplayTemplate _portletDisplayTemplate;
321    
322    }