001package org.andromda.cartridges.spring.metafacades;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Iterator;
006import java.util.List;
007import org.andromda.cartridges.spring.SpringHibernateUtils;
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/**
016 * MetafacadeLogic implementation for org.andromda.cartridges.spring.metafacades.SpringDependency.
017 *
018 * @see org.andromda.cartridges.spring.metafacades.SpringDependency
019 */
020public class SpringDependencyLogicImpl
021    extends SpringDependencyLogic
022{
023    private static final long serialVersionUID = 34L;
024    /**
025     * Public constructor for SpringDependencyLogicImpl
026     * @param metaObject
027     * @param context
028     * @see org.andromda.cartridges.spring.metafacades.SpringDependency
029     */
030    public SpringDependencyLogicImpl(
031        Object metaObject,
032        String context)
033    {
034        super(metaObject, context);
035    }
036
037    /**
038     * @return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase()
039     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationConstantName()
040     */
041    protected String handleGetTransformationConstantName()
042    {
043        return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX + this.getName().toUpperCase();
044    }
045
046    /**
047     * @return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName())
048     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationMethodName()
049     */
050    protected String handleGetTransformationMethodName()
051    {
052        return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName());
053    }
054
055    /**
056     * The suffix for the transformation annonymous name.
057     */
058    private static final String TRANSFORMATION_ANONYMOUS_NAME_SUFFIX = "_TRANSFORMER";
059
060    /**
061     * @return getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX
062     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationAnonymousName()
063     */
064    protected String handleGetTransformationAnonymousName()
065    {
066        return this.getName().toUpperCase() + TRANSFORMATION_ANONYMOUS_NAME_SUFFIX;
067    }
068
069    /**
070     * @return dependency.getTargetElement().equals(sourceElement)
071     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#isCircularReference()
072     */
073    protected boolean handleIsCircularReference()
074    {
075        boolean circularReference = false;
076        final ModelElementFacade sourceElement = this.getSourceElement();
077        final ModelElementFacade targetElement = this.getTargetElement();
078        final Collection<DependencyFacade> sourceDependencies = targetElement.getSourceDependencies();
079        if (sourceDependencies != null && !sourceDependencies.isEmpty())
080        {
081            circularReference =
082                CollectionUtils.find(
083                    sourceDependencies,
084                    new Predicate()
085                    {
086                        public boolean evaluate(Object object)
087                        {
088                            DependencyFacade dependency = (DependencyFacade)object;
089                            return dependency != null && dependency.getTargetElement().equals(sourceElement);
090                        }
091                    }) != null;
092        }
093        return circularReference;
094    }
095
096    private boolean isXmlPersistenceActive()
097    {
098        return SpringHibernateUtils.isXmlPersistenceActive(
099            (String)this.getConfiguredProperty(SpringGlobals.HIBERNATE_VERSION),
100            (String)this.getConfiguredProperty(SpringGlobals.HIBERNATE_XML_PERSISTENCE));
101    }
102
103    /**
104     * @return incoming ValueObject references
105     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationConstantValue()
106     */
107    protected int handleGetTransformationConstantValue()
108    {
109        int value = 0;
110        ModelElementFacade element = this.getSourceElement();
111        if (element instanceof SpringEntity)
112        {
113            final List<SpringEntity> hierarchy = new ArrayList<SpringEntity>();
114            for (SpringEntity entity = (SpringEntity)element; entity != null;
115                entity = (SpringEntity)entity.getGeneralization())
116            {
117                hierarchy.add(entity);
118            }
119            boolean breakOut = false;
120            for (int ctr = hierarchy.size() - 1; ctr >= 0; ctr--)
121            {
122                final SpringEntity generalization = hierarchy.get(ctr);
123                //TODO Bug? generalization.getValueObjectReferences() can contain not only ClassifierFacades
124                for (final Iterator referenceIterator = generalization.getValueObjectReferences().iterator();
125                    referenceIterator.hasNext();)
126                {
127                    final Object reference = referenceIterator.next();
128                    value++;
129                    if (reference.equals(this))
130                    {
131                        breakOut = true;
132                        break;
133                    }
134                }
135                if (breakOut)
136                {
137                    break;
138                }
139            }
140        }
141
142        if (isXmlPersistenceActive())
143        {
144            value++;
145        }
146
147        return value;
148    }
149
150    /**
151     * @return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + getName() + SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX
152     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToCollectionMethodName()
153     */
154    protected String handleGetTransformationToCollectionMethodName()
155    {
156        return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName()) +
157        SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
158    }
159
160    /**
161     * @return TRANSFORMATION_METHOD_PREFIX + capitalize(getName()) + TRANSFORMATION_TO_ARRAY_METHOD_SUFFIX
162     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToArrayMethodName()
163     */
164    protected String handleGetTransformationToArrayMethodName()
165    {
166        return SpringGlobals.TRANSFORMATION_METHOD_PREFIX + StringUtils.capitalize(this.getName()) +
167            SpringGlobals.TRANSFORMATION_TO_ARRAY_METHOD_SUFFIX;
168    }
169
170    /**
171     * @return getDaoNamePattern().replaceAll("\\{0\\}", getName())
172     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getDaoName()
173     */
174    protected String handleGetDaoName()
175    {
176        return this.getDaoNamePattern().replaceAll(
177            "\\{0\\}",
178            this.getName());
179    }
180
181    /**
182     * Gets the value of the {@link SpringGlobals#DAO_PATTERN}.
183     *
184     * @return the DAO name pattern.
185     */
186    private String getDaoNamePattern()
187    {
188        return String.valueOf(this.getConfiguredProperty(SpringGlobals.DAO_PATTERN));
189    }
190
191    /**
192     * @return "get" + StringUtils.capitalize(this.getDaoName())
193     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getDaoGetterName()
194     */
195    protected String handleGetDaoGetterName()
196    {
197        return "get" + StringUtils.capitalize(this.getDaoName());
198    }
199
200    /**
201     * @return "set" + StringUtils.capitalize(this.getDaoName())
202     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getDaoSetterName()
203     */
204    protected String handleGetDaoSetterName()
205    {
206        return "set" + StringUtils.capitalize(this.getDaoName());
207    }
208
209    /**
210     * @return getTransformationToEntityMethodName() + TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX
211     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToEntityCollectionMethodName()
212     */
213    protected String handleGetTransformationToEntityCollectionMethodName()
214    {
215        return this.getTransformationToEntityMethodName() + SpringGlobals.TRANSFORMATION_TO_COLLECTION_METHOD_SUFFIX;
216    }
217
218    /**
219     * The suffix for the transformation to entity method name.
220     */
221    private static final String TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX = "ToEntity";
222
223    /**
224     * @return getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX
225     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getTransformationToEntityMethodName()
226     */
227    protected String handleGetTransformationToEntityMethodName()
228    {
229        return this.getName() + TRANSFORMATION_TO_ENTITY_METHOD_NAME_SUFFIX;
230    }
231
232    /**
233     * The suffix for the value object to entity transformer. "Transformer"
234     */
235    private static final String VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX = "Transformer";
236
237    /**
238     * @return capitalize(this.getTransformationToEntityMethodName()) + VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX
239     * @see org.andromda.cartridges.spring.metafacades.SpringDependency#getValueObjectToEntityTransformerName()
240     */
241    protected String handleGetValueObjectToEntityTransformerName()
242    {
243        return StringUtils.capitalize(this.getTransformationToEntityMethodName()) +
244        VALUE_OBJECT_TO_ENTITY_TRANSFORMER_SUFFIX;
245    }
246}