1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.ListUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.util.TextFormatter;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="Entity.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class Entity {
39  
40      public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
41  
42      public static final String DEFAULT_SESSION_FACTORY =
43          "liferaySessionFactory";
44  
45      public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
46  
47      public static EntityColumn getColumn(
48          String name, List<EntityColumn> columnList) {
49  
50          int pos = columnList.indexOf(new EntityColumn(name));
51  
52          if (pos != -1) {
53              return columnList.get(pos);
54          }
55          else {
56              throw new RuntimeException("Column " + name + " not found");
57          }
58      }
59  
60      public static boolean hasColumn(
61          String name, List<EntityColumn> columnList) {
62  
63          int pos = columnList.indexOf(new EntityColumn(name));
64  
65          if (pos != -1) {
66              return true;
67          }
68          else {
69              return false;
70          }
71      }
72  
73      public Entity(String name) {
74          this(
75              null, null, null, name, null, null, false, false, true, null, null,
76              null, null, null, true, null, null, null, null, null, null, null,
77              null);
78      }
79  
80      public Entity(
81          String packagePath, String portletName, String portletShortName,
82          String name, String table, String alias, boolean uuid,
83          boolean localService, boolean remoteService, String persistenceClass,
84          String finderClass, String dataSource, String sessionFactory,
85          String txManager, boolean cacheEnabled, List<EntityColumn> pkList,
86          List<EntityColumn> regularColList, List<EntityColumn> collectionList,
87          List<EntityColumn> columnList, EntityOrder order,
88          List<EntityFinder> finderList, List<Entity> referenceList,
89          List<String> txRequiredList) {
90  
91          _packagePath = packagePath;
92          _portletName = portletName;
93          _portletShortName = portletShortName;
94          _name = name;
95          _table = table;
96          _alias = alias;
97          _uuid = uuid;
98          _localService = localService;
99          _remoteService = remoteService;
100         _persistenceClass = persistenceClass;
101         _finderClass = finderClass;
102         _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
103         _sessionFactory = GetterUtil.getString(
104             sessionFactory, DEFAULT_SESSION_FACTORY);
105         _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
106         _cacheEnabled = cacheEnabled;
107         _pkList = pkList;
108         _regularColList = regularColList;
109         _collectionList = collectionList;
110         _columnList = columnList;
111         _order = order;
112         _finderList = finderList;
113         _referenceList = referenceList;
114         _txRequiredList = txRequiredList;
115     }
116 
117     public boolean equals(Object obj) {
118         Entity entity = (Entity)obj;
119 
120         String name = entity.getName();
121 
122         if (_name.equals(name)) {
123             return true;
124         }
125         else {
126             return false;
127         }
128     }
129 
130     public String getAlias() {
131         return _alias;
132     }
133 
134     public List<EntityFinder> getCollectionFinderList() {
135         List<EntityFinder> finderList = ListUtil.copy(_finderList);
136 
137         Iterator<EntityFinder> itr = finderList.iterator();
138 
139         while (itr.hasNext()) {
140             EntityFinder finder = itr.next();
141 
142             if (!finder.isCollection()) {
143                 itr.remove();
144             }
145         }
146 
147         return finderList;
148     }
149 
150     public List<EntityColumn> getCollectionList() {
151         return _collectionList;
152     }
153 
154     public EntityColumn getColumn(String name) {
155         return getColumn(name, _columnList);
156     }
157 
158     public EntityColumn getColumnByMappingTable(String mappingTable) {
159         for (int i = 0; i < _columnList.size(); i++) {
160             EntityColumn col = _columnList.get(i);
161 
162             if (col.getMappingTable() != null &&
163                 col.getMappingTable().equals(mappingTable)) {
164 
165                 return col;
166             }
167         }
168 
169         return null;
170     }
171 
172     public List<EntityColumn> getColumnList() {
173         return _columnList;
174     }
175 
176     public String getDataSource() {
177         return _dataSource;
178     }
179 
180     public String getFinderClass() {
181         return _finderClass;
182     }
183 
184     public List<EntityFinder> getFinderList() {
185         return _finderList;
186     }
187 
188     public String getName() {
189         return _name;
190     }
191 
192     public String getNames() {
193         return TextFormatter.formatPlural(new String(_name));
194     }
195 
196     public EntityOrder getOrder() {
197         return _order;
198     }
199 
200     public String getPackagePath() {
201         return _packagePath;
202     }
203 
204     public String getPersistenceClass() {
205         return _persistenceClass;
206     }
207 
208     public String getPKClassName() {
209         if (hasCompoundPK()) {
210             return _name + "PK";
211         }
212         else {
213             EntityColumn col = _pkList.get(0);
214 
215             return col.getType();
216         }
217     }
218 
219     public List<EntityColumn> getPKList() {
220         return _pkList;
221     }
222 
223     public String getPKVarName() {
224         if (hasCompoundPK()) {
225             return getVarName() + "PK";
226         }
227         else {
228             EntityColumn col = _pkList.get(0);
229 
230             return col.getName();
231         }
232     }
233 
234     public String getPortletName() {
235         return _portletName;
236     }
237 
238     public String getPortletShortName() {
239         return _portletShortName;
240     }
241 
242     public List<Entity> getReferenceList() {
243         return _referenceList;
244     }
245 
246     public List<EntityColumn> getRegularColList() {
247         return _regularColList;
248     }
249 
250     public String getSessionFactory() {
251         return _sessionFactory;
252     }
253 
254     public String getShortName() {
255         if (_name.startsWith(_portletShortName)) {
256             return _name.substring(_portletShortName.length());
257         }
258         else {
259             return _name;
260         }
261     }
262 
263     public String getSpringPropertyName() {
264         return TextFormatter.format(_name, TextFormatter.L);
265     }
266 
267     public String getTable() {
268         return _table;
269     }
270 
271     public String getTXManager() {
272         return _txManager;
273     }
274 
275     public List<String> getTxRequiredList() {
276         return _txRequiredList;
277     }
278 
279     public List<EntityFinder> getUniqueFinderList() {
280         List<EntityFinder> finderList = ListUtil.copy(_finderList);
281 
282         Iterator<EntityFinder> itr = finderList.iterator();
283 
284         while (itr.hasNext()) {
285             EntityFinder finder = itr.next();
286 
287             if (finder.isCollection()) {
288                 itr.remove();
289             }
290         }
291 
292         return finderList;
293     }
294 
295     public String getVarName() {
296         return TextFormatter.format(_name, TextFormatter.I);
297     }
298 
299     public String getVarNames() {
300         return TextFormatter.formatPlural(new String(getVarName()));
301     }
302 
303     public boolean hasColumn(String name) {
304         return hasColumn(name, _columnList);
305     }
306 
307     public boolean hasColumns() {
308         if ((_columnList == null) || (_columnList.size() == 0)) {
309             return false;
310         }
311         else {
312             return true;
313         }
314     }
315 
316     public boolean hasCompoundPK() {
317         if (_pkList.size() > 1) {
318             return true;
319         }
320         else {
321             return false;
322         }
323     }
324 
325     public boolean hasFinderClass() {
326         if (Validator.isNull(_finderClass)) {
327             return false;
328         }
329         else {
330             return true;
331         }
332     }
333 
334     public boolean hasLocalizedColumn() {
335         for (EntityColumn col : _columnList) {
336             if (col.isLocalized()) {
337                 return true;
338             }
339         }
340 
341         return false;
342     }
343 
344     public boolean hasLocalService() {
345         return _localService;
346     }
347 
348     public boolean hasPrimitivePK() {
349         if (hasCompoundPK()) {
350             return false;
351         }
352         else {
353             EntityColumn col = _pkList.get(0);
354 
355             if (col.isPrimitiveType()) {
356                 return true;
357             }
358             else {
359                 return false;
360             }
361         }
362     }
363 
364     public boolean hasRemoteService() {
365         return _remoteService;
366     }
367 
368     public boolean hasUuid() {
369         return _uuid;
370     }
371 
372     public boolean isCacheEnabled() {
373         return _cacheEnabled;
374     }
375 
376     public boolean isDefaultDataSource() {
377         if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
378             return true;
379         }
380         else {
381             return false;
382         }
383     }
384 
385     public boolean isDefaultSessionFactory() {
386         if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
387             return true;
388         }
389         else {
390             return false;
391         }
392     }
393 
394     public boolean isDefaultTXManager() {
395         if (_txManager.equals(DEFAULT_TX_MANAGER)) {
396             return true;
397         }
398         else {
399             return false;
400         }
401     }
402 
403     public boolean isHierarchicalTree() {
404         if (!hasPrimitivePK()) {
405             return false;
406         }
407 
408         EntityColumn col = _pkList.get(0);
409 
410         if ((_columnList.indexOf(
411                 new EntityColumn("parent" + col.getMethodName())) != -1) &&
412             (_columnList.indexOf(
413                 new EntityColumn("left" + col.getMethodName())) != -1) &&
414             (_columnList.indexOf(
415                 new EntityColumn("right" + col.getMethodName())) != -1)) {
416 
417             return true;
418         }
419         else {
420             return false;
421         }
422     }
423 
424     public boolean isOrdered() {
425         if (_order != null) {
426             return true;
427         }
428         else {
429             return false;
430         }
431     }
432 
433     public boolean isPortalReference() {
434         return _portalReference;
435     }
436 
437     public void setPortalReference(boolean portalReference) {
438         _portalReference = portalReference;
439     }
440 
441     private String _alias;
442     private boolean _cacheEnabled;
443     private List<EntityColumn> _collectionList;
444     private List<EntityColumn> _columnList;
445     private String _dataSource;
446     private String _finderClass;
447     private List<EntityFinder> _finderList;
448     private boolean _localService;
449     private String _name;
450     private EntityOrder _order;
451     private String _packagePath;
452     private String _persistenceClass;
453     private List<EntityColumn> _pkList;
454     private boolean _portalReference;
455     private String _portletName;
456     private String _portletShortName;
457     private List<Entity> _referenceList;
458     private List<EntityColumn> _regularColList;
459     private boolean _remoteService;
460     private String _sessionFactory;
461     private String _table;
462     private String _txManager;
463     private List<String> _txRequiredList;
464     private boolean _uuid;
465 
466 }