001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools.servicebuilder;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.util.TextFormatter;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Charles May
024     */
025    public class EntityColumn implements Cloneable {
026    
027            public EntityColumn(String name) {
028                    this(
029                            name, null, null, false, false, null, null, null, true, true, null,
030                            null, null, null, true, false);
031            }
032    
033            public EntityColumn(
034                    String name, String dbName, String type, boolean primary,
035                    boolean filterPrimary, String ejbName, String mappingKey,
036                    String mappingTable, boolean caseSensitive, boolean orderByAscending,
037                    String comparator, String arrayableOperator, String idType,
038                    String idParam, boolean convertNull, boolean localized) {
039    
040                    _name = name;
041                    _dbName = dbName;
042                    _type = type;
043                    _primary = primary;
044                    _filterPrimary = filterPrimary;
045                    _humanName = TextFormatter.format(name, TextFormatter.H);
046                    _methodName = TextFormatter.format(name, TextFormatter.G);
047                    _ejbName = ejbName;
048                    _mappingKey = mappingKey;
049                    _mappingTable = mappingTable;
050                    _caseSensitive = caseSensitive;
051                    _orderByAscending = orderByAscending;
052                    _comparator = comparator;
053                    _arrayableOperator = arrayableOperator;
054                    _idType = idType;
055                    _idParam = idParam;
056                    _convertNull = convertNull;
057                    _localized = localized;
058            }
059    
060            public EntityColumn(
061                    String name, String dbName, String type, boolean primary,
062                    boolean filterPrimary, String ejbName, String mappingKey,
063                    String mappingTable, String idType, String idParam, boolean convertNull,
064                    boolean localized) {
065    
066                    this(
067                            name, dbName, type, primary, filterPrimary, ejbName, mappingKey,
068                            mappingTable, true, true, null, null, idType, idParam, convertNull,
069                            localized);
070            }
071    
072            public Object clone() {
073                    return new EntityColumn(
074                            getName(), getDBName(), getType(), isPrimary(), isFilterPrimary(),
075                            getEJBName(), getMappingKey(), getMappingTable(), isCaseSensitive(),
076                            isOrderByAscending(), getComparator(), getArrayableOperator(),
077                            getIdType(), getIdParam(), isConvertNull(), isLocalized());
078            }
079    
080            public boolean equals(Object obj) {
081                    EntityColumn col = (EntityColumn)obj;
082    
083                    String name = col.getName();
084    
085                    if (_name.equals(name)) {
086                            return true;
087                    }
088                    else {
089                            return false;
090                    }
091            }
092    
093            public String getArrayableOperator() {
094                    return _arrayableOperator;
095            }
096    
097            public String getComparator() {
098                    return _comparator;
099            }
100    
101            public String getDBName() {
102                    return _dbName;
103            }
104    
105            public String getHumanCondition(boolean arrayable) {
106                    StringBundler sb = new StringBundler();
107    
108                    sb.append(_name);
109                    sb.append(" ");
110                    sb.append(convertComparatorToHtml(_comparator));
111                    sb.append(" ");
112    
113                    if (arrayable && hasArrayableOperator()) {
114                            if (isArrayableAndOperator()) {
115                                    sb.append("all ");
116                            }
117                            else {
118                                    sb.append("any ");
119                            }
120                    }
121    
122                    sb.append("?");
123    
124                    return sb.toString();
125            }
126    
127            public String getHumanName() {
128                    return _humanName;
129            }
130    
131            public String getHumanNames() {
132                    return TextFormatter.formatPlural(getHumanName());
133            }
134    
135            public String getEJBName() {
136                    return _ejbName;
137            }
138    
139            public String getIdParam() {
140                    return _idParam;
141            }
142    
143            public String getIdType() {
144                    return _idType;
145            }
146    
147            public String getMappingKey() {
148                    return _mappingKey;
149            }
150    
151            public String getMappingTable() {
152                    return _mappingTable;
153            }
154    
155            public String getMethodName() {
156                    return _methodName;
157            }
158    
159            public String getMethodNames() {
160                    return TextFormatter.formatPlural(_methodName);
161            }
162    
163            public String getMethodUserUuidName() {
164                    return _methodName.substring(0, _methodName.length() - 2) + "Uuid";
165            }
166    
167            public String getName() {
168                    return _name;
169            }
170    
171            public String getNames() {
172                    return TextFormatter.formatPlural(_name);
173            }
174    
175            public String getType() {
176                    return _type;
177            }
178    
179            public String getUserUuidHumanName() {
180                    return TextFormatter.format(getUserUuidName(), TextFormatter.H);
181            }
182    
183            public String getUserUuidName() {
184                    return _name.substring(0, _name.length() - 2) + "Uuid";
185            }
186    
187            public boolean hasArrayableOperator() {
188                    if (Validator.isNotNull(_arrayableOperator)) {
189                            return true;
190                    }
191                    else {
192                            return false;
193                    }
194            }
195    
196            public int hashCode() {
197                    return _name.hashCode();
198            }
199    
200            public boolean isArrayableAndOperator() {
201                    if (_arrayableOperator.equals("AND")) {
202                            return true;
203                    }
204                    else {
205                            return false;
206                    }
207            }
208    
209            public boolean isCaseSensitive() {
210                    return _caseSensitive;
211            }
212    
213            public boolean isCollection() {
214                    if (_type.equals("Collection")) {
215                            return true;
216                    }
217                    else {
218                            return false;
219                    }
220            }
221    
222            public boolean isConvertNull() {
223                    return _convertNull;
224            }
225    
226            public boolean isFetchFinderPath() {
227                    return _fetchFinderPath;
228            }
229    
230            public boolean isFilterPrimary() {
231                    return _filterPrimary;
232            }
233    
234            public boolean isLocalized() {
235                    return _localized;
236            }
237    
238            public boolean isMappingManyToMany() {
239                    return Validator.isNotNull(_mappingTable);
240            }
241    
242            public boolean isMappingOneToMany() {
243                    return Validator.isNotNull(_mappingKey);
244            }
245    
246            public boolean isOrderByAscending() {
247                    return _orderByAscending;
248            }
249    
250            public boolean isPrimary() {
251                    return _primary;
252            }
253    
254            public boolean isPrimitiveType() {
255                    return isPrimitiveType(true);
256            }
257    
258            public boolean isPrimitiveType(boolean includeWrappers) {
259                    if (Character.isLowerCase(_type.charAt(0))) {
260                            return true;
261                    }
262    
263                    if (!includeWrappers) {
264                            return false;
265                    }
266    
267                    if (_type.equals("Boolean")) {
268                            return true;
269                    }
270                    else if (_type.equals("Double")) {
271                            return true;
272                    }
273                    else if (_type.equals("Float")) {
274                            return true;
275                    }
276                    else if (_type.equals("Integer")) {
277                            return true;
278                    }
279                    else if (_type.equals("Long")) {
280                            return true;
281                    }
282                    else if (_type.equals("Short")) {
283                            return true;
284                    }
285                    else {
286                            return false;
287                    }
288            }
289    
290            public boolean isUserUuid() {
291                    if (_type.equals("long") && _methodName.endsWith("UserId")) {
292                            return true;
293                    }
294                    else {
295                            return false;
296                    }
297            }
298    
299            public void setArrayableOperator(String arrayableOperator) {
300                    _arrayableOperator = arrayableOperator.toUpperCase();
301            }
302    
303            public void setCaseSensitive(boolean caseSensitive) {
304                    _caseSensitive = caseSensitive;
305            }
306    
307            public void setComparator(String comparator) {
308                    _comparator = comparator;
309            }
310    
311            public void setConvertNull(boolean convertNull) {
312                    _convertNull = convertNull;
313            }
314    
315            public void setDBName(String dbName) {
316                    _dbName = dbName;
317            }
318    
319            public void setFetchFinderPath(boolean fetchFinderPath) {
320                    _fetchFinderPath = fetchFinderPath;
321            }
322    
323            public void setIdParam(String idParam) {
324                    _idParam = idParam;
325            }
326    
327            public void setIdType(String idType) {
328                    _idType = idType;
329            }
330    
331            public void setLocalized(boolean localized) {
332                    _localized = localized;
333            }
334    
335            public void setOrderByAscending(boolean orderByAscending) {
336                    _orderByAscending = orderByAscending;
337            }
338    
339            protected String convertComparatorToHtml(String comparator) {
340                    if (comparator.equals(">")) {
341                            return ">";
342                    }
343    
344                    if (comparator.equals("<")) {
345                            return "&lt;";
346                    }
347    
348                    if (comparator.equals(">=")) {
349                            return "&ge;";
350                    }
351    
352                    if (comparator.equals("<=")) {
353                            return "&le;";
354                    }
355    
356                    if (comparator.equals("!=")) {
357                            return "&ne;";
358                    }
359    
360                    return comparator;
361            }
362    
363            private String _arrayableOperator;
364            private boolean _caseSensitive;
365            private String _comparator;
366            private boolean _convertNull;
367            private String _dbName;
368            private String _ejbName;
369            private boolean _fetchFinderPath;
370            private boolean _filterPrimary;
371            private String _humanName;
372            private String _idParam;
373            private String _idType;
374            private boolean _localized;
375            private String _mappingKey;
376            private String _mappingTable;
377            private String _methodName;
378            private String _name;
379            private boolean _orderByAscending;
380            private boolean _primary;
381            private String _type;
382    
383    }