1
14
15 package com.liferay.portal.tools.servicebuilder;
16
17 import com.liferay.portal.kernel.util.GetterUtil;
18 import com.liferay.portal.kernel.util.ListUtil;
19 import com.liferay.portal.kernel.util.Validator;
20 import com.liferay.portal.security.permission.ResourceActionsUtil;
21 import com.liferay.util.TextFormatter;
22
23 import java.util.Iterator;
24 import java.util.List;
25
26
31 public class Entity {
32
33 public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
34
35 public static final String DEFAULT_SESSION_FACTORY =
36 "liferaySessionFactory";
37
38 public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
39
40 public static EntityColumn getColumn(
41 String name, List<EntityColumn> columnList) {
42
43 int pos = columnList.indexOf(new EntityColumn(name));
44
45 if (pos != -1) {
46 return columnList.get(pos);
47 }
48 else {
49 throw new RuntimeException("Column " + name + " not found");
50 }
51 }
52
53 public static boolean hasColumn(
54 String name, List<EntityColumn> columnList) {
55
56 int pos = columnList.indexOf(new EntityColumn(name));
57
58 if (pos != -1) {
59 return true;
60 }
61 else {
62 return false;
63 }
64 }
65
66 public Entity(String name) {
67 this(
68 null, null, null, name, null, null, false, false, true, null, null,
69 null, null, null, true, null, null, null, null, null, null, null,
70 null);
71 }
72
73 public Entity(
74 String packagePath, String portletName, String portletShortName,
75 String name, String table, String alias, boolean uuid,
76 boolean localService, boolean remoteService, String persistenceClass,
77 String finderClass, String dataSource, String sessionFactory,
78 String txManager, boolean cacheEnabled, List<EntityColumn> pkList,
79 List<EntityColumn> regularColList, List<EntityColumn> collectionList,
80 List<EntityColumn> columnList, EntityOrder order,
81 List<EntityFinder> finderList, List<Entity> referenceList,
82 List<String> txRequiredList) {
83
84 _packagePath = packagePath;
85 _portletName = portletName;
86 _portletShortName = portletShortName;
87 _name = name;
88 _table = table;
89 _alias = alias;
90 _uuid = uuid;
91 _localService = localService;
92 _remoteService = remoteService;
93 _persistenceClass = persistenceClass;
94 _finderClass = finderClass;
95 _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
96 _sessionFactory = GetterUtil.getString(
97 sessionFactory, DEFAULT_SESSION_FACTORY);
98 _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
99 _cacheEnabled = cacheEnabled;
100 _pkList = pkList;
101 _regularColList = regularColList;
102 _collectionList = collectionList;
103 _columnList = columnList;
104 _order = order;
105 _finderList = finderList;
106 _referenceList = referenceList;
107 _txRequiredList = txRequiredList;
108 }
109
110 public boolean equals(Object obj) {
111 Entity entity = (Entity)obj;
112
113 String name = entity.getName();
114
115 if (_name.equals(name)) {
116 return true;
117 }
118 else {
119 return false;
120 }
121 }
122
123 public String getAlias() {
124 return _alias;
125 }
126
127 public List<EntityFinder> getCollectionFinderList() {
128 List<EntityFinder> finderList = ListUtil.copy(_finderList);
129
130 Iterator<EntityFinder> itr = finderList.iterator();
131
132 while (itr.hasNext()) {
133 EntityFinder finder = itr.next();
134
135 if (!finder.isCollection()) {
136 itr.remove();
137 }
138 }
139
140 return finderList;
141 }
142
143 public List<EntityColumn> getCollectionList() {
144 return _collectionList;
145 }
146
147 public EntityColumn getColumn(String name) {
148 return getColumn(name, _columnList);
149 }
150
151 public EntityColumn getColumnByMappingTable(String mappingTable) {
152 for (int i = 0; i < _columnList.size(); i++) {
153 EntityColumn col = _columnList.get(i);
154
155 if ((col.getMappingTable() != null) &&
156 col.getMappingTable().equals(mappingTable)) {
157
158 return col;
159 }
160 }
161
162 return null;
163 }
164
165 public List<EntityColumn> getColumnList() {
166 return _columnList;
167 }
168
169 public String getDataSource() {
170 return _dataSource;
171 }
172
173 public String getFinderClass() {
174 return _finderClass;
175 }
176
177 public List<EntityFinder> getFinderList() {
178 return _finderList;
179 }
180
181 public String getName() {
182 return _name;
183 }
184
185 public String getNames() {
186 return TextFormatter.formatPlural(new String(_name));
187 }
188
189 public EntityOrder getOrder() {
190 return _order;
191 }
192
193 public String getPackagePath() {
194 return _packagePath;
195 }
196
197 public List<String> getParentTransients() {
198 return _parentTransients;
199 }
200
201 public String getPersistenceClass() {
202 return _persistenceClass;
203 }
204
205 public String getPKDBName() {
206 if (hasCompoundPK()) {
207 return getVarName() + "PK";
208 }
209 else {
210 EntityColumn col = _getPKColumn();
211
212 return col.getDBName();
213 }
214 }
215
216 public String getPKClassName() {
217 if (hasCompoundPK()) {
218 return _name + "PK";
219 }
220 else {
221 EntityColumn col = _getPKColumn();
222
223 return col.getType();
224 }
225 }
226
227 public List<EntityColumn> getPKList() {
228 return _pkList;
229 }
230
231 public String getPKVarName() {
232 if (hasCompoundPK()) {
233 return getVarName() + "PK";
234 }
235 else {
236 EntityColumn col = _getPKColumn();
237
238 return col.getName();
239 }
240 }
241
242 public String getPortletName() {
243 return _portletName;
244 }
245
246 public String getPortletShortName() {
247 return _portletShortName;
248 }
249
250 public List<Entity> getReferenceList() {
251 return _referenceList;
252 }
253
254 public List<EntityColumn> getRegularColList() {
255 return _regularColList;
256 }
257
258 public String getSessionFactory() {
259 return _sessionFactory;
260 }
261
262 public String getShortName() {
263 if (_name.startsWith(_portletShortName)) {
264 return _name.substring(_portletShortName.length());
265 }
266 else {
267 return _name;
268 }
269 }
270
271 public String getSpringPropertyName() {
272 return TextFormatter.format(_name, TextFormatter.L);
273 }
274
275 public String getTable() {
276 return _table;
277 }
278
279 public List<String> getTransients() {
280 return _transients;
281 }
282
283 public String getTXManager() {
284 return _txManager;
285 }
286
287 public List<String> getTxRequiredList() {
288 return _txRequiredList;
289 }
290
291 public List<EntityFinder> getUniqueFinderList() {
292 List<EntityFinder> finderList = ListUtil.copy(_finderList);
293
294 Iterator<EntityFinder> itr = finderList.iterator();
295
296 while (itr.hasNext()) {
297 EntityFinder finder = itr.next();
298
299 if (finder.isCollection()) {
300 itr.remove();
301 }
302 }
303
304 return finderList;
305 }
306
307 public String getVarName() {
308 return TextFormatter.format(_name, TextFormatter.I);
309 }
310
311 public String getVarNames() {
312 return TextFormatter.formatPlural(new String(getVarName()));
313 }
314
315 public boolean hasColumn(String name) {
316 return hasColumn(name, _columnList);
317 }
318
319 public boolean hasColumns() {
320 if ((_columnList == null) || (_columnList.size() == 0)) {
321 return false;
322 }
323 else {
324 return true;
325 }
326 }
327
328 public boolean hasCompoundPK() {
329 if (_pkList.size() > 1) {
330 return true;
331 }
332 else {
333 return false;
334 }
335 }
336
337 public boolean hasFinderClass() {
338 if (Validator.isNull(_finderClass)) {
339 return false;
340 }
341 else {
342 return true;
343 }
344 }
345
346 public int hashCode() {
347 return _name.hashCode();
348 }
349
350 public boolean hasLocalizedColumn() {
351 for (EntityColumn col : _columnList) {
352 if (col.isLocalized()) {
353 return true;
354 }
355 }
356
357 return false;
358 }
359
360 public boolean hasLocalService() {
361 return _localService;
362 }
363
364 public boolean hasPrimitivePK() {
365 if (hasCompoundPK()) {
366 return false;
367 }
368 else {
369 EntityColumn col = _getPKColumn();
370
371 if (col.isPrimitiveType()) {
372 return true;
373 }
374 else {
375 return false;
376 }
377 }
378 }
379
380 public boolean hasRemoteService() {
381 return _remoteService;
382 }
383
384 public boolean hasUuid() {
385 return _uuid;
386 }
387
388 public boolean isCacheEnabled() {
389 return _cacheEnabled;
390 }
391
392 public boolean isDefaultDataSource() {
393 if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
394 return true;
395 }
396 else {
397 return false;
398 }
399 }
400
401 public boolean isDefaultSessionFactory() {
402 if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
403 return true;
404 }
405 else {
406 return false;
407 }
408 }
409
410 public boolean isDefaultTXManager() {
411 if (_txManager.equals(DEFAULT_TX_MANAGER)) {
412 return true;
413 }
414 else {
415 return false;
416 }
417 }
418
419 public boolean isHierarchicalTree() {
420 if (!hasPrimitivePK()) {
421 return false;
422 }
423
424 EntityColumn col = _getPKColumn();
425
426 if ((_columnList.indexOf(
427 new EntityColumn("parent" + col.getMethodName())) != -1) &&
428 (_columnList.indexOf(
429 new EntityColumn("left" + col.getMethodName())) != -1) &&
430 (_columnList.indexOf(
431 new EntityColumn("right" + col.getMethodName())) != -1)) {
432
433 return true;
434 }
435 else {
436 return false;
437 }
438 }
439
440 public boolean isOrdered() {
441 if (_order != null) {
442 return true;
443 }
444 else {
445 return false;
446 }
447 }
448
449 public boolean isPermissionCheckEnabled() {
450 for (EntityFinder finder : _finderList) {
451 if (isPermissionCheckEnabled(finder)) {
452 return true;
453 }
454 }
455
456 return false;
457 }
458
459 public boolean isPermissionCheckEnabled(EntityFinder finder) {
460 if (!finder.getName().equals("UUID_G") && hasPrimitivePK() &&
461 hasColumn("userId") && finder.hasColumn("groupId") &&
462 ResourceActionsUtil.hasModelResourceActions(
463 _packagePath + ".model." + _name)) {
464
465 return true;
466 }
467 else {
468 return false;
469 }
470 }
471
472 public boolean isPortalReference() {
473 return _portalReference;
474 }
475
476 public boolean isWorkflowEnabled() {
477 if (hasColumn("status") && hasColumn("statusByUserId") &&
478 hasColumn("statusByUserName") && hasColumn("statusDate")) {
479
480 return true;
481 }
482 else {
483 return false;
484 }
485 }
486
487 public void setParentTransients(List<String> transients) {
488 _parentTransients = transients;
489 }
490
491 public void setPortalReference(boolean portalReference) {
492 _portalReference = portalReference;
493 }
494
495 public void setTransients(List<String> transients) {
496 _transients = transients;
497 }
498
499 private EntityColumn _getPKColumn() {
500 if (_pkList.isEmpty()) {
501 throw new RuntimeException(
502 "There is no primary key for entity " + _name);
503 }
504
505 return _pkList.get(0);
506 }
507
508 private String _alias;
509 private boolean _cacheEnabled;
510 private List<EntityColumn> _collectionList;
511 private List<EntityColumn> _columnList;
512 private String _dataSource;
513 private String _finderClass;
514 private List<EntityFinder> _finderList;
515 private boolean _localService;
516 private String _name;
517 private EntityOrder _order;
518 private String _packagePath;
519 private List<String> _parentTransients;
520 private String _persistenceClass;
521 private List<EntityColumn> _pkList;
522 private boolean _portalReference;
523 private String _portletName;
524 private String _portletShortName;
525 private List<Entity> _referenceList;
526 private List<EntityColumn> _regularColList;
527 private boolean _remoteService;
528 private String _sessionFactory;
529 private String _table;
530 private List<String> _transients;
531 private String _txManager;
532 private List<String> _txRequiredList;
533 private boolean _uuid;
534
535 }