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.portlet.journal.template;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.template.TemplateVariableCodeHandler;
020    import com.liferay.portal.kernel.template.TemplateVariableGroup;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalService;
026    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureService;
027    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalService;
028    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateService;
029    import com.liferay.portlet.dynamicdatamapping.template.BaseDDMTemplateHandler;
030    import com.liferay.portlet.dynamicdatamapping.template.DDMTemplateVariableCodeHandler;
031    import com.liferay.portlet.journal.model.JournalArticle;
032    import com.liferay.portlet.journal.service.JournalArticleLocalService;
033    import com.liferay.portlet.journal.service.JournalArticleService;
034    
035    import java.util.Locale;
036    import java.util.Map;
037    
038    /**
039     * @author Jorge Ferrer
040     */
041    public class JournalTemplateHandler extends BaseDDMTemplateHandler {
042    
043            @Override
044            public String getClassName() {
045                    return JournalArticle.class.getName();
046            }
047    
048            @Override
049            public String getName(Locale locale) {
050                    String portletTitle = PortalUtil.getPortletTitle(
051                            PortletKeys.JOURNAL, locale);
052    
053                    return portletTitle.concat(StringPool.SPACE).concat(
054                            LanguageUtil.get(locale, "template"));
055            }
056    
057            @Override
058            public String getResourceName() {
059                    return "com.liferay.portlet.journal.template";
060            }
061    
062            @Override
063            public String[] getRestrictedVariables(String language) {
064                    String[] restrictedVariables;
065    
066                    if (language.equals(TemplateConstants.LANG_TYPE_FTL)) {
067                            restrictedVariables =
068                                    PropsValues.JOURNAL_TEMPLATE_FREEMARKER_RESTRICTED_VARIABLES;
069                    }
070                    else if (language.equals(TemplateConstants.LANG_TYPE_VM)) {
071                            restrictedVariables =
072                                    PropsValues.JOURNAL_TEMPLATE_VELOCITY_RESTRICTED_VARIABLES;
073                    }
074                    else {
075                            restrictedVariables = new String[0];
076                    }
077    
078                    return restrictedVariables;
079            }
080    
081            @Override
082            public Map<String, TemplateVariableGroup> getTemplateVariableGroups(
083                            long classPK, String language, Locale locale)
084                    throws Exception {
085    
086                    Map<String, TemplateVariableGroup> templateVariableGroups =
087                            super.getTemplateVariableGroups(classPK, language, locale);
088    
089                    String[] restrictedVariables = getRestrictedVariables(language);
090    
091                    TemplateVariableGroup journalServicesTemplateVariableGroup =
092                            new TemplateVariableGroup(
093                                    "web-content-services", restrictedVariables);
094    
095                    journalServicesTemplateVariableGroup.setAutocompleteEnabled(false);
096    
097                    journalServicesTemplateVariableGroup.addServiceLocatorVariables(
098                            JournalArticleLocalService.class, JournalArticleService.class,
099                            DDMStructureLocalService.class, DDMStructureService.class,
100                            DDMTemplateLocalService.class, DDMTemplateService.class);
101    
102                    templateVariableGroups.put(
103                            journalServicesTemplateVariableGroup.getLabel(),
104                            journalServicesTemplateVariableGroup);
105    
106                    return templateVariableGroups;
107            }
108    
109            @Override
110            protected TemplateVariableCodeHandler getTemplateVariableCodeHandler() {
111                    return _templateVariableCodeHandler;
112            }
113    
114            @Override
115            protected TemplateVariableGroup getUtilTemplateVariableGroup() {
116                    TemplateVariableGroup utilTemplateVariableGroup =
117                            super.getUtilTemplateVariableGroup();
118    
119                    utilTemplateVariableGroup.addVariable(
120                            "xml-request", String.class, "xmlRequest");
121    
122                    return utilTemplateVariableGroup;
123            }
124    
125            private TemplateVariableCodeHandler _templateVariableCodeHandler =
126                    new DDMTemplateVariableCodeHandler(
127                            "com/liferay/portlet/journal/dependencies/template/");
128    
129    }