001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.servicebuilder;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.ListUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.permission.ResourceActionsUtil;
021    import com.liferay.util.TextFormatter;
022    
023    import java.util.Iterator;
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class Entity {
030    
031            public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
032    
033            public static final String DEFAULT_SESSION_FACTORY =
034                    "liferaySessionFactory";
035    
036            public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
037    
038            public static EntityColumn getColumn(
039                    String name, List<EntityColumn> columnList) {
040    
041                    int pos = columnList.indexOf(new EntityColumn(name));
042    
043                    if (pos != -1) {
044                            return columnList.get(pos);
045                    }
046                    else {
047                            throw new RuntimeException("Column " + name + " not found");
048                    }
049            }
050    
051            public static boolean hasColumn(
052                    String name, List<EntityColumn> columnList) {
053    
054                    int pos = columnList.indexOf(new EntityColumn(name));
055    
056                    if (pos != -1) {
057                            return true;
058                    }
059                    else {
060                            return false;
061                    }
062            }
063    
064            public Entity(String name) {
065                    this(
066                            null, null, null, name,  null,null, null, false, false, true, null,
067                            null, null, null, null, true, null, null, null, null, null, null,
068                            null, null);
069            }
070    
071            public Entity(
072                    String packagePath, String portletName, String portletShortName,
073                    String name, String humanName, String table, String alias, boolean uuid,
074                    boolean localService, boolean remoteService, String persistenceClass,
075                    String finderClass, String dataSource, String sessionFactory,
076                    String txManager, boolean cacheEnabled, List<EntityColumn> pkList,
077                    List<EntityColumn> regularColList, List<EntityColumn> collectionList,
078                    List<EntityColumn> columnList, EntityOrder order,
079                    List<EntityFinder> finderList, List<Entity> referenceList,
080                    List<String> txRequiredList) {
081    
082                    _packagePath = packagePath;
083                    _portletName = portletName;
084                    _portletShortName = portletShortName;
085                    _name = name;
086                    _humanName = GetterUtil.getString(
087                            humanName, TextFormatter.format(name, TextFormatter.H));
088                    _table = table;
089                    _alias = alias;
090                    _uuid = uuid;
091                    _localService = localService;
092                    _remoteService = remoteService;
093                    _persistenceClass = persistenceClass;
094                    _finderClass = finderClass;
095                    _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
096                    _sessionFactory = GetterUtil.getString(
097                            sessionFactory, DEFAULT_SESSION_FACTORY);
098                    _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
099                    _cacheEnabled = cacheEnabled;
100                    _pkList = pkList;
101                    _regularColList = regularColList;
102                    _collectionList = collectionList;
103                    _columnList = columnList;
104                    _order = order;
105                    _finderList = finderList;
106                    _referenceList = referenceList;
107                    _txRequiredList = txRequiredList;
108            }
109    
110            public boolean equals(Object obj) {
111                    Entity entity = (Entity)obj;
112    
113                    String name = entity.getName();
114    
115                    if (_name.equals(name)) {
116                            return true;
117                    }
118                    else {
119                            return false;
120                    }
121            }
122    
123            public String getAlias() {
124                    return _alias;
125            }
126    
127            public List<EntityFinder> getCollectionFinderList() {
128                    List<EntityFinder> finderList = ListUtil.copy(_finderList);
129    
130                    Iterator<EntityFinder> itr = finderList.iterator();
131    
132                    while (itr.hasNext()) {
133                            EntityFinder finder = itr.next();
134    
135                            if (!finder.isCollection()) {
136                                    itr.remove();
137                            }
138                    }
139    
140                    return finderList;
141            }
142    
143            public List<EntityColumn> getCollectionList() {
144                    return _collectionList;
145            }
146    
147            public EntityColumn getColumn(String name) {
148                    return getColumn(name, _columnList);
149            }
150    
151            public EntityColumn getColumnByMappingTable(String mappingTable) {
152                    for (EntityColumn col : _columnList) {
153                            if ((col.getMappingTable() != null) &&
154                                    col.getMappingTable().equals(mappingTable)) {
155    
156                                    return col;
157                            }
158                    }
159    
160                    return null;
161            }
162    
163            public List<EntityColumn> getColumnList() {
164                    return _columnList;
165            }
166    
167            public String getDataSource() {
168                    return _dataSource;
169            }
170    
171            public EntityColumn getFilterPKColumn() {
172                    for (EntityColumn col : _columnList) {
173                            if (col.isFilterPrimary()) {
174                                    return col;
175                            }
176                    }
177    
178                    return _getPKColumn();
179            }
180    
181            public String getFinderClass() {
182                    return _finderClass;
183            }
184    
185            public List<EntityFinder> getFinderList() {
186                    return _finderList;
187            }
188    
189            public String getHumanName() {
190                    return _humanName;
191            }
192    
193            public String getHumanNames() {
194                    return TextFormatter.formatPlural(_humanName);
195            }
196    
197            public String getName() {
198                    return _name;
199            }
200    
201            public String getNames() {
202                    return TextFormatter.formatPlural(_name);
203            }
204    
205            public EntityOrder getOrder() {
206                    return _order;
207            }
208    
209            public String getPackagePath() {
210                    return _packagePath;
211            }
212    
213            public List<String> getParentTransients() {
214                    return _parentTransients;
215            }
216    
217            public String getPersistenceClass() {
218                    return _persistenceClass;
219            }
220    
221            public String getPKDBName() {
222                    if (hasCompoundPK()) {
223                            return getVarName() + "PK";
224                    }
225                    else {
226                            EntityColumn col = _getPKColumn();
227    
228                            return col.getDBName();
229                    }
230            }
231    
232            public String getPKClassName() {
233                    if (hasCompoundPK()) {
234                            return _name + "PK";
235                    }
236                    else {
237                            EntityColumn col = _getPKColumn();
238    
239                            return col.getType();
240                    }
241            }
242    
243            public List<EntityColumn> getPKList() {
244                    return _pkList;
245            }
246    
247            public String getPKVarName() {
248                    if (hasCompoundPK()) {
249                            return getVarName() + "PK";
250                    }
251                    else {
252                            EntityColumn col = _getPKColumn();
253    
254                            return col.getName();
255                    }
256            }
257    
258            public String getPortletName() {
259                    return _portletName;
260            }
261    
262            public String getPortletShortName() {
263                    return _portletShortName;
264            }
265    
266            public List<Entity> getReferenceList() {
267                    return _referenceList;
268            }
269    
270            public List<EntityColumn> getRegularColList() {
271                    return _regularColList;
272            }
273    
274            public String getSessionFactory() {
275                    return _sessionFactory;
276            }
277    
278            public String getShortName() {
279                    if (_name.startsWith(_portletShortName)) {
280                            return _name.substring(_portletShortName.length());
281                    }
282                    else {
283                            return _name;
284                    }
285            }
286    
287            public String getSpringPropertyName() {
288                    return TextFormatter.format(_name, TextFormatter.L);
289            }
290    
291            public String getTable() {
292                    return _table;
293            }
294    
295            public List<String> getTransients() {
296                    return _transients;
297            }
298    
299            public String getTXManager() {
300                    return _txManager;
301            }
302    
303            public List<String> getTxRequiredList() {
304                    return _txRequiredList;
305            }
306    
307            public List<EntityFinder> getUniqueFinderList() {
308                    List<EntityFinder> finderList = ListUtil.copy(_finderList);
309    
310                    Iterator<EntityFinder> itr = finderList.iterator();
311    
312                    while (itr.hasNext()) {
313                            EntityFinder finder = itr.next();
314    
315                            if (finder.isCollection()) {
316                                    itr.remove();
317                            }
318                    }
319    
320                    return finderList;
321            }
322    
323            public String getVarName() {
324                    return TextFormatter.format(_name, TextFormatter.I);
325            }
326    
327            public String getVarNames() {
328                    return TextFormatter.formatPlural(getVarName());
329            }
330    
331            public boolean hasArrayableOperator() {
332                    for (EntityFinder finder : _finderList) {
333                            if (finder.hasArrayableOperator()) {
334                                    return true;
335                            }
336                    }
337    
338                    return false;
339            }
340    
341            public boolean hasColumn(String name) {
342                    return hasColumn(name, _columnList);
343            }
344    
345            public boolean hasColumns() {
346                    if ((_columnList == null) || (_columnList.size() == 0)) {
347                            return false;
348                    }
349                    else {
350                            return true;
351                    }
352            }
353    
354            public boolean hasCompoundPK() {
355                    if (_pkList.size() > 1) {
356                            return true;
357                    }
358                    else {
359                            return false;
360                    }
361            }
362    
363            public boolean hasFinderClass() {
364                    if (Validator.isNull(_finderClass)) {
365                            return false;
366                    }
367                    else {
368                            return true;
369                    }
370            }
371    
372            public int hashCode() {
373                    return _name.hashCode();
374            }
375    
376            public boolean hasLocalizedColumn() {
377                    for (EntityColumn col : _columnList) {
378                            if (col.isLocalized()) {
379                                    return true;
380                            }
381                    }
382    
383                    return false;
384            }
385    
386            public boolean hasLocalService() {
387                    return _localService;
388            }
389    
390            public boolean hasPrimitivePK() {
391                    return     hasPrimitivePK(true);
392            }
393    
394            public boolean hasPrimitivePK(boolean includeWrappers) {
395                    if (hasCompoundPK()) {
396                            return false;
397                    }
398                    else {
399                            EntityColumn col = _getPKColumn();
400    
401                            if (col.isPrimitiveType(includeWrappers)) {
402                                    return true;
403                            }
404                            else {
405                                    return false;
406                            }
407                    }
408            }
409    
410            public boolean hasRemoteService() {
411                    return _remoteService;
412            }
413    
414            public boolean hasUuid() {
415                    return _uuid;
416            }
417    
418            public boolean isCacheEnabled() {
419                    return _cacheEnabled;
420            }
421    
422            public boolean isDefaultDataSource() {
423                    if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
424                            return true;
425                    }
426                    else {
427                            return false;
428                    }
429            }
430    
431            public boolean isDefaultSessionFactory() {
432                    if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
433                            return true;
434                    }
435                    else {
436                            return false;
437                    }
438            }
439    
440            public boolean isDefaultTXManager() {
441                    if (_txManager.equals(DEFAULT_TX_MANAGER)) {
442                            return true;
443                    }
444                    else {
445                            return false;
446                    }
447            }
448    
449            public boolean isHierarchicalTree() {
450                    if (!hasPrimitivePK()) {
451                            return false;
452                    }
453    
454                    EntityColumn col = _getPKColumn();
455    
456                    if ((_columnList.indexOf(
457                                    new EntityColumn("parent" + col.getMethodName())) != -1) &&
458                            (_columnList.indexOf(
459                                    new EntityColumn("left" + col.getMethodName())) != -1) &&
460                            (_columnList.indexOf(
461                                    new EntityColumn("right" + col.getMethodName())) != -1)) {
462    
463                            return true;
464                    }
465                    else {
466                            return false;
467                    }
468            }
469    
470            public boolean isOrdered() {
471                    if (_order != null) {
472                            return true;
473                    }
474                    else {
475                            return false;
476                    }
477            }
478    
479            public boolean isPermissionCheckEnabled() {
480                    for (EntityFinder finder : _finderList) {
481                            if (isPermissionCheckEnabled(finder)) {
482                                    return true;
483                            }
484                    }
485    
486                    return false;
487            }
488    
489            public boolean isPermissionCheckEnabled(EntityFinder finder) {
490                    if (!finder.getName().equals("UUID_G") && hasPrimitivePK() &&
491                            hasColumn("userId") && finder.hasColumn("groupId") &&
492                            ResourceActionsUtil.hasModelResourceActions(
493                                    _packagePath + ".model." + _name)) {
494    
495                            return true;
496                    }
497                    else {
498                            return false;
499                    }
500            }
501    
502            public boolean isPortalReference() {
503                    return _portalReference;
504            }
505    
506            public boolean isWorkflowEnabled() {
507                    if (hasColumn("status") && hasColumn("statusByUserId") &&
508                            hasColumn("statusByUserName") && hasColumn("statusDate")) {
509    
510                            return true;
511                    }
512                    else {
513                            return false;
514                    }
515            }
516    
517            public void setParentTransients(List<String> transients) {
518                    _parentTransients = transients;
519            }
520    
521            public void setPortalReference(boolean portalReference) {
522                    _portalReference = portalReference;
523            }
524    
525            public void setTransients(List<String> transients) {
526                    _transients = transients;
527            }
528    
529            private EntityColumn _getPKColumn() {
530                    if (_pkList.isEmpty()) {
531                            throw new RuntimeException(
532                                    "There is no primary key for entity " + _name);
533                    }
534    
535                    return _pkList.get(0);
536            }
537    
538            private String _alias;
539            private boolean _cacheEnabled;
540            private List<EntityColumn> _collectionList;
541            private List<EntityColumn> _columnList;
542            private String _dataSource;
543            private String _finderClass;
544            private List<EntityFinder> _finderList;
545            private String _humanName;
546            private boolean _localService;
547            private String _name;
548            private EntityOrder _order;
549            private String _packagePath;
550            private List<String> _parentTransients;
551            private String _persistenceClass;
552            private List<EntityColumn> _pkList;
553            private boolean _portalReference;
554            private String _portletName;
555            private String _portletShortName;
556            private List<Entity> _referenceList;
557            private List<EntityColumn> _regularColList;
558            private boolean _remoteService;
559            private String _sessionFactory;
560            private String _table;
561            private List<String> _transients;
562            private String _txManager;
563            private List<String> _txRequiredList;
564            private boolean _uuid;
565    
566    }