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, false, false, true, null, null, null,
77              null, null, true, null, null, null, null, null, null, null, null);
78      }
79  
80      public Entity(
81          String packagePath, String portletName, String portletShortName,
82          String name, String table, boolean uuid, boolean localService,
83          boolean remoteService, String persistenceClass, String finderClass,
84          String dataSource, String sessionFactory, String txManager,
85          boolean cacheEnabled, List<EntityColumn> pkList,
86          List<EntityColumn> regularColList, List<EntityColumn> collectionList,
87          List<EntityColumn> columnList, EntityOrder order,
88          List<EntityFinder> finderList, List<Entity> referenceList,
89          List<String> txRequiredList) {
90  
91          _packagePath = packagePath;
92          _portletName = portletName;
93          _portletShortName = portletShortName;
94          _name = name;
95          _table = table;
96          _uuid = uuid;
97          _localService = localService;
98          _remoteService = remoteService;
99          _persistenceClass = persistenceClass;
100         _finderClass = finderClass;
101         _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
102         _sessionFactory = GetterUtil.getString(
103             sessionFactory, DEFAULT_SESSION_FACTORY);
104         _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
105         _cacheEnabled = cacheEnabled;
106         _pkList = pkList;
107         _regularColList = regularColList;
108         _collectionList = collectionList;
109         _columnList = columnList;
110         _order = order;
111         _finderList = finderList;
112         _referenceList = referenceList;
113         _txRequiredList = txRequiredList;
114     }
115 
116     public boolean equals(Object obj) {
117         Entity entity = (Entity)obj;
118 
119         String name = entity.getName();
120 
121         if (_name.equals(name)) {
122             return true;
123         }
124         else {
125             return false;
126         }
127     }
128 
129     public List<EntityFinder> getCollectionFinderList() {
130         List<EntityFinder> finderList = ListUtil.copy(_finderList);
131 
132         Iterator<EntityFinder> itr = finderList.iterator();
133 
134         while (itr.hasNext()) {
135             EntityFinder finder = itr.next();
136 
137             if (!finder.isCollection()) {
138                 itr.remove();
139             }
140         }
141 
142         return finderList;
143     }
144 
145     public List<EntityColumn> getCollectionList() {
146         return _collectionList;
147     }
148 
149     public EntityColumn getColumn(String name) {
150         return getColumn(name, _columnList);
151     }
152 
153     public EntityColumn getColumnByMappingTable(String mappingTable) {
154         for (int i = 0; i < _columnList.size(); i++) {
155             EntityColumn col = _columnList.get(i);
156 
157             if (col.getMappingTable() != null &&
158                 col.getMappingTable().equals(mappingTable)) {
159 
160                 return col;
161             }
162         }
163 
164         return null;
165     }
166 
167     public List<EntityColumn> getColumnList() {
168         return _columnList;
169     }
170 
171     public String getDataSource() {
172         return _dataSource;
173     }
174 
175     public String getFinderClass() {
176         return _finderClass;
177     }
178 
179     public List<EntityFinder> getFinderList() {
180         return _finderList;
181     }
182 
183     public String getName() {
184         return _name;
185     }
186 
187     public String getNames() {
188         return TextFormatter.formatPlural(new String(_name));
189     }
190 
191     public EntityOrder getOrder() {
192         return _order;
193     }
194 
195     public String getPackagePath() {
196         return _packagePath;
197     }
198 
199     public String getPersistenceClass() {
200         return _persistenceClass;
201     }
202 
203     public String getPKClassName() {
204         if (hasCompoundPK()) {
205             return _name + "PK";
206         }
207         else {
208             EntityColumn col = _pkList.get(0);
209 
210             return col.getType();
211         }
212     }
213 
214     public List<EntityColumn> getPKList() {
215         return _pkList;
216     }
217 
218     public String getPKVarName() {
219         if (hasCompoundPK()) {
220             return getVarName() + "PK";
221         }
222         else {
223             EntityColumn col = _pkList.get(0);
224 
225             return col.getName();
226         }
227     }
228 
229     public String getPortletName() {
230         return _portletName;
231     }
232 
233     public String getPortletShortName() {
234         return _portletShortName;
235     }
236 
237     public List<Entity> getReferenceList() {
238         return _referenceList;
239     }
240 
241     public List<EntityColumn> getRegularColList() {
242         return _regularColList;
243     }
244 
245     public String getSessionFactory() {
246         return _sessionFactory;
247     }
248 
249     public String getShortName() {
250         if (_name.startsWith(_portletShortName)) {
251             return _name.substring(_portletShortName.length());
252         }
253         else {
254             return _name;
255         }
256     }
257 
258     public String getSpringPropertyName() {
259         return TextFormatter.format(_name, TextFormatter.L);
260     }
261 
262     public String getTable() {
263         return _table;
264     }
265 
266     public String getTXManager() {
267         return _txManager;
268     }
269 
270     public List<String> getTxRequiredList() {
271         return _txRequiredList;
272     }
273 
274     public List<EntityFinder> getUniqueFinderList() {
275         List<EntityFinder> finderList = ListUtil.copy(_finderList);
276 
277         Iterator<EntityFinder> itr = finderList.iterator();
278 
279         while (itr.hasNext()) {
280             EntityFinder finder = itr.next();
281 
282             if (finder.isCollection()) {
283                 itr.remove();
284             }
285         }
286 
287         return finderList;
288     }
289 
290     public String getVarName() {
291         return TextFormatter.format(_name, TextFormatter.I);
292     }
293 
294     public String getVarNames() {
295         return TextFormatter.formatPlural(new String(getVarName()));
296     }
297 
298     public boolean hasColumn(String name) {
299         return hasColumn(name, _columnList);
300     }
301 
302     public boolean hasColumns() {
303         if ((_columnList == null) || (_columnList.size() == 0)) {
304             return false;
305         }
306         else {
307             return true;
308         }
309     }
310 
311     public boolean hasCompoundPK() {
312         if (_pkList.size() > 1) {
313             return true;
314         }
315         else {
316             return false;
317         }
318     }
319 
320     public boolean hasFinderClass() {
321         if (Validator.isNull(_finderClass)) {
322             return false;
323         }
324         else {
325             return true;
326         }
327     }
328 
329     public boolean hasLocalService() {
330         return _localService;
331     }
332 
333     public boolean hasPrimitivePK() {
334         if (hasCompoundPK()) {
335             return false;
336         }
337         else {
338             EntityColumn col = _pkList.get(0);
339 
340             if (col.isPrimitiveType()) {
341                 return true;
342             }
343             else {
344                 return false;
345             }
346         }
347     }
348 
349     public boolean hasRemoteService() {
350         return _remoteService;
351     }
352 
353     public boolean hasUuid() {
354         return _uuid;
355     }
356 
357     public boolean isCacheEnabled() {
358         return _cacheEnabled;
359     }
360 
361     public boolean isDefaultDataSource() {
362         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
363             return true;
364         }
365         else {
366             return false;
367         }
368     }
369 
370     public boolean isDefaultSessionFactory() {
371         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
372             return true;
373         }
374         else {
375             return false;
376         }
377     }
378 
379     public boolean isDefaultTXManager() {
380         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
381             return true;
382         }
383         else {
384             return false;
385         }
386     }
387 
388     public boolean isHierarchicalTree() {
389         if (!hasPrimitivePK()) {
390             return false;
391         }
392 
393         EntityColumn col = _pkList.get(0);
394 
395         if ((_columnList.indexOf(
396                 new EntityColumn("parent" + col.getMethodName())) != -1) &&
397             (_columnList.indexOf(
398                 new EntityColumn("left" + col.getMethodName())) != -1) &&
399             (_columnList.indexOf(
400                 new EntityColumn("right" + col.getMethodName())) != -1)) {
401 
402             return true;
403         }
404         else {
405             return false;
406         }
407     }
408 
409     public boolean isOrdered() {
410         if (_order != null) {
411             return true;
412         }
413         else {
414             return false;
415         }
416     }
417 
418     public boolean isPortalReference() {
419         return _portalReference;
420     }
421 
422     public void setPortalReference(boolean portalReference) {
423         _portalReference = portalReference;
424     }
425 
426     private boolean _cacheEnabled;
427     private List<EntityColumn> _collectionList;
428     private List<EntityColumn> _columnList;
429     private String _dataSource;
430     private String _finderClass;
431     private List<EntityFinder> _finderList;
432     private boolean _localService;
433     private String _name;
434     private EntityOrder _order;
435     private String _packagePath;
436     private String _persistenceClass;
437     private List<EntityColumn> _pkList;
438     private boolean _portalReference;
439     private String _portletName;
440     private String _portletShortName;
441     private List<Entity> _referenceList;
442     private List<EntityColumn> _regularColList;
443     private boolean _remoteService;
444     private String _sessionFactory;
445     private String _table;
446     private String _txManager;
447     private List<String> _txRequiredList;
448     private boolean _uuid;
449 
450 }