1 package org.andromda.cartridges.jsf.metafacades;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.LinkedHashSet;
6 import org.andromda.cartridges.jsf.JSFGlobals;
7 import org.andromda.cartridges.jsf.JSFProfile;
8 import org.andromda.cartridges.jsf.JSFUtils;
9 import org.andromda.metafacades.uml.DependencyFacade;
10 import org.andromda.metafacades.uml.ManageableEntityAssociationEnd;
11 import org.andromda.metafacades.uml.ManageableEntityAttribute;
12 import org.andromda.metafacades.uml.Role;
13 import org.andromda.metafacades.uml.UMLMetafacadeProperties;
14 import org.andromda.utils.StringUtilsHelper;
15 import org.apache.commons.collections.Closure;
16 import org.apache.commons.collections.CollectionUtils;
17 import org.apache.commons.collections.Predicate;
18 import org.apache.commons.collections.Transformer;
19 import org.apache.commons.lang.ObjectUtils;
20 import org.apache.commons.lang.StringUtils;
21
22
23
24
25
26
27 public class JSFManageableEntityLogicImpl
28 extends JSFManageableEntityLogic
29 {
30 private static final long serialVersionUID = 34L;
31
32
33
34
35 public JSFManageableEntityLogicImpl(Object metaObject, String context)
36 {
37 super(metaObject, context);
38 }
39
40
41
42
43
44 protected String handleGetViewName()
45 {
46 return this.getName().toLowerCase() + "-crud";
47 }
48
49
50
51
52
53 protected String handleGetViewTitleKey()
54 {
55 return StringUtilsHelper.toResourceMessageKey(this.getName()) + ".view.title";
56 }
57
58
59
60
61
62 protected String handleGetViewTitleValue()
63 {
64 return StringUtilsHelper.toPhrase(getName());
65 }
66
67
68
69
70
71 protected String handleGetListName()
72 {
73 return "manageableList";
74 }
75
76
77
78
79
80 protected String handleGetFormBeanType()
81 {
82 return this.getManageablePackageName() + this.getNamespaceProperty() + this.getFormBeanClassName();
83 }
84
85
86
87
88
89 protected String handleGetFormBeanName()
90 {
91 final String pattern = ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.FORM_BEAN_PATTERN));
92 final String formBeanName = pattern.replaceFirst("\\{0\\}", "manage");
93 return formBeanName.replaceFirst("\\{1\\}", this.getName());
94 }
95
96
97
98
99
100 protected String handleGetExceptionKey()
101 {
102 return StringUtilsHelper.toResourceMessageKey(this.getName()) + ".exception";
103 }
104
105
106
107
108
109 protected String handleGetActionType()
110 {
111 return this.getManageablePackageName() + this.getNamespaceProperty() + this.getActionClassName();
112 }
113
114
115
116
117
118 protected String handleGetActionFullPath()
119 {
120 return '/' + StringUtils.replace(this.getActionType(), this.getNamespaceProperty(), "/");
121 }
122
123
124
125
126
127 protected String handleGetActionPath()
128 {
129 return '/' + this.getName() + "/Manage";
130 }
131
132
133
134
135
136 protected String handleGetActionClassName()
137 {
138 return "Manage" + getName();
139 }
140
141
142
143
144
145 protected String handleGetExceptionPath()
146 {
147 return this.getViewFullPath();
148 }
149
150
151
152
153
154 protected boolean handleIsPreload()
155 {
156 return false;
157
158 }
159
160
161
162
163 @Override
164 public org.andromda.metafacades.uml.ManageableEntityAttribute getManageableIdentifier()
165 {
166 return super.getManageableIdentifier();
167 }
168
169
170
171
172
173 protected String handleGetFormBeanClassName()
174 {
175 return StringUtils.capitalize(this.getFormBeanName());
176 }
177
178
179
180
181
182 protected String handleGetFormBeanFullPath()
183 {
184 return StringUtils.replace(this.getFormBeanType(), this.getNamespaceProperty(), "/");
185 }
186
187
188
189
190
191 protected String handleGetListGetterName()
192 {
193 return "getManageableList";
194 }
195
196
197
198
199
200 protected String handleGetListSetterName()
201 {
202 return "setManageableList";
203 }
204
205
206
207
208
209 protected String handleGetMessageKey()
210 {
211 return StringUtilsHelper.toResourceMessageKey(this.getName());
212 }
213
214
215
216
217
218 protected String handleGetMessageValue()
219 {
220 return StringUtilsHelper.toPhrase(this.getName());
221 }
222
223
224
225
226
227 protected String handleGetOnlineHelpKey()
228 {
229 return this.getMessageKey() + ".online.help";
230 }
231
232
233
234
235
236 protected String handleGetOnlineHelpValue()
237 {
238 final String value = StringUtilsHelper.toResourceMessage(this.getDocumentation("", 64, false));
239 return (value == null) ? "No entity documentation has been specified" : value;
240 }
241
242
243
244
245
246 protected String handleGetOnlineHelpActionPath()
247 {
248 return this.getActionPath() + "Help";
249 }
250
251
252
253
254
255 protected String handleGetOnlineHelpPagePath()
256 {
257 return '/' + this.getManageablePackagePath() + '/' + this.getName().toLowerCase() + "_help";
258 }
259
260
261
262
263
264 protected boolean handleIsTableExportable()
265 {
266 return this.getTableExportTypes().indexOf("none") == -1;
267 }
268
269
270
271
272
273 protected String handleGetTableExportTypes()
274 {
275 return null;
276
277
278
279
280 }
281
282
283
284
285
286 protected int handleGetTableMaxRows()
287 {
288 final Object taggedValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_TABLE_MAXROWS);
289 int pageSize;
290
291 try
292 {
293 pageSize = Integer.parseInt(String.valueOf(taggedValue));
294 }
295 catch (Exception e)
296 {
297 pageSize = JSFProfile.TAGGEDVALUE_TABLE_MAXROWS_DEFAULT_COUNT;
298 }
299
300 return pageSize;
301 }
302
303
304
305
306
307 protected boolean handleIsTableSortable()
308 {
309 final Object taggedValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_TABLE_SORTABLE);
310 return (taggedValue == null)
311 ? JSFProfile.TAGGEDVALUE_TABLE_SORTABLE_DEFAULT_VALUE
312 : Boolean.valueOf(String.valueOf(taggedValue)).booleanValue();
313 }
314
315
316
317
318
319 protected String handleGetControllerType()
320 {
321 return this.getManageablePackageName() + this.getNamespaceProperty() + this.getControllerName();
322 }
323
324
325
326
327
328 protected String handleGetControllerBeanName()
329 {
330 return StringUtils.uncapitalize(this.getName()) + "Controller";
331 }
332
333
334
335
336
337 protected String handleGetControllerFullPath()
338 {
339 return '/' + StringUtils.replace(this.getControllerType(), this.getNamespaceProperty(), "/");
340 }
341
342
343
344
345
346 protected String handleGetControllerName()
347 {
348 return this.getName() + "Controller";
349 }
350
351
352
353
354
355 protected String handleGetValueObjectClassName()
356 {
357 return getName() + this.getConfiguredProperty(JSFGlobals.CRUD_VALUE_OBJECT_SUFFIX);
358 }
359
360
361
362
363
364 protected String handleGetFormSerialVersionUID()
365 {
366 final StringBuilder buffer = new StringBuilder();
367 buffer.append(this.getFormBeanType());
368 addSerialUIDData(buffer);
369 return JSFUtils.calcSerialVersionUID(buffer);
370 }
371
372
373
374
375
376 protected String handleGetActionSerialVersionUID()
377 {
378 final StringBuilder buffer = new StringBuilder();
379 buffer.append(this.getActionFullPath());
380 addSerialUIDData(buffer);
381 return JSFUtils.calcSerialVersionUID(buffer);
382 }
383
384
385
386
387
388 protected String handleGetPopulatorName()
389 {
390 return ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.VIEW_POPULATOR_PATTERN)).replaceAll(
391 "\\{0\\}",
392 StringUtilsHelper.upperCamelCaseName(this.getFormBeanClassName()));
393 }
394
395
396
397
398
399 protected String handleGetPopulatorFullPath()
400 {
401 return '/' + StringUtils.replace(this.getPopulatorType(), this.getNamespaceProperty(), "/");
402 }
403
404
405
406
407
408 protected String handleGetPopulatorType()
409 {
410 return this.getManageablePackageName() + this.getNamespaceProperty() + this.getPopulatorName();
411 }
412
413
414
415
416
417 protected String handleGetViewFullPath()
418 {
419 return '/' + this.getManageablePackagePath() + '/' + this.getViewName();
420 }
421
422
423
424
425
426 protected String handleGetOdsExportFullPath()
427 {
428 return '/' + this.getManageablePackagePath() + '/' + this.getName().toLowerCase()+"-ods-export";
429 }
430
431
432
433
434
435 protected boolean handleIsValidationRequired()
436 {
437 for (final ManageableEntityAttribute attribute : this.getManageableAttributes())
438 {
439 if(attribute instanceof JSFManageableEntityAttribute)
440 {
441 final JSFManageableEntityAttribute jsfAttribute = (JSFManageableEntityAttribute)attribute;
442 if (jsfAttribute.isValidationRequired())
443 {
444 return true;
445 }
446 }
447 }
448 return false;
449 }
450
451
452
453
454
455 protected String handleGetSearchFormBeanName()
456 {
457 final String pattern = ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.FORM_BEAN_PATTERN));
458 final String formBeanName = pattern.replaceFirst("\\{0\\}", "manage");
459 return formBeanName.replaceFirst("\\{1\\}",this.getName() + "Search");
460 }
461
462
463
464
465
466 protected String handleGetSearchFormBeanType()
467 {
468 return this.getManageablePackageName() + this.getNamespaceProperty() + this.getSearchFormBeanClassName();
469 }
470
471
472
473
474
475 protected String handleGetSearchFormBeanFullPath()
476 {
477 return StringUtils.replace(this.getSearchFormBeanType(), this.getNamespaceProperty(), "/");
478 }
479
480
481
482
483
484 protected String handleGetSearchFormBeanClassName()
485 {
486 return StringUtils.capitalize(this.getSearchFormBeanName());
487 }
488
489
490
491
492
493 protected Collection<JSFManageableEntityAttribute> handleGetManageableSearchAttributes()
494 {
495 final Collection<JSFManageableEntityAttribute> coll = new ArrayList<JSFManageableEntityAttribute>();
496 for(final ManageableEntityAttribute attribute : getManageableAttributes())
497 {
498 if(attribute instanceof JSFManageableEntityAttribute)
499 {
500 final JSFManageableEntityAttribute attr=(JSFManageableEntityAttribute)attribute;
501 if(!attr.isHidden())
502 coll.add(attr);
503 }
504 }
505 return coll;
506 }
507
508
509
510
511
512 protected Collection<ManageableEntityAssociationEnd> handleGetManageableSearchAssociationEnds()
513 {
514 return getManageableAssociationEnds();
515 }
516
517
518
519
520
521
522 protected boolean handleIsSearchable(Object element)
523 {
524
525
526
527
528
529
530
531 if(element instanceof JSFManageableEntityAttribute)
532 {
533 return !((JSFManageableEntityAttribute)element).isHidden();
534 }
535 else
536 {
537 return true;
538 }
539 }
540
541
542
543
544 private String getNamespaceProperty()
545 {
546 return (String)this.getConfiguredProperty(UMLMetafacadeProperties.NAMESPACE_SEPARATOR);
547 }
548
549 private void addSerialUIDData(StringBuilder buffer)
550 {
551 for (final ManageableEntityAttribute attribute : this.getManageableAttributes())
552 {
553 buffer.append(attribute.getName());
554 }
555 for (final ManageableEntityAssociationEnd end : this.getManageableAssociationEnds())
556 {
557 buffer.append(end.getName());
558 }
559 }
560
561
562
563
564
565 protected Collection handleGetRoles()
566 {
567
568 final Collection roles = new ArrayList(this.getTargetDependencies());
569 CollectionUtils.filter(roles, new Predicate()
570 {
571 public boolean evaluate(final Object object)
572 {
573 DependencyFacade dependency = (DependencyFacade)object;
574 return dependency != null && dependency.getSourceElement() instanceof Role;
575 }
576 });
577 CollectionUtils.transform(roles, new Transformer()
578 {
579 public Object transform(final Object object)
580 {
581 return ((DependencyFacade)object).getSourceElement();
582 }
583 });
584 final Collection allRoles = new LinkedHashSet(roles);
585
586 CollectionUtils.forAllDo(roles, new Closure()
587 {
588 public void execute(final Object object)
589 {
590 allRoles.addAll(((Role)object).getAllSpecializations());
591 }
592 });
593 return allRoles;
594 }
595
596
597
598
599
600 protected String handleGetActionRoles()
601 {
602
603 final StringBuilder rolesBuffer = new StringBuilder();
604 boolean first = true;
605 for (final Role role : this.getRoles())
606 {
607 if (first)
608 {
609 first = false;
610 }
611 else
612 {
613 rolesBuffer.append(',');
614 }
615 rolesBuffer.append(role.getName());
616 }
617 return rolesBuffer.toString();
618 }
619
620
621
622
623
624 protected boolean handleIsNeedsFileUpload()
625 {
626 for (final ManageableEntityAttribute attribute : this.getManageableAttributes())
627 {
628 if(attribute instanceof JSFManageableEntityAttribute)
629 {
630 final JSFManageableEntityAttribute jsfAttribute = (JSFManageableEntityAttribute)attribute;
631 if(jsfAttribute.isNeedsFileUpload())
632 {
633 return true;
634 }
635 }
636 }
637 return false;
638 }
639
640
641
642
643
644 protected boolean handleIsNeedsUserInterface()
645 {
646 for (final ManageableEntityAttribute attribute : this.getManageableAttributes())
647 {
648 if(attribute instanceof JSFManageableEntityAttribute)
649 {
650 final JSFManageableEntityAttribute jsfAttribute = (JSFManageableEntityAttribute)attribute;
651 if(!jsfAttribute.isHidden())
652 {
653 return true;
654 }
655 }
656 }
657 for (final ManageableEntityAssociationEnd associationEnd : this.getManageableAssociationEnds())
658 {
659 if(associationEnd instanceof JSFManageableEntityAssociationEnd)
660 {
661 final JSFManageableEntityAssociationEnd jsfAssociationEnd = (JSFManageableEntityAssociationEnd)associationEnd;
662 if(!jsfAssociationEnd.isDisplay())
663 {
664 return true;
665 }
666 }
667 }
668 return false;
669 }
670
671
672
673
674
675 public String handleGetConverterClassName()
676 {
677 return StringUtils.replace(
678 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.CONVERTER_PATTERN)),
679 "{0}",
680 this.getName());
681 }
682
683
684
685
686
687 public String handleGetConverterType()
688 {
689 return this.getManageablePackageName() + this.getNamespaceProperty() + this.getConverterClassName();
690 }
691
692
693
694
695
696 public String handleGetConverterFullPath()
697 {
698 return StringUtils.replace(this.getConverterType(), this.getNamespaceProperty(), "/");
699 }
700 }