KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > query > EJBQLQuery


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.query;
20
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.ejbql.EJBQLCompiledExpression;
27 import org.apache.cayenne.ejbql.EJBQLException;
28 import org.apache.cayenne.ejbql.EJBQLParserFactory;
29 import org.apache.cayenne.map.DataMap;
30 import org.apache.cayenne.map.EntityResolver;
31
32 /**
33  * An EJBQL query representation in Cayenne.
34  *
35  * @since 3.0
36  * @author Andrus Adamchik
37  */

38 public class EJBQLQuery implements Query {
39
40     protected String JavaDoc name;
41     protected String JavaDoc ejbqlStatement;
42     protected Map JavaDoc parameters;
43
44     protected transient EJBQLCompiledExpression expression;
45     EJBQLQueryMetadata metadata = new EJBQLQueryMetadata();
46
47     public EJBQLQuery(String JavaDoc ejbqlStatement) {
48         this.ejbqlStatement = ejbqlStatement;
49     }
50
51     public QueryMetadata getMetaData(EntityResolver resolver) {
52         metadata.resolve(resolver, this);
53         return metadata;
54     }
55
56     public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery) {
57         DataMap map = getMetaData(resolver).getDataMap();
58
59         if (map == null) {
60             throw new CayenneRuntimeException("No DataMap found, can't route query "
61                     + this);
62         }
63
64         router.route(router.engineForDataMap(map), this, substitutedQuery);
65     }
66
67     public SQLAction createSQLAction(SQLActionVisitor visitor) {
68         return visitor.ejbqlAction(this);
69     }
70
71     /**
72      * Returns an unparsed EJB QL statement used to initialize this query.
73      */

74     public String JavaDoc getEjbqlStatement() {
75         return ejbqlStatement;
76     }
77
78     /**
79      * Returns lazily initialized EJBQLCompiledExpression for this query EJBQL.
80      */

81     public EJBQLCompiledExpression getExpression(EntityResolver resolver)
82             throws EJBQLException {
83         if (expression == null) {
84             this.expression = EJBQLParserFactory.getParser().compile(
85                     ejbqlStatement,
86                     resolver);
87         }
88
89         return expression;
90     }
91
92     public String JavaDoc getName() {
93         return name;
94     }
95
96     public void setName(String JavaDoc name) {
97         this.name = name;
98     }
99
100     /**
101      * Returns unmodifiable map of combined named and positional parameters. Positional
102      * parameter keys are Integers, while named parameter keys are strings.
103      */

104     public Map JavaDoc getParameters() {
105         return parameters != null
106                 ? Collections.unmodifiableMap(parameters)
107                 : Collections.EMPTY_MAP;
108     }
109
110     /**
111      * Sets a named query parameter value.
112      */

113     public void setParameter(String JavaDoc name, Object JavaDoc object) {
114
115         // do a minimal sanity check
116
if (name == null || name.length() < 1) {
117             throw new IllegalArgumentException JavaDoc("Null or empty parameter name");
118         }
119
120         // TODO: andrus, 6/12/2007 - validate against available query parameters - JPA
121
// spec requires it.
122

123         if (parameters == null) {
124             parameters = new HashMap JavaDoc();
125         }
126
127         parameters.put(name, object);
128     }
129
130     /**
131      * Sets a positional query parameter value. Note that parameter indexes are starting
132      * from 1.
133      */

134     public void setParameter(int position, Object JavaDoc object) {
135
136         if (position < 1) {
137             throw new IllegalArgumentException JavaDoc("Parameter position must be >= 1: "
138                     + position);
139         }
140
141         // TODO: andrus, 6/12/2007 - validate against available query parameters - JPA
142
// spec requires it.
143

144         if (parameters == null) {
145             parameters = new HashMap JavaDoc();
146         }
147
148         parameters.put(new Integer JavaDoc(position), object);
149     }
150 }
151
Popular Tags