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.portal.kernel.template;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    /**
021     * @author Jorge Ferrer
022     */
023    public class TemplateVariableDefinition {
024    
025            public TemplateVariableDefinition(
026                    String label, Class<?> clazz, String name, String accessor) {
027    
028                    this(
029                            label, clazz, StringPool.BLANK, name, accessor,
030                            label.concat("-help"), false, null);
031            }
032    
033            public TemplateVariableDefinition(
034                    String label, Class<?> clazz, String dataType, String name,
035                    String accessor, String help, boolean repeatable,
036                    TemplateVariableCodeHandler templateVariableCodeHandler) {
037    
038                    _label = label;
039                    _clazz = clazz;
040                    _dataType = dataType;
041                    _name = name;
042                    _accessor = accessor;
043                    _help = help;
044                    _repeatable = repeatable;
045                    _templateVariableCodeHandler = templateVariableCodeHandler;
046            }
047    
048            public TemplateVariableDefinition(
049                    String label, Class<?> clazz, String name,
050                    TemplateVariableDefinition itemTemplateVariableDefinition) {
051    
052                    this(label, clazz, name, StringPool.BLANK);
053    
054                    _itemTemplateVariableDefinition = itemTemplateVariableDefinition;
055            }
056    
057            @Override
058            public boolean equals(Object obj) {
059                    if (this == obj) {
060                            return true;
061                    }
062    
063                    if (!(obj instanceof TemplateVariableDefinition)) {
064                            return false;
065                    }
066    
067                    TemplateVariableDefinition templateVariableDefinition =
068                            (TemplateVariableDefinition)obj;
069    
070                    if (Validator.equals(_name, templateVariableDefinition._name) &&
071                            Validator.equals(_accessor, templateVariableDefinition._accessor)) {
072    
073                            return true;
074                    }
075    
076                    return false;
077            }
078    
079            public String[] generateCode(String language) throws Exception {
080                    if (_templateVariableCodeHandler == null) {
081                            return null;
082                    }
083    
084                    return _templateVariableCodeHandler.generate(this, language);
085            }
086    
087            public String getAccessor() {
088                    return _accessor;
089            }
090    
091            public Class<?> getClazz() {
092                    return _clazz;
093            }
094    
095            public String getDataType() {
096                    return _dataType;
097            }
098    
099            public String getHelp() {
100                    return _help;
101            }
102    
103            public TemplateVariableDefinition getItemTemplateVariableDefinition() {
104                    return _itemTemplateVariableDefinition;
105            }
106    
107            public String getLabel() {
108                    return _label;
109            }
110    
111            public String getName() {
112                    return _name;
113            }
114    
115            public TemplateVariableCodeHandler getTemplateVariableCodeHandler() {
116                    return _templateVariableCodeHandler;
117            }
118    
119            public boolean isCollection() {
120                    if (_itemTemplateVariableDefinition != null) {
121                            return true;
122                    }
123    
124                    return false;
125            }
126    
127            public boolean isRepeatable() {
128                    return _repeatable;
129            }
130    
131            private String _accessor;
132            private Class<?> _clazz;
133            private String _dataType;
134            private String _help;
135            private TemplateVariableDefinition _itemTemplateVariableDefinition;
136            private String _label;
137            private String _name;
138            private boolean _repeatable;
139            private TemplateVariableCodeHandler _templateVariableCodeHandler;
140    
141    }