001package org.andromda.cartridges.ejb3.metafacades;
002
003import java.text.MessageFormat;
004import java.util.ArrayList;
005import java.util.Collection;
006import java.util.List;
007import org.andromda.cartridges.ejb3.EJB3Globals;
008import org.andromda.metafacades.uml.DependencyFacade;
009import org.andromda.metafacades.uml.ModelElementFacade;
010import org.apache.commons.collections.CollectionUtils;
011import org.apache.commons.collections.Predicate;
012import org.apache.commons.lang.StringUtils;
013
014/**
015 * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3DependencyFacade.
016 *
017 * @see EJB3DependencyFacade
018 */
019public class EJB3DependencyFacadeLogicImpl
020    extends EJB3DependencyFacadeLogic
021{
022    private static final long serialVersionUID = 34L;
023    /**
024     * The suffix for the transformation anonymous name.
025     */
026    private static final String TRANSFORMATION_ANONYMOUS_NAME_SUFFIX = "_TRANSFORMER";
027
028    /**
029     * The suffix for the transformation to entity method name.
030     */
031    private static final String TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX = "ToEntity";
032
033    /**
034     * The suffix for the value object to entity transformer.
035     */
036    private static final String VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX = "Transformer";
037
038    /**
039     * @param metaObject
040     * @param context
041     */
042    public EJB3DependencyFacadeLogicImpl(final Object metaObject, final String context)
043    {
044        super (metaObject, context);
045    }
046
047    /**
048     * @return EJB3Globals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase()
049     * @see EJB3DependencyFacade#getTransformationConstantName()
050     */
051    @Override
052    protected String handleGetTransformationConstantName()
053    {
054        return EJB3Globals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase();
055    }
056
057    /**
058     * @return EJB3Globals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(getName())
059     * @see EJB3DependencyFacade#getTransformationMethodName()
060     */
061    @Override
062    protected String handleGetTransformationMethodName()
063    {
064        return EJB3Globals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName());
065    }
066
067    /**
068     * @see EJB3DependencyFacade#getTransformationAnonymousName()
069     */
070    @Override
071    protected String handleGetTransformationAnonymousName()
072    {
073        return this.getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX;
074    }
075
076    /**
077     * @return circularReference
078     * @see EJB3DependencyFacade#isCircularReference()
079     */
080    @Override
081    protected boolean handleIsCircularReference()
082    {
083        boolean circularReference = false;
084        final ModelElementFacade sourceElement = this.getSourceElement();
085        final ModelElementFacade targetElement = this.getTargetElement();
086        final Collection<DependencyFacade> sourceDependencies = targetElement.getSourceDependencies();
087        if (sourceDependencies != null && !sourceDependencies.isEmpty())
088        {
089            circularReference = CollectionUtils.find(sourceDependencies, new Predicate()
090            {
091                public boolean evaluate(final Object object)
092                {
093                    DependencyFacade dependency = (DependencyFacade)object;
094                    return (dependency != null) && dependency.getTargetElement().equals(sourceElement);
095                }
096            }) != null;
097        }
098        return circularReference;
099    }
100
101    /**
102     * @see EJB3DependencyFacade#getTransformationConstantValue()
103     */
104    @Override
105    protected int handleGetTransformationConstantValue()
106    {
107        int value = 0;
108        ModelElementFacade element = this.getSourceElement();
109        if (element instanceof EJB3EntityFacade)
110        {
111            final List<EJB3EntityFacade> hierarchyList = new ArrayList<EJB3EntityFacade>();
112            for (EJB3EntityFacade entity = (EJB3EntityFacade)element; entity != null;
113                entity = (EJB3EntityFacade)entity.getGeneralization())
114            {
115                hierarchyList.add(entity);
116            }
117            boolean breakOut = false;
118            for (int ctr = hierarchyList.size() - 1; ctr >= 0; ctr--)
119            {
120                final EJB3EntityFacade generalization = hierarchyList.get(ctr);
121                for (final Object reference : generalization.getValueObjectReferences())
122                {
123                    value++;
124                    if (reference.equals(this))
125                    {
126                        breakOut = true;
127                        break;
128                    }
129                }
130                if (breakOut)
131                {
132                    break;
133                }
134            }
135        }
136        return value;
137    }
138
139    /**
140     * @see EJB3DependencyFacade#getTransformationToCollectionMethodName()
141     */
142    @Override
143    protected String handleGetTransformationToCollectionMethodName()
144    {
145        return EJB3Globals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName()) +
146            EJB3Globals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
147    }
148
149    /**
150     * @see EJB3DependencyFacade#getDaoName()
151     */
152    @Override
153    protected String handleGetDaoName()
154    {
155        return MessageFormat.format(
156                this.getDaoNamePattern(),
157                StringUtils.trimToEmpty(this.getName()));
158    }
159
160    /**
161     * Gets the value of the {@link EJB3Globals#DAO_PATTERN}.
162     *
163     * @return the DAO name pattern.
164     */
165    private String getDaoNamePattern()
166    {
167        return String.valueOf(this.getConfiguredProperty(EJB3Globals.DAO_PATTERN));
168    }
169
170    /**
171     * @see EJB3DependencyFacade#getDaoGetterName()
172     */
173    @Override
174    protected String handleGetDaoGetterName()
175    {
176        return "get" + StringUtils.capitalize(this.getDaoName());
177    }
178
179    /**
180     * @see EJB3DependencyFacade#getDaoSetterName()
181     */
182    @Override
183    protected String handleGetDaoSetterName()
184    {
185        return "set" + StringUtils.capitalize(this.getDaoName());
186    }
187
188    /**
189     * @see EJB3DependencyFacade#getTransformationToEntityCollectionMethodName()
190     */
191    @Override
192    protected String handleGetTransformationToEntityCollectionMethodName()
193    {
194        return this.getTransformationToEntityMethodName() + EJB3Globals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
195    }
196
197    /**
198     * @see EJB3DependencyFacade#getTransformationToEntityMethodName()
199     */
200    @Override
201    protected String handleGetTransformationToEntityMethodName()
202    {
203        return this.getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX;
204    }
205
206    /**
207     * @see EJB3DependencyFacade#getValueObjectToEntityTransformerName()
208     */
209    @Override
210    protected String handleGetValueObjectToEntityTransformerName()
211    {
212        return StringUtils.capitalize(this.getTransformationToEntityMethodName()) +
213            VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX;
214    }
215}