View Javadoc
1   package org.andromda.cartridges.ejb.metafacades;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import org.andromda.cartridges.ejb.EJBProfile;
6   import org.andromda.metafacades.uml.ParameterFacade;
7   import org.apache.commons.lang.StringUtils;
8   
9   /**
10   * <p>
11   * Represents an EJB finder method. </p> Metaclass facade implementation.
12   */
13  public class EJBFinderMethodFacadeLogicImpl
14          extends EJBFinderMethodFacadeLogic
15  {
16      private static final long serialVersionUID = 34L;
17      // ---------------- constructor -------------------------------
18  
19      /**
20       * @param metaObject
21       * @param context
22       */
23      public EJBFinderMethodFacadeLogicImpl(Object metaObject, String context)
24      {
25          super(metaObject, context);
26      }
27  
28      /**
29       * @see org.andromda.cartridges.ejb.metafacades.EJBFinderMethodFacadeLogic#handleGetQuery()
30       */
31      protected String handleGetQuery()
32      {
33  
34          // first see if there is a query stored as a constraint
35          String queryString = super.getQuery("query.EJB-QL");
36  
37          // otherwise see if there is a query stored as a tagged value
38          if (StringUtils.isEmpty(queryString))
39          {
40              Object value = this.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_QUERY);
41              queryString = (String)value;
42              if (queryString != null)
43              {
44                  // remove any excess whitespace
45                  queryString = queryString.replaceAll("[$\\s]+", " ");
46              }
47          }
48  
49          // if there wasn't any stored query, create one by default.
50          if (StringUtils.isEmpty(queryString))
51          {
52              String variableName = StringUtils.uncapitalize(this.getOwner().getName());
53              queryString = "SELECT DISTINCT OBJECT(" + variableName + ") FROM " + this.getOwner().getName() + " as " +
54                      variableName;
55              if (!this.getArguments().isEmpty())
56              {
57                  queryString = queryString + " WHERE";
58                  Collection parameters = this.getArguments();
59                  if (parameters != null && !parameters.isEmpty())
60                  {
61                      Iterator parameterIt = parameters.iterator();
62                      for (int ctr = 1; parameterIt.hasNext(); ctr++)
63                      {
64                          Object test = parameterIt.next();
65                          ParameterFacade param = (ParameterFacade)test;
66                          queryString = queryString + ' ' + variableName + '.' + param.getName() + " = ?" + ctr;
67                          if (parameterIt.hasNext())
68                          {
69                              queryString = queryString + " AND";
70                          }
71                      }
72                  }
73              }
74          }
75          return queryString;
76      }
77  
78      /**
79       * @see org.andromda.cartridges.ejb.metafacades.EJBFinderMethodFacadeLogic#handleGetTransactionType()
80       * @see org.andromda.cartridges.ejb.metafacades.EJBFinderMethodFacade#getTransactionType()
81       */
82      protected String handleGetTransactionType()
83      {
84          return (String)this.findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_TRANSACTION_TYPE, true);
85      }
86  }