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.tools.servicebuilder;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.TextFormatter;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     * @author Charles May
024     * @author Shuyang Zhou
025     */
026    public class EntityColumn implements Cloneable, Comparable<EntityColumn> {
027    
028            public EntityColumn(String name) {
029                    this(
030                            name, null, null, false, false, false, null, null, true, true,
031                            false, null, null, null, null, true, true, false, false, false,
032                            false);
033            }
034    
035            public EntityColumn(
036                    String name, String dbName, String type, boolean primary,
037                    boolean accessor, boolean filterPrimary, String ejbName,
038                    String mappingTable, boolean caseSensitive, boolean orderByAscending,
039                    boolean orderColumn, String comparator, String arrayableOperator,
040                    String idType, String idParam, boolean convertNull, boolean lazy,
041                    boolean localized, boolean jsonEnabled, boolean containerModel,
042                    boolean parentContainerModel) {
043    
044                    _name = name;
045                    _dbName = dbName;
046                    _type = type;
047                    _primary = primary;
048                    _accessor = accessor;
049                    _filterPrimary = filterPrimary;
050                    _humanName = ServiceBuilder.toHumanName(name);
051                    _methodName = TextFormatter.format(name, TextFormatter.G);
052                    _ejbName = ejbName;
053                    _mappingTable = mappingTable;
054                    _caseSensitive = caseSensitive;
055                    _orderByAscending = orderByAscending;
056                    _orderColumn = orderColumn;
057                    _comparator = comparator;
058                    _arrayableOperator = arrayableOperator;
059                    _idType = idType;
060                    _idParam = idParam;
061                    _convertNull = convertNull;
062                    _lazy = lazy;
063                    _localized = localized;
064                    _jsonEnabled = jsonEnabled;
065                    _containerModel = containerModel;
066                    _parentContainerModel = parentContainerModel;
067            }
068    
069            public EntityColumn(
070                    String name, String dbName, String type, boolean primary,
071                    boolean accessor, boolean filterPrimary, String ejbName,
072                    String mappingTable, String idType, String idParam, boolean convertNull,
073                    boolean lazy, boolean localized, boolean jsonEnabled,
074                    boolean containerModel, boolean parentContainerModel) {
075    
076                    this(
077                            name, dbName, type, primary, accessor, filterPrimary, ejbName,
078                            mappingTable, true, true, false, null, null, idType, idParam,
079                            convertNull, lazy, localized, jsonEnabled, containerModel,
080                            parentContainerModel);
081            }
082    
083            @Override
084            public Object clone() {
085                    return new EntityColumn(
086                            getName(), getDBName(), getType(), isPrimary(), isAccessor(),
087                            isFilterPrimary(), getEJBName(), getMappingTable(),
088                            isCaseSensitive(), isOrderByAscending(), isOrderColumn(),
089                            getComparator(), getArrayableOperator(), getIdType(), getIdParam(),
090                            isConvertNull(), isLazy(), isLocalized(), isJsonEnabled(),
091                            isContainerModel(), isParentContainerModel());
092            }
093    
094            @Override
095            public int compareTo(EntityColumn entityColumn) {
096                    return _name.compareTo(entityColumn._name);
097            }
098    
099            @Override
100            public boolean equals(Object obj) {
101                    if (this == obj) {
102                            return true;
103                    }
104    
105                    if (!(obj instanceof EntityColumn)) {
106                            return false;
107                    }
108    
109                    EntityColumn col = (EntityColumn)obj;
110    
111                    String name = col.getName();
112    
113                    if (_name.equals(name)) {
114                            return true;
115                    }
116                    else {
117                            return false;
118                    }
119            }
120    
121            public String getArrayableOperator() {
122                    return _arrayableOperator;
123            }
124    
125            public String getComparator() {
126                    return _comparator;
127            }
128    
129            public String getDBName() {
130                    return _dbName;
131            }
132    
133            public String getEJBName() {
134                    return _ejbName;
135            }
136    
137            public String getHumanCondition(boolean arrayable) {
138                    StringBundler sb = new StringBundler();
139    
140                    sb.append(_name);
141                    sb.append(" ");
142                    sb.append(convertComparatorToHtml(_comparator));
143                    sb.append(" ");
144    
145                    if (arrayable && hasArrayableOperator()) {
146                            if (isArrayableAndOperator()) {
147                                    sb.append("all ");
148                            }
149                            else {
150                                    sb.append("any ");
151                            }
152                    }
153    
154                    sb.append("&#63;");
155    
156                    return sb.toString();
157            }
158    
159            public String getHumanName() {
160                    return _humanName;
161            }
162    
163            public String getHumanNames() {
164                    return TextFormatter.formatPlural(getHumanName());
165            }
166    
167            public String getIdParam() {
168                    return _idParam;
169            }
170    
171            public String getIdType() {
172                    return _idType;
173            }
174    
175            public String getMappingTable() {
176                    return _mappingTable;
177            }
178    
179            public String getMethodName() {
180                    return _methodName;
181            }
182    
183            public String getMethodNames() {
184                    return TextFormatter.formatPlural(_methodName);
185            }
186    
187            public String getMethodUserUuidName() {
188                    return _methodName.substring(0, _methodName.length() - 2) + "Uuid";
189            }
190    
191            public String getName() {
192                    return _name;
193            }
194    
195            public String getNames() {
196                    return TextFormatter.formatPlural(_name);
197            }
198    
199            public String getType() {
200                    return _type;
201            }
202    
203            public String getUserUuidHumanName() {
204                    return ServiceBuilder.toHumanName(getUserUuidName());
205            }
206    
207            public String getUserUuidName() {
208                    return _name.substring(0, _name.length() - 2) + "Uuid";
209            }
210    
211            public boolean hasArrayableOperator() {
212                    if (Validator.isNotNull(_arrayableOperator)) {
213                            return true;
214                    }
215                    else {
216                            return false;
217                    }
218            }
219    
220            @Override
221            public int hashCode() {
222                    return _name.hashCode();
223            }
224    
225            public boolean isAccessor() {
226                    return _accessor;
227            }
228    
229            public boolean isArrayableAndOperator() {
230                    if (_arrayableOperator.equals("AND")) {
231                            return true;
232                    }
233                    else {
234                            return false;
235                    }
236            }
237    
238            public boolean isCaseSensitive() {
239                    return _caseSensitive;
240            }
241    
242            public boolean isCollection() {
243                    if (_type.equals("Collection")) {
244                            return true;
245                    }
246                    else {
247                            return false;
248                    }
249            }
250    
251            public boolean isContainerModel() {
252                    return _containerModel;
253            }
254    
255            public boolean isConvertNull() {
256                    return _convertNull;
257            }
258    
259            public boolean isFilterPrimary() {
260                    return _filterPrimary;
261            }
262    
263            public boolean isFinderPath() {
264                    return _finderPath;
265            }
266    
267            public boolean isJsonEnabled() {
268                    return _jsonEnabled;
269            }
270    
271            public boolean isLazy() {
272                    return _lazy;
273            }
274    
275            public boolean isLocalized() {
276                    return _localized;
277            }
278    
279            public boolean isMappingManyToMany() {
280                    return Validator.isNotNull(_mappingTable);
281            }
282    
283            public boolean isOrderByAscending() {
284                    return _orderByAscending;
285            }
286    
287            public boolean isOrderColumn() {
288                    return _orderColumn;
289            }
290    
291            public boolean isParentContainerModel() {
292                    return _parentContainerModel;
293            }
294    
295            public boolean isPrimary() {
296                    return _primary;
297            }
298    
299            public boolean isPrimitiveType() {
300                    return isPrimitiveType(true);
301            }
302    
303            public boolean isPrimitiveType(boolean includeWrappers) {
304                    if (Character.isLowerCase(_type.charAt(0))) {
305                            return true;
306                    }
307    
308                    if (!includeWrappers) {
309                            return false;
310                    }
311    
312                    if (_type.equals("Boolean")) {
313                            return true;
314                    }
315                    else if (_type.equals("Byte")) {
316                            return true;
317                    }
318                    else if (_type.equals("Char")) {
319                            return true;
320                    }
321                    else if (_type.equals("Double")) {
322                            return true;
323                    }
324                    else if (_type.equals("Float")) {
325                            return true;
326                    }
327                    else if (_type.equals("Integer")) {
328                            return true;
329                    }
330                    else if (_type.equals("Long")) {
331                            return true;
332                    }
333                    else if (_type.equals("Short")) {
334                            return true;
335                    }
336                    else {
337                            return false;
338                    }
339            }
340    
341            public boolean isUserUuid() {
342                    if (_type.equals("long") && _methodName.endsWith("UserId")) {
343                            return true;
344                    }
345                    else {
346                            return false;
347                    }
348            }
349    
350            public void setArrayableOperator(String arrayableOperator) {
351                    _arrayableOperator = arrayableOperator.toUpperCase();
352            }
353    
354            public void setCaseSensitive(boolean caseSensitive) {
355                    _caseSensitive = caseSensitive;
356            }
357    
358            public void setComparator(String comparator) {
359                    _comparator = comparator;
360            }
361    
362            public void setContainerModel(boolean containerModel) {
363                    _containerModel = containerModel;
364            }
365    
366            public void setConvertNull(boolean convertNull) {
367                    _convertNull = convertNull;
368            }
369    
370            public void setDBName(String dbName) {
371                    _dbName = dbName;
372            }
373    
374            public void setFinderPath(boolean finderPath) {
375                    _finderPath = finderPath;
376            }
377    
378            public void setIdParam(String idParam) {
379                    _idParam = idParam;
380            }
381    
382            public void setIdType(String idType) {
383                    _idType = idType;
384            }
385    
386            public void setLazy(boolean lazy) {
387                    _lazy = lazy;
388            }
389    
390            public void setLocalized(boolean localized) {
391                    _localized = localized;
392            }
393    
394            public void setOrderByAscending(boolean orderByAscending) {
395                    _orderByAscending = orderByAscending;
396            }
397    
398            public void setOrderColumn(boolean orderColumn) {
399                    _orderColumn = orderColumn;
400            }
401    
402            public void setParentContainerModel(boolean parentContainerModel) {
403                    _parentContainerModel = parentContainerModel;
404            }
405    
406            protected String convertComparatorToHtml(String comparator) {
407                    if (comparator.equals(">")) {
408                            return "&gt;";
409                    }
410    
411                    if (comparator.equals("<")) {
412                            return "&lt;";
413                    }
414    
415                    if (comparator.equals(">=")) {
416                            return "&ge;";
417                    }
418    
419                    if (comparator.equals("<=")) {
420                            return "&le;";
421                    }
422    
423                    if (comparator.equals("!=")) {
424                            return "&ne;";
425                    }
426    
427                    return comparator;
428            }
429    
430            private boolean _accessor;
431            private String _arrayableOperator;
432            private boolean _caseSensitive;
433            private String _comparator;
434            private boolean _containerModel;
435            private boolean _convertNull;
436            private String _dbName;
437            private String _ejbName;
438            private boolean _filterPrimary;
439            private boolean _finderPath;
440            private String _humanName;
441            private String _idParam;
442            private String _idType;
443            private boolean _jsonEnabled;
444            private boolean _lazy;
445            private boolean _localized;
446            private String _mappingTable;
447            private String _methodName;
448            private String _name;
449            private boolean _orderByAscending;
450            private boolean _orderColumn;
451            private boolean _parentContainerModel;
452            private boolean _primary;
453            private String _type;
454    
455    }