1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools.servicebuilder;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.ListUtil;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.util.TextFormatter;
21  
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /**
26   * <a href="Entity.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class Entity {
31  
32      public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
33  
34      public static final String DEFAULT_SESSION_FACTORY =
35          "liferaySessionFactory";
36  
37      public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
38  
39      public static EntityColumn getColumn(
40          String name, List<EntityColumn> columnList) {
41  
42          int pos = columnList.indexOf(new EntityColumn(name));
43  
44          if (pos != -1) {
45              return columnList.get(pos);
46          }
47          else {
48              throw new RuntimeException("Column " + name + " not found");
49          }
50      }
51  
52      public static boolean hasColumn(
53          String name, List<EntityColumn> columnList) {
54  
55          int pos = columnList.indexOf(new EntityColumn(name));
56  
57          if (pos != -1) {
58              return true;
59          }
60          else {
61              return false;
62          }
63      }
64  
65      public Entity(String name) {
66          this(
67              null, null, null, name, null, null, false, false, true, null, null,
68              null, null, null, true, null, null, null, null, null, null, null,
69              null);
70      }
71  
72      public Entity(
73          String packagePath, String portletName, String portletShortName,
74          String name, String table, String alias, boolean uuid,
75          boolean localService, boolean remoteService, String persistenceClass,
76          String finderClass, String dataSource, String sessionFactory,
77          String txManager, boolean cacheEnabled, List<EntityColumn> pkList,
78          List<EntityColumn> regularColList, List<EntityColumn> collectionList,
79          List<EntityColumn> columnList, EntityOrder order,
80          List<EntityFinder> finderList, List<Entity> referenceList,
81          List<String> txRequiredList) {
82  
83          _packagePath = packagePath;
84          _portletName = portletName;
85          _portletShortName = portletShortName;
86          _name = name;
87          _table = table;
88          _alias = alias;
89          _uuid = uuid;
90          _localService = localService;
91          _remoteService = remoteService;
92          _persistenceClass = persistenceClass;
93          _finderClass = finderClass;
94          _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
95          _sessionFactory = GetterUtil.getString(
96              sessionFactory, DEFAULT_SESSION_FACTORY);
97          _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
98          _cacheEnabled = cacheEnabled;
99          _pkList = pkList;
100         _regularColList = regularColList;
101         _collectionList = collectionList;
102         _columnList = columnList;
103         _order = order;
104         _finderList = finderList;
105         _referenceList = referenceList;
106         _txRequiredList = txRequiredList;
107     }
108 
109     public boolean equals(Object obj) {
110         Entity entity = (Entity)obj;
111 
112         String name = entity.getName();
113 
114         if (_name.equals(name)) {
115             return true;
116         }
117         else {
118             return false;
119         }
120     }
121 
122     public String getAlias() {
123         return _alias;
124     }
125 
126     public List<EntityFinder> getCollectionFinderList() {
127         List<EntityFinder> finderList = ListUtil.copy(_finderList);
128 
129         Iterator<EntityFinder> itr = finderList.iterator();
130 
131         while (itr.hasNext()) {
132             EntityFinder finder = itr.next();
133 
134             if (!finder.isCollection()) {
135                 itr.remove();
136             }
137         }
138 
139         return finderList;
140     }
141 
142     public List<EntityColumn> getCollectionList() {
143         return _collectionList;
144     }
145 
146     public EntityColumn getColumn(String name) {
147         return getColumn(name, _columnList);
148     }
149 
150     public EntityColumn getColumnByMappingTable(String mappingTable) {
151         for (int i = 0; i < _columnList.size(); i++) {
152             EntityColumn col = _columnList.get(i);
153 
154             if (col.getMappingTable() != null &&
155                 col.getMappingTable().equals(mappingTable)) {
156 
157                 return col;
158             }
159         }
160 
161         return null;
162     }
163 
164     public List<EntityColumn> getColumnList() {
165         return _columnList;
166     }
167 
168     public String getDataSource() {
169         return _dataSource;
170     }
171 
172     public String getFinderClass() {
173         return _finderClass;
174     }
175 
176     public List<EntityFinder> getFinderList() {
177         return _finderList;
178     }
179 
180     public String getName() {
181         return _name;
182     }
183 
184     public String getNames() {
185         return TextFormatter.formatPlural(new String(_name));
186     }
187 
188     public EntityOrder getOrder() {
189         return _order;
190     }
191 
192     public String getPackagePath() {
193         return _packagePath;
194     }
195 
196     public List<String> getParentTransients() {
197         return _parentTransients;
198     }
199 
200     public String getPersistenceClass() {
201         return _persistenceClass;
202     }
203 
204     public String getPKDBName() {
205         if (hasCompoundPK()) {
206             return getVarName() + "PK";
207         }
208         else {
209             EntityColumn col = _pkList.get(0);
210 
211             return col.getDBName();
212         }
213     }
214 
215     public String getPKClassName() {
216         if (hasCompoundPK()) {
217             return _name + "PK";
218         }
219         else {
220             EntityColumn col = _pkList.get(0);
221 
222             return col.getType();
223         }
224     }
225 
226     public List<EntityColumn> getPKList() {
227         return _pkList;
228     }
229 
230     public String getPKVarName() {
231         if (hasCompoundPK()) {
232             return getVarName() + "PK";
233         }
234         else {
235             EntityColumn col = _pkList.get(0);
236 
237             return col.getName();
238         }
239     }
240 
241     public String getPortletName() {
242         return _portletName;
243     }
244 
245     public String getPortletShortName() {
246         return _portletShortName;
247     }
248 
249     public List<Entity> getReferenceList() {
250         return _referenceList;
251     }
252 
253     public List<EntityColumn> getRegularColList() {
254         return _regularColList;
255     }
256 
257     public String getSessionFactory() {
258         return _sessionFactory;
259     }
260 
261     public String getShortName() {
262         if (_name.startsWith(_portletShortName)) {
263             return _name.substring(_portletShortName.length());
264         }
265         else {
266             return _name;
267         }
268     }
269 
270     public String getSpringPropertyName() {
271         return TextFormatter.format(_name, TextFormatter.L);
272     }
273 
274     public String getTable() {
275         return _table;
276     }
277 
278     public List<String> getTransients() {
279         return _transients;
280     }
281 
282     public String getTXManager() {
283         return _txManager;
284     }
285 
286     public List<String> getTxRequiredList() {
287         return _txRequiredList;
288     }
289 
290     public List<EntityFinder> getUniqueFinderList() {
291         List<EntityFinder> finderList = ListUtil.copy(_finderList);
292 
293         Iterator<EntityFinder> itr = finderList.iterator();
294 
295         while (itr.hasNext()) {
296             EntityFinder finder = itr.next();
297 
298             if (finder.isCollection()) {
299                 itr.remove();
300             }
301         }
302 
303         return finderList;
304     }
305 
306     public String getVarName() {
307         return TextFormatter.format(_name, TextFormatter.I);
308     }
309 
310     public String getVarNames() {
311         return TextFormatter.formatPlural(new String(getVarName()));
312     }
313 
314     public boolean hasColumn(String name) {
315         return hasColumn(name, _columnList);
316     }
317 
318     public boolean hasColumns() {
319         if ((_columnList == null) || (_columnList.size() == 0)) {
320             return false;
321         }
322         else {
323             return true;
324         }
325     }
326 
327     public boolean hasCompoundPK() {
328         if (_pkList.size() > 1) {
329             return true;
330         }
331         else {
332             return false;
333         }
334     }
335 
336     public boolean hasFinderClass() {
337         if (Validator.isNull(_finderClass)) {
338             return false;
339         }
340         else {
341             return true;
342         }
343     }
344 
345     public int hashCode() {
346         return _name.hashCode();
347     }
348 
349     public boolean hasLocalizedColumn() {
350         for (EntityColumn col : _columnList) {
351             if (col.isLocalized()) {
352                 return true;
353             }
354         }
355 
356         return false;
357     }
358 
359     public boolean hasLocalService() {
360         return _localService;
361     }
362 
363     public boolean hasPrimitivePK() {
364         if (hasCompoundPK()) {
365             return false;
366         }
367         else {
368             EntityColumn col = _pkList.get(0);
369 
370             if (col.isPrimitiveType()) {
371                 return true;
372             }
373             else {
374                 return false;
375             }
376         }
377     }
378 
379     public boolean hasRemoteService() {
380         return _remoteService;
381     }
382 
383     public boolean hasUuid() {
384         return _uuid;
385     }
386 
387     public boolean isCacheEnabled() {
388         return _cacheEnabled;
389     }
390 
391     public boolean isDefaultDataSource() {
392         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
393             return true;
394         }
395         else {
396             return false;
397         }
398     }
399 
400     public boolean isDefaultSessionFactory() {
401         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
402             return true;
403         }
404         else {
405             return false;
406         }
407     }
408 
409     public boolean isDefaultTXManager() {
410         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
411             return true;
412         }
413         else {
414             return false;
415         }
416     }
417 
418     public boolean isHierarchicalTree() {
419         if (!hasPrimitivePK()) {
420             return false;
421         }
422 
423         EntityColumn col = _pkList.get(0);
424 
425         if ((_columnList.indexOf(
426                 new EntityColumn("parent" + col.getMethodName())) != -1) &&
427             (_columnList.indexOf(
428                 new EntityColumn("left" + col.getMethodName())) != -1) &&
429             (_columnList.indexOf(
430                 new EntityColumn("right" + col.getMethodName())) != -1)) {
431 
432             return true;
433         }
434         else {
435             return false;
436         }
437     }
438 
439     public boolean isOrdered() {
440         if (_order != null) {
441             return true;
442         }
443         else {
444             return false;
445         }
446     }
447 
448     public boolean isPortalReference() {
449         return _portalReference;
450     }
451 
452     public void setParentTransients(List<String> transients) {
453         _parentTransients = transients;
454     }
455 
456     public void setPortalReference(boolean portalReference) {
457         _portalReference = portalReference;
458     }
459 
460     public void setTransients(List<String> transients) {
461         _transients = transients;
462     }
463 
464     private String _alias;
465     private boolean _cacheEnabled;
466     private List<EntityColumn> _collectionList;
467     private List<EntityColumn> _columnList;
468     private String _dataSource;
469     private String _finderClass;
470     private List<EntityFinder> _finderList;
471     private boolean _localService;
472     private String _name;
473     private EntityOrder _order;
474     private String _packagePath;
475     private List<String> _parentTransients;
476     private String _persistenceClass;
477     private List<EntityColumn> _pkList;
478     private boolean _portalReference;
479     private String _portletName;
480     private String _portletShortName;
481     private List<Entity> _referenceList;
482     private List<EntityColumn> _regularColList;
483     private boolean _remoteService;
484     private String _sessionFactory;
485     private String _table;
486     private List<String> _transients;
487     private String _txManager;
488     private List<String> _txRequiredList;
489     private boolean _uuid;
490 
491 }