1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools.servicebuilder;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.util.TextFormatter;
25  
26  import java.util.List;
27  
28  /**
29   * <a href="Entity.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   *
33   */
34  public class Entity {
35  
36      public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
37  
38      public static final String DEFAULT_SESSION_FACTORY =
39          "liferaySessionFactory";
40  
41      public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
42  
43      public static EntityColumn getColumn(
44          String name, List<EntityColumn> columnList) {
45  
46          int pos = columnList.indexOf(new EntityColumn(name));
47  
48          if (pos == -1) {
49              throw new RuntimeException("Column " + name + " not found");
50          }
51  
52          return columnList.get(pos);
53      }
54  
55      public Entity(String name) {
56          this(
57              null, null, null, name, null, false, false, true, null, null, null,
58              null, null, true, null, null, null, null, null, null, null, null);
59      }
60  
61      public Entity(
62          String packagePath, String portletName, String portletShortName,
63          String name, String table, boolean uuid, boolean localService,
64          boolean remoteService, String persistenceClass, String finderClass,
65          String dataSource, String sessionFactory, String txManager,
66          boolean cacheEnabled, List<EntityColumn> pkList,
67          List<EntityColumn> regularColList, List<EntityColumn> collectionList,
68          List<EntityColumn> columnList, EntityOrder order,
69          List<EntityFinder> finderList, List<Entity> referenceList,
70          List<String> txRequiredList) {
71  
72          _packagePath = packagePath;
73          _portletName = portletName;
74          _portletShortName = portletShortName;
75          _name = name;
76          _table = table;
77          _uuid = uuid;
78          _localService = localService;
79          _remoteService = remoteService;
80          _persistenceClass = persistenceClass;
81          _finderClass = finderClass;
82          _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
83          _sessionFactory = GetterUtil.getString(
84              sessionFactory, DEFAULT_SESSION_FACTORY);
85          _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
86          _cacheEnabled = cacheEnabled;
87          _pkList = pkList;
88          _regularColList = regularColList;
89          _collectionList = collectionList;
90          _columnList = columnList;
91          _order = order;
92          _finderList = finderList;
93          _referenceList = referenceList;
94          _txRequiredList = txRequiredList;
95      }
96  
97      public String getPackagePath() {
98          return _packagePath;
99      }
100 
101     public String getPortletName() {
102         return _portletName;
103     }
104 
105     public String getPortletShortName() {
106         return _portletShortName;
107     }
108 
109     public String getName() {
110         return _name;
111     }
112 
113     public String getNames() {
114         return TextFormatter.formatPlural(new String(_name));
115     }
116 
117     public String getVarName() {
118         return TextFormatter.format(_name, TextFormatter.I);
119     }
120 
121     public String getVarNames() {
122         return TextFormatter.formatPlural(new String(getVarName()));
123     }
124 
125     public String getShortName() {
126         if (_name.startsWith(_portletShortName)) {
127             return _name.substring(_portletShortName.length());
128         }
129         else {
130             return _name;
131         }
132     }
133 
134     public String getSpringPropertyName() {
135         return TextFormatter.format(_name, TextFormatter.L);
136     }
137 
138     public String getTable() {
139         return _table;
140     }
141 
142     public boolean hasUuid() {
143         return _uuid;
144     }
145 
146     public boolean hasLocalService() {
147         return _localService;
148     }
149 
150     public boolean hasRemoteService() {
151         return _remoteService;
152     }
153 
154     public String getPersistenceClass() {
155         return _persistenceClass;
156     }
157 
158     public String getFinderClass() {
159         return _finderClass;
160     }
161 
162     public boolean hasFinderClass() {
163         if (Validator.isNull(_finderClass)) {
164             return false;
165         }
166         else {
167             return true;
168         }
169     }
170 
171     public String getDataSource() {
172         return _dataSource;
173     }
174 
175     public boolean isDefaultDataSource() {
176         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
177             return true;
178         }
179         else {
180             return false;
181         }
182     }
183 
184     public String getSessionFactory() {
185         return _sessionFactory;
186     }
187 
188     public boolean isDefaultSessionFactory() {
189         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
190             return true;
191         }
192         else {
193             return false;
194         }
195     }
196 
197     public String getTXManager() {
198         return _txManager;
199     }
200 
201     public boolean isDefaultTXManager() {
202         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
203             return true;
204         }
205         else {
206             return false;
207         }
208     }
209 
210     public boolean isCacheEnabled() {
211         return _cacheEnabled;
212     }
213 
214     public String getPKClassName() {
215         if (hasCompoundPK()) {
216             return _name + "PK";
217         }
218         else {
219             EntityColumn col = _pkList.get(0);
220 
221             return col.getType();
222         }
223     }
224 
225     public String getPKVarName() {
226         if (hasCompoundPK()) {
227             return getVarName() + "PK";
228         }
229         else {
230             EntityColumn col = _pkList.get(0);
231 
232             return col.getName();
233         }
234     }
235 
236     public boolean hasPrimitivePK() {
237         if (hasCompoundPK()) {
238             return false;
239         }
240         else {
241             EntityColumn col = _pkList.get(0);
242 
243             if (col.isPrimitiveType()) {
244                 return true;
245             }
246             else {
247                 return false;
248             }
249         }
250     }
251 
252     public boolean hasCompoundPK() {
253         if (_pkList.size() > 1) {
254             return true;
255         }
256         else {
257             return false;
258         }
259     }
260 
261     public List<EntityColumn> getPKList() {
262         return _pkList;
263     }
264 
265     public List<EntityColumn> getRegularColList() {
266         return _regularColList;
267     }
268 
269     public List<EntityColumn> getCollectionList() {
270         return _collectionList;
271     }
272 
273     public List<EntityColumn> getColumnList() {
274         return _columnList;
275     }
276 
277     public boolean hasColumns() {
278         if ((_columnList == null) || (_columnList.size() == 0)) {
279             return false;
280         }
281         else {
282             return true;
283         }
284     }
285 
286     public EntityOrder getOrder() {
287         return _order;
288     }
289 
290     public boolean isOrdered() {
291         if (_order != null) {
292             return true;
293         }
294         else {
295             return false;
296         }
297     }
298 
299     public List<EntityFinder> getFinderList() {
300         return _finderList;
301     }
302 
303     public boolean isPortalReference() {
304         return _portalReference;
305     }
306 
307     public void setPortalReference(boolean portalReference) {
308         _portalReference = portalReference;
309     }
310 
311     public List<Entity> getReferenceList() {
312         return _referenceList;
313     }
314 
315     public List<String> getTxRequiredList() {
316         return _txRequiredList;
317     }
318 
319     public EntityColumn getColumn(String name) {
320         return getColumn(name, _columnList);
321     }
322 
323     public EntityColumn getColumnByMappingTable(String mappingTable) {
324         for (int i = 0; i < _columnList.size(); i++) {
325             EntityColumn col = _columnList.get(i);
326 
327             if (col.getMappingTable() != null &&
328                 col.getMappingTable().equals(mappingTable)) {
329 
330                 return col;
331             }
332         }
333 
334         return null;
335     }
336 
337     public boolean equals(Object obj) {
338         Entity entity = (Entity)obj;
339 
340         String name = entity.getName();
341 
342         if (_name.equals(name)) {
343             return true;
344         }
345         else {
346             return false;
347         }
348     }
349 
350     private String _packagePath;
351     private String _portletName;
352     private String _portletShortName;
353     private String _name;
354     private String _table;
355     private boolean _uuid;
356     private boolean _localService;
357     private boolean _remoteService;
358     private String _persistenceClass;
359     private String _finderClass;
360     private String _dataSource;
361     private String _sessionFactory;
362     private String _txManager;
363     private boolean _cacheEnabled;
364     private List<EntityColumn> _pkList;
365     private List<EntityColumn> _regularColList;
366     private List<EntityColumn> _collectionList;
367     private List<EntityColumn> _columnList;
368     private EntityOrder _order;
369     private List<EntityFinder> _finderList;
370     private boolean _portalReference;
371     private List<Entity> _referenceList;
372     private List<String> _txRequiredList;
373 
374 }