001package org.andromda.cartridges.java.metafacades;
002
003import java.util.Collection;
004import java.util.Iterator;
005import org.andromda.metafacades.uml.ClassifierFacade;
006import org.andromda.metafacades.uml.GeneralizableElementFacade;
007import org.andromda.metafacades.uml.ModelElementFacade;
008import org.andromda.metafacades.uml.ParameterFacade;
009import org.andromda.metafacades.uml.TemplateParameterFacade;
010import org.andromda.metafacades.uml.UMLMetafacadeProperties;
011import org.apache.commons.lang.BooleanUtils;
012import org.apache.commons.lang.ObjectUtils;
013import org.apache.commons.lang.StringUtils;
014
015
016/**
017 * MetafacadeLogic implementation for org.andromda.cartridges.java.metafacades.JavaClass.
018 *
019 * @see org.andromda.cartridges.java.metafacades.JavaClass
020 */
021public class JavaClassLogicImpl
022    extends JavaClassLogic
023{
024    private static final long serialVersionUID = 34L;
025    /**
026     * @param metaObject
027     * @param context
028     */
029    public JavaClassLogicImpl(
030        Object metaObject,
031        String context)
032    {
033        super(metaObject, context);
034    }
035
036    /**
037     * If WebFault stereotype, change name from XXException to XXFault. Class should
038     * also have XXException stereotype so that the corresponding Exception referencing the
039     * fault is also created.
040     * @param fault true if name is to be converted to Fault version, for VO Template only
041     * @return the class name.
042     */
043    public String getName(boolean fault)
044    {
045        String name = super.getName();
046        if (fault && this.hasStereotype("WebFault"))
047        {
048            name = name.replace("Exception", "Fault");
049            if (!name.contains("Fault"))
050            {
051                name += "Fault";
052            }
053        }
054        return name;
055    }
056
057    /**
058     * UML22 implementation for TemplateParameter logic
059     * @return the class name.
060     */
061    public Object getType()
062    {
063        /*if (this.metaObject instanceof TemplateParameterFacade)
064        {
065            return
066        }
067        System.out.println(this.metaObject);*/
068        return this.metaObject;
069    }
070
071    /**
072     * UML22 implementation for TemplateParameter logic
073     * @return the class name.
074    public Object getOwner()
075    {
076        if (this.metaObject instanceof ClassifierFacade)
077        {
078            ClassifierFacade facade = (ClassifierFacade)this.metaObject;
079            return facade.getOwner();
080        }
081        return this.metaObject;
082    }
083     */
084
085    /**
086     * @return InterfaceImplementionName
087     * @see org.andromda.cartridges.java.metafacades.JavaClass#getInterfaceImplementationName()
088     */
089    @Override
090    protected String handleGetInterfaceImplementationName()
091    {
092        return this.getInterfaceImplementionName().replaceAll(
093            "\\{0\\}",
094            this.getName());
095    }
096
097    /**
098     * Gets the value of the {@link JavaGlobals#INTERFACE_IMPLEMENTATION_NAME_PATTERN}.
099     *
100     * @return the interface implementation name..
101     */
102    private String getInterfaceImplementionName()
103    {
104        return String.valueOf(this.getConfiguredProperty(JavaGlobals.INTERFACE_IMPLEMENTATION_NAME_PATTERN));
105    }
106
107    /**
108     * @return InterfaceImplementationName
109     * @see org.andromda.cartridges.java.metafacades.JavaClass#getFullyQualifiedInterfaceImplementationName()
110     */
111    @Override
112    protected String handleGetFullyQualifiedInterfaceImplementationName()
113    {
114        final StringBuilder fullName = new StringBuilder();
115        final String packageName = this.getPackageName();
116        if (StringUtils.isNotBlank(packageName))
117        {
118            fullName.append(packageName).append('.');
119        }
120        return fullName.append(this.getInterfaceImplementationName()).toString();
121    }
122
123    /**
124     * @return abstractImplementation
125     * @see org.andromda.cartridges.java.metafacades.JavaClass#isAbstractInterfaceImplementation()
126     */
127    @Override
128    protected boolean handleIsAbstractInterfaceImplementation()
129    {
130        boolean abstractImplementation = !this.getOperations().isEmpty();
131        if (!abstractImplementation)
132        {
133            for (GeneralizableElementFacade generalizableElementFacade : this.getAllGeneralizations())
134            {
135                final ClassifierFacade classifier = (ClassifierFacade) generalizableElementFacade;
136                abstractImplementation = !classifier.getOperations().isEmpty();
137                if (abstractImplementation)
138                {
139                    break;
140                }
141            }
142        }
143        return abstractImplementation;
144    }
145
146    /**
147     * @return templateParams
148     * @see org.andromda.cartridges.java.metafacades.JavaClassLogic#getTemplateParameters()
149     */
150    //@Override
151    protected String handleGetTemplateParams()
152    {
153        String fullName = "";
154        if (this.isTemplateParametersPresent() &&
155            BooleanUtils.toBoolean(
156                ObjectUtils.toString(this.getConfiguredProperty(UMLMetafacadeProperties.ENABLE_TEMPLATING))))
157        {
158            // we'll be constructing the parameter list in this buffer
159            final StringBuilder buffer = new StringBuilder();
160
161            // add the name we've constructed so far
162            buffer.append(fullName);
163
164            // start the parameter list
165            buffer.append('<');
166
167            // loop over the parameters, we are so to have at least one (see
168            // outer condition)
169            final Collection<TemplateParameterFacade> templateParameters = this.getTemplateParameters();
170            for (Iterator<TemplateParameterFacade> parameterIterator = templateParameters.iterator(); parameterIterator.hasNext();)
171            {
172                final ModelElementFacade modelElement =
173                    (parameterIterator.next()).getParameter();
174
175                // TODO: UML14 returns ParameterFacade, UML2 returns ModelElementFacade, so types are wrong from fullyQualifiedName
176                // Mapping from UML2 should return ParameterFacade, with a getType method. Need TemplateParameter.getName method.
177                if (modelElement instanceof ParameterFacade)
178                {
179                    buffer.append(((ParameterFacade)modelElement).getType().getFullyQualifiedName());
180                }
181                else
182                {
183                    buffer.append(modelElement.getFullyQualifiedName());
184                }
185
186                if (parameterIterator.hasNext())
187                {
188                    buffer.append(", ");
189                }
190            }
191
192            // we're finished listing the parameters
193            buffer.append('>');
194
195            // we have constructed the full name in the buffer
196            fullName = buffer.toString();
197        }
198        return fullName;
199    }
200}