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