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.ddm.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.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     */
037    public abstract class BaseDDMTemplateHandler extends BaseTemplateHandler {
038    
039            public Map<String, TemplateVariableGroup> getTemplateVariableGroups(
040                            long classPK, Locale locale)
041                    throws Exception {
042    
043                    Map<String, TemplateVariableGroup> templateVariableGroups =
044                            new LinkedHashMap<String, TemplateVariableGroup>();
045    
046                    templateVariableGroups.put(
047                            "fields", getStructureFieldsTemplateVariableGroup(classPK, locale));
048    
049                    templateVariableGroups.put(
050                            "general-variables", getGeneralVariablesTemplateVariableGroup());
051    
052                    templateVariableGroups.put("util", getUtilTemplateVariableGroup());
053    
054                    return templateVariableGroups;
055            }
056    
057            protected TemplateVariableGroup getGeneralVariablesTemplateVariableGroup() {
058                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
059                            "general-variables");
060    
061                    templateVariableGroup.addVariable(
062                            "portal-instance", Company.class, "company");
063                    templateVariableGroup.addVariable(
064                            "portal-instance-id", null, "companyId");
065                    templateVariableGroup.addVariable("device", Device.class, "device");
066                    templateVariableGroup.addVariable("site-id", null, "groupId");
067                    templateVariableGroup.addVariable(
068                            "view-mode", String.class, "viewMode");
069    
070                    return templateVariableGroup;
071            }
072    
073            protected TemplateVariableGroup getStructureFieldsTemplateVariableGroup(
074                            long ddmStructureId, Locale locale)
075                    throws PortalException, SystemException {
076    
077                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
078                            "fields");
079    
080                    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(
081                            ddmStructureId);
082    
083                    List<String> fieldNames = ddmStructure.getRootFieldNames();
084    
085                    for (String fieldName : fieldNames) {
086                            if (fieldName.startsWith(StringPool.UNDERLINE)) {
087                                    continue;
088                            }
089    
090                            String label = ddmStructure.getFieldLabel(fieldName, locale);
091                            String tip = ddmStructure.getFieldTip(fieldName, locale);
092                            String dataType = ddmStructure.getFieldDataType(fieldName);
093                            boolean repeatable = ddmStructure.getFieldRepeatable(fieldName);
094    
095                            templateVariableGroup.addFieldVariable(
096                                    label, TemplateNode.class, fieldName, tip, dataType,
097                                    repeatable);
098                    }
099    
100                    return templateVariableGroup;
101            }
102    
103            protected TemplateVariableGroup getUtilTemplateVariableGroup() {
104                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
105                            "util");
106    
107                    templateVariableGroup.addVariable(
108                            "permission-checker", PermissionChecker.class, "permissionChecker");
109                    templateVariableGroup.addVariable(
110                            "random-namespace", String.class, "randomNamespace");
111                    templateVariableGroup.addVariable(
112                            "templates-path", String.class, "templatesPath");
113                    templateVariableGroup.addVariable(
114                            "xml-request", String.class, "xmlRequest");
115    
116                    return templateVariableGroup;
117            }
118    
119    }