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