View Javadoc
1   package org.andromda.cartridges.spring.metafacades;
2   
3   import java.util.Arrays;
4   import java.util.Collection;
5   import java.util.Iterator;
6   import org.andromda.cartridges.spring.SpringProfile;
7   import org.andromda.cartridges.spring.SpringUtils;
8   import org.andromda.metafacades.uml.AttributeFacade;
9   import org.andromda.metafacades.uml.ClassifierFacade;
10  import org.andromda.metafacades.uml.ModelElementFacade;
11  import org.andromda.metafacades.uml.ParameterFacade;
12  import org.andromda.metafacades.uml.UMLProfile;
13  import org.apache.commons.lang.StringUtils;
14  
15  /**
16   * MetafacadeLogic implementation for org.andromda.cartridges.spring.metafacades.SpringQueryOperation.
17   *
18   * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperation
19   */
20  public class SpringQueryOperationLogicImpl
21          extends SpringQueryOperationLogic
22  {
23      private static final long serialVersionUID = 34L;
24      /**
25       * Public constructor for SpringQueryOperationLogicImpl
26       * @param metaObject
27       * @param context
28       * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperation
29       */
30      public SpringQueryOperationLogicImpl(Object metaObject, String context)
31      {
32          super(metaObject, context);
33      }
34  
35      /**
36       * @return getQuery((SpringEntity)null)
37       * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperationLogic#getQuery()
38       */
39      protected String handleGetQuery()
40      {
41          return this.getQuery((SpringEntity)null);
42      }
43  
44      /**
45       * Stores the translated query so that its only translated once.
46       */
47      private String translatedQuery = null;
48  
49      /**
50       * Retrieves the translated query.
51       */
52      private String getTranslatedQuery()
53      {
54          if (this.translatedQuery == null)
55          {
56              this.translatedQuery = super.getQuery("query.Hibernate-QL");
57          }
58          return this.translatedQuery;
59      }
60  
61      /**
62       * Stores whether or not named parameters should be used in hibernate queries.
63       */
64      private static final String USE_NAMED_PARAMETERS = "hibernateQueryUseNamedParameters";
65  
66      /**
67       * @return SpringMetafacadeUtils.getUseNamedParameters(this, useNamedParameters)
68       * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperationLogic#isUseNamedParameters()
69       */
70      protected boolean handleIsUseNamedParameters()
71      {
72          boolean useNamedParameters = Boolean.valueOf(String.valueOf(this.getConfiguredProperty(USE_NAMED_PARAMETERS)))
73                  .booleanValue()
74                  || StringUtils.isNotBlank(this.getTranslatedQuery());
75  
76          return SpringMetafacadeUtils.getUseNamedParameters(this, useNamedParameters);
77      }
78  
79      /**
80       * @return getCriteriaArgument() != null
81       * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperation#isCriteriaFinder()
82       */
83      protected boolean handleIsCriteriaFinder()
84      {
85          return this.getCriteriaArgument() != null;
86      }
87  
88      /**
89       * @return Parameter with UMLProfile.STEREOTYPE_CRITERIA
90       * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperation#getCriteriaArgument()
91       */
92      protected ParameterFacade handleGetCriteriaArgument()
93      {
94          ParameterFacade foundParameter = null;
95          for (final ParameterFacade parameter : this.getParameters())
96          {
97              final ClassifierFacade type = parameter.getType();
98              if (type != null && type.hasStereotype(UMLProfile.STEREOTYPE_CRITERIA))
99              {
100                 foundParameter = parameter;
101                 break;
102             }
103         }
104         return foundParameter;
105     }
106 
107     /**
108      * @param entity
109      * @return query
110      * @see org.andromda.cartridges.spring.metafacades.SpringQueryOperation#getQuery(org.andromda.cartridges.spring.metafacades.SpringEntity)
111      */
112     protected String handleGetQuery(SpringEntity entity)
113     {
114         // first see if we can retrieve the query from the super class as an OCL
115         // translation
116         String queryString = this.getTranslatedQuery();
117 
118         // otherwise see if there is a query stored as a tagged value
119         if (StringUtils.isBlank(queryString))
120         {
121             Object value = this.findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_QUERY);
122             queryString = (String)value;
123             if (queryString != null)
124             {
125                 // remove any excess whitespace
126                 queryString = queryString.replaceAll("[$\\s]+", " ");
127             }
128         }
129 
130         // if there wasn't any stored query, create one by default.
131         if (StringUtils.isBlank(queryString))
132         {
133             ModelElementFacade owner = null;
134             String entityName = null;
135             if (entity == null)
136             {
137                 owner = this.getOwner();
138                 entityName = owner.getFullyQualifiedName();
139             }
140             else
141             {
142                 owner = entity;
143                 //retrieve the entity implementation for proper hibernate mapping resolving
144                 entityName = entity.getFullyQualifiedEntityImplementationName();
145             }
146             String variableName = StringUtils.uncapitalize(owner.getName());
147             queryString = "from " + entityName + " as " + variableName;
148             if (!this.getArguments().isEmpty())
149             {
150                 queryString += " where";
151                 Collection<ParameterFacade> arguments = this.getArguments();
152                 Iterator<ParameterFacade> iterator = arguments.iterator();
153                 while(iterator.hasNext())
154                 {
155                     final ParameterFacade argument = iterator.next();
156                     final ClassifierFacade type = argument.getType();
157                     if (type != null)
158                     {
159                         final String parameterName = argument.getName();
160                         if (type.isEmbeddedValue())
161                         {
162                             for (final Iterator<AttributeFacade> attributeIterator = type.getAttributes(true).iterator(); attributeIterator.hasNext();)
163                             {
164                                 final AttributeFacade attribute = attributeIterator.next();
165                                 String parameter = "?";
166                                 if (this.isUseNamedParameters())
167                                 {
168                                     parameter = ':' + SpringUtils.concatNamesCamelCase(Arrays.asList(parameterName, attribute.getName()));
169                                 }
170                                 queryString += ' ' + variableName + '.' + parameterName + '.' + attribute.getName() + " = " + parameter;
171                                 if (attributeIterator.hasNext())
172                                 {
173                                     queryString += " and";
174                                 }
175                             }
176                         }
177                         else
178                         {
179                             String parameter = "?";
180                             if (this.isUseNamedParameters())
181                             {
182                                 parameter = ':' + parameterName;
183                             }
184                             queryString += ' ' + variableName + '.' + parameterName + " = " + parameter;
185                             if (iterator.hasNext())
186                             {
187                                 queryString += " and";
188                             }
189                         }
190                     }
191                 }
192             }
193         }
194         return queryString;
195     }
196 }