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, null, true, true,
031                            false, null, null, null, null, true, true, false, false);
032            }
033    
034            public EntityColumn(
035                    String name, String dbName, String type, boolean primary,
036                    boolean accessor, boolean filterPrimary, String ejbName,
037                    String mappingKey, String mappingTable, boolean caseSensitive,
038                    boolean orderByAscending, boolean orderColumn, String comparator,
039                    String arrayableOperator, String idType, String idParam,
040                    boolean convertNull, boolean lazy, boolean localized,
041                    boolean jsonEnabled) {
042    
043                    _name = name;
044                    _dbName = dbName;
045                    _type = type;
046                    _primary = primary;
047                    _accessor = accessor;
048                    _filterPrimary = filterPrimary;
049                    _humanName = ServiceBuilder.toHumanName(name);
050                    _methodName = TextFormatter.format(name, TextFormatter.G);
051                    _ejbName = ejbName;
052                    _mappingKey = mappingKey;
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            }
066    
067            public EntityColumn(
068                    String name, String dbName, String type, boolean primary,
069                    boolean accessor, boolean filterPrimary, String ejbName,
070                    String mappingKey, String mappingTable, String idType, String idParam,
071                    boolean convertNull, boolean lazy, boolean localized,
072                    boolean jsonEnabled) {
073    
074                    this(
075                            name, dbName, type, primary, accessor, filterPrimary, ejbName,
076                            mappingKey, mappingTable, true, true, false, null, null, idType,
077                            idParam, convertNull, lazy, localized, jsonEnabled);
078            }
079    
080            @Override
081            public Object clone() {
082                    return new EntityColumn(
083                            getName(), getDBName(), getType(), isPrimary(), isAccessor(),
084                            isFilterPrimary(), getEJBName(), getMappingKey(), getMappingTable(),
085                            isCaseSensitive(), isOrderByAscending(), isOrderColumn(),
086                            getComparator(), getArrayableOperator(), getIdType(), getIdParam(),
087                            isConvertNull(), isLazy(), isLocalized(), isJsonEnabled());
088            }
089    
090            @Override
091            public int compareTo(EntityColumn entityColumn) {
092                    return _name.compareTo(entityColumn._name);
093            }
094    
095            @Override
096            public boolean equals(Object obj) {
097                    if (this == obj) {
098                            return true;
099                    }
100    
101                    if (!(obj instanceof EntityColumn)) {
102                            return false;
103                    }
104    
105                    EntityColumn col = (EntityColumn)obj;
106    
107                    String name = col.getName();
108    
109                    if (_name.equals(name)) {
110                            return true;
111                    }
112                    else {
113                            return false;
114                    }
115            }
116    
117            public String getArrayableOperator() {
118                    return _arrayableOperator;
119            }
120    
121            public String getComparator() {
122                    return _comparator;
123            }
124    
125            public String getDBName() {
126                    return _dbName;
127            }
128    
129            public String getEJBName() {
130                    return _ejbName;
131            }
132    
133            public String getHumanCondition(boolean arrayable) {
134                    StringBundler sb = new StringBundler();
135    
136                    sb.append(_name);
137                    sb.append(" ");
138                    sb.append(convertComparatorToHtml(_comparator));
139                    sb.append(" ");
140    
141                    if (arrayable && hasArrayableOperator()) {
142                            if (isArrayableAndOperator()) {
143                                    sb.append("all ");
144                            }
145                            else {
146                                    sb.append("any ");
147                            }
148                    }
149    
150                    sb.append("&#63;");
151    
152                    return sb.toString();
153            }
154    
155            public String getHumanName() {
156                    return _humanName;
157            }
158    
159            public String getHumanNames() {
160                    return TextFormatter.formatPlural(getHumanName());
161            }
162    
163            public String getIdParam() {
164                    return _idParam;
165            }
166    
167            public String getIdType() {
168                    return _idType;
169            }
170    
171            public String getMappingKey() {
172                    return _mappingKey;
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 isConvertNull() {
252                    return _convertNull;
253            }
254    
255            public boolean isFilterPrimary() {
256                    return _filterPrimary;
257            }
258    
259            public boolean isFinderPath() {
260                    return _finderPath;
261            }
262    
263            public boolean isJsonEnabled() {
264                    return _jsonEnabled;
265            }
266    
267            public boolean isLazy() {
268                    return _lazy;
269            }
270    
271            public boolean isLocalized() {
272                    return _localized;
273            }
274    
275            public boolean isMappingManyToMany() {
276                    return Validator.isNotNull(_mappingTable);
277            }
278    
279            public boolean isMappingOneToMany() {
280                    return Validator.isNotNull(_mappingKey);
281            }
282    
283            public boolean isOrderByAscending() {
284                    return _orderByAscending;
285            }
286    
287            public boolean isOrderColumn() {
288                    return _orderColumn;
289            }
290    
291            public boolean isPrimary() {
292                    return _primary;
293            }
294    
295            public boolean isPrimitiveType() {
296                    return isPrimitiveType(true);
297            }
298    
299            public boolean isPrimitiveType(boolean includeWrappers) {
300                    if (Character.isLowerCase(_type.charAt(0))) {
301                            return true;
302                    }
303    
304                    if (!includeWrappers) {
305                            return false;
306                    }
307    
308                    if (_type.equals("Boolean")) {
309                            return true;
310                    }
311                    else if (_type.equals("Double")) {
312                            return true;
313                    }
314                    else if (_type.equals("Float")) {
315                            return true;
316                    }
317                    else if (_type.equals("Integer")) {
318                            return true;
319                    }
320                    else if (_type.equals("Long")) {
321                            return true;
322                    }
323                    else if (_type.equals("Short")) {
324                            return true;
325                    }
326                    else {
327                            return false;
328                    }
329            }
330    
331            public boolean isUserUuid() {
332                    if (_type.equals("long") && _methodName.endsWith("UserId")) {
333                            return true;
334                    }
335                    else {
336                            return false;
337                    }
338            }
339    
340            public void setArrayableOperator(String arrayableOperator) {
341                    _arrayableOperator = arrayableOperator.toUpperCase();
342            }
343    
344            public void setCaseSensitive(boolean caseSensitive) {
345                    _caseSensitive = caseSensitive;
346            }
347    
348            public void setComparator(String comparator) {
349                    _comparator = comparator;
350            }
351    
352            public void setConvertNull(boolean convertNull) {
353                    _convertNull = convertNull;
354            }
355    
356            public void setDBName(String dbName) {
357                    _dbName = dbName;
358            }
359    
360            public void setFinderPath(boolean finderPath) {
361                    _finderPath = finderPath;
362            }
363    
364            public void setIdParam(String idParam) {
365                    _idParam = idParam;
366            }
367    
368            public void setIdType(String idType) {
369                    _idType = idType;
370            }
371    
372            public void setLazy(boolean lazy) {
373                    _lazy = lazy;
374            }
375    
376            public void setLocalized(boolean localized) {
377                    _localized = localized;
378            }
379    
380            public void setOrderByAscending(boolean orderByAscending) {
381                    _orderByAscending = orderByAscending;
382            }
383    
384            public void setOrderColumn(boolean orderColumn) {
385                    _orderColumn = orderColumn;
386            }
387    
388            protected String convertComparatorToHtml(String comparator) {
389                    if (comparator.equals(">")) {
390                            return "&gt;";
391                    }
392    
393                    if (comparator.equals("<")) {
394                            return "&lt;";
395                    }
396    
397                    if (comparator.equals(">=")) {
398                            return "&ge;";
399                    }
400    
401                    if (comparator.equals("<=")) {
402                            return "&le;";
403                    }
404    
405                    if (comparator.equals("!=")) {
406                            return "&ne;";
407                    }
408    
409                    return comparator;
410            }
411    
412            private boolean _accessor;
413            private String _arrayableOperator;
414            private boolean _caseSensitive;
415            private String _comparator;
416            private boolean _convertNull;
417            private String _dbName;
418            private String _ejbName;
419            private boolean _filterPrimary;
420            private boolean _finderPath;
421            private String _humanName;
422            private String _idParam;
423            private String _idType;
424            private boolean _jsonEnabled;
425            private boolean _lazy;
426            private boolean _localized;
427            private String _mappingKey;
428            private String _mappingTable;
429            private String _methodName;
430            private String _name;
431            private boolean _orderByAscending;
432            private boolean _orderColumn;
433            private boolean _primary;
434            private String _type;
435    
436    }