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