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 getPKClassName() {
205         if (hasCompoundPK()) {
206             return _name + "PK";
207         }
208         else {
209             EntityColumn col = _pkList.get(0);
210 
211             return col.getType();
212         }
213     }
214 
215     public List<EntityColumn> getPKList() {
216         return _pkList;
217     }
218 
219     public String getPKVarName() {
220         if (hasCompoundPK()) {
221             return getVarName() + "PK";
222         }
223         else {
224             EntityColumn col = _pkList.get(0);
225 
226             return col.getName();
227         }
228     }
229 
230     public String getPortletName() {
231         return _portletName;
232     }
233 
234     public String getPortletShortName() {
235         return _portletShortName;
236     }
237 
238     public List<Entity> getReferenceList() {
239         return _referenceList;
240     }
241 
242     public List<EntityColumn> getRegularColList() {
243         return _regularColList;
244     }
245 
246     public String getSessionFactory() {
247         return _sessionFactory;
248     }
249 
250     public String getShortName() {
251         if (_name.startsWith(_portletShortName)) {
252             return _name.substring(_portletShortName.length());
253         }
254         else {
255             return _name;
256         }
257     }
258 
259     public String getSpringPropertyName() {
260         return TextFormatter.format(_name, TextFormatter.L);
261     }
262 
263     public String getTable() {
264         return _table;
265     }
266 
267     public List<String> getTransients() {
268         return _transients;
269     }
270 
271     public String getTXManager() {
272         return _txManager;
273     }
274 
275     public List<String> getTxRequiredList() {
276         return _txRequiredList;
277     }
278 
279     public List<EntityFinder> getUniqueFinderList() {
280         List<EntityFinder> finderList = ListUtil.copy(_finderList);
281 
282         Iterator<EntityFinder> itr = finderList.iterator();
283 
284         while (itr.hasNext()) {
285             EntityFinder finder = itr.next();
286 
287             if (finder.isCollection()) {
288                 itr.remove();
289             }
290         }
291 
292         return finderList;
293     }
294 
295     public String getVarName() {
296         return TextFormatter.format(_name, TextFormatter.I);
297     }
298 
299     public String getVarNames() {
300         return TextFormatter.formatPlural(new String(getVarName()));
301     }
302 
303     public boolean hasColumn(String name) {
304         return hasColumn(name, _columnList);
305     }
306 
307     public boolean hasColumns() {
308         if ((_columnList == null) || (_columnList.size() == 0)) {
309             return false;
310         }
311         else {
312             return true;
313         }
314     }
315 
316     public boolean hasCompoundPK() {
317         if (_pkList.size() > 1) {
318             return true;
319         }
320         else {
321             return false;
322         }
323     }
324 
325     public boolean hasFinderClass() {
326         if (Validator.isNull(_finderClass)) {
327             return false;
328         }
329         else {
330             return true;
331         }
332     }
333 
334     public boolean hasLocalizedColumn() {
335         for (EntityColumn col : _columnList) {
336             if (col.isLocalized()) {
337                 return true;
338             }
339         }
340 
341         return false;
342     }
343 
344     public boolean hasLocalService() {
345         return _localService;
346     }
347 
348     public boolean hasPrimitivePK() {
349         if (hasCompoundPK()) {
350             return false;
351         }
352         else {
353             EntityColumn col = _pkList.get(0);
354 
355             if (col.isPrimitiveType()) {
356                 return true;
357             }
358             else {
359                 return false;
360             }
361         }
362     }
363 
364     public boolean hasRemoteService() {
365         return _remoteService;
366     }
367 
368     public boolean hasUuid() {
369         return _uuid;
370     }
371 
372     public boolean isCacheEnabled() {
373         return _cacheEnabled;
374     }
375 
376     public boolean isDefaultDataSource() {
377         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
378             return true;
379         }
380         else {
381             return false;
382         }
383     }
384 
385     public boolean isDefaultSessionFactory() {
386         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
387             return true;
388         }
389         else {
390             return false;
391         }
392     }
393 
394     public boolean isDefaultTXManager() {
395         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
396             return true;
397         }
398         else {
399             return false;
400         }
401     }
402 
403     public boolean isHierarchicalTree() {
404         if (!hasPrimitivePK()) {
405             return false;
406         }
407 
408         EntityColumn col = _pkList.get(0);
409 
410         if ((_columnList.indexOf(
411                 new EntityColumn("parent" + col.getMethodName())) != -1) &&
412             (_columnList.indexOf(
413                 new EntityColumn("left" + col.getMethodName())) != -1) &&
414             (_columnList.indexOf(
415                 new EntityColumn("right" + col.getMethodName())) != -1)) {
416 
417             return true;
418         }
419         else {
420             return false;
421         }
422     }
423 
424     public boolean isOrdered() {
425         if (_order != null) {
426             return true;
427         }
428         else {
429             return false;
430         }
431     }
432 
433     public boolean isPortalReference() {
434         return _portalReference;
435     }
436 
437     public void setParentTransients(List<String> transients) {
438         _parentTransients = transients;
439     }
440 
441     public void setPortalReference(boolean portalReference) {
442         _portalReference = portalReference;
443     }
444 
445     public void setTransients(List<String> transients) {
446         _transients = transients;
447     }
448 
449     private String _alias;
450     private boolean _cacheEnabled;
451     private List<EntityColumn> _collectionList;
452     private List<EntityColumn> _columnList;
453     private String _dataSource;
454     private String _finderClass;
455     private List<EntityFinder> _finderList;
456     private boolean _localService;
457     private String _name;
458     private EntityOrder _order;
459     private String _packagePath;
460     private List<String> _parentTransients;
461     private String _persistenceClass;
462     private List<EntityColumn> _pkList;
463     private boolean _portalReference;
464     private String _portletName;
465     private String _portletShortName;
466     private List<Entity> _referenceList;
467     private List<EntityColumn> _regularColList;
468     private boolean _remoteService;
469     private String _sessionFactory;
470     private String _table;
471     private List<String> _transients;
472     private String _txManager;
473     private List<String> _txRequiredList;
474     private boolean _uuid;
475 
476 }