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