1   /**
2    * Copyright (c) 2000-2008 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.Validator;
27  import com.liferay.util.TextFormatter;
28  
29  import java.util.List;
30  
31  /**
32   * <a href="Entity.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class Entity {
38  
39      public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
40  
41      public static final String DEFAULT_SESSION_FACTORY =
42          "liferaySessionFactory";
43  
44      public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
45  
46      public static EntityColumn getColumn(
47          String name, List<EntityColumn> columnList) {
48  
49          int pos = columnList.indexOf(new EntityColumn(name));
50  
51          if (pos == -1) {
52              throw new RuntimeException("Column " + name + " not found");
53          }
54  
55          return columnList.get(pos);
56      }
57  
58      public Entity(String name) {
59          this(
60              null, null, null, name, null, false, false, true, null, null, null,
61              null, null, true, null, null, null, null, null, null, null, null);
62      }
63  
64      public Entity(
65          String packagePath, String portletName, String portletShortName,
66          String name, String table, boolean uuid, boolean localService,
67          boolean remoteService, String persistenceClass, String finderClass,
68          String dataSource, String sessionFactory, String txManager,
69          boolean cacheEnabled, List<EntityColumn> pkList,
70          List<EntityColumn> regularColList, List<EntityColumn> collectionList,
71          List<EntityColumn> columnList, EntityOrder order,
72          List<EntityFinder> finderList, List<Entity> referenceList,
73          List<String> txRequiredList) {
74  
75          _packagePath = packagePath;
76          _portletName = portletName;
77          _portletShortName = portletShortName;
78          _name = name;
79          _table = table;
80          _uuid = uuid;
81          _localService = localService;
82          _remoteService = remoteService;
83          _persistenceClass = persistenceClass;
84          _finderClass = finderClass;
85          _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
86          _sessionFactory = GetterUtil.getString(
87              sessionFactory, DEFAULT_SESSION_FACTORY);
88          _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
89          _cacheEnabled = cacheEnabled;
90          _pkList = pkList;
91          _regularColList = regularColList;
92          _collectionList = collectionList;
93          _columnList = columnList;
94          _order = order;
95          _finderList = finderList;
96          _referenceList = referenceList;
97          _txRequiredList = txRequiredList;
98      }
99  
100     public String getPackagePath() {
101         return _packagePath;
102     }
103 
104     public String getPortletName() {
105         return _portletName;
106     }
107 
108     public String getPortletShortName() {
109         return _portletShortName;
110     }
111 
112     public String getName() {
113         return _name;
114     }
115 
116     public String getNames() {
117         return TextFormatter.formatPlural(new String(_name));
118     }
119 
120     public String getVarName() {
121         return TextFormatter.format(_name, TextFormatter.I);
122     }
123 
124     public String getVarNames() {
125         return TextFormatter.formatPlural(new String(getVarName()));
126     }
127 
128     public String getShortName() {
129         if (_name.startsWith(_portletShortName)) {
130             return _name.substring(_portletShortName.length());
131         }
132         else {
133             return _name;
134         }
135     }
136 
137     public String getTable() {
138         return _table;
139     }
140 
141     public boolean hasUuid() {
142         return _uuid;
143     }
144 
145     public boolean hasLocalService() {
146         return _localService;
147     }
148 
149     public boolean hasRemoteService() {
150         return _remoteService;
151     }
152 
153     public String getPersistenceClass() {
154         return _persistenceClass;
155     }
156 
157     public String getFinderClass() {
158         return _finderClass;
159     }
160 
161     public boolean hasFinderClass() {
162         if (Validator.isNull(_finderClass)) {
163             return false;
164         }
165         else {
166             return true;
167         }
168     }
169 
170     public String getDataSource() {
171         return _dataSource;
172     }
173 
174     public boolean isDefaultDataSource() {
175         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
176             return true;
177         }
178         else {
179             return false;
180         }
181     }
182 
183     public String getSessionFactory() {
184         return _sessionFactory;
185     }
186 
187     public boolean isDefaultSessionFactory() {
188         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
189             return true;
190         }
191         else {
192             return false;
193         }
194     }
195 
196     public String getTXManager() {
197         return _txManager;
198     }
199 
200     public boolean isDefaultTXManager() {
201         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
202             return true;
203         }
204         else {
205             return false;
206         }
207     }
208 
209     public boolean isCacheEnabled() {
210         return _cacheEnabled;
211     }
212 
213     public String getPKClassName() {
214         if (hasCompoundPK()) {
215             return _name + "PK";
216         }
217         else {
218             EntityColumn col = _pkList.get(0);
219 
220             return col.getType();
221         }
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 boolean hasPrimitivePK() {
236         if (hasCompoundPK()) {
237             return false;
238         }
239         else {
240             EntityColumn col = _pkList.get(0);
241 
242             if (col.isPrimitiveType()) {
243                 return true;
244             }
245             else {
246                 return false;
247             }
248         }
249     }
250 
251     public boolean hasCompoundPK() {
252         if (_pkList.size() > 1) {
253             return true;
254         }
255         else {
256             return false;
257         }
258     }
259 
260     public List<EntityColumn> getPKList() {
261         return _pkList;
262     }
263 
264     public List<EntityColumn> getRegularColList() {
265         return _regularColList;
266     }
267 
268     public List<EntityColumn> getCollectionList() {
269         return _collectionList;
270     }
271 
272     public List<EntityColumn> getColumnList() {
273         return _columnList;
274     }
275 
276     public boolean hasColumns() {
277         if ((_columnList == null) || (_columnList.size() == 0)) {
278             return false;
279         }
280         else {
281             return true;
282         }
283     }
284 
285     public EntityOrder getOrder() {
286         return _order;
287     }
288 
289     public boolean isOrdered() {
290         if (_order != null) {
291             return true;
292         }
293         else {
294             return false;
295         }
296     }
297 
298     public List<EntityFinder> getFinderList() {
299         return _finderList;
300     }
301 
302     public boolean isPortalReference() {
303         return _portalReference;
304     }
305 
306     public void setPortalReference(boolean portalReference) {
307         _portalReference = portalReference;
308     }
309 
310     public List<Entity> getReferenceList() {
311         return _referenceList;
312     }
313 
314     public List<String> getTxRequiredList() {
315         return _txRequiredList;
316     }
317 
318     public EntityColumn getColumn(String name) {
319         return getColumn(name, _columnList);
320     }
321 
322     public EntityColumn getColumnByMappingTable(String mappingTable) {
323         for (int i = 0; i < _columnList.size(); i++) {
324             EntityColumn col = _columnList.get(i);
325 
326             if (col.getMappingTable() != null &&
327                 col.getMappingTable().equals(mappingTable)) {
328 
329                 return col;
330             }
331         }
332 
333         return null;
334     }
335 
336     public boolean equals(Object obj) {
337         Entity entity = (Entity)obj;
338 
339         String name = entity.getName();
340 
341         if (_name.equals(name)) {
342             return true;
343         }
344         else {
345             return false;
346         }
347     }
348 
349     private String _packagePath;
350     private String _portletName;
351     private String _portletShortName;
352     private String _name;
353     private String _table;
354     private boolean _uuid;
355     private boolean _localService;
356     private boolean _remoteService;
357     private String _persistenceClass;
358     private String _finderClass;
359     private String _dataSource;
360     private String _sessionFactory;
361     private String _txManager;
362     private boolean _cacheEnabled;
363     private List<EntityColumn> _pkList;
364     private List<EntityColumn> _regularColList;
365     private List<EntityColumn> _collectionList;
366     private List<EntityColumn> _columnList;
367     private EntityOrder _order;
368     private List<EntityFinder> _finderList;
369     private boolean _portalReference;
370     private List<Entity> _referenceList;
371     private List<String> _txRequiredList;
372 
373 }