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.Validator;
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<>();
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            @Override
060            public boolean isDisplayTemplateHandler() {
061                    return false;
062            }
063    
064            protected void addTemplateVariableGroup(
065                    Map<String, TemplateVariableGroup> templateVariableGroups,
066                    TemplateVariableGroup templateVariableGroup) {
067    
068                    if (templateVariableGroup == null) {
069                            return;
070                    }
071    
072                    templateVariableGroups.put(
073                            templateVariableGroup.getLabel(), templateVariableGroup);
074            }
075    
076            protected Class<?> getFieldVariableClass() {
077                    return TemplateNode.class;
078            }
079    
080            protected TemplateVariableGroup getGeneralVariablesTemplateVariableGroup() {
081                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
082                            "general-variables");
083    
084                    templateVariableGroup.addVariable("device", Device.class, "device");
085                    templateVariableGroup.addVariable(
086                            "portal-instance", Company.class, "company");
087                    templateVariableGroup.addVariable(
088                            "portal-instance-id", null, "companyId");
089                    templateVariableGroup.addVariable("site-id", null, "groupId");
090                    templateVariableGroup.addVariable(
091                            "view-mode", String.class, "viewMode");
092    
093                    return templateVariableGroup;
094            }
095    
096            protected TemplateVariableGroup getStructureFieldsTemplateVariableGroup(
097                            long ddmStructureId, Locale locale)
098                    throws PortalException {
099    
100                    if (ddmStructureId <= 0) {
101                            return null;
102                    }
103    
104                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
105                            "fields");
106    
107                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
108                            ddmStructureId);
109    
110                    List<String> fieldNames = ddmStructure.getRootFieldNames();
111    
112                    for (String fieldName : fieldNames) {
113                            String label = ddmStructure.getFieldLabel(fieldName, locale);
114                            String tip = ddmStructure.getFieldTip(fieldName, locale);
115                            String dataType = ddmStructure.getFieldDataType(fieldName);
116                            boolean repeatable = ddmStructure.getFieldRepeatable(fieldName);
117    
118                            if (Validator.isNull(dataType)) {
119                                    continue;
120                            }
121    
122                            templateVariableGroup.addFieldVariable(
123                                    label, getFieldVariableClass(), fieldName, tip, dataType,
124                                    repeatable, getTemplateVariableCodeHandler());
125                    }
126    
127                    return templateVariableGroup;
128            }
129    
130            protected abstract TemplateVariableCodeHandler
131                    getTemplateVariableCodeHandler();
132    
133            protected TemplateVariableGroup getUtilTemplateVariableGroup() {
134                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
135                            "util");
136    
137                    templateVariableGroup.addVariable(
138                            "permission-checker", PermissionChecker.class, "permissionChecker");
139                    templateVariableGroup.addVariable(
140                            "random-namespace", String.class, "randomNamespace");
141                    templateVariableGroup.addVariable(
142                            "templates-path", String.class, "templatesPath");
143    
144                    return templateVariableGroup;
145            }
146    
147    }