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