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.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 getSpringPropertyName() {
138         return TextFormatter.format(_name, TextFormatter.L);
139     }
140 
141     public String getTable() {
142         return _table;
143     }
144 
145     public boolean hasUuid() {
146         return _uuid;
147     }
148 
149     public boolean hasLocalService() {
150         return _localService;
151     }
152 
153     public boolean hasRemoteService() {
154         return _remoteService;
155     }
156 
157     public String getPersistenceClass() {
158         return _persistenceClass;
159     }
160 
161     public String getFinderClass() {
162         return _finderClass;
163     }
164 
165     public boolean hasFinderClass() {
166         if (Validator.isNull(_finderClass)) {
167             return false;
168         }
169         else {
170             return true;
171         }
172     }
173 
174     public String getDataSource() {
175         return _dataSource;
176     }
177 
178     public boolean isDefaultDataSource() {
179         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
180             return true;
181         }
182         else {
183             return false;
184         }
185     }
186 
187     public String getSessionFactory() {
188         return _sessionFactory;
189     }
190 
191     public boolean isDefaultSessionFactory() {
192         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
193             return true;
194         }
195         else {
196             return false;
197         }
198     }
199 
200     public String getTXManager() {
201         return _txManager;
202     }
203 
204     public boolean isDefaultTXManager() {
205         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
206             return true;
207         }
208         else {
209             return false;
210         }
211     }
212 
213     public boolean isCacheEnabled() {
214         return _cacheEnabled;
215     }
216 
217     public String getPKClassName() {
218         if (hasCompoundPK()) {
219             return _name + "PK";
220         }
221         else {
222             EntityColumn col = _pkList.get(0);
223 
224             return col.getType();
225         }
226     }
227 
228     public String getPKVarName() {
229         if (hasCompoundPK()) {
230             return getVarName() + "PK";
231         }
232         else {
233             EntityColumn col = _pkList.get(0);
234 
235             return col.getName();
236         }
237     }
238 
239     public boolean hasPrimitivePK() {
240         if (hasCompoundPK()) {
241             return false;
242         }
243         else {
244             EntityColumn col = _pkList.get(0);
245 
246             if (col.isPrimitiveType()) {
247                 return true;
248             }
249             else {
250                 return false;
251             }
252         }
253     }
254 
255     public boolean hasCompoundPK() {
256         if (_pkList.size() > 1) {
257             return true;
258         }
259         else {
260             return false;
261         }
262     }
263 
264     public List<EntityColumn> getPKList() {
265         return _pkList;
266     }
267 
268     public List<EntityColumn> getRegularColList() {
269         return _regularColList;
270     }
271 
272     public List<EntityColumn> getCollectionList() {
273         return _collectionList;
274     }
275 
276     public List<EntityColumn> getColumnList() {
277         return _columnList;
278     }
279 
280     public boolean hasColumns() {
281         if ((_columnList == null) || (_columnList.size() == 0)) {
282             return false;
283         }
284         else {
285             return true;
286         }
287     }
288 
289     public EntityOrder getOrder() {
290         return _order;
291     }
292 
293     public boolean isOrdered() {
294         if (_order != null) {
295             return true;
296         }
297         else {
298             return false;
299         }
300     }
301 
302     public List<EntityFinder> getFinderList() {
303         return _finderList;
304     }
305 
306     public boolean isPortalReference() {
307         return _portalReference;
308     }
309 
310     public void setPortalReference(boolean portalReference) {
311         _portalReference = portalReference;
312     }
313 
314     public List<Entity> getReferenceList() {
315         return _referenceList;
316     }
317 
318     public List<String> getTxRequiredList() {
319         return _txRequiredList;
320     }
321 
322     public EntityColumn getColumn(String name) {
323         return getColumn(name, _columnList);
324     }
325 
326     public EntityColumn getColumnByMappingTable(String mappingTable) {
327         for (int i = 0; i < _columnList.size(); i++) {
328             EntityColumn col = _columnList.get(i);
329 
330             if (col.getMappingTable() != null &&
331                 col.getMappingTable().equals(mappingTable)) {
332 
333                 return col;
334             }
335         }
336 
337         return null;
338     }
339 
340     public boolean equals(Object obj) {
341         Entity entity = (Entity)obj;
342 
343         String name = entity.getName();
344 
345         if (_name.equals(name)) {
346             return true;
347         }
348         else {
349             return false;
350         }
351     }
352 
353     private String _packagePath;
354     private String _portletName;
355     private String _portletShortName;
356     private String _name;
357     private String _table;
358     private boolean _uuid;
359     private boolean _localService;
360     private boolean _remoteService;
361     private String _persistenceClass;
362     private String _finderClass;
363     private String _dataSource;
364     private String _sessionFactory;
365     private String _txManager;
366     private boolean _cacheEnabled;
367     private List<EntityColumn> _pkList;
368     private List<EntityColumn> _regularColList;
369     private List<EntityColumn> _collectionList;
370     private List<EntityColumn> _columnList;
371     private EntityOrder _order;
372     private List<EntityFinder> _finderList;
373     private boolean _portalReference;
374     private List<Entity> _referenceList;
375     private List<String> _txRequiredList;
376 
377 }