KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > QueryMethodHelper


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ddloaders.multiview;
21
22 import org.netbeans.modules.j2ee.dd.api.ejb.MethodParams;
23 import org.netbeans.modules.j2ee.dd.api.ejb.Query;
24 import org.netbeans.modules.j2ee.dd.api.ejb.QueryMethod;
25
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * @author pfiala
34  */

35 public class QueryMethodHelper {
36
37     final Query query;
38     private final EntityHelper entityHelper;
39     private boolean isSelectMethod;
40 // private Method implementationMethod;
41
// public Method remoteMethod;
42
// public Method localMethod;
43

44     public QueryMethodHelper(EntityHelper helper, Query query) {
45         this.query = query;
46         this.entityHelper = helper;
47         isSelectMethod = query.getQueryMethod().getMethodName().startsWith(EntityHelper.Queries.SELECT_PREFIX);
48         init();
49     }
50
51     protected void init() {
52         QueryMethod queryMethod = query.getQueryMethod();
53         List JavaDoc parameters = getQueryMethodParams(queryMethod);
54         String JavaDoc methodName = queryMethod.getMethodName();
55 // JavaClass beanClass = entityHelper.getBeanClass();
56
// implementationMethod = beanClass == null ? null : beanClass.getMethod(methodName, parameters, false);
57
// remoteMethod = getMethod(entityHelper.getHomeInterfaceClass(), methodName, parameters);
58
// localMethod = getMethod(entityHelper.getLocalHomeInterfaceClass(), methodName, parameters);
59
}
60
61 // private static Method getMethod(JavaClass javaClass, String methodName, List parameters) {
62
// return javaClass == null ? null : javaClass.getMethod(methodName, parameters, false);
63
// }
64

65     public String JavaDoc getMethodName() {
66         return query.getQueryMethod().getMethodName();
67     }
68
69     public String JavaDoc getReturnType() {
70 // JMIUtils.beginJmiTransaction();
71
// try {
72
// Method method = getImplementationMethod();
73
// if (method != null) {
74
// Type type = method.getType();
75
// if (type == null) {
76
// return null;
77
// } else {
78
// return type.getName();
79
// }
80
// }
81
// return null;
82
// } finally {
83
// JMIUtils.endJmiTransaction();
84
// }
85
return null;
86     }
87
88     public boolean returnsCollection() {
89         return "java.util.Collection".equals(getReturnType());//NOI18N
90
}
91
92     public String JavaDoc getResultInterface() {
93 // JavaClass localHomeClass = entityHelper.getLocalHomeInterfaceClass();
94
// JavaClass homeClass = entityHelper.getHomeInterfaceClass();
95

96         QueryMethod queryMethod = query.getQueryMethod();
97         List JavaDoc parameters = getQueryMethodParams(queryMethod);
98         String JavaDoc methodName = queryMethod.getMethodName();
99 // boolean hasLocal = localHomeClass != null && localHomeClass.getMethod(methodName, parameters, false) != null;
100
// boolean hasRemote = homeClass != null && homeClass.getMethod(methodName, parameters, false) != null;
101
// String remote = "remote"; //NOI18N;
102
// String local = "local"; //NOI18N;
103
// if (hasLocal) {
104
// if (hasRemote) {
105
// return local + "+" + remote; //NOI18N;
106
// } else {
107
// return local;
108
// }
109
// } else {
110
// if (hasRemote) {
111
// return remote;
112
// } else {
113
// return "-"; //NOI18N;
114
// }
115
// }
116
return null;
117     }
118
119     private List JavaDoc getQueryMethodParams(QueryMethod queryMethod) {
120         String JavaDoc[] methodParam = queryMethod.getMethodParams().getMethodParam();
121         List JavaDoc params = new LinkedList JavaDoc();
122         for (int i = 0; i < methodParam.length; i++) {
123 // params.add(JMIUtils.resolveType(methodParam[i]));
124
}
125         return params;
126     }
127
128     public void removeQuery() {
129         init();
130 // Utils.removeMethod(entityHelper.getBeanClass(), implementationMethod);
131
// Utils.removeMethod(entityHelper.getLocalHomeInterfaceClass(), localMethod);
132
// Utils.removeMethod(entityHelper.getHomeInterfaceClass(), remoteMethod);
133
entityHelper.removeQuery(query);
134     }
135
136 // public Method getPrototypeMethod() {
137
// Method prototypeMethod = JMIUtils.createMethod(null);
138
// Method implementationMethod = getImplementationMethod();
139
// if (implementationMethod == null) {
140
// QueryMethod queryMethod = query.getQueryMethod();
141
// prototypeMethod.setName(queryMethod.getMethodName());
142
// MethodParams queryParams = queryMethod.getMethodParams();
143
// List parameters = prototypeMethod.getParameters();
144
// for (int i = 0, n = queryParams.sizeMethodParam(); i < n; i++) {
145
// parameters.add(JMIUtils.createParameter(prototypeMethod, "p" + i,
146
// JMIUtils.resolveType(queryParams.getMethodParam(i)), false));
147
// }
148
// return prototypeMethod;
149
// } else {
150
// prototypeMethod.setName(implementationMethod.getName());
151
// prototypeMethod.setType(implementationMethod.getType());
152
// JMIUtils.replaceParameters(prototypeMethod, implementationMethod.getParameters());
153
// return prototypeMethod;
154
//
155
// }
156
// }
157
//
158
// public Method getImplementationMethod() {
159
// Method prototypeMethod = null;
160
// if (isSelectMethod) {
161
// //select method
162
// prototypeMethod = implementationMethod;
163
// } else {
164
// //finder method
165
// if (localMethod != null) {
166
// prototypeMethod = localMethod;
167
// } else if (remoteMethod != null) {
168
// prototypeMethod = remoteMethod;
169
// }
170
// }
171
// return (prototypeMethod != null && prototypeMethod.isValid()) ? prototypeMethod : null;
172
// }
173
//
174
// public void updateFinderMethod(Method prototype, Query query, boolean singleReturn, boolean publishToLocal,
175
// boolean publishToRemote) {
176
// //todo: validation
177
// prototype.setModifiers(0);
178
// if (publishToLocal) {
179
// localMethod = setMethod(localMethod, prototype, singleReturn, false);
180
// } else {
181
// localMethod = removeMethod(localMethod, false);
182
// }
183
// if (publishToRemote) {
184
// remoteMethod = setMethod(remoteMethod, prototype, singleReturn, true);
185
// } else {
186
// remoteMethod = removeMethod(remoteMethod, true);
187
// }
188
// updateQuery(query);
189
// }
190

191     private void updateQuery(Query query) {
192         this.query.setQueryMethod(query.getQueryMethod());
193         this.query.setDescription(query.getDefaultDescription());
194         this.query.setEjbQl(query.getEjbQl());
195         entityHelper.modelUpdatedFromUI();
196     }
197
198 // private Method setMethod(Method method, Method prototype, boolean singleReturn,
199
// boolean remote) {
200
// JavaClass interfaceClass = getHomeClass(remote);
201
// assert interfaceClass != null;
202
// setReturn(prototype, singleReturn, remote);
203
// if (method == null) {
204
// Utils.addMethod(interfaceClass, (Method) prototype.duplicate(), remote);
205
// method = Utils.getMethod(interfaceClass, prototype);
206
// } else {
207
// updateMethod(method, prototype);
208
// }
209
// return method;
210
// }
211

212 // private Method removeMethod(Method method, boolean remote) {
213
// Utils.removeMethod(getHomeClass(remote), method);
214
// return null;
215
// }
216
//
217
// private JavaClass getHomeClass(boolean remote) {
218
// return remote ? entityHelper.getHomeInterfaceClass() : entityHelper.getLocalHomeInterfaceClass();
219
// }
220
//
221
// private void setReturn(Method prototype, boolean singleReturn, boolean remote) {
222
// String interfaceName = remote ? entityHelper.getRemote() : entityHelper.getLocal();
223
// String typeName = singleReturn ? interfaceName : Collection.class.getName();
224
// prototype.setType(JMIUtils.resolveType(typeName));
225
// }
226
//
227
// public void updateSelectMethod(Method prototype, Query query) {
228
// //todo: validation
229
// prototype.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
230
// if (implementationMethod == null) {
231
// JavaClass beanClass = entityHelper.getBeanClass();
232
// assert beanClass != null;
233
// Utils.addMethod(beanClass, prototype);
234
// implementationMethod = prototype;
235
// } else {
236
// updateMethod(implementationMethod, prototype);
237
// }
238
// updateQuery(query);
239
// }
240
//
241
// private static void updateMethod(Method method, Method prototype) {
242
// if (method != null) {
243
// method.setName(prototype.getName());
244
// method.setType(prototype.getType());
245
// final List newParameters = prototype.getParameters();
246
// JMIUtils.replaceParameters(method, newParameters);
247
// for (Iterator it = prototype.getExceptions().iterator(); it.hasNext();) {
248
// JMIUtils.addException(method, ((JavaClass) it.next()).getName());
249
// }
250
// method.setModifiers(prototype.getModifiers());
251
// }
252
// }
253

254     public QueryMethod getQueryMethod() {
255         return query.getQueryMethod();
256     }
257
258     public String JavaDoc getEjbQl() {
259         return query.getEjbQl();
260     }
261
262     public String JavaDoc getDefaultDescription() {
263         return query.getDefaultDescription();
264     }
265 }
266
Popular Tags