001package org.andromda.cartridges.jsf.metafacades;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.LinkedHashMap;
007import java.util.List;
008import java.util.Map;
009import org.andromda.cartridges.jsf.JSFGlobals;
010import org.andromda.cartridges.jsf.JSFProfile;
011import org.andromda.cartridges.jsf.JSFUtils;
012import org.andromda.metafacades.uml.AttributeFacade;
013import org.andromda.metafacades.uml.FrontEndAction;
014import org.andromda.metafacades.uml.FrontEndForward;
015import org.andromda.metafacades.uml.FrontEndParameter;
016import org.andromda.metafacades.uml.ModelElementFacade;
017import org.andromda.metafacades.uml.UseCaseFacade;
018import org.andromda.utils.StringUtilsHelper;
019import org.apache.commons.lang.ObjectUtils;
020import org.apache.commons.lang.StringUtils;
021
022/**
023 * MetafacadeLogic implementation for org.andromda.cartridges.jsf.metafacades.JSFView.
024 *
025 * @see org.andromda.cartridges.jsf.metafacades.JSFView
026 */
027public class JSFViewLogicImpl
028    extends JSFViewLogic
029{
030    private static final long serialVersionUID = 34L;
031    /**
032     * @param metaObject
033     * @param context
034     */
035    public JSFViewLogicImpl(
036        Object metaObject,
037        String context)
038    {
039        super(metaObject, context);
040    }
041
042    /**
043     * @return getMessageKey() + '.' + JSFGlobals.DOCUMENTATION_MESSAGE_KEY_SUFFIX
044     * @see org.andromda.cartridges.jsf.metafacades.JSFView#getDocumentationKey()
045     */
046    protected String handleGetDocumentationKey()
047    {
048        return getMessageKey() + '.' + JSFGlobals.DOCUMENTATION_MESSAGE_KEY_SUFFIX;
049    }
050
051    /**
052     * @return messageKey
053     * @see org.andromda.cartridges.jsf.metafacades.JSFView#getMessageKey()
054     */
055    protected String handleGetMessageKey()
056    {
057        final StringBuilder messageKey = new StringBuilder();
058
059        if (!this.isNormalizeMessages())
060        {
061            final UseCaseFacade useCase = this.getUseCase();
062            if (useCase != null)
063            {
064                messageKey.append(StringUtilsHelper.toResourceMessageKey(useCase.getName()));
065                messageKey.append('.');
066            }
067        }
068
069        messageKey.append(StringUtilsHelper.toResourceMessageKey(getName()));
070        return messageKey.toString();
071    }
072
073    /**
074     * Indicates whether or not we should normalize messages.
075     *
076     * @return true/false
077     */
078    private boolean isNormalizeMessages()
079    {
080        final String normalizeMessages = (String)getConfiguredProperty(JSFGlobals.NORMALIZE_MESSAGES);
081        return Boolean.valueOf(normalizeMessages).booleanValue();
082    }
083
084    /**
085     * @see org.andromda.cartridges.jsf.metafacades.JSFViewLogic#handleGetMessageValue()
086     */
087    protected String handleGetMessageValue()
088    {
089        return StringUtilsHelper.toPhrase(getName());
090    }
091
092    /**
093     * @return documentationValue
094     * @see org.andromda.cartridges.jsf.metafacades.JSFView#getDocumentationValue()
095     */
096    protected String handleGetDocumentationValue()
097    {
098        final String value = StringUtilsHelper.toResourceMessage(getDocumentation(""));
099        return value == null ? "" : value;
100    }
101
102    /**
103     * @return getMessageKey() + '.' + JSFGlobals.TITLE_MESSAGE_KEY_SUFFIX
104     * @see org.andromda.cartridges.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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.jsf.metafacades.JSFView#isPopup()
303     */
304    protected boolean handleIsPopup()
305    {
306        return ObjectUtils.toString(this.findTaggedValue(JSFProfile.TAGGEDVALUE_VIEW_TYPE)).equalsIgnoreCase(
307            JSFGlobals.VIEW_TYPE_POPUP);
308    }
309
310    /**
311     * @return nonTableVariablesPresent
312     * @see org.andromda.cartridges.jsf.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.jsf.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.jsf.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.jsf.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.jsf.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}