001package org.andromda.cartridges.hibernate.metafacades;
002
003import java.util.Collection;
004import org.andromda.cartridges.hibernate.HibernateProfile;
005import org.andromda.core.common.ExceptionUtils;
006import org.andromda.metafacades.uml.ClassifierFacade;
007import org.andromda.metafacades.uml.FilteredCollection;
008import org.andromda.metafacades.uml.ModelElementFacade;
009import org.andromda.metafacades.uml.OperationFacade;
010import org.apache.commons.collections.CollectionUtils;
011import org.apache.commons.collections.Predicate;
012import org.apache.commons.lang.StringUtils;
013
014/**
015 * Contains utilities for use with Hibernate metafacades.
016 *
017 * @author Chad Brandon
018 */
019class HibernateMetafacadeUtils
020{
021    /**
022     * Gets the view type for the passed in <code>classifier</code>. If the
023     * view type can be retrieved from the <code>classifier</code>, then that
024     * is used, otherwise the <code>defaultViewType</code> is returned.
025     * @param classifier
026     * @param defaultViewType
027     * @return String the view type name.
028     */
029    static String getViewType(
030        ClassifierFacade classifier,
031        String defaultViewType)
032    {
033        ExceptionUtils.checkNull("classifer", classifier);
034        String viewType = null;
035        if (classifier.hasStereotype(HibernateProfile.STEREOTYPE_SERVICE))
036        {
037            String viewTypeValue = (String)classifier.findTaggedValue(HibernateProfile.TAGGEDVALUE_EJB_VIEWTYPE);
038
039            // if the view type wasn't found, search all super classes
040            if (StringUtils.isEmpty(viewTypeValue))
041            {
042                viewType =
043                    (String)CollectionUtils.find(
044                        classifier.getAllGeneralizations(),
045                        new Predicate()
046                        {
047                            public boolean evaluate(Object object)
048                            {
049                                return ((ModelElementFacade)object).findTaggedValue(
050                                    HibernateProfile.TAGGEDVALUE_EJB_VIEWTYPE) != null;
051                            }
052                        });
053            }
054            if (StringUtils.isNotBlank(viewTypeValue))
055            {
056                viewType = viewTypeValue;
057            }
058        }
059        if (StringUtils.isEmpty(viewType) || viewType==null)
060        {
061            viewType = defaultViewType;
062        }
063        return viewType.toLowerCase();
064    }
065
066    /**
067     * Creates a fully qualified Hibernate name from the given <code>packageName</code>,
068     * <code>name</code>, and <code>suffix</code>. Not to be used for attributes, types, or parameters
069     * which could possibly be java.lang. types.
070     *
071     * @param packageName the name of the model element package.
072     * @param name the name of the model element.
073     * @param suffix the suffix to append.
074     * @return the new fully qualified name.
075     */
076    static String getFullyQualifiedName(
077        String packageName,
078        String name,
079        String suffix)
080    {
081        StringBuilder fullyQualifiedName = new StringBuilder(StringUtils.trimToEmpty(packageName));
082        if (StringUtils.isNotBlank(packageName))
083        {
084            fullyQualifiedName.append('.');
085        }
086        fullyQualifiedName.append(StringUtils.trimToEmpty(name));
087        fullyQualifiedName.append(StringUtils.trimToEmpty(suffix));
088        return fullyQualifiedName.toString();
089    }
090
091    /**
092     * filters all static operations
093     * @param operations
094     * @return businessOperations
095     */
096    static Collection<OperationFacade> filterBusinessOperations(Collection<OperationFacade> operations)
097    {
098        Collection<OperationFacade> businessOperations =
099            new FilteredCollection(operations)
100            {
101                private static final long serialVersionUID = 34L;
102                public boolean evaluate(Object object)
103                {
104                    return !((OperationFacade)object).isStatic();
105                }
106            };
107        return businessOperations;
108    }
109
110    /**
111     * Checks whether the passed in operation is a query and should be using named parameters.
112     *
113     * @param operation the operation.
114     * @param defaultUseNamedParameters the default value.
115     * @return whether named parameters should be used.
116     */
117    static boolean getUseNamedParameters(OperationFacade operation,
118        boolean defaultUseNamedParameters)
119    {
120        ExceptionUtils.checkNull("operation", operation);
121        boolean useNamedParameters = defaultUseNamedParameters;
122        if (operation.isQuery())
123        {
124            String useNamedParametersValue = StringUtils.trimToEmpty((String)operation
125                    .findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_USE_NAMED_PARAMETERS));
126            if (StringUtils.isNotBlank(useNamedParametersValue))
127            {
128                useNamedParameters = Boolean.valueOf(useNamedParametersValue).booleanValue();
129            }
130        }
131        return useNamedParameters;
132    }
133}