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