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