View Javadoc
1   package org.andromda.cartridges.jsf2.metafacades;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import java.util.LinkedHashMap;
7   import java.util.List;
8   import java.util.Map;
9   import org.andromda.cartridges.jsf2.JSFGlobals;
10  import org.andromda.cartridges.jsf2.JSFProfile;
11  import org.andromda.cartridges.jsf2.JSFUtils;
12  import org.andromda.metafacades.uml.AttributeFacade;
13  import org.andromda.metafacades.uml.FrontEndAction;
14  import org.andromda.metafacades.uml.FrontEndForward;
15  import org.andromda.metafacades.uml.FrontEndParameter;
16  import org.andromda.metafacades.uml.ModelElementFacade;
17  import org.andromda.metafacades.uml.UseCaseFacade;
18  import org.andromda.utils.StringUtilsHelper;
19  import org.apache.commons.lang.ObjectUtils;
20  import org.apache.commons.lang.StringUtils;
21  
22  /**
23   * MetafacadeLogic implementation for org.andromda.cartridges.jsf2.metafacades.JSFView.
24   *
25   * @see org.andromda.cartridges.jsf2.metafacades.JSFView
26   */
27  public class JSFViewLogicImpl
28      extends JSFViewLogic
29  {
30      private static final long serialVersionUID = 34L;
31      /**
32       * @param metaObject
33       * @param context
34       */
35      public JSFViewLogicImpl(
36          Object metaObject,
37          String context)
38      {
39          super(metaObject, context);
40      }
41  
42      /**
43       * @return getMessageKey() + '.' + JSFGlobals.DOCUMENTATION_MESSAGE_KEY_SUFFIX
44       * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getDocumentationKey()
45       */
46      protected String handleGetDocumentationKey()
47      {
48          return getMessageKey() + '.' + JSFGlobals.DOCUMENTATION_MESSAGE_KEY_SUFFIX;
49      }
50  
51      /**
52       * @return messageKey
53       * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getMessageKey()
54       */
55      protected String handleGetMessageKey()
56      {
57          final StringBuilder messageKey = new StringBuilder();
58  
59          if (!this.isNormalizeMessages())
60          {
61              final UseCaseFacade useCase = this.getUseCase();
62              if (useCase != null)
63              {
64                  messageKey.append(StringUtilsHelper.toResourceMessageKey(useCase.getName()));
65                  messageKey.append('.');
66              }
67          }
68  
69          messageKey.append(StringUtilsHelper.toResourceMessageKey(getName()));
70          return messageKey.toString();
71      }
72  
73      /**
74       * Indicates whether or not we should normalize messages.
75       *
76       * @return true/false
77       */
78      private boolean isNormalizeMessages()
79      {
80          final String normalizeMessages = (String)getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES);
81          return Boolean.valueOf(normalizeMessages).booleanValue();
82      }
83  
84      /**
85       * @see org.andromda.cartridges.jsf2.metafacades.JSFViewLogic#handleGetMessageValue()
86       */
87      protected String handleGetMessageValue()
88      {
89          return StringUtilsHelper.toPhrase(getName());
90      }
91  
92      /**
93       * @return documentationValue
94       * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getDocumentationValue()
95       */
96      protected String handleGetDocumentationValue()
97      {
98          final String value = StringUtilsHelper.toResourceMessage(getDocumentation(""));
99          return value == null ? "" : value;
100     }
101 
102     /**
103      * @return getMessageKey() + '.' + JSFGlobals.TITLE_MESSAGE_KEY_SUFFIX
104      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getTitleKey()
105      */
106     protected String handleGetTitleKey()
107     {
108         return this.getMessageKey() + '.' + JSFGlobals.TITLE_MESSAGE_KEY_SUFFIX;
109     }
110 
111     /**
112      * @return toPhrase(getName())
113      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getTitleValue()
114      */
115     protected String handleGetTitleValue()
116     {
117         return StringUtilsHelper.toPhrase(getName());
118     }
119 
120     /**
121      * @return path
122      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getPath()
123      */
124     protected String handleGetPath()
125     {
126         final StringBuilder path = new StringBuilder();
127         final String packageName = this.getPackageName();
128         if (StringUtils.isNotBlank(packageName))
129         {
130             path.append(packageName + '.');
131         }
132         path.append(JSFUtils.toWebResourceName(StringUtils.trimToEmpty(this.getName())).replace(
133                 '.',
134                 '/'));
135         return '/' + path.toString().replace(
136             '.',
137             '/');
138     }
139 
140     /**
141      * @return forwards
142      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getForwards()
143      */
144     protected List<ModelElementFacade> handleGetForwards()
145     {
146         final Map<String, ModelElementFacade> forwards = new LinkedHashMap<String, ModelElementFacade>();
147         for (final FrontEndAction action : this.getActions())
148         {
149             if (action != null && !action.isUseCaseStart())
150             {
151                 for (final FrontEndForward forward : action.getActionForwards())
152                 {
153                     if (forward instanceof JSFForward)
154                     {
155                         forwards.put(((JSFForward)forward).getName(), forward);
156                     }
157                     else if (forward instanceof JSFAction)
158                     {
159                         forwards.put(((JSFAction)forward).getName(), forward);
160                     }
161                 }
162             }
163         }
164         return new ArrayList<ModelElementFacade>(forwards.values());
165     }
166 
167     /**
168      * @return tables
169      * @see org.andromda.cartridges.jsf2.metafacades.JSFAction#isTableLink()
170      */
171     protected List<JSFParameter> handleGetTables()
172     {
173         final List<JSFParameter> tables = new ArrayList<JSFParameter>();
174         final List<FrontEndParameter> variables = this.getVariables();
175         for (FrontEndParameter parameter : variables)
176         {
177             if (parameter instanceof JSFParameter)
178             {
179                 final JSFParameter variable = (JSFParameter)parameter;
180                 if (variable.isTable())
181                 {
182                     tables.add(variable);
183                 }
184             }
185         }
186         return tables;
187     }
188 
189     /**
190      * @return actionForwards
191      * @see org.andromda.cartridges.jsf2.metafacades.JSFViewLogic#getActionForwards()
192      */
193     protected List<JSFForward> handleGetActionForwards()
194     {
195         final List<JSFForward> actionForwards = new ArrayList<JSFForward>(this.getForwards());
196         for (final Iterator<JSFForward> iterator = actionForwards.iterator(); iterator.hasNext();)
197         {
198             if (!(iterator.next() instanceof JSFAction))
199             {
200                 iterator.remove();
201             }
202         }
203         return actionForwards;
204     }
205 
206     /**
207      * @return populatorName
208      * @see org.andromda.cartridges.jsf2.metafacades.JSFViewLogic#getFullyQualifiedPopulator()
209      */
210     protected String handleGetFullyQualifiedPopulator()
211     {
212         final StringBuilder name = new StringBuilder();
213         final String packageName = this.getPackageName();
214         if (StringUtils.isNotBlank(packageName))
215         {
216             name.append(packageName);
217             name.append('.');
218         }
219         name.append(this.getPopulator());
220         return name.toString();
221     }
222 
223     /**
224      * @return populator
225      * @see org.andromda.cartridges.jsf2.metafacades.JSFViewLogic#getPopulator()
226      */
227     protected String handleGetPopulator()
228     {
229         return ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.VIEW_POPULATOR_PATTERN)).replaceAll(
230             "\\{0\\}",
231             StringUtilsHelper.upperCamelCaseName(this.getName()));
232     }
233 
234     /**
235      * @see org.andromda.cartridges.jsf2.metafacades.JSFViewLogic#handleGetFormActions()
236      */
237     protected List<FrontEndAction> handleGetFormActions()
238     {
239         final List<FrontEndAction> actions = new ArrayList<FrontEndAction>(this.getActions());
240         for (final Iterator<FrontEndAction> iterator = actions.iterator(); iterator.hasNext();)
241         {
242             final FrontEndAction action = iterator.next();
243             if (action.getFormFields().isEmpty())
244             {
245                 iterator.remove();
246             }
247         }
248         return actions;
249     }
250 
251     /**
252      * @return formKey
253      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getFormKey()
254      */
255     protected String handleGetFormKey()
256     {
257         final Object formKeyValue = this.findTaggedValue(JSFProfile.TAGGEDVALUE_ACTION_FORM_KEY);
258         return formKeyValue == null ? ObjectUtils.toString(this.getConfiguredProperty(JSFGlobals.ACTION_FORM_KEY))
259                                     : String.valueOf(formKeyValue);
260     }
261 
262     /**
263      * @return getFullyQualifiedPopulator().replace('.', '/')
264      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getPopulatorPath()
265      */
266     protected String handleGetPopulatorPath()
267     {
268         return this.getFullyQualifiedPopulator().replace(
269             '.',
270             '/');
271     }
272 
273     /**
274      * @return !getFormActions().isEmpty() || !getVariables().isEmpty()
275      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#isPopulatorRequired()
276      */
277     protected boolean handleIsPopulatorRequired()
278     {
279         return !this.getFormActions().isEmpty() || !this.getVariables().isEmpty();
280     }
281 
282     /**
283      * @return validationRequired
284      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#isPopulatorRequired()
285      */
286     protected boolean handleIsValidationRequired()
287     {
288         boolean required = false;
289         for (final FrontEndAction action : this.getActions())
290         {
291             if (((JSFAction)action).isValidationRequired())
292             {
293                 required = true;
294                 break;
295             }
296         }
297         return required;
298     }
299 
300     /**
301      * @return isPopup
302      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#isPopup()
303      */
304     protected boolean handleIsPopup()
305     {
306         return ObjectUtils.toString(this.findTaggedValue(JSFProfile.TAGGEDVALUE_VIEW_TYPE)).equalsIgnoreCase(
307             JSFGlobals.ACTION_TYPE_POPUP);
308     }
309 
310     /**
311      * @return nonTableVariablesPresent
312      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#isNonTableVariablesPresent()
313      */
314     protected boolean handleIsNonTableVariablesPresent()
315     {
316         boolean present = false;
317         for (final FrontEndParameter variable : this.getVariables())
318         {
319             if (!variable.isTable())
320             {
321                 present = true;
322                 break;
323             }
324         }
325         return present;
326     }
327 
328     /**
329      * @return hasNameOfUseCase
330      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#isHasNameOfUseCase()
331      */
332     protected boolean handleIsHasNameOfUseCase()
333     {
334         boolean sameName = false;
335         final ModelElementFacade useCase = this.getUseCase();
336         final String useCaseName = useCase != null ? useCase.getName() : null;
337         if (useCaseName != null && useCaseName.equalsIgnoreCase(this.getName()))
338         {
339             sameName = true;
340         }
341         return sameName;
342     }
343 
344     /**
345      * @return backingValueVariables
346      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getBackingValueVariables()
347      */
348     protected List<JSFParameter> handleGetBackingValueVariables()
349     {
350         final Map<String, JSFParameter> variables = new LinkedHashMap<String, JSFParameter>();
351         for (final FrontEndParameter frontEndParameter : this.getAllActionParameters())
352         {
353             if (frontEndParameter instanceof JSFParameter)
354             {
355                 final JSFParameter parameter = (JSFParameter)frontEndParameter;
356                 final String parameterName = parameter.getName();
357                 final Collection<AttributeFacade> attributes = parameter.getAttributes();
358                 if (parameter.isBackingValueRequired() || parameter.isSelectable())
359                 {
360                     if (parameter.isBackingValueRequired() || parameter.isSelectable())
361                     {
362                         variables.put(parameterName, parameter);
363                     }
364                 }
365                 else
366                 {
367                     boolean hasBackingValue = false;
368                     for (final AttributeFacade attribute : attributes)
369                     {
370                         final JSFAttribute jsfAttribute = (JSFAttribute)attribute;
371                         if (jsfAttribute.isSelectable(parameter) || jsfAttribute.isBackingValueRequired(parameter))
372                         {
373                             hasBackingValue = true;
374                             break;
375                         }
376                     }
377                     if (hasBackingValue)
378                     {
379                         variables.put(parameterName, parameter);
380                     }
381                 }
382             }
383         }
384         return new ArrayList<JSFParameter>(variables.values());
385     }
386 
387     /**
388      * @return toWebResourceName(this.getUseCase().getName() + "-" + this.getName())
389      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getFromOutcome()
390      */
391     protected String handleGetFromOutcome()
392     {
393         return JSFUtils.toWebResourceName(this.getUseCase().getName() + "-" + this.getName());
394     }
395 
396     /**
397      * @return needsFileUpload
398      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#isNeedsFileUpload()
399      */
400     protected boolean handleIsNeedsFileUpload()
401     {
402         if(this.getAllActionParameters().size() == 0)
403         {
404             return false;
405         }
406 
407         for (final FrontEndParameter feParameter : this.getAllActionParameters())
408         {
409             if (feParameter instanceof JSFParameter)
410             {
411                 final JSFParameter parameter = (JSFParameter)feParameter;
412                 if(parameter.isInputFile())
413                 {
414                     return true;
415                 }
416                 if(parameter.isComplex())
417                 {
418                     for(final Iterator attributes = parameter.getAttributes().iterator(); attributes.hasNext();)
419                     {
420                         if(((JSFAttribute)attributes.next()).isInputFile())
421                         {
422                             return true;
423                         }
424                     }
425                 }
426             }
427         }
428         return false;
429     }
430 
431     /**
432      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getFullyQualifiedPageObjectClassPath()
433      */
434     @Override
435     protected String handleGetFullyQualifiedPageObjectClassPath() 
436     {
437         return this.getFullyQualifiedPageObjectClassName().replace(
438                         '.',
439                         '/');
440     }
441 
442     /**
443      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getFullyQualifiedPageObjectClassName()
444      */
445     @Override
446     protected String handleGetFullyQualifiedPageObjectClassName() 
447     {
448         final StringBuilder fullyQualifiedName = new StringBuilder();
449         final String packageName = this.getPackageName();
450         if (StringUtils.isNotBlank(packageName))
451         {
452             fullyQualifiedName.append(packageName + '.');
453         }
454         return fullyQualifiedName.append(this.getPageObjectClassName()).toString();
455     }
456 
457     /**
458      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getPageObjectClassName()
459      */
460     @Override
461     protected String handleGetPageObjectClassName() 
462     {
463         return StringUtilsHelper.upperCamelCaseName(this.getName());
464     }
465 
466     /**
467      * @see org.andromda.cartridges.jsf2.metafacades.JSFView#getPageObjectBeanName()
468      */
469     @Override
470     protected String handleGetPageObjectBeanName() 
471     {
472         return StringUtilsHelper.lowerCamelCaseName(this.getName());
473     }
474 }