001package org.andromda.cartridges.jsf2.metafacades;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.HashSet;
006import java.util.Iterator;
007import java.util.List;
008import java.util.Set;
009
010import org.andromda.cartridges.jsf2.JSFGlobals;
011import org.andromda.cartridges.jsf2.JSFUtils;
012import org.andromda.metafacades.uml.ActionStateFacade;
013import org.andromda.metafacades.uml.ClassifierFacade;
014import org.andromda.metafacades.uml.DependencyFacade;
015import org.andromda.metafacades.uml.FilteredCollection;
016import org.andromda.metafacades.uml.FrontEndAction;
017import org.andromda.metafacades.uml.FrontEndActionState;
018import org.andromda.metafacades.uml.ModelElementFacade;
019import org.andromda.metafacades.uml.OperationFacade;
020import org.andromda.metafacades.uml.PackageFacade;
021import org.andromda.metafacades.uml.Service;
022import org.andromda.utils.StringUtilsHelper;
023import org.apache.commons.lang.ObjectUtils;
024import org.apache.commons.lang.StringUtils;
025
026/**
027 * MetafacadeLogic implementation for org.andromda.cartridges.jsf2.metafacades.JSFController.
028 *
029 * @see org.andromda.cartridges.jsf2.metafacades.JSFController
030 */
031public class JSFControllerLogicImpl
032    extends JSFControllerLogic
033{
034    private static final long serialVersionUID = 34L;
035    /**
036     * @param metaObject
037     * @param context
038     */
039    public JSFControllerLogicImpl(Object metaObject, String context)
040    {
041        super(metaObject, context);
042    }
043
044    /**
045     * @return implementationName
046     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getImplementationName()
047     */
048    protected String handleGetImplementationName()
049    {
050        final String pattern = ObjectUtils.toString(
051            this.getConfiguredProperty(JSFGlobals.CONTROLLER_IMPLEMENTATION_PATTERN));
052        return pattern.replaceFirst("\\{0\\}", StringUtils.capitalize(this.getName()));
053    }
054
055    /**
056     * @return fullyQualifiedImplementationName
057     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getFullyQualifiedImplementationName()
058     */
059    protected String handleGetFullyQualifiedImplementationName()
060    {
061        final StringBuilder fullyQualifiedName = new StringBuilder();
062        final String packageName = this.getPackageName();
063        if (StringUtils.isNotBlank(packageName))
064        {
065            fullyQualifiedName.append(packageName + '.');
066        }
067        return fullyQualifiedName.append(this.getImplementationName()).toString();
068    }
069
070    /**
071     * @return getFullyQualifiedImplementationName().replace('.', '/')
072     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getFullyQualifiedImplementationPath()
073     */
074    protected String handleGetFullyQualifiedImplementationPath()
075    {
076        return this.getFullyQualifiedImplementationName().replace('.', '/');
077    }
078
079    /**
080     * @return StringUtilsHelper.lowerCamelCaseName(this.getName())
081     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getBeanName()
082     */
083    protected String handleGetBeanName()
084    {
085        return StringUtilsHelper.lowerCamelCaseName(this.getName());
086    }
087
088    /**
089     * @return references
090     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getSessionObjectReferences()
091     */
092    protected List<DependencyFacade> handleGetSessionObjectReferences()
093    {
094        final List<DependencyFacade> references = new ArrayList<DependencyFacade>(this.getSourceDependencies());
095        for (final Iterator<DependencyFacade> iterator = references.iterator(); iterator.hasNext();)
096        {
097            final ModelElementFacade targetElement = (iterator.next()).getTargetElement();
098            if (!(targetElement instanceof JSFSessionObject))
099            {
100                iterator.remove();
101            }
102        }
103        return references;
104    }
105
106    /**
107     * @return controllerSerialVersionUID
108     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getControllerSerialVersionUID()
109     */
110    protected String handleGetControllerSerialVersionUID()
111    {
112       final StringBuilder buffer = new StringBuilder();
113       buffer.append(this.getFullyQualifiedImplementationName());
114       addSerialUIDData(buffer);
115       return JSFUtils.calcSerialVersionUID(buffer);
116    }
117
118    private void addSerialUIDData(StringBuilder buffer)
119    {
120        for (final FrontEndAction action : this.getUseCase().getActions())
121        {
122            buffer.append(action.getName());
123        }
124    }
125
126    /**
127     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getAllServices()
128     */
129    @Override
130    protected Collection<Service> handleGetAllServices() {
131        final Set<Service> allServices=new HashSet<Service>();
132        for(final DependencyFacade dependency: this.getServiceReferences())
133        {
134            allServices.add((Service)dependency.getTargetElement());
135        }
136        for(final DependencyFacade dependency: this.getServicesPackagesReferences())
137        {
138            final PackageFacade pack=(PackageFacade)dependency.getTargetElement();
139            for(final ClassifierFacade clazz: pack.getClasses())
140            {
141                if(clazz instanceof Service)
142                {
143                    allServices.add((Service)clazz);
144                }
145            }
146        }
147        for(final FrontEndAction action: getUseCase().getActions())
148        {
149            for(final FrontEndActionState as: action.getActionStates())
150            {
151                for(final OperationFacade operation: as.getServiceCalls())
152                {
153                    allServices.add((Service)operation.getOwner());
154                }
155            }
156        }
157        return allServices;
158    }
159
160    /**
161     * @see org.andromda.cartridges.jsf2.metafacades.JSFController#getServicesPackagesReferences()
162     */
163    @SuppressWarnings("unchecked")
164    @Override
165    protected List<PackageFacade> handleGetServicesPackagesReferences() {
166        return (List<PackageFacade>)new FilteredCollection(this.getSourceDependencies())
167        {
168            private static final long serialVersionUID = 134L;
169            @Override
170            public boolean evaluate(final Object object)
171            {
172                return ((DependencyFacade)object).getTargetElement() instanceof PackageFacade;
173            }
174        };
175    }
176}