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