View Javadoc
1   package org.andromda.cartridges.ejb3.metafacades;
2   
3   import java.util.Collection;
4   import org.andromda.cartridges.ejb3.EJB3Profile;
5   import org.andromda.metafacades.uml.ModelElementFacade;
6   import org.andromda.metafacades.uml.ParameterFacade;
7   import org.apache.commons.lang.BooleanUtils;
8   import org.apache.commons.lang.StringUtils;
9   
10  /**
11   * <p/>
12   * Represents an EJB finder method. </p>
13   *
14   * MetafacadeLogic implementation for org.andromda.cartridges.ejb3.metafacades.EJB3FinderMethodFacade.
15   *
16   * @see EJB3FinderMethodFacade
17   */
18  public class EJB3FinderMethodFacadeLogicImpl
19      extends EJB3FinderMethodFacadeLogic
20  {
21      private static final long serialVersionUID = 34L;
22      /**
23       * Stores whether or not named parameters should be used in EJB queries.
24       */
25      private static final String QUERY_USE_NAMED_PARAMETERS = "queryUseNamedParameters";
26  
27      // ---------------- constructor -------------------------------
28  
29      /**
30       * @param metaObject
31       * @param context
32       */
33      public EJB3FinderMethodFacadeLogicImpl(final Object metaObject, final String context)
34      {
35          super (metaObject, context);
36      }
37  
38      // --------------- methods ---------------------
39  
40      /**
41       * Stores the translated query so that its only translated once.
42       */
43      private String translatedQuery = null;
44  
45      /**
46       * Retrieves the translated query.
47       */
48      private String getTranslatedQuery()
49      {
50          if (this.translatedQuery == null)
51          {
52              this.translatedQuery = super.getQuery("query.Hibernate-QL");
53          }
54          return this.translatedQuery;
55      }
56  
57      /**
58       * @see EJB3FinderMethodFacade#getQuery()
59       */
60      @Override
61      protected String handleGetQuery()
62      {
63          return this.getQuery((EJB3EntityFacade)null);
64      }
65  
66      /**
67       * @see EJB3FinderMethodFacade#getTransactionType()
68       */
69      @Override
70      protected String handleGetTransactionType()
71      {
72          return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_TRANSACTION_TYPE, true);
73      }
74  
75      /**
76       * @see EJB3FinderMethodFacadeLogic#handleIsUseNamedParameters()
77       */
78      @Override
79      protected boolean handleIsUseNamedParameters()
80      {
81          return BooleanUtils.toBoolean(String.valueOf(
82                  this.getConfiguredProperty(QUERY_USE_NAMED_PARAMETERS)));
83      }
84  
85      /**
86       * @see EJB3FinderMethodFacadeLogic#handleIsUseQueryCache()
87       */
88      @Override
89      protected boolean handleIsUseQueryCache()
90      {
91          boolean queryCacheEnabled = ((EJB3EntityFacade)this.getOwner()).isUseQueryCache();
92          String queryCacheEnabledStr = (String)super.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_USE_QUERY_CACHE);
93          if (StringUtils.isNotBlank(queryCacheEnabledStr))
94          {
95              queryCacheEnabled = BooleanUtils.toBoolean(queryCacheEnabledStr);
96          }
97          return queryCacheEnabled;
98      }
99  
100     /**
101      * @see EJB3FinderMethodFacadeLogic#handleGetQuery(EJB3EntityFacade)
102      */
103     @Override
104     protected String handleGetQuery(final EJB3EntityFacade entity)
105     {
106         // first see if we can retrieve the query from the super class as an OCL
107         // translation
108         String queryString = this.getTranslatedQuery();
109 
110         boolean whereClauseExists = false;
111 
112         // otherwise see if there is a query stored as a tagged value
113         if (StringUtils.isBlank(queryString))
114         {
115             queryString = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_QUERY);
116             if (queryString != null)
117             {
118                 // remove any excess whitespace
119                 queryString = queryString.replaceAll("[$\\s]+", " ");
120             }
121         }
122 
123         // if there wasn't any stored query, create one by default.
124         if (StringUtils.isBlank(queryString))
125         {
126             ModelElementFacade owner;
127             if (entity == null)
128             {
129                 owner = this.getOwner();
130             }
131             else
132             {
133                 owner = entity;
134             }
135             String variableName = StringUtils.uncapitalize(owner.getName()).substring(0,1);
136             queryString = "SELECT " + variableName + " from " + owner.getName() + " as " + variableName;
137             Collection<ParameterFacade> arguments = this.getArguments();
138             //int size = this.getArguments().size();
139             int argCount = 0;
140             if (arguments != null && !arguments.isEmpty())
141             {
142                 for (ParameterFacade facade : arguments)
143                 {
144                     EJB3FinderMethodArgumentFacade argument = (EJB3FinderMethodArgumentFacade)facade;
145                     if (!argument.isFirstResult() && !argument.isMaxResults())
146                     {
147                         if (!whereClauseExists)
148                         {
149                             queryString += " WHERE";
150                             whereClauseExists = true;
151                         }
152                         String parameter = "?";
153                         if (this.isUseNamedParameters())
154                         {
155                             parameter = ':' + argument.getName();
156                         }
157                         queryString += ' ' + variableName + '.' + argument.getName() + " = " + parameter + " AND";
158                         argCount++;
159                     }
160                 }
161                 if (argCount > 0)
162                 {
163                     // Remove the final ' and'
164                     queryString = queryString.substring(0, queryString.length()-4);
165                 }
166             }
167         }
168         return queryString;
169     }
170 
171     /**
172      * @see EJB3FinderMethodFacadeLogic#handleIsNamedQuery()
173      */
174     @Override
175     protected boolean handleIsNamedQuery()
176     {
177         boolean isNamedQuery = true;
178         final String query = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_QUERY);
179         if (StringUtils.isNotBlank(query))
180         {
181             isNamedQuery = false;
182         }
183         return isNamedQuery;
184     }
185 }