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