KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > filters > AbstractJoSQLFilter


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.filters;
16
17 import org.josql.Query;
18 import org.josql.QueryParseException;
19
20 /**
21  * This class just provides a base-abstract implementation that allow "Filters" to be
22  * built on top of it.
23  * <p>
24  * Last Modified By: $Author: barrygently $<br />
25  * Last Modified On: $Date: 2005/01/07 17:10:40 $<br />
26  * Current Revision: $Revision: 1.1 $<br />
27  */

28 public abstract class AbstractJoSQLFilter
29 {
30
31     protected Query q = null;
32     protected Exception JavaDoc exp = null;
33     protected boolean badQuery = false;
34
35     /**
36      * Protected constructor to allow sub-classes to init the query when they are ready.
37      */

38     protected AbstractJoSQLFilter ()
39     {
40
41     }
42
43     /**
44      * Init this filter with the query.
45      *
46      * @param q The query.
47      * @throws QueryParseException If there is an issue with the parsing of the query,
48      * or if the FROM class is not equal to the expected class.
49      */

50     public AbstractJoSQLFilter (String JavaDoc q)
51                             throws QueryParseException
52     {
53
54     this.setQuery (q);
55
56     }
57
58     /**
59      * Should sub-classes should return the type that they expect to be present in
60      * a Query.
61      *
62      * @return The class that should be used in the Query for the filter.
63      */

64     public abstract Class JavaDoc getExpectedClass ();
65
66     /**
67      * Init this file filter with the query already built and parsed.
68      *
69      * @param q The query.
70      * @throws IllegalStateException If the Query object has not been parsed.
71      * @throws QueryParseException If the FROM class is not as expected.
72      */

73     public AbstractJoSQLFilter (Query q)
74                             throws IllegalStateException JavaDoc,
75                                    QueryParseException
76     {
77
78     this.setQuery (q);
79
80     }
81
82     private void checkFrom ()
83                         throws QueryParseException
84     {
85
86     if (!this.getExpectedClass ().isAssignableFrom (this.q.getFromObjectClass ()))
87     {
88
89         throw new QueryParseException ("Query FROM class is: " +
90                        this.q.getFromObjectClass ().getName () +
91                        ", however only: " +
92                        this.getExpectedClass ().getName () +
93                        " or sub-classes are supported.");
94
95     }
96
97     }
98
99     /*
100      * Optional operation that allows a "generic" filter to be created. Sub-classes that do not
101      * wish to support this operation should throw an instance of: {@link UnsupportedOperationException}.
102      *
103      * @param o The object to evaluate the WHERE clause against.
104      * @return <code>true</code> if the WHERE clause evaluates to <code>true</code> for the specified
105      * object.
106      */

107     public abstract boolean accept (Object JavaDoc o)
108                                 throws UnsupportedOperationException JavaDoc;
109
110     /**
111      * Clear any exception stored.
112      */

113     public void clearException ()
114     {
115
116     this.exp = null;
117     this.badQuery = false;
118
119     }
120
121     /**
122      * Most "filter accept" methods do not allow for any exceptions to be thrown however since
123      * the execution of the WHERE clause on the object can cause the throwing of a
124      * {@link QueryParseException} it should be captured. If the exception is thrown then
125      * this method will return it.
126      *
127      * @return The exception thrown by the execution of the WHERE clause in {@link #accept(Object)}
128      * or by sub-class/interface specific methods, this may be null if no exception was thrown.
129      */

130     public Exception JavaDoc getException ()
131     {
132
133     return this.exp;
134
135     }
136
137     /**
138      * Set a new Query (string form) for use in this filter.
139      *
140      * @param q The Query to use.
141      * @throws QueryParseException If there is an issue with the parsing of the query,
142      * or if the FROM class is not as expected.
143      */

144     public void setQuery (String JavaDoc q)
145                       throws QueryParseException
146     {
147
148     this.q = new Query ();
149     this.q.parse (q);
150
151     this.badQuery = false;
152     this.exp = null;
153
154     this.checkFrom ();
155
156     }
157
158     /**
159      * Set a new Query object for use in this filter.
160      *
161      * @param q The Query to use.
162      * @throws IllegalStateException If the Query object has not been parsed.
163      * @throws QueryParseException If the FROM class is not as expected.
164      */

165     public void setQuery (Query q)
166                       throws IllegalStateException JavaDoc,
167                              QueryParseException
168     {
169
170     if (!q.parsed ())
171     {
172
173         throw new IllegalStateException JavaDoc ("Query has not yet been parsed.");
174
175     }
176
177     this.q = q;
178
179     this.checkFrom ();
180
181     this.badQuery = false;
182     this.exp = null;
183
184     }
185
186     /**
187      * Get the Query we are using to process objects.
188      *
189      * @return The Query.
190      */

191     public Query getQuery ()
192     {
193
194     return this.q;
195
196     }
197
198 }
199
Popular Tags