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