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                    TemplateVariableGroup ddlServicesTemplateVariableGroup =
094                            new TemplateVariableGroup("data-list-services");
095    
096                    ddlServicesTemplateVariableGroup.setAutocompleteEnabled(false);
097    
098                    ddlServicesTemplateVariableGroup.addServiceLocatorVariables(
099                            DDLRecordLocalService.class, DDLRecordService.class,
100                            DDLRecordSetLocalService.class, DDLRecordSetService.class,
101                            DDMStructureLocalService.class, DDMStructureService.class,
102                            DDMTemplateLocalService.class, DDMTemplateService.class);
103    
104                    templateVariableGroups.put(
105                            ddlServicesTemplateVariableGroup.getLabel(),
106                            ddlServicesTemplateVariableGroup);
107    
108                    return templateVariableGroups;
109            }
110    
111            protected TemplateVariableGroup getDDLVariablesTemplateVariableGroups() {
112                    TemplateVariableGroup templateVariableGroup = new TemplateVariableGroup(
113                            "data-list-variables");
114    
115                    templateVariableGroup.addVariable(
116                            "data-definition-id", null, DDLConstants.RESERVED_DDM_STRUCTURE_ID);
117                    templateVariableGroup.addVariable(
118                            "data-list-description", String.class,
119                            DDLConstants.RESERVED_RECORD_SET_DESCRIPTION);
120                    templateVariableGroup.addVariable(
121                            "data-list-id", null, DDLConstants.RESERVED_RECORD_SET_ID);
122                    templateVariableGroup.addVariable(
123                            "data-list-name", String.class,
124                            DDLConstants.RESERVED_RECORD_SET_NAME);
125                    templateVariableGroup.addCollectionVariable(
126                            "data-list-records", List.class, "records", "record",
127                            DDLRecord.class, "cur_record", null);
128                    templateVariableGroup.addVariable(
129                            "template-id", null, DDLConstants.RESERVED_DDM_TEMPLATE_ID);
130    
131                    return templateVariableGroup;
132            }
133    
134            @Override
135            protected Class<?> getFieldVariableClass() {
136                    return Field.class;
137            }
138    
139            @Override
140            protected TemplateVariableCodeHandler getTemplateVariableCodeHandler() {
141                    return _templateVariableCodeHandler;
142            }
143    
144            private TemplateVariableCodeHandler _templateVariableCodeHandler =
145                    new DDMTemplateVariableCodeHandler(
146                            "com/liferay/portlet/dynamicdatalists/dependencies/template/");
147    
148    }