1 package org.andromda.cartridges.jsf2.metafacades;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.List;
6 import org.andromda.cartridges.jsf2.JSFGlobals;
7 import org.andromda.cartridges.jsf2.JSFProfile;
8 import org.andromda.cartridges.jsf2.JSFUtils;
9 import org.andromda.metafacades.uml.ClassifierFacade;
10 import org.andromda.metafacades.uml.FrontEndAction;
11 import org.andromda.metafacades.uml.FrontEndParameter;
12 import org.andromda.metafacades.uml.FrontEndView;
13 import org.andromda.metafacades.uml.ModelElementFacade;
14 import org.andromda.metafacades.uml.ParameterFacade;
15 import org.andromda.utils.StringUtilsHelper;
16 import org.apache.commons.lang.ObjectUtils;
17 import org.apache.commons.lang.StringUtils;
18
19
20
21
22
23
24 public class JSFAttributeLogicImpl
25 extends JSFAttributeLogic
26 {
27 private static final long serialVersionUID = 34L;
28
29
30
31
32 public JSFAttributeLogicImpl(
33 Object metaObject,
34 String context)
35 {
36 super(metaObject, context);
37 }
38
39
40
41
42
43 protected String handleGetMessageKey()
44 {
45 final StringBuilder messageKey = new StringBuilder();
46 if (!this.isNormalizeMessages())
47 {
48 final ClassifierFacade owner = this.getOwner();
49 if (owner != null)
50 {
51 messageKey.append(StringUtilsHelper.toResourceMessageKey(owner.getName()));
52 messageKey.append('.');
53 }
54 }
55 final String name = this.getName();
56 if (name != null && name.trim().length() > 0)
57 {
58 messageKey.append(StringUtilsHelper.toResourceMessageKey(name));
59 }
60 return messageKey.toString();
61 }
62
63
64
65
66
67
68 private boolean isNormalizeMessages()
69 {
70 final String normalizeMessages =
71 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES));
72 return Boolean.valueOf(normalizeMessages).booleanValue();
73 }
74
75
76
77
78
79 protected String handleGetMessageValue()
80 {
81 return StringUtilsHelper.toPhrase(super.getName());
82 }
83
84
85
86
87
88 protected String handleGetFormat()
89 {
90 return JSFUtils.getFormat(
91 (ModelElementFacade)this.THIS(),
92 this.getType(),
93 this.getDefaultDateFormat(),
94 this.getDefaultTimeFormat());
95 }
96
97
98
99
100 private String getDefaultTimeFormat()
101 {
102 return (String)this.getConfiguredProperty(JSFGlobals.PROPERTY_DEFAULT_TIMEFORMAT);
103 }
104
105
106
107
108 private String getDefaultDateFormat()
109 {
110 return (String)this.getConfiguredProperty(JSFGlobals.PROPERTY_DEFAULT_DATEFORMAT);
111 }
112
113
114
115
116
117 protected String handleGetDummyValue()
118 {
119 final ClassifierFacade type = this.getType();
120 if (type != null)
121 {
122 final String typeName = type.getFullyQualifiedName();
123 final String name = this.getName();
124 if ("String".equals(typeName))
125 {
126 return "\"" + name + "-test" + "\"";
127 }
128 if ("java.util.Date".equals(typeName))
129 {
130 return "new java.util.Date()";
131 }
132 if ("java.sql.Date".equals(typeName))
133 {
134 return "new java.sql.Date(new java.util.Date().getTime())";
135 }
136 if ("java.sql.Timestamp".equals(typeName))
137 {
138 return "new java.sql.Timestamp(new Date().getTime())";
139 }
140 if ("java.util.Calendar".equals(typeName))
141 {
142 return "java.util.Calendar.getInstance()";
143 }
144 if ("int".equals(typeName))
145 {
146 return "(int)" + name.hashCode();
147 }
148 if ("boolean".equals(typeName))
149 {
150 return "false";
151 }
152 if ("long".equals(typeName))
153 {
154 return "(long)" + name.hashCode();
155 }
156 if ("char".equals(typeName))
157 {
158 return "(char)" + name.hashCode();
159 }
160 if ("float".equals(typeName))
161 {
162 return "(float)" + name.hashCode() / hashCode();
163 }
164 if ("double".equals(typeName))
165 {
166 return "(double)" + name.hashCode() / hashCode();
167 }
168 if ("short".equals(typeName))
169 {
170 return "(short)" + name.hashCode();
171 }
172 if ("byte".equals(typeName))
173 {
174 return "(byte)" + name.hashCode();
175 }
176 if ("java.lang.Integer".equals(typeName) || "Integer".equals(typeName))
177 {
178 return "new Integer((int)" + name.hashCode() + ")";
179 }
180 if ("java.lang.Boolean".equals(typeName) || "Boolean".equals(typeName))
181 {
182 return "Boolean.FALSE";
183 }
184 if ("java.lang.Long".equals(typeName) || "Long".equals(typeName))
185 {
186 return "new Long((long)" + name.hashCode() + ")";
187 }
188 if ("java.lang.Character".equals(typeName) || "Character".equals(typeName))
189 {
190 return "new Character(char)" + name.hashCode() + ")";
191 }
192 if ("java.lang.Float".equals(typeName) || "Float".equals(typeName))
193 {
194 return "new Float((float)" + name.hashCode() / hashCode() + ")";
195 }
196 if ("java.lang.Double".equals(typeName) || "Double".equals(typeName))
197 {
198 return "new Double((double)" + name.hashCode() / hashCode() + ")";
199 }
200 if ("java.lang.Short".equals(typeName) || "Short".equals(typeName))
201 {
202 return "new Short((short)" + name.hashCode() + ")";
203 }
204 if ("java.lang.Byte".equals(typeName) || "Byte".equals(typeName))
205 {
206 return "new Byte((byte)" + name.hashCode() + ")";
207 }
208
209
210 if (type.isSetType())
211 {
212 return "new java.util.HashSet(java.util.Arrays.asList(" + constructDummyArray() + "))";
213 }
214 if (type.isCollectionType())
215 {
216 return "java.util.Arrays.asList(" + constructDummyArray() + ")";
217 }
218
219
220 }
221 return "null";
222 }
223
224
225
226
227
228
229 private String constructDummyArray()
230 {
231 return JSFUtils.constructDummyArrayDeclaration(
232 this.getName(),
233 JSFGlobals.DUMMY_ARRAY_COUNT);
234 }
235
236
237
238
239
240
241 protected String handleGetFormPropertyName(final ParameterFacade ownerParameter)
242 {
243 final StringBuilder propertyName = new StringBuilder();
244 if (ownerParameter != null)
245 {
246 propertyName.append(ownerParameter.getName());
247 propertyName.append('.');
248 }
249 final String name = this.getName();
250 if (name != null && name.trim().length() > 0)
251 {
252 propertyName.append(name);
253 }
254 return propertyName.toString();
255 }
256
257
258
259
260
261
262 protected String handleGetBackingListName(final ParameterFacade ownerParameter)
263 {
264 final String backingListName =
265 StringUtils.replace(
266 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.BACKING_LIST_PATTERN)),
267 "{0}",
268 this.getFormPropertyId(ownerParameter));
269 return org.andromda.utils.StringUtilsHelper.lowerCamelCaseName(backingListName);
270 }
271
272
273
274
275
276
277 protected String handleGetBackingValueName(final ParameterFacade ownerParameter)
278 {
279 final String backingListName =
280 StringUtils.replace(
281 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.BACKING_VALUE_PATTERN)),
282 "{0}",
283 this.getFormPropertyId(ownerParameter));
284 return org.andromda.utils.StringUtilsHelper.lowerCamelCaseName(backingListName);
285 }
286
287
288
289
290
291
292 protected String handleGetLabelListName(final ParameterFacade ownerParameter)
293 {
294 return StringUtils.replace(
295 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.LABEL_LIST_PATTERN)),
296 "{0}",
297 this.getFormPropertyId(ownerParameter));
298 }
299
300
301
302
303
304
305 protected String handleGetValueListName(final ParameterFacade ownerParameter)
306 {
307 return StringUtils.replace(
308 ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.VALUE_LIST_PATTERN)),
309 "{0}",
310 this.getFormPropertyId(ownerParameter));
311 }
312
313
314
315
316
317
318 protected String handleGetFormPropertyId(final ParameterFacade ownerParameter)
319 {
320 return StringUtilsHelper.lowerCamelCaseName(this.getFormPropertyName(ownerParameter));
321 }
322
323
324
325
326
327
328 protected boolean handleIsSelectable(final FrontEndParameter ownerParameter)
329 {
330 boolean selectable = false;
331 if (ownerParameter != null)
332 {
333 if (ownerParameter.isActionParameter())
334 {
335 selectable = this.isInputMultibox() || this.isInputSelect() || this.isInputRadio();
336 final ClassifierFacade type = this.getType();
337
338 if (!selectable && type != null)
339 {
340 final String name = this.getName();
341 final String typeName = type.getFullyQualifiedName();
342
343
344
345 final Collection<FrontEndView> views = ownerParameter.getAction().getTargetViews();
346 for (final Iterator<FrontEndView> iterator = views.iterator(); iterator.hasNext() && !selectable;)
347 {
348 final Collection<FrontEndParameter> parameters = iterator.next().getAllActionParameters();
349 for (final Iterator<FrontEndParameter> parameterIterator = parameters.iterator();
350 parameterIterator.hasNext() && !selectable;)
351 {
352 final FrontEndParameter object = parameterIterator.next();
353 if (object instanceof JSFParameter)
354 {
355 final JSFParameter parameter = (JSFParameter)object;
356 final String parameterName = parameter.getName();
357 final ClassifierFacade parameterType = parameter.getType();
358 if (parameterType != null)
359 {
360 final String parameterTypeName = parameterType.getFullyQualifiedName();
361 if (name.equals(parameterName) && typeName.equals(parameterTypeName))
362 {
363 selectable =
364 parameter.isInputMultibox() || parameter.isInputSelect() ||
365 parameter.isInputRadio();
366 }
367 }
368 }
369 }
370 }
371 }
372 }
373 else if (ownerParameter.isControllerOperationArgument())
374 {
375 final String name = this.getName();
376 for (final FrontEndAction action : ownerParameter.getControllerOperation().getDeferringActions())
377 {
378 final Collection<FrontEndParameter> formFields = action.getFormFields();
379 for (final Iterator<FrontEndParameter> fieldIterator = formFields.iterator(); fieldIterator.hasNext() && !selectable;)
380 {
381 final FrontEndParameter object = fieldIterator.next();
382 if (object instanceof JSFParameter)
383 {
384 final JSFParameter parameter = (JSFParameter)object;
385 if (name.equals(parameter.getName()))
386 {
387 selectable = parameter.isSelectable();
388 }
389 }
390 }
391 }
392 }
393 }
394 return selectable;
395 }
396
397
398
399
400
401 protected boolean handleIsValidationRequired()
402 {
403 return !this.getValidatorTypes().isEmpty();
404 }
405
406
407
408
409
410 protected Collection<String> handleGetValidatorTypes()
411 {
412 return JSFUtils.getValidatorTypes(
413 (ModelElementFacade)this.THIS(),
414 this.getType());
415 }
416
417
418
419
420
421
422 protected Collection<List<String>> handleGetValidatorVars(JSFParameter ownerParameter)
423 {
424 return JSFUtils.getValidatorVars(
425 (ModelElementFacade)this.THIS(),
426 this.getType(),
427 ownerParameter);
428 }
429
430
431
432
433
434 protected String handleGetValidWhen()
435 {
436 return JSFUtils.getValidWhen(this);
437 }
438
439
440
441
442
443 protected boolean handleIsInputTextarea()
444 {
445 return this.isInputType(JSFGlobals.INPUT_TEXTAREA);
446 }
447
448
449
450
451
452 protected boolean handleIsInputSelect()
453 {
454 return this.isInputType(JSFGlobals.INPUT_SELECT);
455 }
456
457
458
459
460
461 protected boolean handleIsInputSecret()
462 {
463 return this.isInputType(JSFGlobals.INPUT_PASSWORD);
464 }
465
466
467
468
469
470 protected boolean handleIsInputHidden()
471 {
472 return this.isInputType(JSFGlobals.INPUT_HIDDEN);
473 }
474
475
476
477
478
479 protected boolean handleIsPlaintext()
480 {
481 return this.isInputType(JSFGlobals.PLAIN_TEXT);
482 }
483
484
485
486
487
488 protected boolean handleIsInputRadio()
489 {
490 return this.isInputType(JSFGlobals.INPUT_RADIO);
491 }
492
493
494
495
496
497 protected boolean handleIsInputText()
498 {
499 return this.isInputType(JSFGlobals.INPUT_TEXT);
500 }
501
502
503
504
505
506 protected boolean handleIsInputMultibox()
507 {
508 return this.isInputType(JSFGlobals.INPUT_MULTIBOX);
509 }
510
511
512
513
514
515 protected boolean handleIsInputTable()
516 {
517 return this.getInputTableIdentifierColumns().length() > 0 || this.isInputType(JSFGlobals.INPUT_TABLE);
518 }
519
520
521
522
523
524 protected boolean handleIsInputCheckbox()
525 {
526 boolean checkbox = this.isInputType(JSFGlobals.INPUT_CHECKBOX);
527 if (!checkbox && this.getInputType().length() == 0)
528 {
529 final ClassifierFacade type = this.getType();
530 checkbox = type != null ? type.isBooleanType() : false;
531 }
532 return checkbox;
533 }
534
535
536
537
538
539
540
541 private String getInputType()
542 {
543 return ObjectUtils.toString(this.findTaggedValue(JSFProfile.TAGGEDVALUE_INPUT_TYPE)).trim();
544 }
545
546
547
548
549
550
551
552 private boolean isInputType(final String inputType)
553 {
554 return inputType.equalsIgnoreCase(this.getInputType());
555 }
556
557
558
559
560
561 protected boolean handleIsInputFile()
562 {
563 boolean file = false;
564 ClassifierFacade type = getType();
565 if (type != null)
566 {
567 file = type.isFileType();
568 }
569 return file;
570 }
571
572
573
574
575
576
577 public boolean isReadOnly()
578 {
579 return JSFUtils.isReadOnly(this);
580 }
581
582
583
584
585
586 protected String handleGetValueListDummyValue()
587 {
588 return this.constructDummyArray();
589 }
590
591
592
593
594
595
596 protected Collection handleGetValidatorArgs(final String validatorType)
597 {
598 return JSFUtils.getValidatorArgs(
599 (ModelElementFacade)this.THIS(),
600 validatorType);
601 }
602
603
604
605
606
607 protected boolean handleIsStrictDateFormat()
608 {
609 return JSFUtils.isStrictDateFormat((ModelElementFacade)this.THIS());
610 }
611
612
613
614
615
616
617 protected String handleGetDateFormatter(final JSFParameter ownerParameter)
618 {
619 final ClassifierFacade type = this.getType();
620 return type != null && type.isDateType() ? this.getFormPropertyId(ownerParameter) + "DateFormatter" : null;
621 }
622
623
624
625
626
627
628 protected String handleGetTimeFormatter(final JSFParameter ownerParameter)
629 {
630 final ClassifierFacade type = this.getType();
631 return type != null && type.isTimeType() ? this.getFormPropertyId(ownerParameter) + "TimeFormatter" : null;
632 }
633
634
635
636
637
638
639 public String getDefaultValue()
640 {
641 String defaultValue = super.getDefaultValue();
642 if (StringUtils.isNotBlank(defaultValue))
643 {
644 final ClassifierFacade type = this.getType();
645 if (type != null && type.isStringType())
646 {
647 defaultValue = "\"" + defaultValue + "\"";
648 }
649 }
650 return defaultValue;
651 }
652
653
654
655
656
657 protected boolean handleIsEqualValidator()
658 {
659 final String equal = JSFUtils.getEqual((ModelElementFacade)this.THIS());
660 return equal != null && equal.trim().length() > 0;
661 }
662
663
664
665
666
667
668 protected boolean handleIsBackingValueRequired(final FrontEndParameter ownerParameter)
669 {
670 boolean required = false;
671 if (ownerParameter != null)
672 {
673 if (ownerParameter.isActionParameter())
674 {
675 required = this.isInputTable();
676 final ClassifierFacade type = this.getType();
677
678 if (!required && type != null)
679 {
680 final String name = this.getName();
681 final String typeName = type.getFullyQualifiedName();
682
683
684
685 final Collection<FrontEndView> views = ownerParameter.getAction().getTargetViews();
686 for (final Iterator<FrontEndView> iterator = views.iterator(); iterator.hasNext() && !required;)
687 {
688 final Collection<FrontEndParameter> parameters = iterator.next().getAllActionParameters();
689 for (final Iterator<FrontEndParameter> parameterIterator = parameters.iterator();
690 parameterIterator.hasNext() && !required;)
691 {
692 final FrontEndParameter object = parameterIterator.next();
693 if (object instanceof JSFParameter)
694 {
695 final JSFParameter parameter = (JSFParameter)object;
696 final String parameterName = parameter.getName();
697 final ClassifierFacade parameterType = parameter.getType();
698 if (parameterType != null)
699 {
700 final String parameterTypeName = parameterType.getFullyQualifiedName();
701 if (name.equals(parameterName) && typeName.equals(parameterTypeName))
702 {
703 required = parameter.isInputTable();
704 }
705 }
706 }
707 }
708 }
709 }
710 }
711 else if (ownerParameter.isControllerOperationArgument())
712 {
713 final String name = this.getName();
714 final Collection<FrontEndAction> actions = ownerParameter.getControllerOperation().getDeferringActions();
715 for (final Iterator<FrontEndAction> actionIterator = actions.iterator(); actionIterator.hasNext();)
716 {
717 final JSFAction action = (JSFAction)actionIterator.next();
718 final Collection<FrontEndParameter> formFields = action.getFormFields();
719 for (final Iterator<FrontEndParameter> fieldIterator = formFields.iterator();
720 fieldIterator.hasNext() && !required;)
721 {
722 final FrontEndParameter object = fieldIterator.next();
723 if (object instanceof JSFParameter)
724 {
725 final JSFParameter parameter = (JSFParameter)object;
726 if (name.equals(parameter.getName()))
727 {
728 required = parameter.isBackingValueRequired();
729 }
730 }
731 }
732 }
733 }
734 }
735 return required;
736 }
737
738
739
740
741
742 protected boolean handleIsInputTypePresent()
743 {
744 boolean present = false;
745 final ClassifierFacade type = this.getType();
746 if (type != null)
747 {
748 present =
749 (StringUtils.isNotBlank(this.getInputType()) || type.isDateType() || type.isBooleanType()) &&
750 !this.isPlaintext();
751 }
752 return present;
753 }
754
755
756
757
758
759 protected String handleGetInputTableIdentifierColumns()
760 {
761 return ObjectUtils.toString(this.findTaggedValue(JSFProfile.TAGGEDVALUE_INPUT_TABLE_IDENTIFIER_COLUMNS)).trim();
762 }
763
764
765
766
767
768 protected String handleGetMaxLength()
769 {
770 final Collection<List<String>> vars = this.getValidatorVars(null);
771 if(vars == null)
772 {
773 return null;
774 }
775 for(final List<String> values : vars)
776 {
777 if("maxlength".equals(values.get(0)))
778 {
779 return values.get(1);
780 }
781 }
782 return null;
783 }
784 }