View Javadoc
1   package org.andromda.cartridges.hibernate.metafacades;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   import org.andromda.cartridges.hibernate.HibernateProfile;
6   import org.andromda.metafacades.uml.ParameterFacade;
7   import org.apache.commons.lang.StringUtils;
8   
9   /**
10   * @author Chad Brandon
11   * @author Carlos Cuenca
12   * @see HibernateFinderMethodLogic
13   *      Metaclass facade implementation.
14   */
15  public class HibernateFinderMethodLogicImpl
16      extends HibernateFinderMethodLogic
17  {
18      private static final long serialVersionUID = 34L;
19      // ---------------- constructor -------------------------------
20      /**
21       * @param metaObject
22       * @param context
23       */
24      public HibernateFinderMethodLogicImpl(
25          Object metaObject,
26          String context)
27      {
28          super(metaObject, context);
29      }
30  
31      /**
32       * @see org.andromda.cartridges.hibernate.metafacades.HibernateFinderMethod#getQuery()
33       */
34      @Override
35      protected String handleGetQuery()
36      {
37          // first see if we can retrieve the query from the super class as an OCL
38          // translation
39          String queryString = this.getTranslatedQuery();
40  
41          // otherwise see if there is a query stored as a tagged value
42          if (StringUtils.isEmpty(queryString))
43          {
44              Object value = this.findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_QUERY);
45              queryString = (String)value;
46              if (queryString != null)
47              {
48                  // remove any excess whitespace
49                  queryString = queryString.replaceAll("[$\\s]+", " ");
50              }
51          }
52  
53          // if there wasn't any stored query, create one by default.
54          if (StringUtils.isEmpty(queryString))
55          {
56              String variableName = StringUtils.uncapitalize(this.getOwner().getName()).substring(0,1);
57              queryString = "SELECT " + variableName + " FROM " + this.getOwner().getName() + " AS " + variableName;
58              Collection arguments = this.getArguments();
59              if (arguments != null && !arguments.isEmpty())
60              {
61                  queryString = queryString + " WHERE";
62                  Iterator argumentIt = arguments.iterator();
63                  for (; argumentIt.hasNext();)
64                  {
65                      ParameterFacade argument = (ParameterFacade)argumentIt.next();
66                      String parameter = "?";
67                      if (this.isUseNamedParameters())
68                      {
69                          parameter = ':' + argument.getName();
70                      }
71                      queryString = queryString + ' ' + variableName + '.' + argument.getName() + " = " + parameter;
72                      if (argumentIt.hasNext())
73                      {
74                          queryString = queryString + " AND";
75                      }
76                  }
77              }
78          }
79          return queryString;
80      }
81  
82      /**
83       * Stores the translated query so that its only translated once.
84       */
85      private String translatedQuery = null;
86  
87      /**
88       * Retrieves the translated query.
89       */
90      private String getTranslatedQuery()
91      {
92          if (StringUtils.isBlank(this.translatedQuery))
93          {
94              this.translatedQuery = super.getQuery("query.Hibernate-QL");
95          }
96          return this.translatedQuery;
97      }
98  
99      /**
100      * Stores whether or not named parameters should be used in hibernate
101      * queries.
102      */
103     private static final String USE_NAMED_PARAMETERS = "hibernateQueryUseNamedParameters";
104 
105     /**
106      * @see org.andromda.cartridges.hibernate.metafacades.HibernateFinderMethod#isUseNamedParameters()
107      */
108     @Override
109     protected boolean handleIsUseNamedParameters()
110     {
111         boolean useNamedParameters = Boolean.valueOf(String.valueOf(this.getConfiguredProperty(USE_NAMED_PARAMETERS))).booleanValue()
112                 || StringUtils.isNotBlank(this.getTranslatedQuery());
113         return HibernateMetafacadeUtils.getUseNamedParameters(this, useNamedParameters);
114     }
115 
116     /**
117      * Stores the value indicating whether or not to use hibernate query
118      * caching.
119      */
120     private static final String HIBERNATE_USE_QUERY_CACHE = "hibernateUseQueryCache";
121 
122     /**
123      * @see org.andromda.cartridges.hibernate.metafacades.HibernateFinderMethod#isUseQueryCache()
124      */
125     @Override
126     protected boolean handleIsUseQueryCache()
127     {
128         boolean useQueryCache =
129             Boolean.valueOf(String.valueOf(this.getConfiguredProperty(HIBERNATE_USE_QUERY_CACHE))).booleanValue();
130 
131         if (useQueryCache)
132         {
133             useQueryCache =
134                     Boolean.valueOf(
135                     String.valueOf(findTaggedValue(HibernateProfile.TAGGEDVALUE_HIBERNATE_USE_QUERY_CACHE)))
136                        .booleanValue();
137         }
138         return useQueryCache;
139     }
140 }