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
12
13
14
15
16
17
18 public class EJB3FinderMethodFacadeLogicImpl
19 extends EJB3FinderMethodFacadeLogic
20 {
21 private static final long serialVersionUID = 34L;
22
23
24
25 private static final String QUERY_USE_NAMED_PARAMETERS = "queryUseNamedParameters";
26
27
28
29
30
31
32
33 public EJB3FinderMethodFacadeLogicImpl(final Object metaObject, final String context)
34 {
35 super (metaObject, context);
36 }
37
38
39
40
41
42
43 private String translatedQuery = null;
44
45
46
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
59
60 @Override
61 protected String handleGetQuery()
62 {
63 return this.getQuery((EJB3EntityFacade)null);
64 }
65
66
67
68
69 @Override
70 protected String handleGetTransactionType()
71 {
72 return (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_TRANSACTION_TYPE, true);
73 }
74
75
76
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
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
102
103 @Override
104 protected String handleGetQuery(final EJB3EntityFacade entity)
105 {
106
107
108 String queryString = this.getTranslatedQuery();
109
110 boolean whereClauseExists = false;
111
112
113 if (StringUtils.isBlank(queryString))
114 {
115 queryString = (String)this.findTaggedValue(EJB3Profile.TAGGEDVALUE_EJB_QUERY);
116 if (queryString != null)
117 {
118
119 queryString = queryString.replaceAll("[$\\s]+", " ");
120 }
121 }
122
123
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
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
164 queryString = queryString.substring(0, queryString.length()-4);
165 }
166 }
167 }
168 return queryString;
169 }
170
171
172
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 }