001package org.andromda.cartridges.ejb.metafacades;
002
003import java.util.Collection;
004import java.util.Iterator;
005import org.andromda.cartridges.ejb.EJBProfile;
006import org.andromda.metafacades.uml.ParameterFacade;
007import org.apache.commons.lang.StringUtils;
008
009/**
010 * <p>
011 * Represents an EJB finder method. </p> Metaclass facade implementation.
012 */
013public class EJBFinderMethodFacadeLogicImpl
014        extends EJBFinderMethodFacadeLogic
015{
016    private static final long serialVersionUID = 34L;
017    // ---------------- constructor -------------------------------
018
019    /**
020     * @param metaObject
021     * @param context
022     */
023    public EJBFinderMethodFacadeLogicImpl(Object metaObject, String context)
024    {
025        super(metaObject, context);
026    }
027
028    /**
029     * @see org.andromda.cartridges.ejb.metafacades.EJBFinderMethodFacadeLogic#handleGetQuery()
030     */
031    protected String handleGetQuery()
032    {
033
034        // first see if there is a query stored as a constraint
035        String queryString = super.getQuery("query.EJB-QL");
036
037        // otherwise see if there is a query stored as a tagged value
038        if (StringUtils.isEmpty(queryString))
039        {
040            Object value = this.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_QUERY);
041            queryString = (String)value;
042            if (queryString != null)
043            {
044                // remove any excess whitespace
045                queryString = queryString.replaceAll("[$\\s]+", " ");
046            }
047        }
048
049        // if there wasn't any stored query, create one by default.
050        if (StringUtils.isEmpty(queryString))
051        {
052            String variableName = StringUtils.uncapitalize(this.getOwner().getName());
053            queryString = "SELECT DISTINCT OBJECT(" + variableName + ") FROM " + this.getOwner().getName() + " as " +
054                    variableName;
055            if (!this.getArguments().isEmpty())
056            {
057                queryString = queryString + " WHERE";
058                Collection parameters = this.getArguments();
059                if (parameters != null && !parameters.isEmpty())
060                {
061                    Iterator parameterIt = parameters.iterator();
062                    for (int ctr = 1; parameterIt.hasNext(); ctr++)
063                    {
064                        Object test = parameterIt.next();
065                        ParameterFacade param = (ParameterFacade)test;
066                        queryString = queryString + ' ' + variableName + '.' + param.getName() + " = ?" + ctr;
067                        if (parameterIt.hasNext())
068                        {
069                            queryString = queryString + " AND";
070                        }
071                    }
072                }
073            }
074        }
075        return queryString;
076    }
077
078    /**
079     * @see org.andromda.cartridges.ejb.metafacades.EJBFinderMethodFacadeLogic#handleGetTransactionType()
080     * @see org.andromda.cartridges.ejb.metafacades.EJBFinderMethodFacade#getTransactionType()
081     */
082    protected String handleGetTransactionType()
083    {
084        return (String)this.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_TRANSACTION_TYPE, true);
085    }
086}