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.io.unsync.UnsyncStringWriter;
018    import com.liferay.portal.kernel.template.ClassLoaderTemplateResource;
019    import com.liferay.portal.kernel.template.Template;
020    import com.liferay.portal.kernel.template.TemplateConstants;
021    import com.liferay.portal.kernel.template.TemplateManagerUtil;
022    import com.liferay.portal.kernel.template.TemplateResource;
023    import com.liferay.portal.kernel.template.TemplateVariableCodeHandler;
024    import com.liferay.portal.kernel.template.TemplateVariableDefinition;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    
029    import java.io.Writer;
030    
031    /**
032     * @author Marcellus Tavares
033     */
034    public class DDMTemplateVariableCodeHandler
035            implements TemplateVariableCodeHandler {
036    
037            public DDMTemplateVariableCodeHandler(
038                    ClassLoader classLoader, String templatePath) {
039    
040                    _classLoader = classLoader;
041                    _templatePath = templatePath;
042            }
043    
044            @Override
045            public String[] generate(
046                            TemplateVariableDefinition templateVariableDefinition,
047                            String language)
048                    throws Exception {
049    
050                    String resourceName = getResourceName(
051                            templateVariableDefinition.getDataType());
052    
053                    Template template = getTemplate(resourceName);
054    
055                    String content = getTemplateContent(
056                            template, templateVariableDefinition, language);
057    
058                    if (templateVariableDefinition.isRepeatable()) {
059                            content = handleRepeatableField(
060                                    templateVariableDefinition, language, content);
061                    }
062    
063                    return new String[] {content};
064            }
065    
066            protected String getResourceName(String dataType) {
067                    if (isCommonResource(dataType)) {
068                            dataType = "common";
069                    }
070    
071                    StringBundler sb = new StringBundler(3);
072    
073                    sb.append(_templatePath);
074                    sb.append(dataType);
075                    sb.append(".ftl");
076    
077                    return sb.toString();
078            }
079    
080            protected Template getTemplate(String resource) throws Exception {
081                    TemplateResource templateResource = new ClassLoaderTemplateResource(
082                            _classLoader, resource);
083    
084                    return TemplateManagerUtil.getTemplate(
085                            TemplateConstants.LANG_TYPE_FTL, templateResource, false);
086            }
087    
088            protected String getTemplateContent(
089                            Template template,
090                            TemplateVariableDefinition templateVariableDefinition,
091                            String language)
092                    throws Exception {
093    
094                    prepareTemplate(template, templateVariableDefinition, language);
095    
096                    Writer writer = new UnsyncStringWriter();
097    
098                    template.processTemplate(writer);
099    
100                    return StringUtil.trim(writer.toString());
101            }
102    
103            protected String handleRepeatableField(
104                            TemplateVariableDefinition templateVariableDefinition,
105                            String language, String templateContent)
106                    throws Exception {
107    
108                    Template template = getTemplate(_templatePath + "repeatable.ftl");
109    
110                    templateContent = StringUtil.replace(
111                            templateContent, StringPool.NEW_LINE,
112                            StringPool.NEW_LINE + StringPool.TAB + StringPool.TAB);
113    
114                    template.put("templateContent", templateContent);
115    
116                    return getTemplateContent(
117                            template, templateVariableDefinition, language);
118            }
119    
120            protected boolean isCommonResource(String dataType) {
121                    if (dataType.equals("boolean") || dataType.equals("date") ||
122                            dataType.equals("document-library") || dataType.equals("image") ||
123                            dataType.equals("link-to-page")) {
124    
125                            return false;
126                    }
127    
128                    return true;
129            }
130    
131            protected void prepareTemplate(
132                    Template template,
133                    TemplateVariableDefinition templateVariableDefinition,
134                    String language) {
135    
136                    template.put("dataType", templateVariableDefinition.getDataType());
137                    template.put("help", templateVariableDefinition.getHelp());
138                    template.put("label", templateVariableDefinition.getLabel());
139                    template.put("language", language);
140                    template.put("name", templateVariableDefinition.getName());
141                    template.put("repeatable", templateVariableDefinition.isRepeatable());
142            }
143    
144            private final ClassLoader _classLoader;
145            private final String _templatePath;
146    
147    }