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, 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            public int compareTo(EntityColumn entityColumn) {
095                    return _name.compareTo(entityColumn._name);
096            }
097    
098            @Override
099            public boolean equals(Object obj) {
100                    EntityColumn col = (EntityColumn)obj;
101    
102                    String name = col.getName();
103    
104                    if (_name.equals(name)) {
105                            return true;
106                    }
107                    else {
108                            return false;
109                    }
110            }
111    
112            public String getArrayableOperator() {
113                    return _arrayableOperator;
114            }
115    
116            public String getComparator() {
117                    return _comparator;
118            }
119    
120            public String getDBName() {
121                    return _dbName;
122            }
123    
124            public String getEJBName() {
125                    return _ejbName;
126            }
127    
128            public String getHumanCondition(boolean arrayable) {
129                    StringBundler sb = new StringBundler();
130    
131                    sb.append(_name);
132                    sb.append(" ");
133                    sb.append(convertComparatorToHtml(_comparator));
134                    sb.append(" ");
135    
136                    if (arrayable && hasArrayableOperator()) {
137                            if (isArrayableAndOperator()) {
138                                    sb.append("all ");
139                            }
140                            else {
141                                    sb.append("any ");
142                            }
143                    }
144    
145                    sb.append("&#63;");
146    
147                    return sb.toString();
148            }
149    
150            public String getHumanName() {
151                    return _humanName;
152            }
153    
154            public String getHumanNames() {
155                    return TextFormatter.formatPlural(getHumanName());
156            }
157    
158            public String getIdParam() {
159                    return _idParam;
160            }
161    
162            public String getIdType() {
163                    return _idType;
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 isContainerModel() {
243                    return _containerModel;
244            }
245    
246            public boolean isConvertNull() {
247                    return _convertNull;
248            }
249    
250            public boolean isFilterPrimary() {
251                    return _filterPrimary;
252            }
253    
254            public boolean isFinderPath() {
255                    return _finderPath;
256            }
257    
258            public boolean isJsonEnabled() {
259                    return _jsonEnabled;
260            }
261    
262            public boolean isLazy() {
263                    return _lazy;
264            }
265    
266            public boolean isLocalized() {
267                    return _localized;
268            }
269    
270            public boolean isMappingManyToMany() {
271                    return Validator.isNotNull(_mappingTable);
272            }
273    
274            public boolean isOrderByAscending() {
275                    return _orderByAscending;
276            }
277    
278            public boolean isOrderColumn() {
279                    return _orderColumn;
280            }
281    
282            public boolean isParentContainerModel() {
283                    return _parentContainerModel;
284            }
285    
286            public boolean isPrimary() {
287                    return _primary;
288            }
289    
290            public boolean isPrimitiveType() {
291                    return isPrimitiveType(true);
292            }
293    
294            public boolean isPrimitiveType(boolean includeWrappers) {
295                    if (Character.isLowerCase(_type.charAt(0))) {
296                            return true;
297                    }
298    
299                    if (!includeWrappers) {
300                            return false;
301                    }
302    
303                    if (_type.equals("Boolean")) {
304                            return true;
305                    }
306                    else if (_type.equals("Byte")) {
307                            return true;
308                    }
309                    else if (_type.equals("Char")) {
310                            return true;
311                    }
312                    else if (_type.equals("Double")) {
313                            return true;
314                    }
315                    else if (_type.equals("Float")) {
316                            return true;
317                    }
318                    else if (_type.equals("Integer")) {
319                            return true;
320                    }
321                    else if (_type.equals("Long")) {
322                            return true;
323                    }
324                    else if (_type.equals("Short")) {
325                            return true;
326                    }
327                    else {
328                            return false;
329                    }
330            }
331    
332            public boolean isUserUuid() {
333                    if (_type.equals("long") && _methodName.endsWith("UserId")) {
334                            return true;
335                    }
336                    else {
337                            return false;
338                    }
339            }
340    
341            public void setArrayableOperator(String arrayableOperator) {
342                    _arrayableOperator = arrayableOperator.toUpperCase();
343            }
344    
345            public void setCaseSensitive(boolean caseSensitive) {
346                    _caseSensitive = caseSensitive;
347            }
348    
349            public void setComparator(String comparator) {
350                    _comparator = comparator;
351            }
352    
353            public void setContainerModel(boolean containerModel) {
354                    _containerModel = containerModel;
355            }
356    
357            public void setConvertNull(boolean convertNull) {
358                    _convertNull = convertNull;
359            }
360    
361            public void setDBName(String dbName) {
362                    _dbName = dbName;
363            }
364    
365            public void setFinderPath(boolean finderPath) {
366                    _finderPath = finderPath;
367            }
368    
369            public void setIdParam(String idParam) {
370                    _idParam = idParam;
371            }
372    
373            public void setIdType(String idType) {
374                    _idType = idType;
375            }
376    
377            public void setLazy(boolean lazy) {
378                    _lazy = lazy;
379            }
380    
381            public void setLocalized(boolean localized) {
382                    _localized = localized;
383            }
384    
385            public void setOrderByAscending(boolean orderByAscending) {
386                    _orderByAscending = orderByAscending;
387            }
388    
389            public void setOrderColumn(boolean orderColumn) {
390                    _orderColumn = orderColumn;
391            }
392    
393            public void setParentContainerModel(boolean parentContainerModel) {
394                    _parentContainerModel = parentContainerModel;
395            }
396    
397            protected String convertComparatorToHtml(String comparator) {
398                    if (comparator.equals(">")) {
399                            return "&gt;";
400                    }
401    
402                    if (comparator.equals("<")) {
403                            return "&lt;";
404                    }
405    
406                    if (comparator.equals(">=")) {
407                            return "&ge;";
408                    }
409    
410                    if (comparator.equals("<=")) {
411                            return "&le;";
412                    }
413    
414                    if (comparator.equals("!=")) {
415                            return "&ne;";
416                    }
417    
418                    return comparator;
419            }
420    
421            private boolean _accessor;
422            private String _arrayableOperator;
423            private boolean _caseSensitive;
424            private String _comparator;
425            private boolean _containerModel;
426            private boolean _convertNull;
427            private String _dbName;
428            private String _ejbName;
429            private boolean _filterPrimary;
430            private boolean _finderPath;
431            private String _humanName;
432            private String _idParam;
433            private String _idType;
434            private boolean _jsonEnabled;
435            private boolean _lazy;
436            private boolean _localized;
437            private String _mappingTable;
438            private String _methodName;
439            private String _name;
440            private boolean _orderByAscending;
441            private boolean _orderColumn;
442            private boolean _parentContainerModel;
443            private boolean _primary;
444            private String _type;
445    
446    }