KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > ejb > ejbqlc > ParameterSupport


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25
26 /*
27  * ParameterSupport.java
28  *
29  * Created on December 07, 2001
30  */

31
32 package com.sun.jdo.spi.persistence.support.ejb.ejbqlc;
33
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36
37 import com.sun.jdo.spi.persistence.utility.I18NHelper;
38
39 /**
40  * Helper class to handle EJBQL query parameters.
41  *
42  * @author Michael Bouschen
43  * @author Shing Wai Chan
44  */

45 public class ParameterSupport
46 {
47     /** The types of the parameters of the finder/selector method. */
48     private Class JavaDoc[] parameterTypes;
49     
50     /**
51      * The EJB names corresponding to types of parameters of the
52      * finder/selector method.
53      */

54     private String JavaDoc[] parameterEjbNames;
55     
56     /** I18N support. */
57     protected final static ResourceBundle JavaDoc msgs = I18NHelper.loadBundle(
58         ParameterSupport.class);
59
60     /**
61      * Constructor.
62      * @param method the Method instance of the finder/selector method.
63      */

64     public ParameterSupport(Method JavaDoc method)
65     {
66         this.parameterTypes =
67             (method == null) ? new Class JavaDoc[0] : method.getParameterTypes();
68         this.parameterEjbNames = new String JavaDoc[this.parameterTypes.length];
69     }
70
71     /**
72      * Returns type of the EJBQL parameter by input parameter declaration
73      * string. The specified string denotes a parameter application in EJBQL.
74      * It has the form "?<number>" where <number> is the parameter number
75      * starting with 1.
76      * @return class instance representing the parameter type.
77      */

78     public Class JavaDoc getParameterType(String JavaDoc ejbqlParamDecl)
79     {
80         return getParameterType(getParamNumber(ejbqlParamDecl));
81     }
82
83     /**
84      * Returns the type of the EJBQL parameter by number.
85      * Note, the numbering of EJBQL parameters starts with 1,
86      * so the method expects 1 as the number of the first parameter.
87      * @return class instance representing the parameter type.
88      */

89     public Class JavaDoc getParameterType(int paramNumber)
90     {
91         // InputParams are numbered starting at 1, so adjust for
92
// array indexing.
93
return parameterTypes[paramNumber - 1];
94     }
95
96     /**
97      * Get EJB name corresponding to the EJBQL parameter by input
98      * parameter declaration string.
99      * @param ejbqlParamDecl denotes a parameter application in EJBQL.
100      * It has the form "?<number>" where <number> is the parameter number
101      * starting with 1.
102      * @return class instance representing the parameter type.
103      */

104     public String JavaDoc getParameterEjbName(String JavaDoc ejbqlParamDecl)
105     {
106         return getParameterEjbName(getParamNumber(ejbqlParamDecl));
107     }
108
109     /**
110      * Get EJB name corresponding to the EJBQL parameter number.
111      * @param paramNumber numbering of parameters starting with 1
112      * @return class instance representing the parameter type.
113      */

114     public String JavaDoc getParameterEjbName(int paramNumber)
115     {
116         return parameterEjbNames[paramNumber - 1];
117     }
118
119     /**
120      * Set EJB name corresponding to the EJBQL parameter by input
121      * parameter declaration string.
122      * @param ejbqlParamDecl denotes a parameter application in EJBQL.
123      * It has the form "?<number>" where <number> is the parameter number
124      * starting with 1.
125      * @param ejbName
126      * @return class instance representing the parameter type.
127      */

128     public void setParameterEjbName(String JavaDoc ejbqlParamDecl, String JavaDoc ejbName)
129     {
130         parameterEjbNames[getParamNumber(ejbqlParamDecl) - 1] = ejbName;
131     }
132
133     /**
134      * Get all EJB names corresponding to the EJBQL parameters.
135      * @return class instance representing the parameter type.
136      */

137     public String JavaDoc[] getParameterEjbNames()
138     {
139         return parameterEjbNames;
140     }
141
142     /**
143      * Returns the name of the corresponding JDO parameter.
144      * The specified string denotes a parameter application in EJBQL.
145      * It has the form "?<number>" where <number> is the parameter number
146      * starting with 1.
147      * @return name of JDOQL parameter
148      */

149     public String JavaDoc getParameterName(String JavaDoc ejbqlParamDecl)
150     {
151         return getParameterName(getParamNumber(ejbqlParamDecl));
152     }
153
154     /**
155      * Returns the name of the corresponding JDO parameter by parameter number.
156      * @return name of JDOQL parameter
157      */

158     public String JavaDoc getParameterName(int paramNumber)
159     {
160         return "_jdoParam" + String.valueOf(paramNumber);
161     }
162
163     /**
164      * Returns the number of parameters.
165      * @return parameter count.
166      */

167     public int getParameterCount()
168     {
169         return parameterTypes.length;
170     }
171
172     // Internal methods
173

174     /**
175      * Internal method to extract the number from a parameter application
176      * in EJBQL.
177      */

178     private int getParamNumber(String JavaDoc ejbqlParamDecl)
179     {
180         int paramNum = 0;
181         try {
182             paramNum = Integer.parseInt(ejbqlParamDecl.substring(1));
183         } catch(Exception JavaDoc ex) {
184             ErrorMsg.error(I18NHelper.getMessage(
185                 msgs, "EXC_InvalidParameterIndex", //NOI18N
186
ejbqlParamDecl, String.valueOf(parameterTypes.length)));
187         }
188         if (paramNum < 1 || paramNum > parameterTypes.length) {
189             ErrorMsg.error(I18NHelper.getMessage(
190                 msgs, "EXC_InvalidParameterIndex", //NOI18N
191
ejbqlParamDecl, String.valueOf(parameterTypes.length)));
192         }
193         return paramNum;
194     }
195 }
196
Popular Tags