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