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.dynamicdatamapping.template;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.mobile.device.Device;
019    import com.liferay.portal.kernel.template.BaseTemplateHandler;
020    import com.liferay.portal.kernel.template.TemplateVariableCodeHandler;
021    import com.liferay.portal.kernel.template.TemplateVariableGroup;
022    import com.liferay.portal.kernel.templateparser.TemplateNode;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.Company;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
027    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
028    
029    import java.util.LinkedHashMap;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    /**
035     * @author Jorge Ferrer
036     * @author Marcellus Tavares
037     */
038    public abstract class BaseDDMTemplateHandler extends BaseTemplateHandler {
039    
040            @Override
041            public Map<String, TemplateVariableGroup> getTemplateVariableGroups(
042                            long classPK, String language, Locale locale)
043                    throws Exception {
044    
045                    Map<String, TemplateVariableGroup> templateVariableGroups =
046                            new LinkedHashMap<String, TemplateVariableGroup>();
047    
048                    addTemplateVariableGroup(
049                            templateVariableGroups, getGeneralVariablesTemplateVariableGroup());
050                    addTemplateVariableGroup(
051                            templateVariableGroups,
052                            getStructureFieldsTemplateVariableGroup(classPK, locale));
053                    addTemplateVariableGroup(
054                            templateVariableGroups, getUtilTemplateVariableGroup());
055    
056                    return templateVariableGroups;
057            }
058    
059            protected void addTemplateVariableGroup(
060                    Map<String, TemplateVariableGroup> templateVariableGroups,
061                    TemplateVariableGroup templateVariableGroup) {
062    
063                    if (templateVariableGroup == null) {
064                            return;
065                    }
066    
067                    templateVariableGroups.put(
068                            templateVariableGroup.getLabel(), templateVariableGroup);
069            }
070    
071            protected Class<?> getFieldVariableClass() {
072                    return TemplateNode.class;
073            }
074    
075            protected TemplateVariableGroup getGeneralVariablesTemplateVariableGroup() {
076                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
077                            "general-variables");
078    
079                    templateVariableGroup.addVariable(
080                            "portal-instance", Company.class, "company");
081                    templateVariableGroup.addVariable(
082                            "portal-instance-id", null, "companyId");
083                    templateVariableGroup.addVariable("device", Device.class, "device");
084                    templateVariableGroup.addVariable("site-id", null, "groupId");
085                    templateVariableGroup.addVariable(
086                            "view-mode", String.class, "viewMode");
087    
088                    return templateVariableGroup;
089            }
090    
091            protected TemplateVariableGroup getStructureFieldsTemplateVariableGroup(
092                            long ddmStructureId, Locale locale)
093                    throws PortalException {
094    
095                    if (ddmStructureId <= 0) {
096                            return null;
097                    }
098    
099                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
100                            "fields");
101    
102                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
103                            ddmStructureId);
104    
105                    List<String> fieldNames = ddmStructure.getRootFieldNames();
106    
107                    for (String fieldName : fieldNames) {
108                            if (fieldName.startsWith(StringPool.UNDERLINE)) {
109                                    continue;
110                            }
111    
112                            String label = ddmStructure.getFieldLabel(fieldName, locale);
113                            String tip = ddmStructure.getFieldTip(fieldName, locale);
114                            String dataType = ddmStructure.getFieldDataType(fieldName);
115                            boolean repeatable = ddmStructure.getFieldRepeatable(fieldName);
116    
117                            templateVariableGroup.addFieldVariable(
118                                    label, getFieldVariableClass(), fieldName, tip, dataType,
119                                    repeatable, getTemplateVariableCodeHandler());
120                    }
121    
122                    return templateVariableGroup;
123            }
124    
125            protected abstract TemplateVariableCodeHandler
126                    getTemplateVariableCodeHandler();
127    
128            protected TemplateVariableGroup getUtilTemplateVariableGroup() {
129                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
130                            "util");
131    
132                    templateVariableGroup.addVariable(
133                            "permission-checker", PermissionChecker.class, "permissionChecker");
134                    templateVariableGroup.addVariable(
135                            "random-namespace", String.class, "randomNamespace");
136                    templateVariableGroup.addVariable(
137                            "templates-path", String.class, "templatesPath");
138    
139                    return templateVariableGroup;
140            }
141    
142    }