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