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