001
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
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
325 EntityColumn col = _getPKColumn();
326
327 return col.getType();
328 }
329
330 public String getPKDBName() {
331 if (hasCompoundPK()) {
332 return getVarName() + "PK";
333 }
334
335 EntityColumn col = _getPKColumn();
336
337 return col.getDBName();
338 }
339
340 public List<EntityColumn> getPKList() {
341 return _pkList;
342 }
343
344 public String getPKVarName() {
345 if (hasCompoundPK()) {
346 return getVarName() + "PK";
347 }
348
349 EntityColumn col = _getPKColumn();
350
351 return col.getName();
352 }
353
354 public String getPKVarNames() {
355 if (hasCompoundPK()) {
356 return getVarName() + "PKs";
357 }
358
359 EntityColumn col = _getPKColumn();
360
361 return col.getNames();
362 }
363
364 public String getPortletName() {
365 return _portletName;
366 }
367
368 public String getPortletShortName() {
369 return _portletShortName;
370 }
371
372 public List<Entity> getReferenceList() {
373 return _referenceList;
374 }
375
376 public List<EntityColumn> getRegularColList() {
377 return _regularColList;
378 }
379
380 public String getSessionFactory() {
381 return _sessionFactory;
382 }
383
384 public String getShortName() {
385 if (_name.startsWith(_portletShortName)) {
386 return _name.substring(_portletShortName.length());
387 }
388 else {
389 return _name;
390 }
391 }
392
393 public String getSpringPropertyName() {
394 return TextFormatter.format(_name, TextFormatter.L);
395 }
396
397 public String getTable() {
398 return _table;
399 }
400
401 public List<String> getTransients() {
402 return _transients;
403 }
404
405 public String getTXManager() {
406 return _txManager;
407 }
408
409 public List<String> getTxRequiredList() {
410 return _txRequiredList;
411 }
412
413 public List<EntityFinder> getUniqueFinderList() {
414 List<EntityFinder> finderList = ListUtil.copy(_finderList);
415
416 Iterator<EntityFinder> itr = finderList.iterator();
417
418 while (itr.hasNext()) {
419 EntityFinder finder = itr.next();
420
421 if (finder.isCollection() && !finder.isUnique()) {
422 itr.remove();
423 }
424 }
425
426 return finderList;
427 }
428
429 public String getVarName() {
430 return TextFormatter.format(_name, TextFormatter.I);
431 }
432
433 public String getVarNames() {
434 return TextFormatter.formatPlural(getVarName());
435 }
436
437 public boolean hasActionableDynamicQuery() {
438 if (hasColumns() && hasLocalService()) {
439 if (hasCompoundPK()) {
440 EntityColumn col = _pkList.get(0);
441
442 return col.isPrimitiveType();
443 }
444 else {
445 return hasPrimitivePK();
446 }
447 }
448
449 return false;
450 }
451
452 public boolean hasArrayableOperator() {
453 for (EntityFinder finder : _finderList) {
454 if (finder.hasArrayableOperator()) {
455 return true;
456 }
457 }
458
459 return false;
460 }
461
462 public boolean hasColumn(String name) {
463 return hasColumn(name, _columnList);
464 }
465
466 public boolean hasColumn(String name, String type) {
467 return hasColumn(name, type, _columnList);
468 }
469
470 public boolean hasColumns() {
471 if ((_columnList == null) || (_columnList.size() == 0)) {
472 return false;
473 }
474 else {
475 return true;
476 }
477 }
478
479 public boolean hasCompoundPK() {
480 if (_pkList.size() > 1) {
481 return true;
482 }
483 else {
484 return false;
485 }
486 }
487
488 public boolean hasEagerBlobColumn() {
489 if ((_blobList == null) || _blobList.isEmpty()) {
490 return false;
491 }
492
493 for (EntityColumn col : _blobList) {
494 if (!col.isLazy()) {
495 return true;
496 }
497 }
498
499 return false;
500 }
501
502 public boolean hasFinderClass() {
503 if (Validator.isNull(_finderClass)) {
504 return false;
505 }
506 else {
507 return true;
508 }
509 }
510
511 @Override
512 public int hashCode() {
513 return _name.hashCode();
514 }
515
516 public boolean hasLazyBlobColumn() {
517 if ((_blobList == null) || _blobList.isEmpty()) {
518 return false;
519 }
520
521 for (EntityColumn col : _blobList) {
522 if (col.isLazy()) {
523 return true;
524 }
525 }
526
527 return false;
528 }
529
530 public boolean hasLocalizedColumn() {
531 for (EntityColumn col : _columnList) {
532 if (col.isLocalized()) {
533 return true;
534 }
535 }
536
537 return false;
538 }
539
540 public boolean hasLocalService() {
541 return _localService;
542 }
543
544 public boolean hasPrimitivePK() {
545 return hasPrimitivePK(true);
546 }
547
548 public boolean hasPrimitivePK(boolean includeWrappers) {
549 if (hasCompoundPK()) {
550 return false;
551 }
552
553 EntityColumn col = _getPKColumn();
554
555 if (col.isPrimitiveType(includeWrappers)) {
556 return true;
557 }
558 else {
559 return false;
560 }
561 }
562
563 public boolean hasRemoteService() {
564 return _remoteService;
565 }
566
567 public boolean hasUuid() {
568 return _uuid;
569 }
570
571 public boolean hasUuidAccessor() {
572 return _uuidAccessor;
573 }
574
575 public boolean isAttachedModel() {
576 if (!isTypedModel()) {
577 return false;
578 }
579
580 if (hasColumn("classPK")) {
581 EntityColumn classPKCol = getColumn("classPK");
582
583 String classPKColType = classPKCol.getType();
584
585 if (classPKColType.equals("long")) {
586 return true;
587 }
588 }
589
590 return false;
591 }
592
593 public boolean isAuditedModel() {
594 if (hasColumn("companyId") && hasColumn("createDate", "Date") &&
595 hasColumn("modifiedDate", "Date") && hasColumn("userId") &&
596 hasColumn("userName")) {
597
598 return true;
599 }
600
601 return false;
602 }
603
604 public boolean isCacheEnabled() {
605 return _cacheEnabled;
606 }
607
608 public boolean isContainerModel() {
609 return _containerModel;
610 }
611
612 public boolean isDefaultDataSource() {
613 if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
614 return true;
615 }
616 else {
617 return false;
618 }
619 }
620
621 public boolean isDefaultSessionFactory() {
622 if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
623 return true;
624 }
625 else {
626 return false;
627 }
628 }
629
630 public boolean isDefaultTXManager() {
631 if (_txManager.equals(DEFAULT_TX_MANAGER)) {
632 return true;
633 }
634 else {
635 return false;
636 }
637 }
638
639 public boolean isDeprecated() {
640 return _deprecated;
641 }
642
643 public boolean isGroupedModel() {
644 String pkVarName = getPKVarName();
645
646 if (isAuditedModel() && hasColumn("groupId") &&
647 !pkVarName.equals("groupId")) {
648
649 return true;
650 }
651 else {
652 return false;
653 }
654 }
655
656 public boolean isHierarchicalTree() {
657 if (!hasPrimitivePK()) {
658 return false;
659 }
660
661 EntityColumn col = _getPKColumn();
662
663 if ((_columnList.indexOf(
664 new EntityColumn("parent" + col.getMethodName())) != -1) &&
665 (_columnList.indexOf(
666 new EntityColumn("left" + col.getMethodName())) != -1) &&
667 (_columnList.indexOf(
668 new EntityColumn("right" + col.getMethodName())) != -1)) {
669
670 return true;
671 }
672 else {
673 return false;
674 }
675 }
676
677 public boolean isJsonEnabled() {
678 return _jsonEnabled;
679 }
680
681 public boolean isOrdered() {
682 if (_order != null) {
683 return true;
684 }
685 else {
686 return false;
687 }
688 }
689
690 public boolean isPermissionCheckEnabled() {
691 for (EntityFinder finder : _finderList) {
692 if (isPermissionCheckEnabled(finder)) {
693 return true;
694 }
695 }
696
697 return false;
698 }
699
700 public boolean isPermissionCheckEnabled(EntityFinder finder) {
701 if (_name.equals("Group") || _name.equals("User") ||
702 finder.getName().equals("UUID_G") || !finder.isCollection() ||
703 !hasPrimitivePK() ||
704 !ResourceActionsUtil.hasModelResourceActions(
705 _packagePath + ".model." + _name)) {
706
707 return false;
708 }
709
710 if (hasColumn("groupId") && !finder.hasColumn("groupId")) {
711 return false;
712 }
713
714 return true;
715 }
716
717 public boolean isPermissionedModel() {
718 if (hasColumn("resourceBlockId")) {
719 return true;
720 }
721 else {
722 return false;
723 }
724 }
725
726 public boolean isPortalReference() {
727 return _portalReference;
728 }
729
730 public boolean isResourcedModel() {
731 String pkVarName = getPKVarName();
732
733 if (hasColumn("resourcePrimKey") &&
734 !pkVarName.equals("resourcePrimKey")) {
735
736 return true;
737 }
738 else {
739 return false;
740 }
741 }
742
743 public boolean isStagedAuditedModel() {
744 if (isAuditedModel() && isStagedModel()) {
745 return true;
746 }
747
748 return false;
749 }
750
751 public boolean isStagedGroupedModel() {
752 if (isGroupedModel() && isStagedModel()) {
753 return true;
754 }
755
756 return false;
757 }
758
759 public boolean isStagedModel() {
760 if (hasUuid() && hasColumn("companyId") &&
761 hasColumn("createDate", "Date") &&
762 hasColumn("modifiedDate", "Date")) {
763
764 return true;
765 }
766
767 return false;
768 }
769
770 public boolean isTypedModel() {
771 if (hasColumn("classNameId")) {
772 EntityColumn classNameIdCol = getColumn("classNameId");
773
774 String classNameIdColType = classNameIdCol.getType();
775
776 if (classNameIdColType.equals("long")) {
777 return true;
778 }
779 }
780
781 return false;
782 }
783
784 public boolean isWorkflowEnabled() {
785 if (hasColumn("status") && hasColumn("statusByUserId") &&
786 hasColumn("statusByUserName") && hasColumn("statusDate")) {
787
788 return true;
789 }
790 else {
791 return false;
792 }
793 }
794
795 public void setParentTransients(List<String> transients) {
796 _parentTransients = transients;
797 }
798
799 public void setPortalReference(boolean portalReference) {
800 _portalReference = portalReference;
801 }
802
803 public void setTransients(List<String> transients) {
804 _transients = transients;
805 }
806
807 private EntityColumn _getPKColumn() {
808 if (_pkList.isEmpty()) {
809 throw new RuntimeException(
810 "There is no primary key for entity " + _name);
811 }
812
813 return _pkList.get(0);
814 }
815
816 private String _alias;
817 private List<EntityColumn> _blobList;
818 private boolean _cacheEnabled;
819 private List<EntityColumn> _collectionList;
820 private List<EntityColumn> _columnList;
821 private boolean _containerModel;
822 private String _dataSource;
823 private boolean _deprecated;
824 private String _finderClass;
825 private List<EntityColumn> _finderColumnsList;
826 private List<EntityFinder> _finderList;
827 private String _humanName;
828 private boolean _jsonEnabled;
829 private boolean _localService;
830 private String _name;
831 private EntityOrder _order;
832 private String _packagePath;
833 private List<String> _parentTransients;
834 private String _persistenceClass;
835 private List<EntityColumn> _pkList;
836 private boolean _portalReference;
837 private String _portletName;
838 private String _portletShortName;
839 private List<Entity> _referenceList;
840 private List<EntityColumn> _regularColList;
841 private boolean _remoteService;
842 private String _sessionFactory;
843 private String _table;
844 private List<String> _transients;
845 private String _txManager;
846 private List<String> _txRequiredList;
847 private boolean _uuid;
848 private boolean _uuidAccessor;
849
850 }