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 = _pkList.get(0);
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 = _pkList.get(0);
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 = _pkList.get(0);
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         if (hasCompoundPK()) {
357             return false;
358         }
359         else {
360             EntityColumn col = _pkList.get(0);
361 
362             if (col.isPrimitiveType()) {
363                 return true;
364             }
365             else {
366                 return false;
367             }
368         }
369     }
370 
371     public boolean hasRemoteService() {
372         return _remoteService;
373     }
374 
375     public boolean hasUuid() {
376         return _uuid;
377     }
378 
379     public boolean isCacheEnabled() {
380         return _cacheEnabled;
381     }
382 
383     public boolean isDefaultDataSource() {
384         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
385             return true;
386         }
387         else {
388             return false;
389         }
390     }
391 
392     public boolean isDefaultSessionFactory() {
393         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
394             return true;
395         }
396         else {
397             return false;
398         }
399     }
400 
401     public boolean isDefaultTXManager() {
402         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
403             return true;
404         }
405         else {
406             return false;
407         }
408     }
409 
410     public boolean isHierarchicalTree() {
411         if (!hasPrimitivePK()) {
412             return false;
413         }
414 
415         EntityColumn col = _pkList.get(0);
416 
417         if ((_columnList.indexOf(
418                 new EntityColumn("parent" + col.getMethodName())) != -1) &&
419             (_columnList.indexOf(
420                 new EntityColumn("left" + col.getMethodName())) != -1) &&
421             (_columnList.indexOf(
422                 new EntityColumn("right" + col.getMethodName())) != -1)) {
423 
424             return true;
425         }
426         else {
427             return false;
428         }
429     }
430 
431     public boolean isOrdered() {
432         if (_order != null) {
433             return true;
434         }
435         else {
436             return false;
437         }
438     }
439 
440     public boolean isPortalReference() {
441         return _portalReference;
442     }
443 
444     public void setPortalReference(boolean portalReference) {
445         _portalReference = portalReference;
446     }
447 
448     private String _alias;
449     private boolean _cacheEnabled;
450     private List<EntityColumn> _collectionList;
451     private List<EntityColumn> _columnList;
452     private String _dataSource;
453     private String _finderClass;
454     private List<EntityFinder> _finderList;
455     private boolean _localService;
456     private String _name;
457     private EntityOrder _order;
458     private String _packagePath;
459     private String _persistenceClass;
460     private List<EntityColumn> _pkList;
461     private boolean _portalReference;
462     private String _portletName;
463     private String _portletShortName;
464     private List<Entity> _referenceList;
465     private List<EntityColumn> _regularColList;
466     private boolean _remoteService;
467     private String _sessionFactory;
468     private String _table;
469     private String _txManager;
470     private List<String> _txRequiredList;
471     private boolean _uuid;
472 
473 }