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