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