001    /**
002     * Copyright (c) 2000-2012 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.Accessor;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.TextFormatter;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.permission.ResourceActionsUtil;
023    
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.HashSet;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Set;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Shuyang Zhou
034     */
035    public class Entity {
036    
037            public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
038    
039            public static final String DEFAULT_SESSION_FACTORY =
040                    "liferaySessionFactory";
041    
042            public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
043    
044            public static final Accessor<Entity, String> NAME_ACCESSOR =
045                    new Accessor<Entity, String>() {
046    
047                            public String get(Entity entity) {
048                                    return entity.getName();
049                            }
050    
051                    };
052    
053            public static EntityColumn getColumn(
054                    String name, List<EntityColumn> columnList) {
055    
056                    for (EntityColumn col : columnList) {
057                            if (name.equals(col.getName())) {
058                                    return col;
059                            }
060                    }
061    
062                    throw new RuntimeException("Column " + name + " not found");
063            }
064    
065            public static boolean hasColumn(
066                    String name, List<EntityColumn> columnList) {
067    
068                    int pos = columnList.indexOf(new EntityColumn(name));
069    
070                    if (pos != -1) {
071                            return true;
072                    }
073                    else {
074                            return false;
075                    }
076            }
077    
078            public Entity(String name) {
079                    this(
080                            null, null, null, name, null, null, null, false, false, false, true,
081                            null, null, null, null, null, true, false, null, null, null, null,
082                            null, null, null, null, null);
083            }
084    
085            public Entity(
086                    String packagePath, String portletName, String portletShortName,
087                    String name, String humanName, String table, String alias, boolean uuid,
088                    boolean uuidAccessor, boolean localService, boolean remoteService,
089                    String persistenceClass, String finderClass, String dataSource,
090                    String sessionFactory, String txManager, boolean cacheEnabled,
091                    boolean jsonEnabled, List<EntityColumn> pkList,
092                    List<EntityColumn> regularColList, List<EntityColumn> blobList,
093                    List<EntityColumn> collectionList, List<EntityColumn> columnList,
094                    EntityOrder order, List<EntityFinder> finderList,
095                    List<Entity> referenceList, List<String> txRequiredList) {
096    
097                    _packagePath = packagePath;
098                    _portletName = portletName;
099                    _portletShortName = portletShortName;
100                    _name = name;
101                    _humanName = GetterUtil.getString(
102                            humanName, ServiceBuilder.toHumanName(name));
103                    _table = table;
104                    _alias = alias;
105                    _uuid = uuid;
106                    _uuidAccessor = uuidAccessor;
107                    _localService = localService;
108                    _remoteService = remoteService;
109                    _persistenceClass = persistenceClass;
110                    _finderClass = finderClass;
111                    _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
112                    _sessionFactory = GetterUtil.getString(
113                            sessionFactory, DEFAULT_SESSION_FACTORY);
114                    _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
115                    _cacheEnabled = cacheEnabled;
116                    _jsonEnabled = jsonEnabled;
117                    _pkList = pkList;
118                    _regularColList = regularColList;
119                    _blobList = blobList;
120                    _collectionList = collectionList;
121                    _columnList = columnList;
122                    _order = order;
123                    _finderList = finderList;
124                    _referenceList = referenceList;
125                    _txRequiredList = txRequiredList;
126    
127                    if (_finderList != null) {
128                            Set<EntityColumn> finderColumns = new HashSet<EntityColumn>();
129    
130                            for (EntityFinder entityFinder : _finderList) {
131                                    finderColumns.addAll(entityFinder.getColumns());
132                            }
133    
134                            _finderColumnsList = new ArrayList<EntityColumn>(finderColumns);
135    
136                            Collections.sort(_finderColumnsList);
137                    }
138                    else {
139                            _finderColumnsList = Collections.emptyList();
140                    }
141    
142                    if ((_blobList != null) && !_blobList.isEmpty()) {
143                            for (EntityColumn col : _blobList) {
144                                    if (!col.isLazy()) {
145                                            _cacheEnabled = false;
146    
147                                            break;
148                                    }
149                            }
150                    }
151    
152                    if ((_columnList != null) && !_columnList.isEmpty()) {
153                            for (EntityColumn col : _columnList) {
154                                    if (col.isContainerModel() || col.isParentContainerModel()) {
155                                            _containerModel = true;
156    
157                                            break;
158                                    }
159                            }
160                    }
161            }
162    
163            @Override
164            public boolean equals(Object obj) {
165                    Entity entity = (Entity)obj;
166    
167                    String name = entity.getName();
168    
169                    if (_name.equals(name)) {
170                            return true;
171                    }
172                    else {
173                            return false;
174                    }
175            }
176    
177            public String getAlias() {
178                    return _alias;
179            }
180    
181            public List<EntityColumn> getBlobList() {
182                    return _blobList;
183            }
184    
185            public List<EntityFinder> getCollectionFinderList() {
186                    List<EntityFinder> finderList = ListUtil.copy(_finderList);
187    
188                    Iterator<EntityFinder> itr = finderList.iterator();
189    
190                    while (itr.hasNext()) {
191                            EntityFinder finder = itr.next();
192    
193                            if (!finder.isCollection()) {
194                                    itr.remove();
195                            }
196                    }
197    
198                    return finderList;
199            }
200    
201            public List<EntityColumn> getCollectionList() {
202                    return _collectionList;
203            }
204    
205            public EntityColumn getColumn(String name) {
206                    return getColumn(name, _columnList);
207            }
208    
209            public EntityColumn getColumnByMappingTable(String mappingTable) {
210                    for (EntityColumn col : _columnList) {
211                            if ((col.getMappingTable() != null) &&
212                                    col.getMappingTable().equals(mappingTable)) {
213    
214                                    return col;
215                            }
216                    }
217    
218                    return null;
219            }
220    
221            public List<EntityColumn> getColumnList() {
222                    return _columnList;
223            }
224    
225            public String getDataSource() {
226                    return _dataSource;
227            }
228    
229            public EntityColumn getFilterPKColumn() {
230                    for (EntityColumn col : _columnList) {
231                            if (col.isFilterPrimary()) {
232                                    return col;
233                            }
234                    }
235    
236                    return _getPKColumn();
237            }
238    
239            public String getFinderClass() {
240                    return _finderClass;
241            }
242    
243            public List<EntityColumn> getFinderColumnsList() {
244                    return _finderColumnsList;
245            }
246    
247            public List<EntityFinder> getFinderList() {
248                    return _finderList;
249            }
250    
251            public String getHumanName() {
252                    return _humanName;
253            }
254    
255            public String getHumanNames() {
256                    return TextFormatter.formatPlural(_humanName);
257            }
258    
259            public String getName() {
260                    return _name;
261            }
262    
263            public String getNames() {
264                    return TextFormatter.formatPlural(_name);
265            }
266    
267            public EntityOrder getOrder() {
268                    return _order;
269            }
270    
271            public String getPackagePath() {
272                    return _packagePath;
273            }
274    
275            public List<String> getParentTransients() {
276                    return _parentTransients;
277            }
278    
279            public String getPersistenceClass() {
280                    return _persistenceClass;
281            }
282    
283            public String getPKClassName() {
284                    if (hasCompoundPK()) {
285                            return _name + "PK";
286                    }
287                    else {
288                            EntityColumn col = _getPKColumn();
289    
290                            return col.getType();
291                    }
292            }
293    
294            public String getPKDBName() {
295                    if (hasCompoundPK()) {
296                            return getVarName() + "PK";
297                    }
298                    else {
299                            EntityColumn col = _getPKColumn();
300    
301                            return col.getDBName();
302                    }
303            }
304    
305            public List<EntityColumn> getPKList() {
306                    return _pkList;
307            }
308    
309            public String getPKVarName() {
310                    if (hasCompoundPK()) {
311                            return getVarName() + "PK";
312                    }
313                    else {
314                            EntityColumn col = _getPKColumn();
315    
316                            return col.getName();
317                    }
318            }
319    
320            public String getPortletName() {
321                    return _portletName;
322            }
323    
324            public String getPortletShortName() {
325                    return _portletShortName;
326            }
327    
328            public List<Entity> getReferenceList() {
329                    return _referenceList;
330            }
331    
332            public List<EntityColumn> getRegularColList() {
333                    return _regularColList;
334            }
335    
336            public String getSessionFactory() {
337                    return _sessionFactory;
338            }
339    
340            public String getShortName() {
341                    if (_name.startsWith(_portletShortName)) {
342                            return _name.substring(_portletShortName.length());
343                    }
344                    else {
345                            return _name;
346                    }
347            }
348    
349            public String getSpringPropertyName() {
350                    return TextFormatter.format(_name, TextFormatter.L);
351            }
352    
353            public String getTable() {
354                    return _table;
355            }
356    
357            public List<String> getTransients() {
358                    return _transients;
359            }
360    
361            public String getTXManager() {
362                    return _txManager;
363            }
364    
365            public List<String> getTxRequiredList() {
366                    return _txRequiredList;
367            }
368    
369            public List<EntityFinder> getUniqueFinderList() {
370                    List<EntityFinder> finderList = ListUtil.copy(_finderList);
371    
372                    Iterator<EntityFinder> itr = finderList.iterator();
373    
374                    while (itr.hasNext()) {
375                            EntityFinder finder = itr.next();
376    
377                            if (finder.isCollection()) {
378                                    itr.remove();
379                            }
380                    }
381    
382                    return finderList;
383            }
384    
385            public String getVarName() {
386                    return TextFormatter.format(_name, TextFormatter.I);
387            }
388    
389            public String getVarNames() {
390                    return TextFormatter.formatPlural(getVarName());
391            }
392    
393            public boolean hasArrayableOperator() {
394                    for (EntityFinder finder : _finderList) {
395                            if (finder.hasArrayableOperator()) {
396                                    return true;
397                            }
398                    }
399    
400                    return false;
401            }
402    
403            public boolean hasColumn(String name) {
404                    return hasColumn(name, _columnList);
405            }
406    
407            public boolean hasColumns() {
408                    if ((_columnList == null) || (_columnList.size() == 0)) {
409                            return false;
410                    }
411                    else {
412                            return true;
413                    }
414            }
415    
416            public boolean hasCompoundPK() {
417                    if (_pkList.size() > 1) {
418                            return true;
419                    }
420                    else {
421                            return false;
422                    }
423            }
424    
425            public boolean hasEagerBlobColumn() {
426                    if ((_blobList == null) || _blobList.isEmpty()) {
427                            return false;
428                    }
429    
430                    for (EntityColumn col : _blobList) {
431                            if (!col.isLazy()) {
432                                    return true;
433                            }
434                    }
435    
436                    return false;
437            }
438    
439            public boolean hasFinderClass() {
440                    if (Validator.isNull(_finderClass)) {
441                            return false;
442                    }
443                    else {
444                            return true;
445                    }
446            }
447    
448            @Override
449            public int hashCode() {
450                    return _name.hashCode();
451            }
452    
453            public boolean hasLazyBlobColumn() {
454                    if ((_blobList == null) || _blobList.isEmpty()) {
455                            return false;
456                    }
457    
458                    for (EntityColumn col : _blobList) {
459                            if (col.isLazy()) {
460                                    return true;
461                            }
462                    }
463    
464                    return false;
465            }
466    
467            public boolean hasLocalizedColumn() {
468                    for (EntityColumn col : _columnList) {
469                            if (col.isLocalized()) {
470                                    return true;
471                            }
472                    }
473    
474                    return false;
475            }
476    
477            public boolean hasLocalService() {
478                    return _localService;
479            }
480    
481            public boolean hasPrimitivePK() {
482                    return hasPrimitivePK(true);
483            }
484    
485            public boolean hasPrimitivePK(boolean includeWrappers) {
486                    if (hasCompoundPK()) {
487                            return false;
488                    }
489                    else {
490                            EntityColumn col = _getPKColumn();
491    
492                            if (col.isPrimitiveType(includeWrappers)) {
493                                    return true;
494                            }
495                            else {
496                                    return false;
497                            }
498                    }
499            }
500    
501            public boolean hasRemoteService() {
502                    return _remoteService;
503            }
504    
505            public boolean hasUuid() {
506                    return _uuid;
507            }
508    
509            public boolean hasUuidAccessor() {
510                    return _uuidAccessor;
511            }
512    
513            public boolean isAttachedModel() {
514                    if (hasColumn("classNameId") && hasColumn("classPK")) {
515                            EntityColumn classNameIdCol = getColumn("classNameId");
516    
517                            String classNameIdColType = classNameIdCol.getType();
518    
519                            EntityColumn classPKCol = getColumn("classPK");
520    
521                            String classPKColType = classPKCol.getType();
522    
523                            if (classNameIdColType.equals("long") &&
524                                    classPKColType.equals("long")) {
525    
526                                    return true;
527                            }
528                    }
529    
530                    return false;
531            }
532    
533            public boolean isAuditedModel() {
534                    if (hasColumn("companyId") && hasColumn("createDate") &&
535                            hasColumn("modifiedDate") && hasColumn("userId") &&
536                            hasColumn("userName")) {
537    
538                            return true;
539                    }
540                    else {
541                            return false;
542                    }
543            }
544    
545            public boolean isCacheEnabled() {
546                    return _cacheEnabled;
547            }
548    
549            public boolean isContainerModel() {
550                    return _containerModel;
551            }
552    
553            public boolean isDefaultDataSource() {
554                    if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
555                            return true;
556                    }
557                    else {
558                            return false;
559                    }
560            }
561    
562            public boolean isDefaultSessionFactory() {
563                    if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
564                            return true;
565                    }
566                    else {
567                            return false;
568                    }
569            }
570    
571            public boolean isDefaultTXManager() {
572                    if (_txManager.equals(DEFAULT_TX_MANAGER)) {
573                            return true;
574                    }
575                    else {
576                            return false;
577                    }
578            }
579    
580            public boolean isGroupedModel() {
581                    String pkVarName = getPKVarName();
582    
583                    if (isAuditedModel() && hasColumn("groupId") &&
584                            !pkVarName.equals("groupId")) {
585    
586                            return true;
587                    }
588                    else {
589                            return false;
590                    }
591            }
592    
593            public boolean isHierarchicalTree() {
594                    if (!hasPrimitivePK()) {
595                            return false;
596                    }
597    
598                    EntityColumn col = _getPKColumn();
599    
600                    if ((_columnList.indexOf(
601                                    new EntityColumn("parent" + col.getMethodName())) != -1) &&
602                            (_columnList.indexOf(
603                                    new EntityColumn("left" + col.getMethodName())) != -1) &&
604                            (_columnList.indexOf(
605                                    new EntityColumn("right" + col.getMethodName())) != -1)) {
606    
607                            return true;
608                    }
609                    else {
610                            return false;
611                    }
612            }
613    
614            public boolean isJsonEnabled() {
615                    return _jsonEnabled;
616            }
617    
618            public boolean isOrdered() {
619                    if (_order != null) {
620                            return true;
621                    }
622                    else {
623                            return false;
624                    }
625            }
626    
627            public boolean isPermissionCheckEnabled() {
628                    for (EntityFinder finder : _finderList) {
629                            if (isPermissionCheckEnabled(finder)) {
630                                    return true;
631                            }
632                    }
633    
634                    return false;
635            }
636    
637            public boolean isPermissionCheckEnabled(EntityFinder finder) {
638                    if (_name.equals("Group") || _name.equals("User") ||
639                            finder.getName().equals("UUID_G") || !finder.isCollection() ||
640                            !hasPrimitivePK() ||
641                            !ResourceActionsUtil.hasModelResourceActions(
642                                    _packagePath + ".model." + _name)) {
643    
644                            return false;
645                    }
646    
647                    if (hasColumn("groupId") && !finder.hasColumn("groupId")) {
648                            return false;
649                    }
650    
651                    return true;
652            }
653    
654            public boolean isPermissionedModel() {
655                    if (hasColumn("resourceBlockId")) {
656                            return true;
657                    }
658                    else {
659                            return false;
660                    }
661            }
662    
663            public boolean isPortalReference() {
664                    return _portalReference;
665            }
666    
667            public boolean isResourcedModel() {
668                    String pkVarName = getPKVarName();
669    
670                    if (hasColumn("resourcePrimKey") &&
671                            !pkVarName.equals("resourcePrimKey")) {
672    
673                            return true;
674                    }
675                    else {
676                            return false;
677                    }
678            }
679    
680            public boolean isWorkflowEnabled() {
681                    if (hasColumn("status") && hasColumn("statusByUserId") &&
682                            hasColumn("statusByUserName") && hasColumn("statusDate")) {
683    
684                            return true;
685                    }
686                    else {
687                            return false;
688                    }
689            }
690    
691            public void setParentTransients(List<String> transients) {
692                    _parentTransients = transients;
693            }
694    
695            public void setPortalReference(boolean portalReference) {
696                    _portalReference = portalReference;
697            }
698    
699            public void setTransients(List<String> transients) {
700                    _transients = transients;
701            }
702    
703            private EntityColumn _getPKColumn() {
704                    if (_pkList.isEmpty()) {
705                            throw new RuntimeException(
706                                    "There is no primary key for entity " + _name);
707                    }
708    
709                    return _pkList.get(0);
710            }
711    
712            private String _alias;
713            private List<EntityColumn> _blobList;
714            private boolean _cacheEnabled;
715            private List<EntityColumn> _collectionList;
716            private List<EntityColumn> _columnList;
717            private boolean _containerModel;
718            private String _dataSource;
719            private String _finderClass;
720            private List<EntityColumn> _finderColumnsList;
721            private List<EntityFinder> _finderList;
722            private String _humanName;
723            private boolean _jsonEnabled;
724            private boolean _localService;
725            private String _name;
726            private EntityOrder _order;
727            private String _packagePath;
728            private List<String> _parentTransients;
729            private String _persistenceClass;
730            private List<EntityColumn> _pkList;
731            private boolean _portalReference;
732            private String _portletName;
733            private String _portletShortName;
734            private List<Entity> _referenceList;
735            private List<EntityColumn> _regularColList;
736            private boolean _remoteService;
737            private String _sessionFactory;
738            private String _table;
739            private List<String> _transients;
740            private String _txManager;
741            private List<String> _txRequiredList;
742            private boolean _uuid;
743            private boolean _uuidAccessor;
744    
745    }