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