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