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.freemarker;
016    
017    import com.liferay.portal.kernel.template.TemplateConstants;
018    import com.liferay.portal.kernel.templateparser.TemplateContext;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.SetUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Theme;
024    import com.liferay.portal.template.TemplateContextHelper;
025    import com.liferay.portal.template.TemplatePortletPreferences;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
029    
030    import freemarker.ext.beans.BeansWrapper;
031    
032    import java.util.Map;
033    import java.util.Set;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Mika Koivisto
039     * @author Raymond Augé
040     */
041    public class FreeMarkerTemplateContextHelper extends TemplateContextHelper {
042    
043            @Override
044            public Set<String> getRestrictedVariables() {
045                    return SetUtil.fromArray(
046                            PropsValues.JOURNAL_TEMPLATE_FREEMARKER_RESTRICTED_VARIABLES);
047            }
048    
049            @Override
050            public void prepare(
051                    TemplateContext templateContext, HttpServletRequest request) {
052    
053                    super.prepare(templateContext, request);
054    
055                    // Theme display
056    
057                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
058                            WebKeys.THEME_DISPLAY);
059    
060                    if (themeDisplay != null) {
061                            Theme theme = themeDisplay.getTheme();
062    
063                            // Full css and templates path
064    
065                            String servletContextName = GetterUtil.getString(
066                                    theme.getServletContextName());
067    
068                            templateContext.put(
069                                    "fullCssPath",
070                                    StringPool.SLASH + servletContextName +
071                                            theme.getFreeMarkerTemplateLoader() + theme.getCssPath());
072    
073                            templateContext.put(
074                                    "fullTemplatesPath",
075                                    StringPool.SLASH + servletContextName +
076                                            theme.getFreeMarkerTemplateLoader() +
077                                                    theme.getTemplatesPath());
078    
079                            // Init
080    
081                            templateContext.put(
082                                    "init",
083                                    StringPool.SLASH + themeDisplay.getPathContext() +
084                                            TemplateConstants.SERVLET_SEPARATOR +
085                                                    "/html/themes/_unstyled/templates/init.ftl");
086                    }
087    
088                    // Insert custom ftl variables
089    
090                    Map<String, Object> ftlVariables =
091                            (Map<String, Object>)request.getAttribute(WebKeys.FTL_VARIABLES);
092    
093                    if (ftlVariables != null) {
094                            for (Map.Entry<String, Object> entry : ftlVariables.entrySet()) {
095                                    String key = entry.getKey();
096                                    Object value = entry.getValue();
097    
098                                    if (Validator.isNotNull(key)) {
099                                            templateContext.put(key, value);
100                                    }
101                            }
102                    }
103            }
104    
105            @Override
106            protected void populateExtraHelperUtilities(
107                    Map<String, Object> helperUtilities) {
108    
109                    // Enum util
110    
111                    helperUtilities.put(
112                            "enumUtil", BeansWrapper.getDefaultInstance().getEnumModels());
113    
114                    // Object util
115    
116                    helperUtilities.put("objectUtil", new LiferayObjectConstructor());
117    
118                    // Portlet preferences
119    
120                    helperUtilities.put(
121                            "freeMarkerPortletPreferences", new TemplatePortletPreferences());
122    
123                    // Static class util
124    
125                    helperUtilities.put(
126                            "staticUtil", BeansWrapper.getDefaultInstance().getStaticModels());
127            }
128    
129    }