001
014
015 package com.liferay.portal.tools.servicebuilder;
016
017 import com.liferay.portal.kernel.util.GetterUtil;
018 import com.liferay.portal.kernel.util.ListUtil;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.security.permission.ResourceActionsUtil;
021 import com.liferay.util.TextFormatter;
022
023 import java.util.Iterator;
024 import java.util.List;
025
026
029 public class Entity {
030
031 public static final String DEFAULT_DATA_SOURCE = "liferayDataSource";
032
033 public static final String DEFAULT_SESSION_FACTORY =
034 "liferaySessionFactory";
035
036 public static final String DEFAULT_TX_MANAGER = "liferayTransactionManager";
037
038 public static EntityColumn getColumn(
039 String name, List<EntityColumn> columnList) {
040
041 int pos = columnList.indexOf(new EntityColumn(name));
042
043 if (pos != -1) {
044 return columnList.get(pos);
045 }
046 else {
047 throw new RuntimeException("Column " + name + " not found");
048 }
049 }
050
051 public static boolean hasColumn(
052 String name, List<EntityColumn> columnList) {
053
054 int pos = columnList.indexOf(new EntityColumn(name));
055
056 if (pos != -1) {
057 return true;
058 }
059 else {
060 return false;
061 }
062 }
063
064 public Entity(String name) {
065 this(
066 null, null, null, name, null,null, null, false, false, true, null,
067 null, null, null, null, true, null, null, null, null, null, null,
068 null, null);
069 }
070
071 public Entity(
072 String packagePath, String portletName, String portletShortName,
073 String name, String humanName, String table, String alias, boolean uuid,
074 boolean localService, boolean remoteService, String persistenceClass,
075 String finderClass, String dataSource, String sessionFactory,
076 String txManager, boolean cacheEnabled, List<EntityColumn> pkList,
077 List<EntityColumn> regularColList, List<EntityColumn> collectionList,
078 List<EntityColumn> columnList, EntityOrder order,
079 List<EntityFinder> finderList, List<Entity> referenceList,
080 List<String> txRequiredList) {
081
082 _packagePath = packagePath;
083 _portletName = portletName;
084 _portletShortName = portletShortName;
085 _name = name;
086 _humanName = GetterUtil.getString(
087 humanName, TextFormatter.format(name, TextFormatter.H));
088 _table = table;
089 _alias = alias;
090 _uuid = uuid;
091 _localService = localService;
092 _remoteService = remoteService;
093 _persistenceClass = persistenceClass;
094 _finderClass = finderClass;
095 _dataSource = GetterUtil.getString(dataSource, DEFAULT_DATA_SOURCE);
096 _sessionFactory = GetterUtil.getString(
097 sessionFactory, DEFAULT_SESSION_FACTORY);
098 _txManager = GetterUtil.getString(txManager, DEFAULT_TX_MANAGER);
099 _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 (EntityColumn col : _columnList) {
153 if ((col.getMappingTable() != null) &&
154 col.getMappingTable().equals(mappingTable)) {
155
156 return col;
157 }
158 }
159
160 return null;
161 }
162
163 public List<EntityColumn> getColumnList() {
164 return _columnList;
165 }
166
167 public String getDataSource() {
168 return _dataSource;
169 }
170
171 public EntityColumn getFilterPKColumn() {
172 for (EntityColumn col : _columnList) {
173 if (col.isFilterPrimary()) {
174 return col;
175 }
176 }
177
178 return _getPKColumn();
179 }
180
181 public String getFinderClass() {
182 return _finderClass;
183 }
184
185 public List<EntityFinder> getFinderList() {
186 return _finderList;
187 }
188
189 public String getHumanName() {
190 return _humanName;
191 }
192
193 public String getHumanNames() {
194 return TextFormatter.formatPlural(_humanName);
195 }
196
197 public String getName() {
198 return _name;
199 }
200
201 public String getNames() {
202 return TextFormatter.formatPlural(_name);
203 }
204
205 public EntityOrder getOrder() {
206 return _order;
207 }
208
209 public String getPackagePath() {
210 return _packagePath;
211 }
212
213 public List<String> getParentTransients() {
214 return _parentTransients;
215 }
216
217 public String getPersistenceClass() {
218 return _persistenceClass;
219 }
220
221 public String getPKDBName() {
222 if (hasCompoundPK()) {
223 return getVarName() + "PK";
224 }
225 else {
226 EntityColumn col = _getPKColumn();
227
228 return col.getDBName();
229 }
230 }
231
232 public String getPKClassName() {
233 if (hasCompoundPK()) {
234 return _name + "PK";
235 }
236 else {
237 EntityColumn col = _getPKColumn();
238
239 return col.getType();
240 }
241 }
242
243 public List<EntityColumn> getPKList() {
244 return _pkList;
245 }
246
247 public String getPKVarName() {
248 if (hasCompoundPK()) {
249 return getVarName() + "PK";
250 }
251 else {
252 EntityColumn col = _getPKColumn();
253
254 return col.getName();
255 }
256 }
257
258 public String getPortletName() {
259 return _portletName;
260 }
261
262 public String getPortletShortName() {
263 return _portletShortName;
264 }
265
266 public List<Entity> getReferenceList() {
267 return _referenceList;
268 }
269
270 public List<EntityColumn> getRegularColList() {
271 return _regularColList;
272 }
273
274 public String getSessionFactory() {
275 return _sessionFactory;
276 }
277
278 public String getShortName() {
279 if (_name.startsWith(_portletShortName)) {
280 return _name.substring(_portletShortName.length());
281 }
282 else {
283 return _name;
284 }
285 }
286
287 public String getSpringPropertyName() {
288 return TextFormatter.format(_name, TextFormatter.L);
289 }
290
291 public String getTable() {
292 return _table;
293 }
294
295 public List<String> getTransients() {
296 return _transients;
297 }
298
299 public String getTXManager() {
300 return _txManager;
301 }
302
303 public List<String> getTxRequiredList() {
304 return _txRequiredList;
305 }
306
307 public List<EntityFinder> getUniqueFinderList() {
308 List<EntityFinder> finderList = ListUtil.copy(_finderList);
309
310 Iterator<EntityFinder> itr = finderList.iterator();
311
312 while (itr.hasNext()) {
313 EntityFinder finder = itr.next();
314
315 if (finder.isCollection()) {
316 itr.remove();
317 }
318 }
319
320 return finderList;
321 }
322
323 public String getVarName() {
324 return TextFormatter.format(_name, TextFormatter.I);
325 }
326
327 public String getVarNames() {
328 return TextFormatter.formatPlural(getVarName());
329 }
330
331 public boolean hasArrayableOperator() {
332 for (EntityFinder finder : _finderList) {
333 if (finder.hasArrayableOperator()) {
334 return true;
335 }
336 }
337
338 return false;
339 }
340
341 public boolean hasColumn(String name) {
342 return hasColumn(name, _columnList);
343 }
344
345 public boolean hasColumns() {
346 if ((_columnList == null) || (_columnList.size() == 0)) {
347 return false;
348 }
349 else {
350 return true;
351 }
352 }
353
354 public boolean hasCompoundPK() {
355 if (_pkList.size() > 1) {
356 return true;
357 }
358 else {
359 return false;
360 }
361 }
362
363 public boolean hasFinderClass() {
364 if (Validator.isNull(_finderClass)) {
365 return false;
366 }
367 else {
368 return true;
369 }
370 }
371
372 public int hashCode() {
373 return _name.hashCode();
374 }
375
376 public boolean hasLocalizedColumn() {
377 for (EntityColumn col : _columnList) {
378 if (col.isLocalized()) {
379 return true;
380 }
381 }
382
383 return false;
384 }
385
386 public boolean hasLocalService() {
387 return _localService;
388 }
389
390 public boolean hasPrimitivePK() {
391 if (hasCompoundPK()) {
392 return false;
393 }
394 else {
395 EntityColumn col = _getPKColumn();
396
397 if (col.isPrimitiveType()) {
398 return true;
399 }
400 else {
401 return false;
402 }
403 }
404 }
405
406 public boolean hasRemoteService() {
407 return _remoteService;
408 }
409
410 public boolean hasUuid() {
411 return _uuid;
412 }
413
414 public boolean isCacheEnabled() {
415 return _cacheEnabled;
416 }
417
418 public boolean isDefaultDataSource() {
419 if (_dataSource.equals(DEFAULT_DATA_SOURCE)) {
420 return true;
421 }
422 else {
423 return false;
424 }
425 }
426
427 public boolean isDefaultSessionFactory() {
428 if (_sessionFactory.equals(DEFAULT_SESSION_FACTORY)) {
429 return true;
430 }
431 else {
432 return false;
433 }
434 }
435
436 public boolean isDefaultTXManager() {
437 if (_txManager.equals(DEFAULT_TX_MANAGER)) {
438 return true;
439 }
440 else {
441 return false;
442 }
443 }
444
445 public boolean isHierarchicalTree() {
446 if (!hasPrimitivePK()) {
447 return false;
448 }
449
450 EntityColumn col = _getPKColumn();
451
452 if ((_columnList.indexOf(
453 new EntityColumn("parent" + col.getMethodName())) != -1) &&
454 (_columnList.indexOf(
455 new EntityColumn("left" + col.getMethodName())) != -1) &&
456 (_columnList.indexOf(
457 new EntityColumn("right" + col.getMethodName())) != -1)) {
458
459 return true;
460 }
461 else {
462 return false;
463 }
464 }
465
466 public boolean isOrdered() {
467 if (_order != null) {
468 return true;
469 }
470 else {
471 return false;
472 }
473 }
474
475 public boolean isPermissionCheckEnabled() {
476 for (EntityFinder finder : _finderList) {
477 if (isPermissionCheckEnabled(finder)) {
478 return true;
479 }
480 }
481
482 return false;
483 }
484
485 public boolean isPermissionCheckEnabled(EntityFinder finder) {
486 if (!finder.getName().equals("UUID_G") && hasPrimitivePK() &&
487 hasColumn("userId") && finder.hasColumn("groupId") &&
488 ResourceActionsUtil.hasModelResourceActions(
489 _packagePath + ".model." + _name)) {
490
491 return true;
492 }
493 else {
494 return false;
495 }
496 }
497
498 public boolean isPortalReference() {
499 return _portalReference;
500 }
501
502 public boolean isWorkflowEnabled() {
503 if (hasColumn("status") && hasColumn("statusByUserId") &&
504 hasColumn("statusByUserName") && hasColumn("statusDate")) {
505
506 return true;
507 }
508 else {
509 return false;
510 }
511 }
512
513 public void setParentTransients(List<String> transients) {
514 _parentTransients = transients;
515 }
516
517 public void setPortalReference(boolean portalReference) {
518 _portalReference = portalReference;
519 }
520
521 public void setTransients(List<String> transients) {
522 _transients = transients;
523 }
524
525 private EntityColumn _getPKColumn() {
526 if (_pkList.isEmpty()) {
527 throw new RuntimeException(
528 "There is no primary key for entity " + _name);
529 }
530
531 return _pkList.get(0);
532 }
533
534 private String _alias;
535 private boolean _cacheEnabled;
536 private List<EntityColumn> _collectionList;
537 private List<EntityColumn> _columnList;
538 private String _dataSource;
539 private String _finderClass;
540 private List<EntityFinder> _finderList;
541 private String _humanName;
542 private boolean _localService;
543 private String _name;
544 private EntityOrder _order;
545 private String _packagePath;
546 private List<String> _parentTransients;
547 private String _persistenceClass;
548 private List<EntityColumn> _pkList;
549 private boolean _portalReference;
550 private String _portletName;
551 private String _portletShortName;
552 private List<Entity> _referenceList;
553 private List<EntityColumn> _regularColList;
554 private boolean _remoteService;
555 private String _sessionFactory;
556 private String _table;
557 private List<String> _transients;
558 private String _txManager;
559 private List<String> _txRequiredList;
560 private boolean _uuid;
561
562 }