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.dynamicdatalists.template;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.template.TemplateVariableCodeHandler;
019    import com.liferay.portal.kernel.template.TemplateVariableGroup;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.PortletKeys;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalService;
026    import com.liferay.portlet.dynamicdatalists.service.DDLRecordService;
027    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalService;
028    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetService;
029    import com.liferay.portlet.dynamicdatalists.util.DDLConstants;
030    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalService;
031    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureService;
032    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalService;
033    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateService;
034    import com.liferay.portlet.dynamicdatamapping.storage.Field;
035    import com.liferay.portlet.dynamicdatamapping.template.BaseDDMTemplateHandler;
036    import com.liferay.portlet.dynamicdatamapping.template.DDMTemplateVariableCodeHandler;
037    
038    import java.util.LinkedHashMap;
039    import java.util.List;
040    import java.util.Locale;
041    import java.util.Map;
042    
043    /**
044     * @author Jorge Ferrer
045     * @author Marcellus Tavares
046     */
047    public class DDLTemplateHandler extends BaseDDMTemplateHandler {
048    
049            @Override
050            public String getClassName() {
051                    return DDLRecordSet.class.getName();
052            }
053    
054            @Override
055            public String getName(Locale locale) {
056                    String portletTitle = PortalUtil.getPortletTitle(
057                            PortletKeys.DYNAMIC_DATA_LISTS, locale);
058    
059                    return portletTitle.concat(StringPool.SPACE).concat(
060                            LanguageUtil.get(locale, "template"));
061            }
062    
063            @Override
064            public String getResourceName() {
065                    return "com.liferay.portlet.dynamicdatalists.template";
066            }
067    
068            @Override
069            public Map<String, TemplateVariableGroup> getTemplateVariableGroups(
070                            long classPK, String language, Locale locale)
071                    throws Exception {
072    
073                    Map<String, TemplateVariableGroup> templateVariableGroups =
074                            new LinkedHashMap<String, TemplateVariableGroup>();
075    
076                    addTemplateVariableGroup(
077                            templateVariableGroups, getDDLVariablesTemplateVariableGroups());
078                    addTemplateVariableGroup(
079                            templateVariableGroups, getGeneralVariablesTemplateVariableGroup());
080    
081                    TemplateVariableGroup structureFieldsTemplateVariableGroup =
082                            getStructureFieldsTemplateVariableGroup(classPK, locale);
083    
084                    structureFieldsTemplateVariableGroup.setLabel(
085                            "data-list-record-fields");
086    
087                    addTemplateVariableGroup(
088                            templateVariableGroups, structureFieldsTemplateVariableGroup);
089    
090                    addTemplateVariableGroup(
091                            templateVariableGroups, getUtilTemplateVariableGroup());
092    
093                    String[] restrictedVariables = getRestrictedVariables(language);
094    
095                    TemplateVariableGroup ddlServicesTemplateVariableGroup =
096                            new TemplateVariableGroup(
097                                    "data-list-services", restrictedVariables);
098    
099                    ddlServicesTemplateVariableGroup.setAutocompleteEnabled(false);
100    
101                    ddlServicesTemplateVariableGroup.addServiceLocatorVariables(
102                            DDLRecordLocalService.class, DDLRecordService.class,
103                            DDLRecordSetLocalService.class, DDLRecordSetService.class,
104                            DDMStructureLocalService.class, DDMStructureService.class,
105                            DDMTemplateLocalService.class, DDMTemplateService.class);
106    
107                    templateVariableGroups.put(
108                            ddlServicesTemplateVariableGroup.getLabel(),
109                            ddlServicesTemplateVariableGroup);
110    
111                    return templateVariableGroups;
112            }
113    
114            protected TemplateVariableGroup getDDLVariablesTemplateVariableGroups() {
115                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
116                            "data-list-variables");
117    
118                    templateVariableGroup.addVariable(
119                            "data-definition-id", null, DDLConstants.RESERVED_DDM_STRUCTURE_ID);
120                    templateVariableGroup.addVariable(
121                            "data-list-description", String.class,
122                            DDLConstants.RESERVED_RECORD_SET_DESCRIPTION);
123                    templateVariableGroup.addVariable(
124                            "data-list-id", null, DDLConstants.RESERVED_RECORD_SET_ID);
125                    templateVariableGroup.addVariable(
126                            "data-list-name", String.class,
127                            DDLConstants.RESERVED_RECORD_SET_NAME);
128                    templateVariableGroup.addCollectionVariable(
129                            "data-list-records", List.class, "records", "record",
130                            DDLRecord.class, "cur_record", null);
131                    templateVariableGroup.addVariable(
132                            "template-id", null, DDLConstants.RESERVED_DDM_TEMPLATE_ID);
133    
134                    return templateVariableGroup;
135            }
136    
137            @Override
138            protected Class<?> getFieldVariableClass() {
139                    return Field.class;
140            }
141    
142            @Override
143            protected TemplateVariableCodeHandler getTemplateVariableCodeHandler() {
144                    return _templateVariableCodeHandler;
145            }
146    
147            private TemplateVariableCodeHandler _templateVariableCodeHandler =
148                    new DDMTemplateVariableCodeHandler(
149                            "com/liferay/portlet/dynamicdatalists/dependencies/template/");
150    
151    }