001: package org.apache.ojb.broker.query;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.util.List;
019: import java.util.Map;
020:
021: /**
022: * abstract baseclass of all criteria classes, can't be instantiated.
023: *
024: * This code is based on stuff from
025: * COBRA - Java Object Persistence Layer
026: * Copyright (C) 1997, 1998 DB Harvey-George
027: * eMail: cobra@lowrent.org
028:
029: * @author DB Harvey-George
030: * @author Thomas Mahler
031: * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
032: * @version $Id: SelectionCriteria.java,v 1.17.2.2 2005/12/21 22:27:09 tomdz Exp $
033: */
034: public abstract class SelectionCriteria implements java.io.Serializable {
035: static final long serialVersionUID = -5194901539702756536L;
036: protected static final String EQUAL = " = ";
037: protected static final String NOT_EQUAL = " <> ";
038: protected static final String GREATER = " > ";
039: protected static final String NOT_GREATER = " <= ";
040: protected static final String LESS = " < ";
041: protected static final String NOT_LESS = " >= ";
042: protected static final String LIKE = " LIKE ";
043: protected static final String NOT_LIKE = " NOT LIKE ";
044: protected static final String IS_NULL = " IS NULL ";
045: protected static final String NOT_IS_NULL = " IS NOT NULL ";
046: protected static final String BETWEEN = " BETWEEN ";
047: protected static final String NOT_BETWEEN = " NOT BETWEEN ";
048: protected static final String IN = " IN ";
049: protected static final String NOT_IN = " NOT IN ";
050:
051: private Object m_attribute;
052: private Object m_value;
053:
054: // BRJ: true if criterion is bound
055: private boolean m_bound = false;
056:
057: // BRJ: the criterion must be bound for the main class and for all extents
058: private int m_numberOfExtentsToBind = 0;
059:
060: private String m_alias = null;
061: private UserAlias m_userAlias = null;
062:
063: // BRJ: indicate whether attribute name should be translated into column name
064: private boolean m_translateAttribute = true;
065:
066: private Criteria m_criteria;
067:
068: /**
069: * Constructor declaration
070: *
071: * @param anAttribute column- or fieldName or a Query
072: * @param aValue the value to compare with
073: * @param negative criteria is negated (ie NOT LIKE instead of LIKE)
074: * @param alias use alias to link anAttribute to
075: */
076: SelectionCriteria(Object anAttribute, Object aValue, String alias) {
077: if (!(anAttribute instanceof String || anAttribute instanceof Query)) {
078: throw new IllegalArgumentException(
079: "An attribute must be a String or a Query !");
080: }
081:
082: m_attribute = anAttribute;
083: m_value = aValue;
084: this .m_bound = !isBindable();
085: this .m_alias = alias;
086: this .m_userAlias = m_alias == null ? null : new UserAlias(
087: m_alias, (String) getAttribute(), true);
088: }
089:
090: /**
091: * Constructor declaration
092: *
093: * @param anAttribute column- or fieldName or a Query
094: * @param aValue the value to compare with
095: * @param aUserAlias userAlias to link anAttribute to
096: */
097: SelectionCriteria(Object anAttribute, Object aValue,
098: UserAlias aUserAlias) {
099: if (!(anAttribute instanceof String || anAttribute instanceof Query)) {
100: throw new IllegalArgumentException(
101: "An attribute must be a String or a Query !");
102: }
103:
104: m_attribute = anAttribute;
105: m_value = aValue;
106: this .m_bound = !isBindable();
107: this .m_userAlias = aUserAlias;
108: this .m_alias = m_userAlias == null ? null : m_userAlias
109: .getName();
110: }
111:
112: /**
113: * Answer the SQL compare-clause for this criteria
114: */
115: abstract public String 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 newValue) {
121: setValue(newValue);
122: setBound(true);
123: }
124:
125: /**
126: * Answer the value
127: */
128: public Object getValue() {
129: return m_value;
130: }
131:
132: /**
133: * Answer the attribute
134: */
135: public Object getAttribute() {
136: return m_attribute;
137: }
138:
139: /**
140: * String representation
141: */
142: public String toString() {
143: return m_attribute + getClause() + m_value;
144: }
145:
146: /**
147: * BRJ : Used by the ODMG OQLQuery.bind() operation
148: * @return Returns a boolean indicator
149: */
150: public boolean isBound() {
151: return m_bound;
152: }
153:
154: /**
155: * Sets the bound.
156: * @param bound The bound to set
157: */
158: protected void setBound(boolean bound) {
159: this .m_bound = bound;
160: }
161:
162: /**
163: * Sets the value.
164: * @param value The value to set
165: */
166: protected void setValue(Object value) {
167: this .m_value = value;
168: }
169:
170: /**
171: * answer true if the selection criteria is bindable
172: * BRJ: value null is bindable
173: */
174: protected boolean isBindable() {
175: return (getValue() == null);
176: }
177:
178: /**
179: * Returns the numberOfExtentsToBind.
180: * @return int
181: */
182: public int getNumberOfExtentsToBind() {
183: return m_numberOfExtentsToBind;
184: }
185:
186: /**
187: * Sets the numberOfExtentsToBind.
188: * @param numberOfExtentsToBind The numberOfExtentsToBind to set
189: */
190: public void setNumberOfExtentsToBind(int numberOfExtentsToBind) {
191: this .m_numberOfExtentsToBind = numberOfExtentsToBind;
192: }
193:
194: /**
195: * @return String
196: */
197: public String getAlias() {
198: return m_alias;
199: }
200:
201: /**
202: * Sets the alias. By default the entire attribute path participates in the alias
203: * @param alias The name of the alias to set
204: */
205: public void setAlias(String alias) {
206: m_alias = alias;
207: String attributePath = (String) getAttribute();
208: boolean allPathsAliased = true;
209: m_userAlias = new UserAlias(alias, attributePath,
210: allPathsAliased);
211:
212: }
213:
214: /**
215: * Sets the alias.
216: * @param alias The alias to set
217: */
218: public void setAlias(String alias, String aliasPath) {
219: m_alias = alias;
220: m_userAlias = new UserAlias(alias, (String) getAttribute(),
221: aliasPath);
222: }
223:
224: /**
225: * Sets the alias using a userAlias object.
226: * @param userAlias The alias to set
227: */
228: public void setAlias(UserAlias userAlias) {
229: m_alias = userAlias.getName();
230: m_userAlias = userAlias;
231: }
232:
233: public UserAlias getUserAlias() {
234: return m_userAlias;
235: }
236:
237: /**
238: * @return true if attribute name should be translated into column name
239: */
240: public boolean isTranslateAttribute() {
241: return m_translateAttribute;
242: }
243:
244: /**
245: * @param b
246: */
247: void setTranslateAttribute(boolean b) {
248: m_translateAttribute = b;
249: }
250:
251: /**
252: * @return
253: */
254: public Criteria getCriteria() {
255: return m_criteria;
256: }
257:
258: /**
259: * @param criteria
260: */
261: void setCriteria(Criteria criteria) {
262: m_criteria = criteria;
263: }
264:
265: public QueryByCriteria getQuery() {
266: if (getCriteria() != null) {
267: return getCriteria().getQuery();
268: } else {
269: return null;
270: }
271: }
272:
273: /**
274: * Gets the pathClasses from the parent Criteria.
275: * A Map containing hints about what Class to be used for what path segment
276: * @return Returns a Map
277: */
278: public Map getPathClasses() {
279: return getCriteria().getPathClasses();
280: }
281:
282: /**
283: * Get the a List of Class objects used as hints for a path
284: *
285: * @param aPath the path segment ie: allArticlesInGroup
286: * @return a List o Class objects to be used in SqlStatment
287: * @see org.apache.ojb.broker.QueryTest#testInversePathExpression()
288: */
289: public List getClassesForPath(String aPath) {
290: return getCriteria().getClassesForPath(aPath);
291: }
292: }
|