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