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