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 EntityColumn getColumn(
40          String name, List<EntityColumn> columnList) {
41  
42          int pos = columnList.indexOf(new EntityColumn(name));
43  
44          if (pos == -1) {
45              throw new RuntimeException("Column " + name + " not found");
46          }
47  
48          return columnList.get(pos);
49      }
50  
51      public Entity(String name) {
52          this(
53              null, null, null, name, null, false, false, true, null, null, null,
54              null, null, null, null, null, null, null, null, null, null);
55      }
56  
57      public Entity(
58          String packagePath, String portletName, String portletShortName,
59          String name, String table, boolean uuid, boolean localService,
60          boolean remoteService, String persistenceClass, String finderClass,
61          String dataSource, String sessionFactory, String txManager,
62          List<EntityColumn> pkList, List<EntityColumn> regularColList,
63          List<EntityColumn> collectionList, List<EntityColumn> columnList,
64          EntityOrder order, List<EntityFinder> finderList,
65          List<Entity> referenceList, List<String> txRequiredList) {
66  
67          _packagePath = packagePath;
68          _portletName = portletName;
69          _portletShortName = portletShortName;
70          _name = name;
71          _table = table;
72          _uuid = uuid;
73          _localService = localService;
74          _remoteService = remoteService;
75          _persistenceClass = persistenceClass;
76          _finderClass = finderClass;
77          _dataSource = GetterUtil.getString(dataSource, "liferayDataSource");
78          _sessionFactory = GetterUtil.getString(
79              sessionFactory, "liferaySessionFactory");
80          _txManager = GetterUtil.getString(
81              txManager, "liferayTransactionManager");
82          _pkList = pkList;
83          _regularColList = regularColList;
84          _collectionList = collectionList;
85          _columnList = columnList;
86          _order = order;
87          _finderList = finderList;
88          _referenceList = referenceList;
89          _txRequiredList = txRequiredList;
90      }
91  
92      public String getPackagePath() {
93          return _packagePath;
94      }
95  
96      public String getPortletName() {
97          return _portletName;
98      }
99  
100     public String getPortletShortName() {
101         return _portletShortName;
102     }
103 
104     public String getName() {
105         return _name;
106     }
107 
108     public String getNames() {
109         return TextFormatter.formatPlural(new String(_name));
110     }
111 
112     public String getVarName() {
113         return TextFormatter.format(_name, TextFormatter.I);
114     }
115 
116     public String getVarNames() {
117         return TextFormatter.formatPlural(new String(getVarName()));
118     }
119 
120     public String getShortName() {
121         if (_name.startsWith(_portletShortName)) {
122             return _name.substring(_portletShortName.length());
123         }
124         else {
125             return _name;
126         }
127     }
128 
129     public String getTable() {
130         return _table;
131     }
132 
133     public boolean hasUuid() {
134         return _uuid;
135     }
136 
137     public boolean hasLocalService() {
138         return _localService;
139     }
140 
141     public boolean hasRemoteService() {
142         return _remoteService;
143     }
144 
145     public String getPersistenceClass() {
146         return _persistenceClass;
147     }
148 
149     public String getFinderClass() {
150         return _finderClass;
151     }
152 
153     public boolean hasFinderClass() {
154         if (Validator.isNull(_finderClass)) {
155             return false;
156         }
157         else {
158             return true;
159         }
160     }
161 
162     public String getDataSource() {
163         return _dataSource;
164     }
165 
166     public String getSessionFactory() {
167         return _sessionFactory;
168     }
169 
170     public String getTXManager() {
171         return _txManager;
172     }
173 
174     public String getPKClassName() {
175         if (hasCompoundPK()) {
176             return _name + "PK";
177         }
178         else {
179             EntityColumn col = _pkList.get(0);
180 
181             return col.getType();
182         }
183     }
184 
185     public String getPKVarName() {
186         if (hasCompoundPK()) {
187             return getVarName() + "PK";
188         }
189         else {
190             EntityColumn col = _pkList.get(0);
191 
192             return col.getName();
193         }
194     }
195 
196     public boolean hasPrimitivePK() {
197         if (hasCompoundPK()) {
198             return false;
199         }
200         else {
201             EntityColumn col = _pkList.get(0);
202 
203             if (col.isPrimitiveType()) {
204                 return true;
205             }
206             else {
207                 return false;
208             }
209         }
210     }
211 
212     public boolean hasCompoundPK() {
213         if (_pkList.size() > 1) {
214             return true;
215         }
216         else {
217             return false;
218         }
219     }
220 
221     public List<EntityColumn> getPKList() {
222         return _pkList;
223     }
224 
225     public List<EntityColumn> getRegularColList() {
226         return _regularColList;
227     }
228 
229     public List<EntityColumn> getCollectionList() {
230         return _collectionList;
231     }
232 
233     public List<EntityColumn> getColumnList() {
234         return _columnList;
235     }
236 
237     public boolean hasColumns() {
238         if ((_columnList == null) || (_columnList.size() == 0)) {
239             return false;
240         }
241         else {
242             return true;
243         }
244     }
245 
246     public EntityOrder getOrder() {
247         return _order;
248     }
249 
250     public boolean isOrdered() {
251         if (_order != null) {
252             return true;
253         }
254         else {
255             return false;
256         }
257     }
258 
259     public List<EntityFinder> getFinderList() {
260         return _finderList;
261     }
262 
263     public List<Entity> getReferenceList() {
264         return _referenceList;
265     }
266 
267     public List<String> getTxRequiredList() {
268         return _txRequiredList;
269     }
270 
271     public EntityColumn getColumn(String name) {
272         return getColumn(name, _columnList);
273     }
274 
275     public EntityColumn getColumnByMappingTable(String mappingTable) {
276         for (int i = 0; i < _columnList.size(); i++) {
277             EntityColumn col = _columnList.get(i);
278 
279             if (col.getMappingTable() != null &&
280                 col.getMappingTable().equals(mappingTable)) {
281 
282                 return col;
283             }
284         }
285 
286         return null;
287     }
288 
289     public boolean equals(Object obj) {
290         Entity entity = (Entity)obj;
291 
292         String name = entity.getName();
293 
294         if (_name.equals(name)) {
295             return true;
296         }
297         else {
298             return false;
299         }
300     }
301 
302     private String _packagePath;
303     private String _portletName;
304     private String _portletShortName;
305     private String _name;
306     private String _table;
307     private boolean _uuid;
308     private boolean _localService;
309     private boolean _remoteService;
310     private String _persistenceClass;
311     private String _finderClass;
312     private String _dataSource;
313     private String _sessionFactory;
314     private String _txManager;
315     private List<EntityColumn> _pkList;
316     private List<EntityColumn> _regularColList;
317     private List<EntityColumn> _collectionList;
318     private List<EntityColumn> _columnList;
319     private EntityOrder _order;
320     private List<EntityFinder> _finderList;
321     private List<Entity> _referenceList;
322     private List<String> _txRequiredList;
323 
324 }