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