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