001    /**
002     * Copyright (c) 2000-2013 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.template;
016    
017    import com.liferay.portal.kernel.audit.AuditMessageFactoryUtil;
018    import com.liferay.portal.kernel.audit.AuditRouterUtil;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.language.UnicodeLanguageUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.portlet.PortletModeFactory_IW;
025    import com.liferay.portal.kernel.portlet.WindowStateFactory_IW;
026    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
027    import com.liferay.portal.kernel.template.Template;
028    import com.liferay.portal.kernel.template.TemplateHandler;
029    import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
030    import com.liferay.portal.kernel.template.TemplateVariableGroup;
031    import com.liferay.portal.kernel.util.ArrayUtil_IW;
032    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
033    import com.liferay.portal.kernel.util.DateUtil_IW;
034    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.GetterUtil_IW;
037    import com.liferay.portal.kernel.util.HtmlUtil;
038    import com.liferay.portal.kernel.util.HttpUtil;
039    import com.liferay.portal.kernel.util.JavaConstants;
040    import com.liferay.portal.kernel.util.ListMergeable;
041    import com.liferay.portal.kernel.util.LocaleUtil;
042    import com.liferay.portal.kernel.util.ParamUtil_IW;
043    import com.liferay.portal.kernel.util.PrefsPropsUtil;
044    import com.liferay.portal.kernel.util.PropsUtil;
045    import com.liferay.portal.kernel.util.StaticFieldGetter;
046    import com.liferay.portal.kernel.util.StringPool;
047    import com.liferay.portal.kernel.util.StringUtil_IW;
048    import com.liferay.portal.kernel.util.TimeZoneUtil_IW;
049    import com.liferay.portal.kernel.util.UnicodeFormatter_IW;
050    import com.liferay.portal.kernel.util.Validator_IW;
051    import com.liferay.portal.kernel.xml.SAXReader;
052    import com.liferay.portal.model.Layout;
053    import com.liferay.portal.model.Theme;
054    import com.liferay.portal.service.GroupLocalService;
055    import com.liferay.portal.service.GroupService;
056    import com.liferay.portal.service.LayoutLocalService;
057    import com.liferay.portal.service.LayoutService;
058    import com.liferay.portal.service.OrganizationLocalService;
059    import com.liferay.portal.service.OrganizationService;
060    import com.liferay.portal.service.UserLocalService;
061    import com.liferay.portal.service.UserService;
062    import com.liferay.portal.service.permission.AccountPermissionUtil;
063    import com.liferay.portal.service.permission.CommonPermissionUtil;
064    import com.liferay.portal.service.permission.GroupPermissionUtil;
065    import com.liferay.portal.service.permission.LayoutPermissionUtil;
066    import com.liferay.portal.service.permission.OrganizationPermissionUtil;
067    import com.liferay.portal.service.permission.PasswordPolicyPermissionUtil;
068    import com.liferay.portal.service.permission.PortalPermissionUtil;
069    import com.liferay.portal.service.permission.PortletPermissionUtil;
070    import com.liferay.portal.service.permission.RolePermissionUtil;
071    import com.liferay.portal.service.permission.UserGroupPermissionUtil;
072    import com.liferay.portal.service.permission.UserPermissionUtil;
073    import com.liferay.portal.theme.NavItem;
074    import com.liferay.portal.theme.ThemeDisplay;
075    import com.liferay.portal.util.ClassLoaderUtil;
076    import com.liferay.portal.util.PortalUtil;
077    import com.liferay.portal.util.SessionClicks_IW;
078    import com.liferay.portal.util.WebKeys;
079    import com.liferay.portal.webserver.WebServerServletTokenUtil;
080    import com.liferay.portlet.PortletURLFactoryUtil;
081    import com.liferay.portlet.documentlibrary.util.DLUtil;
082    import com.liferay.portlet.dynamicdatamapping.util.DDMUtil;
083    import com.liferay.portlet.expando.service.ExpandoColumnLocalService;
084    import com.liferay.portlet.expando.service.ExpandoRowLocalService;
085    import com.liferay.portlet.expando.service.ExpandoTableLocalService;
086    import com.liferay.portlet.expando.service.ExpandoValueLocalService;
087    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
088    import com.liferay.taglib.util.VelocityTaglibImpl;
089    import com.liferay.util.portlet.PortletRequestUtil;
090    
091    import java.lang.reflect.Method;
092    
093    import java.util.Collections;
094    import java.util.HashMap;
095    import java.util.List;
096    import java.util.Locale;
097    import java.util.Map;
098    import java.util.Set;
099    import java.util.concurrent.ConcurrentHashMap;
100    
101    import javax.portlet.PortletConfig;
102    import javax.portlet.PortletRequest;
103    import javax.portlet.PortletResponse;
104    import javax.portlet.RenderRequest;
105    import javax.portlet.RenderResponse;
106    
107    import javax.servlet.http.HttpServletRequest;
108    
109    import org.apache.struts.taglib.tiles.ComponentConstants;
110    import org.apache.struts.tiles.ComponentContext;
111    
112    /**
113     * @author Tina Tian
114     * @author Jorge Ferrer
115     */
116    public class TemplateContextHelper {
117    
118            public static Map<String, TemplateVariableGroup> getTemplateVariableGroups(
119                            long classNameId, long classPK, String language, Locale locale)
120                    throws Exception {
121    
122                    TemplateHandler templateHandler =
123                            TemplateHandlerRegistryUtil.getTemplateHandler(classNameId);
124    
125                    if (templateHandler == null) {
126                            return Collections.emptyMap();
127                    }
128    
129                    Map<String, TemplateVariableGroup> templateVariableGroups =
130                            templateHandler.getTemplateVariableGroups(
131                                    classPK, language, locale);
132    
133                    String[] restrictedVariables = templateHandler.getRestrictedVariables(
134                            language);
135    
136                    TemplateVariableGroup portalServicesTemplateVariableGroup =
137                            new TemplateVariableGroup("portal-services", restrictedVariables);
138    
139                    portalServicesTemplateVariableGroup.setAutocompleteEnabled(false);
140    
141                    portalServicesTemplateVariableGroup.addServiceLocatorVariables(
142                            GroupLocalService.class, GroupService.class,
143                            LayoutLocalService.class, LayoutService.class,
144                            OrganizationLocalService.class, OrganizationService.class,
145                            UserLocalService.class, UserService.class);
146    
147                    templateVariableGroups.put(
148                            portalServicesTemplateVariableGroup.getLabel(),
149                            portalServicesTemplateVariableGroup);
150    
151                    return templateVariableGroups;
152            }
153    
154            public Map<String, Object> getHelperUtilities(
155                    ClassLoader classLoader, boolean restricted) {
156    
157                    Map<String, Object>[] helperUtilitiesArray = _helperUtilitiesMaps.get(
158                            classLoader);
159    
160                    if (helperUtilitiesArray == null) {
161                            helperUtilitiesArray = (Map<String, Object>[])new Map<?, ?>[2];
162    
163                            _helperUtilitiesMaps.put(classLoader, helperUtilitiesArray);
164                    }
165                    else {
166                            Map<String, Object> helperUtilities = null;
167    
168                            if (restricted) {
169                                    helperUtilities = helperUtilitiesArray[1];
170                            }
171                            else {
172                                    helperUtilities = helperUtilitiesArray[0];
173                            }
174    
175                            if (helperUtilities != null) {
176                                    return helperUtilities;
177                            }
178                    }
179    
180                    Map<String, Object> helperUtilities = new HashMap<String, Object>();
181    
182                    populateCommonHelperUtilities(helperUtilities);
183                    populateExtraHelperUtilities(helperUtilities);
184    
185                    if (restricted) {
186                            Set<String> restrictedVariables = getRestrictedVariables();
187    
188                            for (String restrictedVariable : restrictedVariables) {
189                                    helperUtilities.remove(restrictedVariable);
190                            }
191    
192                            helperUtilitiesArray[1] = helperUtilities;
193                    }
194                    else {
195                            helperUtilitiesArray[0] = helperUtilities;
196                    }
197    
198                    return helperUtilities;
199            }
200    
201            public Set<String> getRestrictedVariables() {
202                    return Collections.emptySet();
203            }
204    
205            public TemplateControlContext getTemplateControlContext() {
206                    return _pacl.getTemplateControlContext();
207            }
208    
209            public void prepare(Template template, HttpServletRequest request) {
210    
211                    // Request
212    
213                    template.put("request", request);
214    
215                    // Portlet config
216    
217                    PortletConfig portletConfig = (PortletConfig)request.getAttribute(
218                            JavaConstants.JAVAX_PORTLET_CONFIG);
219    
220                    if (portletConfig != null) {
221                            template.put("portletConfig", portletConfig);
222                    }
223    
224                    // Render request
225    
226                    final PortletRequest portletRequest =
227                            (PortletRequest)request.getAttribute(
228                                    JavaConstants.JAVAX_PORTLET_REQUEST);
229    
230                    if (portletRequest != null) {
231                            if (portletRequest instanceof RenderRequest) {
232                                    template.put("renderRequest", portletRequest);
233                            }
234                    }
235    
236                    // Render response
237    
238                    final PortletResponse portletResponse =
239                            (PortletResponse)request.getAttribute(
240                                    JavaConstants.JAVAX_PORTLET_RESPONSE);
241    
242                    if (portletResponse != null) {
243                            if (portletResponse instanceof RenderResponse) {
244                                    template.put("renderResponse", portletResponse);
245                            }
246                    }
247    
248                    // XML request
249    
250                    if ((portletRequest != null) && (portletResponse != null)) {
251                            template.put(
252                                    "xmlRequest",
253                                    new Object() {
254    
255                                            @Override
256                                            public String toString() {
257                                                    return PortletRequestUtil.toXML(
258                                                            portletRequest, portletResponse);
259                                            }
260    
261                                    }
262                            );
263                    }
264    
265                    // Theme display
266    
267                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
268                            WebKeys.THEME_DISPLAY);
269    
270                    if (themeDisplay != null) {
271                            Layout layout = themeDisplay.getLayout();
272                            List<Layout> layouts = themeDisplay.getLayouts();
273    
274                            template.put("themeDisplay", themeDisplay);
275                            template.put("company", themeDisplay.getCompany());
276                            template.put("user", themeDisplay.getUser());
277                            template.put("realUser", themeDisplay.getRealUser());
278                            template.put("layout", layout);
279                            template.put("layouts", layouts);
280                            template.put("plid", String.valueOf(themeDisplay.getPlid()));
281                            template.put(
282                                    "layoutTypePortlet", themeDisplay.getLayoutTypePortlet());
283                            template.put(
284                                    "scopeGroupId", new Long(themeDisplay.getScopeGroupId()));
285                            template.put(
286                                    "permissionChecker", themeDisplay.getPermissionChecker());
287                            template.put("locale", themeDisplay.getLocale());
288                            template.put("timeZone", themeDisplay.getTimeZone());
289                            template.put("colorScheme", themeDisplay.getColorScheme());
290                            template.put("portletDisplay", themeDisplay.getPortletDisplay());
291    
292                            // Navigation items
293    
294                            if (layout != null) {
295                                    List<NavItem> navItems = NavItem.fromLayouts(
296                                            request, layouts, template);
297    
298                                    template.put("navItems", navItems);
299                            }
300    
301                            // Deprecated
302    
303                            template.put(
304                                    "portletGroupId", new Long(themeDisplay.getScopeGroupId()));
305                    }
306    
307                    // Theme
308    
309                    Theme theme = (Theme)request.getAttribute(WebKeys.THEME);
310    
311                    if ((theme == null) && (themeDisplay != null)) {
312                            theme = themeDisplay.getTheme();
313                    }
314    
315                    if (theme != null) {
316                            template.put("theme", theme);
317                    }
318    
319                    // Tiles attributes
320    
321                    prepareTiles(template, request);
322    
323                    // Page title and subtitle
324    
325                    ListMergeable<String> pageTitleListMergeable =
326                            (ListMergeable<String>)request.getAttribute(WebKeys.PAGE_TITLE);
327    
328                    if (pageTitleListMergeable != null) {
329                            String pageTitle = pageTitleListMergeable.mergeToString(
330                                    StringPool.SPACE);
331    
332                            template.put("pageTitle", pageTitle);
333                    }
334    
335                    ListMergeable<String> pageSubtitleListMergeable =
336                            (ListMergeable<String>)request.getAttribute(WebKeys.PAGE_SUBTITLE);
337    
338                    if (pageSubtitleListMergeable != null) {
339                            String pageSubtitle = pageSubtitleListMergeable.mergeToString(
340                                    StringPool.SPACE);
341    
342                            template.put("pageSubtitle", pageSubtitle);
343                    }
344            }
345    
346            public void removeAllHelperUtilities() {
347                    _helperUtilitiesMaps.clear();
348            }
349    
350            public void removeHelperUtilities(ClassLoader classLoader) {
351                    _helperUtilitiesMaps.remove(classLoader);
352            }
353    
354            public static interface PACL {
355    
356                    public TemplateControlContext getTemplateControlContext();
357    
358            }
359    
360            protected void populateCommonHelperUtilities(
361                    Map<String, Object> variables) {
362    
363                    // Array util
364    
365                    variables.put("arrayUtil", ArrayUtil_IW.getInstance());
366    
367                    // Audit message factory
368    
369                    try {
370                            variables.put(
371                                    "auditMessageFactoryUtil",
372                                    AuditMessageFactoryUtil.getAuditMessageFactory());
373                    }
374                    catch (SecurityException se) {
375                            _log.error(se, se);
376                    }
377    
378                    // Audit router util
379    
380                    try {
381                            variables.put("auditRouterUtil", AuditRouterUtil.getAuditRouter());
382                    }
383                    catch (SecurityException se) {
384                            _log.error(se, se);
385                    }
386    
387                    // Browser sniffer
388    
389                    try {
390                            variables.put(
391                                    "browserSniffer", BrowserSnifferUtil.getBrowserSniffer());
392                    }
393                    catch (SecurityException se) {
394                            _log.error(se, se);
395                    }
396    
397                    // Calendar factory
398    
399                    try {
400                            variables.put(
401                                    "calendarFactory", CalendarFactoryUtil.getCalendarFactory());
402                    }
403                    catch (SecurityException se) {
404                            _log.error(se, se);
405                    }
406    
407                    // Date format
408    
409                    try {
410                            variables.put(
411                                    "dateFormatFactory",
412                                    FastDateFormatFactoryUtil.getFastDateFormatFactory());
413                    }
414                    catch (SecurityException se) {
415                            _log.error(se, se);
416                    }
417    
418                    // Date util
419    
420                    variables.put("dateUtil", DateUtil_IW.getInstance());
421    
422                    // Dynamic data mapping util
423    
424                    try {
425                            variables.put("ddmUtil", DDMUtil.getDDM());
426                    }
427                    catch (SecurityException se) {
428                            _log.error(se, se);
429                    }
430    
431                    // Document library util
432    
433                    try {
434                            variables.put("dlUtil", DLUtil.getDL());
435                    }
436                    catch (SecurityException se) {
437                            _log.error(se, se);
438                    }
439    
440                    // Expando column service
441    
442                    try {
443                            ServiceLocator serviceLocator = ServiceLocator.getInstance();
444    
445                            // Service locator
446    
447                            variables.put("serviceLocator", serviceLocator);
448    
449                            try {
450                                    variables.put(
451                                            "expandoColumnLocalService",
452                                            serviceLocator.findService(
453                                                    ExpandoColumnLocalService.class.getName()));
454                            }
455                            catch (SecurityException se) {
456                                    _log.error(se, se);
457                            }
458    
459                            // Expando row service
460    
461                            try {
462                                    variables.put(
463                                            "expandoRowLocalService",
464                                            serviceLocator.findService(
465                                                    ExpandoRowLocalService.class.getName()));
466                            }
467                            catch (SecurityException se) {
468                                    _log.error(se, se);
469                            }
470    
471                            // Expando table service
472    
473                            try {
474                                    variables.put(
475                                            "expandoTableLocalService",
476                                            serviceLocator.findService(
477                                                    ExpandoTableLocalService.class.getName()));
478                            }
479                            catch (SecurityException se) {
480                                    _log.error(se, se);
481                            }
482    
483                            // Expando value service
484    
485                            try {
486                                    variables.put(
487                                            "expandoValueLocalService",
488                                            serviceLocator.findService(
489                                                    ExpandoValueLocalService.class.getName()));
490                            }
491                            catch (SecurityException se) {
492                                    _log.error(se, se);
493                            }
494                    }
495                    catch (SecurityException se) {
496                            _log.error(se, se);
497                    }
498    
499                    // Getter util
500    
501                    variables.put("getterUtil", GetterUtil_IW.getInstance());
502    
503                    // Html util
504    
505                    try {
506                            variables.put("htmlUtil", HtmlUtil.getHtml());
507                    }
508                    catch (SecurityException se) {
509                            _log.error(se, se);
510                    }
511    
512                    // Http util
513    
514                    try {
515                            variables.put("httpUtil", HttpUtil.getHttp());
516                    }
517                    catch (SecurityException se) {
518                            _log.error(se, se);
519                    }
520    
521                    // Journal content util
522    
523                    try {
524                            variables.put(
525                                    "journalContentUtil", JournalContentUtil.getJournalContent());
526                    }
527                    catch (SecurityException se) {
528                            _log.error(se, se);
529                    }
530    
531                    // JSON factory util
532    
533                    try {
534                            variables.put("jsonFactoryUtil", JSONFactoryUtil.getJSONFactory());
535                    }
536                    catch (SecurityException se) {
537                            _log.error(se, se);
538                    }
539    
540                    // Language util
541    
542                    try {
543                            variables.put("languageUtil", LanguageUtil.getLanguage());
544                    }
545                    catch (SecurityException se) {
546                            _log.error(se, se);
547                    }
548    
549                    try {
550                            variables.put(
551                                    "unicodeLanguageUtil",
552                                    UnicodeLanguageUtil.getUnicodeLanguage());
553                    }
554                    catch (SecurityException se) {
555                            _log.error(se, se);
556                    }
557    
558                    // Locale util
559    
560                    try {
561                            variables.put("localeUtil", LocaleUtil.getInstance());
562                    }
563                    catch (SecurityException se) {
564                            _log.error(se, se);
565                    }
566    
567                    // Param util
568    
569                    variables.put("paramUtil", ParamUtil_IW.getInstance());
570    
571                    // Portal util
572    
573                    try {
574                            variables.put("portalUtil", PortalUtil.getPortal());
575                    }
576                    catch (SecurityException se) {
577                            _log.error(se, se);
578                    }
579    
580                    try {
581                            variables.put("portal", PortalUtil.getPortal());
582                    }
583                    catch (SecurityException se) {
584                            _log.error(se, se);
585                    }
586    
587                    // Prefs props util
588    
589                    try {
590                            variables.put("prefsPropsUtil", PrefsPropsUtil.getPrefsProps());
591                    }
592                    catch (SecurityException se) {
593                            _log.error(se, se);
594                    }
595    
596                    // Props util
597    
598                    try {
599                            variables.put("propsUtil", PropsUtil.getProps());
600                    }
601                    catch (SecurityException se) {
602                            _log.error(se, se);
603                    }
604    
605                    // Portlet mode factory
606    
607                    variables.put(
608                            "portletModeFactory", PortletModeFactory_IW.getInstance());
609    
610                    // Portlet URL factory
611    
612                    try {
613                            variables.put(
614                                    "portletURLFactory",
615                                    PortletURLFactoryUtil.getPortletURLFactory());
616                    }
617                    catch (SecurityException se) {
618                            _log.error(se, se);
619                    }
620    
621                    try {
622                            UtilLocator utilLocator = UtilLocator.getInstance();
623    
624                            // Util locator
625    
626                            variables.put("utilLocator", utilLocator);
627    
628                            // SAX reader util
629    
630                            try {
631                                    variables.put(
632                                            "saxReaderUtil",
633                                            utilLocator.findUtil(SAXReader.class.getName()));
634                            }
635                            catch (SecurityException se) {
636                                    _log.error(se, se);
637                            }
638                    }
639                    catch (SecurityException se) {
640                            _log.error(se, se);
641                    }
642    
643                    // Session clicks
644    
645                    variables.put("sessionClicks", SessionClicks_IW.getInstance());
646    
647                    // Static field getter
648    
649                    variables.put("staticFieldGetter", StaticFieldGetter.getInstance());
650    
651                    // String util
652    
653                    variables.put("stringUtil", StringUtil_IW.getInstance());
654    
655                    // Time zone util
656    
657                    variables.put("timeZoneUtil", TimeZoneUtil_IW.getInstance());
658    
659                    // Unicode formatter
660    
661                    variables.put("unicodeFormatter", UnicodeFormatter_IW.getInstance());
662    
663                    // Validator
664    
665                    variables.put("validator", Validator_IW.getInstance());
666    
667                    // VelocityTaglib methods
668    
669                    try {
670                            Class<?> clazz = VelocityTaglibImpl.class;
671    
672                            Method method = clazz.getMethod(
673                                    "layoutIcon", new Class[] {Layout.class});
674    
675                            variables.put("velocityTaglib_layoutIcon", method);
676                    }
677                    catch (Exception e) {
678                            _log.error(e, e);
679                    }
680    
681                    // Web server servlet token
682    
683                    try {
684                            variables.put(
685                                    "webServerToken",
686                                    WebServerServletTokenUtil.getWebServerServletToken());
687                    }
688                    catch (SecurityException se) {
689                            _log.error(se, se);
690                    }
691    
692                    // Window state factory
693    
694                    variables.put(
695                            "windowStateFactory", WindowStateFactory_IW.getInstance());
696    
697                    // Permissions
698    
699                    try {
700                            variables.put(
701                                    "accountPermission",
702                                    AccountPermissionUtil.getAccountPermission());
703                    }
704                    catch (SecurityException se) {
705                            _log.error(se, se);
706                    }
707    
708                    try {
709                            variables.put(
710                                    "commonPermission", CommonPermissionUtil.getCommonPermission());
711                    }
712                    catch (SecurityException se) {
713                            _log.error(se, se);
714                    }
715    
716                    try {
717                            variables.put(
718                                    "groupPermission", GroupPermissionUtil.getGroupPermission());
719                    }
720                    catch (SecurityException se) {
721                            _log.error(se, se);
722                    }
723    
724                    try {
725                            variables.put(
726                                    "layoutPermission", LayoutPermissionUtil.getLayoutPermission());
727                    }
728                    catch (SecurityException se) {
729                            _log.error(se, se);
730                    }
731    
732                    try {
733                            variables.put(
734                                    "organizationPermission",
735                                    OrganizationPermissionUtil.getOrganizationPermission());
736                    }
737                    catch (SecurityException se) {
738                            _log.error(se, se);
739                    }
740    
741                    try {
742                            variables.put(
743                                    "passwordPolicyPermission",
744                                    PasswordPolicyPermissionUtil.getPasswordPolicyPermission());
745                    }
746                    catch (SecurityException se) {
747                            _log.error(se, se);
748                    }
749    
750                    try {
751                            variables.put(
752                                    "portalPermission", PortalPermissionUtil.getPortalPermission());
753                    }
754                    catch (SecurityException se) {
755                            _log.error(se, se);
756                    }
757    
758                    try {
759                            variables.put(
760                                    "portletPermission",
761                                    PortletPermissionUtil.getPortletPermission());
762                    }
763                    catch (SecurityException se) {
764                            _log.error(se, se);
765                    }
766    
767                    try {
768                            variables.put(
769                                    "rolePermission", RolePermissionUtil.getRolePermission());
770                    }
771                    catch (SecurityException se) {
772                            _log.error(se, se);
773                    }
774    
775                    try {
776                            variables.put(
777                                    "userGroupPermission",
778                                    UserGroupPermissionUtil.getUserGroupPermission());
779                    }
780                    catch (SecurityException se) {
781                            _log.error(se, se);
782                    }
783    
784                    try {
785                            variables.put(
786                                    "userPermission", UserPermissionUtil.getUserPermission());
787                    }
788                    catch (SecurityException se) {
789                            _log.error(se, se);
790                    }
791    
792                    // Deprecated
793    
794                    populateDeprecatedCommonHelperUtilities(variables);
795            }
796    
797            @SuppressWarnings("deprecation")
798            protected void populateDeprecatedCommonHelperUtilities(
799                    Map<String, Object> variables) {
800    
801                    try {
802                            variables.put(
803                                    "dateFormats",
804                                    FastDateFormatFactoryUtil.getFastDateFormatFactory());
805                    }
806                    catch (SecurityException se) {
807                            _log.error(se, se);
808                    }
809    
810                    try {
811                            variables.put(
812                                    "imageToken",
813                                    WebServerServletTokenUtil.getWebServerServletToken());
814                    }
815                    catch (SecurityException se) {
816                            _log.error(se, se);
817                    }
818    
819                    try {
820                            variables.put(
821                                    "locationPermission",
822                                    OrganizationPermissionUtil.getOrganizationPermission());
823                    }
824                    catch (SecurityException se) {
825                            _log.error(se, se);
826                    }
827    
828                    try {
829                            com.liferay.portal.kernel.util.Randomizer_IW randomizer =
830                                    com.liferay.portal.kernel.util.Randomizer_IW.getInstance();
831    
832                            variables.put("randomizer", randomizer.getWrappedInstance());
833                    }
834                    catch (SecurityException se) {
835                            _log.error(se, se);
836                    }
837            }
838    
839            protected void populateExtraHelperUtilities(Map<String, Object> variables) {
840            }
841    
842            protected void prepareTiles(Template template, HttpServletRequest request) {
843                    ComponentContext componentContext =
844                            (ComponentContext)request.getAttribute(
845                                    ComponentConstants.COMPONENT_CONTEXT);
846    
847                    if (componentContext == null) {
848                            return;
849                    }
850    
851                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
852                            WebKeys.THEME_DISPLAY);
853    
854                    String tilesTitle = (String)componentContext.getAttribute("title");
855    
856                    themeDisplay.setTilesTitle(tilesTitle);
857    
858                    template.put("tilesTitle", tilesTitle);
859    
860                    String tilesContent = (String)componentContext.getAttribute("content");
861    
862                    themeDisplay.setTilesContent(tilesContent);
863    
864                    template.put("tilesContent", tilesContent);
865    
866                    boolean tilesSelectable = GetterUtil.getBoolean(
867                            (String)componentContext.getAttribute("selectable"));
868    
869                    themeDisplay.setTilesSelectable(tilesSelectable);
870    
871                    template.put("tilesSelectable", tilesSelectable);
872            }
873    
874            private static Log _log = LogFactoryUtil.getLog(
875                    TemplateContextHelper.class);
876    
877            private static PACL _pacl = new NoPACL();
878    
879            private Map<ClassLoader, Map<String, Object>[]> _helperUtilitiesMaps =
880                    new ConcurrentHashMap<ClassLoader, Map<String, Object>[]>();
881    
882            private static class NoPACL implements PACL {
883    
884                    @Override
885                    public TemplateControlContext getTemplateControlContext() {
886                            ClassLoader contextClassLoader =
887                                    ClassLoaderUtil.getContextClassLoader();
888    
889                            return new TemplateControlContext(null, contextClassLoader);
890                    }
891    
892            }
893    
894    }