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