001package org.andromda.cartridges.jsf2.metafacades;
002
003import org.andromda.metafacades.uml.ClassifierFacade;
004import org.andromda.metafacades.uml.FrontEndUseCase;
005import org.andromda.metafacades.uml.ModelElementFacade;
006import org.andromda.metafacades.uml.UMLProfile;
007
008/**
009 * MetafacadeLogic implementation for org.andromda.cartridges.jsf2.metafacades.JSFFinalState.
010 *
011 * @see org.andromda.cartridges.jsf2.metafacades.JSFFinalState
012 */
013public class JSFFinalStateLogicImpl
014    extends JSFFinalStateLogic
015{
016    private static final long serialVersionUID = 34L;
017    /**
018     * @param metaObject
019     * @param context
020     */
021    public JSFFinalStateLogicImpl(Object metaObject, String context)
022    {
023        super(metaObject, context);
024    }
025    
026    private JSFManageableEntity findManageableByName(String name)
027    {
028        for(ClassifierFacade clazz: getModel().getAllClasses())
029        {
030            if(clazz instanceof JSFManageableEntity && 
031               (clazz.getName().equals(name) || clazz.getFullyQualifiedName().equals(name)))
032            {
033                return (JSFManageableEntity)clazz;
034            }
035        }
036        return null;
037    }
038
039    /**
040     * @return fullPath
041     * @see org.andromda.cartridges.jsf2.metafacades.JSFFinalState#getPath()
042     */
043    protected String handleGetPath()
044    {
045        String fullPath = null;
046
047        FrontEndUseCase useCase = this.getTargetUseCase();
048        if (useCase == null)
049        {
050            // - perhaps this final state links outside of the UML model
051            final Object taggedValue = this.findTaggedValue(UMLProfile.TAGGEDVALUE_EXTERNAL_HYPERLINK);
052            if (taggedValue == null)
053            {
054                String name = getName();
055                if (name != null && (name.startsWith("/") || name.startsWith("http://") || name.startsWith("file:")))
056                {
057                    fullPath = name;
058                }
059            }
060            else
061            {
062                fullPath = String.valueOf(taggedValue);
063            }
064            
065            if(fullPath == null && getName() != null)
066            {
067                fullPath = ((JSFManageableEntity)getTargetElement()).getActionFullPath();
068            }
069            
070        }
071        else if (useCase instanceof JSFUseCase)
072        {
073            fullPath = ((JSFUseCase)useCase).getPath();
074        }
075
076        return fullPath;
077    }
078
079    /**
080     * @return the target controller
081     * @see org.andromda.cartridges.jsf2.metafacades.JSFFinalState#getTargetControllerFullyQualifiedName()
082     */
083    @Override
084    protected String handleGetTargetControllerFullyQualifiedName() {
085        String result=null;
086        
087        if(getTargetElement() instanceof JSFUseCase)
088        {
089            result=((JSFUseCase)getTargetElement()).getController().getFullyQualifiedName();
090        }
091        else if(getTargetElement() instanceof JSFManageableEntity)
092        {
093            result=((JSFManageableEntity)getTargetElement()).getControllerType();
094        }
095        
096        return result;
097    }
098
099    /**
100     * @return the target controller bean name
101     * @see org.andromda.cartridges.jsf2.metafacades.JSFFinalState#getTargetControllerBeanName()
102     */
103    @Override
104    protected String handleGetTargetControllerBeanName() {
105        String result=null;
106        
107        if(getTargetElement() instanceof JSFUseCase)
108        {
109            result=((JSFController)((JSFUseCase)getTargetElement()).getController()).getBeanName();
110        }
111        else if(getTargetElement() instanceof JSFManageableEntity)
112        {
113            result=((JSFManageableEntity)getTargetElement()).getControllerBeanName();
114        }
115        
116        return result;
117    }
118
119    /**
120     * @return the target element (use case or manageable class)
121     * @see org.andromda.cartridges.jsf2.metafacades.JSFFinalState#getTargetElement()
122     */
123    @Override
124    protected ModelElementFacade handleGetTargetElement() {
125        ModelElementFacade targetElement=getTargetUseCase();
126        
127        if(targetElement == null)
128        {
129            String nameParts[] = getName().split(" ");
130            if(nameParts.length >= 2 && nameParts[0].equalsIgnoreCase("Manage"))
131            {
132                JSFManageableEntity manageable=findManageableByName(nameParts[1]);
133                if(manageable != null)
134                {
135                    return targetElement=manageable;
136                }
137            }
138        }
139        
140        return targetElement;
141    }
142}