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