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