001    /**
002     * Copyright (c) 2000-2012 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.portal.velocity;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.template.TemplateConstants;
020    import com.liferay.portal.kernel.templateparser.TemplateContext;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.SetUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.Theme;
025    import com.liferay.portal.service.permission.RolePermissionUtil;
026    import com.liferay.portal.template.TemplateContextHelper;
027    import com.liferay.portal.template.TemplatePortletPreferences;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portal.util.WebKeys;
031    
032    import java.util.Map;
033    import java.util.Set;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    import org.apache.velocity.tools.generic.DateTool;
038    import org.apache.velocity.tools.generic.EscapeTool;
039    import org.apache.velocity.tools.generic.IteratorTool;
040    import org.apache.velocity.tools.generic.ListTool;
041    import org.apache.velocity.tools.generic.MathTool;
042    import org.apache.velocity.tools.generic.NumberTool;
043    import org.apache.velocity.tools.generic.SortTool;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Raymond Augé
048     */
049    public class VelocityTemplateContextHelper extends TemplateContextHelper {
050    
051            @Override
052            public Set<String> getRestrictedVariables() {
053                    return SetUtil.fromArray(
054                            PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES);
055            }
056    
057            @Override
058            public void prepare(
059                    TemplateContext templateContext, HttpServletRequest request) {
060    
061                    super.prepare(templateContext, request);
062    
063                    // Theme display
064    
065                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
066                            WebKeys.THEME_DISPLAY);
067    
068                    if (themeDisplay != null) {
069    
070                            // Init
071    
072                            templateContext.put(
073                                    "init",
074                                    themeDisplay.getPathContext() +
075                                            TemplateConstants.SERVLET_SEPARATOR +
076                                                    "/html/themes/_unstyled/templates/init.vm");
077                    }
078    
079                    // Theme
080    
081                    Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
082    
083                    if ((theme == null) && (themeDisplay != null)) {
084                            theme = themeDisplay.getTheme();
085                    }
086    
087                    if (theme != null) {
088    
089                            // Full css and templates path
090    
091                            String servletContextName = GetterUtil.getString(
092                                    theme.getServletContextName());
093    
094                            templateContext.put(
095                                    "fullCssPath",
096                                    servletContextName + theme.getVelocityResourceListener() +
097                                            theme.getCssPath());
098    
099                            templateContext.put(
100                                    "fullTemplatesPath",
101                                    servletContextName + theme.getVelocityResourceListener() +
102                                            theme.getTemplatesPath());
103                    }
104    
105                    // Insert custom vm variables
106    
107                    Map<String, Object> vmVariables =
108                            (Map<String, Object>)request.getAttribute(WebKeys.VM_VARIABLES);
109    
110                    if (vmVariables != null) {
111                            for (Map.Entry<String, Object> entry : vmVariables.entrySet()) {
112                                    String key = entry.getKey();
113                                    Object value = entry.getValue();
114    
115                                    if (Validator.isNotNull(key)) {
116                                            templateContext.put(key, value);
117                                    }
118                            }
119                    }
120            }
121    
122            @Override
123            protected void populateExtraHelperUtilities(
124                    Map<String, Object> velocityContext) {
125    
126                    // Date tool
127    
128                    velocityContext.put("dateTool", new DateTool());
129    
130                    // Escape tool
131    
132                    velocityContext.put("escapeTool", new EscapeTool());
133    
134                    // Iterator tool
135    
136                    velocityContext.put("iteratorTool", new IteratorTool());
137    
138                    // List tool
139    
140                    velocityContext.put("listTool", new ListTool());
141    
142                    // Math tool
143    
144                    velocityContext.put("mathTool", new MathTool());
145    
146                    // Number tool
147    
148                    velocityContext.put("numberTool", new NumberTool());
149    
150                    // Portlet preferences
151    
152                    velocityContext.put(
153                            "velocityPortletPreferences", new TemplatePortletPreferences());
154    
155                    // Sort tool
156    
157                    velocityContext.put("sortTool", new SortTool());
158    
159                    // Permissions
160    
161                    try {
162                            velocityContext.put(
163                                    "rolePermission", RolePermissionUtil.getRolePermission());
164                    }
165                    catch (SecurityException se) {
166                            _log.error(se, se);
167                    }
168            }
169    
170            private static Log _log = LogFactoryUtil.getLog(
171                    VelocityTemplateContextHelper.class);
172    
173    }