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.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                    this(
039                            label, clazz, dataType, name, accessor, help, repeatable,
040                            templateVariableCodeHandler, null);
041            }
042    
043            public TemplateVariableDefinition(
044                    String label, Class<?> clazz, String name,
045                    TemplateVariableDefinition itemTemplateVariableDefinition) {
046    
047                    this(
048                            label, clazz, StringPool.BLANK, name, StringPool.BLANK,
049                            label.concat("-help"), false, null, itemTemplateVariableDefinition);
050            }
051    
052            @Override
053            public boolean equals(Object obj) {
054                    if (this == obj) {
055                            return true;
056                    }
057    
058                    if (!(obj instanceof TemplateVariableDefinition)) {
059                            return false;
060                    }
061    
062                    TemplateVariableDefinition templateVariableDefinition =
063                            (TemplateVariableDefinition)obj;
064    
065                    if (Validator.equals(_name, templateVariableDefinition._name) &&
066                            Validator.equals(_accessor, templateVariableDefinition._accessor)) {
067    
068                            return true;
069                    }
070    
071                    return false;
072            }
073    
074            public String[] generateCode(String language) throws Exception {
075                    if (_templateVariableCodeHandler == null) {
076                            return null;
077                    }
078    
079                    return _templateVariableCodeHandler.generate(this, language);
080            }
081    
082            public String getAccessor() {
083                    return _accessor;
084            }
085    
086            public Class<?> getClazz() {
087                    return _clazz;
088            }
089    
090            public String getDataType() {
091                    return _dataType;
092            }
093    
094            public String getHelp() {
095                    return _help;
096            }
097    
098            public TemplateVariableDefinition getItemTemplateVariableDefinition() {
099                    return _itemTemplateVariableDefinition;
100            }
101    
102            public String getLabel() {
103                    return _label;
104            }
105    
106            public String getName() {
107                    return _name;
108            }
109    
110            public TemplateVariableCodeHandler getTemplateVariableCodeHandler() {
111                    return _templateVariableCodeHandler;
112            }
113    
114            public boolean isCollection() {
115                    if (_itemTemplateVariableDefinition != null) {
116                            return true;
117                    }
118    
119                    return false;
120            }
121    
122            public boolean isRepeatable() {
123                    return _repeatable;
124            }
125    
126            protected TemplateVariableDefinition(
127                    String label, Class<?> clazz, String dataType, String name,
128                    String accessor, String help, boolean repeatable,
129                    TemplateVariableCodeHandler templateVariableCodeHandler,
130                    TemplateVariableDefinition itemTemplateVariableDefinition) {
131    
132                    _label = label;
133                    _clazz = clazz;
134                    _dataType = dataType;
135                    _name = name;
136                    _accessor = accessor;
137                    _help = help;
138                    _repeatable = repeatable;
139                    _templateVariableCodeHandler = templateVariableCodeHandler;
140                    _itemTemplateVariableDefinition = itemTemplateVariableDefinition;
141            }
142    
143            private final String _accessor;
144            private final Class<?> _clazz;
145            private final String _dataType;
146            private final String _help;
147            private final TemplateVariableDefinition _itemTemplateVariableDefinition;
148            private final String _label;
149            private final String _name;
150            private final boolean _repeatable;
151            private TemplateVariableCodeHandler _templateVariableCodeHandler;
152    
153    }