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
11
12
13
14
15 public class HibernateFinderMethodLogicImpl
16 extends HibernateFinderMethodLogic
17 {
18 private static final long serialVersionUID = 34L;
19
20
21
22
23
24 public HibernateFinderMethodLogicImpl(
25 Object metaObject,
26 String context)
27 {
28 super(metaObject, context);
29 }
30
31
32
33
34 @Override
35 protected String handleGetQuery()
36 {
37
38
39 String queryString = this.getTranslatedQuery();
40
41
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
49 queryString = queryString.replaceAll("[$\\s]+", " ");
50 }
51 }
52
53
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
84
85 private String translatedQuery = null;
86
87
88
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
101
102
103 private static final String USE_NAMED_PARAMETERS = "hibernateQueryUseNamedParameters";
104
105
106
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
118
119
120 private static final String HIBERNATE_USE_QUERY_CACHE = "hibernateUseQueryCache";
121
122
123
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 }