KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > query > SelectionCriteria


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

17
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * abstract baseclass of all criteria classes, can't be instantiated.
23  *
24  * This code is based on stuff from
25  * COBRA - Java Object Persistence Layer
26  * Copyright (C) 1997, 1998 DB Harvey-George
27  * eMail: cobra@lowrent.org
28
29  * @author DB Harvey-George
30  * @author Thomas Mahler
31  * @author <a HREF="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
32  * @version $Id: SelectionCriteria.java,v 1.17.2.2 2005/12/21 22:27:09 tomdz Exp $
33  */

34 public abstract class SelectionCriteria implements java.io.Serializable JavaDoc
35 {
36     static final long serialVersionUID = -5194901539702756536L; protected static final String JavaDoc EQUAL = " = ";
37     protected static final String JavaDoc NOT_EQUAL = " <> ";
38     protected static final String JavaDoc GREATER = " > ";
39     protected static final String JavaDoc NOT_GREATER = " <= ";
40     protected static final String JavaDoc LESS = " < ";
41     protected static final String JavaDoc NOT_LESS = " >= ";
42     protected static final String JavaDoc LIKE = " LIKE ";
43     protected static final String JavaDoc NOT_LIKE = " NOT LIKE ";
44     protected static final String JavaDoc IS_NULL = " IS NULL ";
45     protected static final String JavaDoc NOT_IS_NULL = " IS NOT NULL ";
46     protected static final String JavaDoc BETWEEN = " BETWEEN ";
47     protected static final String JavaDoc NOT_BETWEEN = " NOT BETWEEN ";
48     protected static final String JavaDoc IN = " IN ";
49     protected static final String JavaDoc NOT_IN = " NOT IN ";
50
51     private Object JavaDoc m_attribute;
52     private Object JavaDoc m_value;
53
54
55     // BRJ: true if criterion is bound
56
private boolean m_bound = false;
57
58     // BRJ: the criterion must be bound for the main class and for all extents
59
private int m_numberOfExtentsToBind = 0;
60
61     private String JavaDoc m_alias = null;
62     private UserAlias m_userAlias = null;
63         
64     // BRJ: indicate whether attribute name should be translated into column name
65
private boolean m_translateAttribute = true;
66
67     private Criteria m_criteria;
68     
69     /**
70      * Constructor declaration
71      *
72      * @param anAttribute column- or fieldName or a Query
73      * @param aValue the value to compare with
74      * @param negative criteria is negated (ie NOT LIKE instead of LIKE)
75      * @param alias use alias to link anAttribute to
76      */

77     SelectionCriteria(Object JavaDoc anAttribute, Object JavaDoc aValue, String JavaDoc alias)
78     {
79         if (!(anAttribute instanceof String JavaDoc || anAttribute instanceof Query))
80         {
81             throw new IllegalArgumentException JavaDoc("An attribute must be a String or a Query !");
82         }
83             
84         m_attribute = anAttribute;
85         m_value = aValue;
86         this.m_bound = !isBindable();
87         this.m_alias = alias;
88         this.m_userAlias = m_alias == null ? null : new UserAlias(m_alias, (String JavaDoc)getAttribute(), true);
89     }
90
91     /**
92      * Constructor declaration
93      *
94      * @param anAttribute column- or fieldName or a Query
95      * @param aValue the value to compare with
96      * @param aUserAlias userAlias to link anAttribute to
97      */

98     SelectionCriteria(Object JavaDoc anAttribute, Object JavaDoc aValue, UserAlias aUserAlias)
99     {
100         if (!(anAttribute instanceof String JavaDoc || anAttribute instanceof Query))
101         {
102             throw new IllegalArgumentException JavaDoc("An attribute must be a String or a Query !");
103         }
104
105         m_attribute = anAttribute;
106         m_value = aValue;
107         this.m_bound = !isBindable();
108         this.m_userAlias = aUserAlias;
109         this.m_alias = m_userAlias == null ? null : m_userAlias.getName();
110     }
111
112     /**
113      * Answer the SQL compare-clause for this criteria
114      */

115     abstract public String JavaDoc getClause();
116
117     /**
118      * sets the value of the criteria to newValue. Used by the ODMG OQLQuery.bind() operation
119      */

120     public void bind(Object JavaDoc newValue)
121     {
122         setValue(newValue);
123         setBound(true);
124     }
125
126     /**
127      * Answer the value
128      */

129     public Object JavaDoc getValue()
130     {
131         return m_value;
132     }
133
134     /**
135      * Answer the attribute
136      */

137     public Object JavaDoc getAttribute()
138     {
139         return m_attribute;
140     }
141
142     /**
143      * String representation
144      */

145     public String JavaDoc toString()
146     {
147         return m_attribute + getClause() + m_value;
148     }
149
150     /**
151      * BRJ : Used by the ODMG OQLQuery.bind() operation
152      * @return Returns a boolean indicator
153      */

154     public boolean isBound()
155     {
156         return m_bound;
157     }
158
159     /**
160      * Sets the bound.
161      * @param bound The bound to set
162      */

163     protected void setBound(boolean bound)
164     {
165         this.m_bound = bound;
166     }
167
168     /**
169      * Sets the value.
170      * @param value The value to set
171      */

172     protected void setValue(Object JavaDoc value)
173     {
174         this.m_value = value;
175     }
176
177     /**
178      * answer true if the selection criteria is bindable
179      * BRJ: value null is bindable
180      */

181     protected boolean isBindable()
182     {
183         return (getValue() == null);
184     }
185     /**
186      * Returns the numberOfExtentsToBind.
187      * @return int
188      */

189     public int getNumberOfExtentsToBind()
190     {
191         return m_numberOfExtentsToBind;
192     }
193
194     /**
195      * Sets the numberOfExtentsToBind.
196      * @param numberOfExtentsToBind The numberOfExtentsToBind to set
197      */

198     public void setNumberOfExtentsToBind(int numberOfExtentsToBind)
199     {
200         this.m_numberOfExtentsToBind = numberOfExtentsToBind;
201     }
202
203     /**
204      * @return String
205      */

206     public String JavaDoc getAlias()
207     {
208         return m_alias;
209     }
210
211     /**
212      * Sets the alias. By default the entire attribute path participates in the alias
213      * @param alias The name of the alias to set
214      */

215     public void setAlias(String JavaDoc alias)
216     {
217         m_alias = alias;
218         String JavaDoc attributePath = (String JavaDoc)getAttribute();
219         boolean allPathsAliased = true;
220         m_userAlias = new UserAlias(alias, attributePath, allPathsAliased);
221         
222     }
223
224     /**
225      * Sets the alias.
226      * @param alias The alias to set
227      */

228     public void setAlias(String JavaDoc alias, String JavaDoc aliasPath)
229     {
230         m_alias = alias;
231         m_userAlias = new UserAlias(alias, (String JavaDoc)getAttribute(), aliasPath);
232     }
233     
234     /**
235      * Sets the alias using a userAlias object.
236      * @param userAlias The alias to set
237      */

238     public void setAlias(UserAlias userAlias)
239     {
240         m_alias = userAlias.getName();
241         m_userAlias = userAlias;
242     }
243
244     public UserAlias getUserAlias()
245     {
246         return m_userAlias;
247     }
248     /**
249      * @return true if attribute name should be translated into column name
250      */

251     public boolean isTranslateAttribute()
252     {
253         return m_translateAttribute;
254     }
255
256     /**
257      * @param b
258      */

259     void setTranslateAttribute(boolean b)
260     {
261         m_translateAttribute = b;
262     }
263
264     /**
265      * @return
266      */

267     public Criteria getCriteria()
268     {
269         return m_criteria;
270     }
271
272     /**
273      * @param criteria
274      */

275     void setCriteria(Criteria criteria)
276     {
277         m_criteria = criteria;
278     }
279
280     public QueryByCriteria getQuery()
281     {
282         if (getCriteria() != null)
283         {
284             return getCriteria().getQuery();
285         }
286         else
287         {
288             return null;
289         }
290     }
291     
292     /**
293      * Gets the pathClasses from the parent Criteria.
294      * A Map containing hints about what Class to be used for what path segment
295      * @return Returns a Map
296      */

297     public Map JavaDoc getPathClasses()
298     {
299         return getCriteria().getPathClasses();
300     }
301
302     /**
303      * Get the a List of Class objects used as hints for a path
304      *
305      * @param aPath the path segment ie: allArticlesInGroup
306      * @return a List o Class objects to be used in SqlStatment
307      * @see org.apache.ojb.broker.QueryTest#testInversePathExpression()
308      */

309     public List JavaDoc getClassesForPath(String JavaDoc aPath)
310     {
311         return getCriteria().getClassesForPath(aPath);
312     }
313 }
314
Popular Tags