001    /**
002     * Copyright (c) 2000-present 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                            @Override
048                            public String get(Entity entity) {
049                                    return entity.getName();
050                            }
051    
052                            @Override
053                            public Class<String> getAttributeClass() {
054                                    return String.class;
055                            }
056    
057                            @Override
058                            public Class<Entity> getTypeClass() {
059                                    return Entity.class;
060                            }
061    
062                    };
063    
064            public static EntityColumn getColumn(
065                    String name, List<EntityColumn> columnList) {
066    
067                    for (EntityColumn col : columnList) {
068                            if (name.equals(col.getName())) {
069                                    return col;
070                            }
071                    }
072    
073                    throw new RuntimeException("Column " + name + " not found");
074            }
075    
076            public static boolean hasColumn(
077                    String name, List<EntityColumn> columnList) {
078    
079                    return hasColumn(name, null, columnList);
080            }
081    
082            public static boolean hasColumn(
083                    String name, String type, List<EntityColumn> columnList) {
084    
085                    int index = columnList.indexOf(new EntityColumn(name));
086    
087                    if (index != -1) {
088                            EntityColumn col = columnList.get(index);
089    
090                            if ((type == null) || type.equals(col.getType())) {
091                                    return true;
092                            }
093                    }
094    
095                    return false;
096            }
097    
098            public Entity(String name) {
099                    this(
100                            null, null, null, name, null, null, null, false, false, false, true,
101                            null, null, null, null, null, true, false, false, false, false,
102                            false, null, null, null, null, null, null, null, null, null, null);
103            }
104    
105            public Entity(
106                    String packagePath, String portletName, String portletShortName,
107                    String name, String humanName, String table, String alias, boolean uuid,
108                    boolean uuidAccessor, boolean localService, boolean remoteService,
109                    String persistenceClass, String finderClass, String dataSource,
110                    String sessionFactory, String txManager, boolean cacheEnabled,
111                    boolean dynamicUpdateEnabled, boolean jsonEnabled, boolean mvccEnabled,
112                    boolean trashEnabled, boolean deprecated, List<EntityColumn> pkList,
113                    List<EntityColumn> regularColList, List<EntityColumn> blobList,
114                    List<EntityColumn> collectionList, List<EntityColumn> columnList,
115                    EntityOrder order, List<EntityFinder> finderList,
116                    List<Entity> referenceList, List<String> unresolvedReferenceList,
117                    List<String> txRequiredList) {
118    
119                    _packagePath = packagePath;
120                    _portletName = portletName;
121                    _portletShortName = portletShortName;
122                    _name = name;
123                    _humanName = GetterUtil.getString(
124                            humanName, ServiceBuilder.toHumanName(name));
125                    _table = table;
126                    _alias = alias;
127                    _uuid = uuid;
128                    _uuidAccessor = uuidAccessor;
129                    _localService = localService;
130                    _remoteService = remoteService;
131                    _persistenceClass = persistenceClass;
132                    _finderClass = finderClass;
133                    _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
134                    _sessionFactory = GetterUtil.getString(
135                            sessionFactory, DEFAULT_SESSION_FACTORY);
136                    _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
137                    _dynamicUpdateEnabled = dynamicUpdateEnabled;
138                    _jsonEnabled = jsonEnabled;
139                    _mvccEnabled = mvccEnabled;
140                    _trashEnabled = trashEnabled;
141                    _deprecated = deprecated;
142                    _pkList = pkList;
143                    _regularColList = regularColList;
144                    _blobList = blobList;
145                    _collectionList = collectionList;
146                    _columnList = columnList;
147                    _order = order;
148                    _finderList = finderList;
149                    _referenceList = referenceList;
150                    _unresolvedReferenceList = unresolvedReferenceList;
151                    _txRequiredList = txRequiredList;
152    
153                    if (_finderList != null) {
154                            Set<EntityColumn> finderColumns = new HashSet<EntityColumn>();
155    
156                            for (EntityFinder entityFinder : _finderList) {
157                                    finderColumns.addAll(entityFinder.getColumns());
158                            }
159    
160                            _finderColumnsList = new ArrayList<EntityColumn>(finderColumns);
161    
162                            Collections.sort(_finderColumnsList);
163                    }
164                    else {
165                            _finderColumnsList = Collections.emptyList();
166                    }
167    
168                    if ((_blobList != null) && !_blobList.isEmpty()) {
169                            for (EntityColumn col : _blobList) {
170                                    if (!col.isLazy()) {
171                                            cacheEnabled = false;
172    
173                                            break;
174                                    }
175                            }
176                    }
177    
178                    _cacheEnabled = cacheEnabled;
179    
180                    boolean containerModel = false;
181    
182                    if ((_columnList != null) && !_columnList.isEmpty()) {
183                            for (EntityColumn col : _columnList) {
184                                    if (col.isContainerModel() || col.isParentContainerModel()) {
185                                            containerModel = true;
186    
187                                            break;
188                                    }
189                            }
190                    }
191    
192                    _containerModel = containerModel;
193            }
194    
195            public void addReference(Entity reference) {
196                    _referenceList.add(reference);
197            }
198    
199            @Override
200            public boolean equals(Object obj) {
201                    if (this == obj) {
202                            return true;
203                    }
204    
205                    if (!(obj instanceof Entity)) {
206                            return false;
207                    }
208    
209                    Entity entity = (Entity)obj;
210    
211                    String name = entity.getName();
212    
213                    if (_name.equals(name)) {
214                            return true;
215                    }
216                    else {
217                            return false;
218                    }
219            }
220    
221            public String getAlias() {
222                    return _alias;
223            }
224    
225            public List<EntityColumn> getBadNamedColumnsList() {
226                    List<EntityColumn> badNamedColumnsList = ListUtil.copy(_columnList);
227    
228                    Iterator<EntityColumn> itr = badNamedColumnsList.iterator();
229    
230                    while (itr.hasNext()) {
231                            EntityColumn col = itr.next();
232    
233                            String name = col.getName();
234    
235                            if (name.equals(col.getDBName())) {
236                                    itr.remove();
237                            }
238                    }
239    
240                    return badNamedColumnsList;
241            }
242    
243            public List<EntityColumn> getBlobList() {
244                    return _blobList;
245            }
246    
247            public List<EntityFinder> getCollectionFinderList() {
248                    List<EntityFinder> finderList = ListUtil.copy(_finderList);
249    
250                    Iterator<EntityFinder> itr = finderList.iterator();
251    
252                    while (itr.hasNext()) {
253                            EntityFinder finder = itr.next();
254    
255                            if (!finder.isCollection()) {
256                                    itr.remove();
257                            }
258                    }
259    
260                    return finderList;
261            }
262    
263            public List<EntityColumn> getCollectionList() {
264                    return _collectionList;
265            }
266    
267            public EntityColumn getColumn(String name) {
268                    return getColumn(name, _columnList);
269            }
270    
271            public EntityColumn getColumnByMappingTable(String mappingTable) {
272                    for (EntityColumn col : _columnList) {
273                            if ((col.getMappingTable() != null) &&
274                                    col.getMappingTable().equals(mappingTable)) {
275    
276                                    return col;
277                            }
278                    }
279    
280                    return null;
281            }
282    
283            public List<EntityColumn> getColumnList() {
284                    return _columnList;
285            }
286    
287            public String getDataSource() {
288                    return _dataSource;
289            }
290    
291            public EntityColumn getFilterPKColumn() {
292                    for (EntityColumn col : _columnList) {
293                            if (col.isFilterPrimary()) {
294                                    return col;
295                            }
296                    }
297    
298                    return _getPKColumn();
299            }
300    
301            public String getFinderClass() {
302                    return _finderClass;
303            }
304    
305            public List<EntityColumn> getFinderColumnsList() {
306                    return _finderColumnsList;
307            }
308    
309            public List<EntityFinder> getFinderList() {
310                    return _finderList;
311            }
312    
313            public String getHumanName() {
314                    return _humanName;
315            }
316    
317            public String getHumanNames() {
318                    return TextFormatter.formatPlural(_humanName);
319            }
320    
321            public String getName() {
322                    return _name;
323            }
324    
325            public String getNames() {
326                    return TextFormatter.formatPlural(_name);
327            }
328    
329            public EntityOrder getOrder() {
330                    return _order;
331            }
332    
333            public String getPackagePath() {
334                    return _packagePath;
335            }
336    
337            public List<String> getParentTransients() {
338                    return _parentTransients;
339            }
340    
341            public String getPersistenceClass() {
342                    return _persistenceClass;
343            }
344    
345            public String getPKClassName() {
346                    if (hasCompoundPK()) {
347                            return _name + "PK";
348                    }
349    
350                    EntityColumn col = _getPKColumn();
351    
352                    return col.getType();
353            }
354    
355            public String getPKDBName() {
356                    if (hasCompoundPK()) {
357                            return getVarName() + "PK";
358                    }
359    
360                    EntityColumn col = _getPKColumn();
361    
362                    return col.getDBName();
363            }
364    
365            public List<EntityColumn> getPKList() {
366                    return _pkList;
367            }
368    
369            public String getPKVarName() {
370                    if (hasCompoundPK()) {
371                            return getVarName() + "PK";
372                    }
373    
374                    EntityColumn col = _getPKColumn();
375    
376                    return col.getName();
377            }
378    
379            public String getPKVarNames() {
380                    if (hasCompoundPK()) {
381                            return getVarName() + "PKs";
382                    }
383    
384                    EntityColumn col = _getPKColumn();
385    
386                    return col.getNames();
387            }
388    
389            public String getPortletName() {
390                    return _portletName;
391            }
392    
393            public String getPortletShortName() {
394                    return _portletShortName;
395            }
396    
397            public List<Entity> getReferenceList() {
398                    return _referenceList;
399            }
400    
401            public List<EntityColumn> getRegularColList() {
402                    return _regularColList;
403            }
404    
405            public String getSessionFactory() {
406                    return _sessionFactory;
407            }
408    
409            public String getShortName() {
410                    if (_name.startsWith(_portletShortName)) {
411                            return _name.substring(_portletShortName.length());
412                    }
413                    else {
414                            return _name;
415                    }
416            }
417    
418            public String getSpringPropertyName() {
419                    return TextFormatter.format(_name, TextFormatter.L);
420            }
421    
422            public String getTable() {
423                    return _table;
424            }
425    
426            public List<String> getTransients() {
427                    return _transients;
428            }
429    
430            public String getTXManager() {
431                    return _txManager;
432            }
433    
434            public List<String> getTxRequiredList() {
435                    return _txRequiredList;
436            }
437    
438            public List<EntityFinder> getUniqueFinderList() {
439                    List<EntityFinder> finderList = ListUtil.copy(_finderList);
440    
441                    Iterator<EntityFinder> itr = finderList.iterator();
442    
443                    while (itr.hasNext()) {
444                            EntityFinder finder = itr.next();
445    
446                            if (finder.isCollection() && !finder.isUnique()) {
447                                    itr.remove();
448                            }
449                    }
450    
451                    return finderList;
452            }
453    
454            public List<String> getUnresolvedReferenceList() {
455                    if (_unresolvedReferenceList == null) {
456                            return new ArrayList<String>();
457                    }
458    
459                    return _unresolvedReferenceList;
460            }
461    
462            public String getVarName() {
463                    return TextFormatter.format(_name, TextFormatter.I);
464            }
465    
466            public String getVarNames() {
467                    return TextFormatter.formatPlural(getVarName());
468            }
469    
470            public boolean hasActionableDynamicQuery() {
471                    if (hasColumns() && hasLocalService()) {
472                            if (hasCompoundPK()) {
473                                    EntityColumn col = _pkList.get(0);
474    
475                                    return col.isPrimitiveType();
476                            }
477                            else {
478                                    return hasPrimitivePK();
479                            }
480                    }
481    
482                    return false;
483            }
484    
485            public boolean hasArrayableOperator() {
486                    for (EntityFinder finder : _finderList) {
487                            if (finder.hasArrayableOperator()) {
488                                    return true;
489                            }
490                    }
491    
492                    return false;
493            }
494    
495            public boolean hasColumn(String name) {
496                    return hasColumn(name, _columnList);
497            }
498    
499            public boolean hasColumn(String name, String type) {
500                    return hasColumn(name, type, _columnList);
501            }
502    
503            public boolean hasColumns() {
504                    if (ListUtil.isEmpty(_columnList)) {
505                            return false;
506                    }
507                    else {
508                            return true;
509                    }
510            }
511    
512            public boolean hasCompoundPK() {
513                    if (_pkList.size() > 1) {
514                            return true;
515                    }
516                    else {
517                            return false;
518                    }
519            }
520    
521            public boolean hasEagerBlobColumn() {
522                    if ((_blobList == null) || _blobList.isEmpty()) {
523                            return false;
524                    }
525    
526                    for (EntityColumn col : _blobList) {
527                            if (!col.isLazy()) {
528                                    return true;
529                            }
530                    }
531    
532                    return false;
533            }
534    
535            public boolean hasFinderClass() {
536                    if (Validator.isNull(_finderClass)) {
537                            return false;
538                    }
539                    else {
540                            return true;
541                    }
542            }
543    
544            @Override
545            public int hashCode() {
546                    return _name.hashCode();
547            }
548    
549            public boolean hasLazyBlobColumn() {
550                    if ((_blobList == null) || _blobList.isEmpty()) {
551                            return false;
552                    }
553    
554                    for (EntityColumn col : _blobList) {
555                            if (col.isLazy()) {
556                                    return true;
557                            }
558                    }
559    
560                    return false;
561            }
562    
563            public boolean hasLocalService() {
564                    return _localService;
565            }
566    
567            public boolean hasPrimitivePK() {
568                    return hasPrimitivePK(true);
569            }
570    
571            public boolean hasPrimitivePK(boolean includeWrappers) {
572                    if (hasCompoundPK()) {
573                            return false;
574                    }
575    
576                    EntityColumn col = _getPKColumn();
577    
578                    if (col.isPrimitiveType(includeWrappers)) {
579                            return true;
580                    }
581                    else {
582                            return false;
583                    }
584            }
585    
586            public boolean hasRemoteService() {
587                    return _remoteService;
588            }
589    
590            public boolean hasUuid() {
591                    return _uuid;
592            }
593    
594            public boolean hasUuidAccessor() {
595                    return _uuidAccessor;
596            }
597    
598            public boolean isAttachedModel() {
599                    if (!isTypedModel()) {
600                            return false;
601                    }
602    
603                    if (hasColumn("classPK")) {
604                            EntityColumn classPKCol = getColumn("classPK");
605    
606                            String classPKColType = classPKCol.getType();
607    
608                            if (classPKColType.equals("long")) {
609                                    return true;
610                            }
611                    }
612    
613                    return false;
614            }
615    
616            public boolean isAuditedModel() {
617                    if (hasColumn("companyId") && hasColumn("createDate", "Date") &&
618                            hasColumn("modifiedDate", "Date") && hasColumn("userId") &&
619                            hasColumn("userName")) {
620    
621                            return true;
622                    }
623    
624                    return false;
625            }
626    
627            public boolean isCacheEnabled() {
628                    return _cacheEnabled;
629            }
630    
631            public boolean isContainerModel() {
632                    return _containerModel;
633            }
634    
635            public boolean isDefaultDataSource() {
636                    if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
637                            return true;
638                    }
639                    else {
640                            return false;
641                    }
642            }
643    
644            public boolean isDefaultSessionFactory() {
645                    if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
646                            return true;
647                    }
648                    else {
649                            return false;
650                    }
651            }
652    
653            public boolean isDefaultTXManager() {
654                    if (_txManager.equals(DEFAULT_TX_MANAGER)) {
655                            return true;
656                    }
657                    else {
658                            return false;
659                    }
660            }
661    
662            public boolean isDeprecated() {
663                    return _deprecated;
664            }
665    
666            public boolean isDynamicUpdateEnabled() {
667                    return _dynamicUpdateEnabled;
668            }
669    
670            public boolean isGroupedModel() {
671                    String pkVarName = getPKVarName();
672    
673                    if (isAuditedModel() && hasColumn("groupId") &&
674                            !pkVarName.equals("groupId")) {
675    
676                            return true;
677                    }
678                    else {
679                            return false;
680                    }
681            }
682    
683            public boolean isHierarchicalTree() {
684                    if (!hasPrimitivePK()) {
685                            return false;
686                    }
687    
688                    EntityColumn col = _getPKColumn();
689    
690                    if ((_columnList.indexOf(
691                                    new EntityColumn("parent" + col.getMethodName())) != -1) &&
692                            (_columnList.indexOf(
693                                    new EntityColumn("left" + col.getMethodName())) != -1) &&
694                            (_columnList.indexOf(
695                                    new EntityColumn("right" + col.getMethodName())) != -1)) {
696    
697                            return true;
698                    }
699                    else {
700                            return false;
701                    }
702            }
703    
704            public boolean isJsonEnabled() {
705                    return _jsonEnabled;
706            }
707    
708            public boolean isLocalizedModel() {
709                    for (EntityColumn col : _columnList) {
710                            if (col.isLocalized()) {
711                                    return true;
712                            }
713                    }
714    
715                    return false;
716            }
717    
718            public boolean isMvccEnabled() {
719                    return _mvccEnabled;
720            }
721    
722            public boolean isOrdered() {
723                    if (_order != null) {
724                            return true;
725                    }
726                    else {
727                            return false;
728                    }
729            }
730    
731            public boolean isPermissionCheckEnabled() {
732                    for (EntityFinder finder : _finderList) {
733                            if (isPermissionCheckEnabled(finder)) {
734                                    return true;
735                            }
736                    }
737    
738                    return false;
739            }
740    
741            public boolean isPermissionCheckEnabled(EntityFinder finder) {
742                    if (_name.equals("Group") || _name.equals("User") ||
743                            finder.getName().equals("UUID_G") || !finder.isCollection() ||
744                            !hasPrimitivePK() ||
745                            !ResourceActionsUtil.hasModelResourceActions(
746                                    _packagePath + ".model." + _name)) {
747    
748                            return false;
749                    }
750    
751                    if (hasColumn("groupId") && !finder.hasColumn("groupId")) {
752                            return false;
753                    }
754    
755                    return true;
756            }
757    
758            public boolean isPermissionedModel() {
759                    if (hasColumn("resourceBlockId")) {
760                            return true;
761                    }
762                    else {
763                            return false;
764                    }
765            }
766    
767            public boolean isPortalReference() {
768                    return _portalReference;
769            }
770    
771            public boolean isResolved() {
772                    if ((_unresolvedReferenceList != null) &&
773                            _unresolvedReferenceList.isEmpty()) {
774    
775                            return true;
776                    }
777    
778                    return false;
779            }
780    
781            public boolean isResourcedModel() {
782                    String pkVarName = getPKVarName();
783    
784                    if (hasColumn("resourcePrimKey") &&
785                            !pkVarName.equals("resourcePrimKey")) {
786    
787                            return true;
788                    }
789                    else {
790                            return false;
791                    }
792            }
793    
794            public boolean isStagedAuditedModel() {
795                    if (isAuditedModel() && isStagedModel()) {
796                            return true;
797                    }
798    
799                    return false;
800            }
801    
802            public boolean isStagedGroupedModel() {
803                    if (isGroupedModel() && isStagedModel()) {
804                            return true;
805                    }
806    
807                    return false;
808            }
809    
810            public boolean isStagedModel() {
811                    if (hasUuid() && hasColumn("companyId") &&
812                            hasColumn("createDate", "Date") &&
813                            hasColumn("modifiedDate", "Date")) {
814    
815                            return true;
816                    }
817    
818                    return false;
819            }
820    
821            public boolean isTrashEnabled() {
822                    return _trashEnabled;
823            }
824    
825            public boolean isTreeModel() {
826                    if (hasColumn("treePath")) {
827                            return true;
828                    }
829    
830                    return false;
831            }
832    
833            public boolean isTypedModel() {
834                    if (hasColumn("classNameId")) {
835                            EntityColumn classNameIdCol = getColumn("classNameId");
836    
837                            String classNameIdColType = classNameIdCol.getType();
838    
839                            if (classNameIdColType.equals("long")) {
840                                    return true;
841                            }
842                    }
843    
844                    return false;
845            }
846    
847            public boolean isWorkflowEnabled() {
848                    if (hasColumn("status") && hasColumn("statusByUserId") &&
849                            hasColumn("statusByUserName") && hasColumn("statusDate")) {
850    
851                            return true;
852                    }
853                    else {
854                            return false;
855                    }
856            }
857    
858            public void setParentTransients(List<String> transients) {
859                    _parentTransients = transients;
860            }
861    
862            public void setPortalReference(boolean portalReference) {
863                    _portalReference = portalReference;
864            }
865    
866            public void setResolved() {
867                    _unresolvedReferenceList = null;
868            }
869    
870            public void setTransients(List<String> transients) {
871                    _transients = transients;
872            }
873    
874            private EntityColumn _getPKColumn() {
875                    if (_pkList.isEmpty()) {
876                            throw new RuntimeException(
877                                    "There is no primary key for entity " + _name);
878                    }
879    
880                    return _pkList.get(0);
881            }
882    
883            private final String _alias;
884            private List<EntityColumn> _blobList;
885            private final boolean _cacheEnabled;
886            private final List<EntityColumn> _collectionList;
887            private final List<EntityColumn> _columnList;
888            private final boolean _containerModel;
889            private final String _dataSource;
890            private final boolean _deprecated;
891            private final boolean _dynamicUpdateEnabled;
892            private final String _finderClass;
893            private final List<EntityColumn> _finderColumnsList;
894            private final List<EntityFinder> _finderList;
895            private final String _humanName;
896            private final boolean _jsonEnabled;
897            private final boolean _localService;
898            private final boolean _mvccEnabled;
899            private final String _name;
900            private final EntityOrder _order;
901            private final String _packagePath;
902            private List<String> _parentTransients;
903            private final String _persistenceClass;
904            private final List<EntityColumn> _pkList;
905            private boolean _portalReference;
906            private final String _portletName;
907            private final String _portletShortName;
908            private final List<Entity> _referenceList;
909            private final List<EntityColumn> _regularColList;
910            private final boolean _remoteService;
911            private final String _sessionFactory;
912            private final String _table;
913            private List<String> _transients;
914            private final boolean _trashEnabled;
915            private final String _txManager;
916            private final List<String> _txRequiredList;
917            private List<String> _unresolvedReferenceList;
918            private final boolean _uuid;
919            private final boolean _uuidAccessor;
920    
921    }