001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/query/executor/QueryExecutor.java,v 1.1.1.1 2004/03/25 12:08:37 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:37 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruņa
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruņa nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040: package es.udc.mypersonalizer.kernel.model.query.executor;
041:
042: import es.udc.mypersonalizer.kernel.model.annotators.PersistenceTypeAnnotationHelper;
043: import es.udc.mypersonalizer.kernel.model.metainfo.MetaProperty;
044: import es.udc.mypersonalizer.kernel.model.query.ast.Query;
045: import es.udc.mypersonalizer.kernel.model.query.ast.Step;
046: import es.udc.mypersonalizer.kernel.model.query.parser.ParseException;
047: import es.udc.mypersonalizer.kernel.model.query.parser.QueryParser;
048: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
049:
050: import java.util.List;
051:
052: /**
053: * Utility class which represents the facade for the query execution engine.
054: * <p>
055: * All client code for the execution engine should use this class to perform
056: * a query.
057: *
058: * @author Abel Muinho
059: * @since 1.0
060: */
061: public class QueryExecutor {
062:
063: /**
064: * Compiles and executes the given query and returns the list of
065: * <code>Property</code> objects that satisfy it.
066: *
067: * @see es.udc.mypersonalizer.kernel.model.properties.Property
068: * @param query the query to perform.
069: * @return the list of <code>Property</code> objetcs.
070: * @throws InternalErrorException if some error is found while performing
071: * the query.
072: */
073: public static final List executeQuery(String query)
074: throws InternalErrorException {
075: QueryParser parser = new QueryParser();
076: try {
077: return executeQuery(parser.parse(query));
078: } catch (ParseException e) {
079: throw new InternalErrorException(e);
080: }
081: }
082:
083: /**
084: * Compiles and executes the given query and returns the list of
085: * <code>Property</code> objects that satisfy it.
086: *
087: * @see QueryExecutorDelegate#evaluate(Query, int, int)
088: * @see es.udc.mypersonalizer.kernel.model.properties.Property
089: * @param query the query to execute.
090: * @param start the offset of the first property to return.
091: * @param count the number of properties to return.
092: * @return the list of <code>Property</code> objetcs.
093: * @throws InternalErrorException if some error is found while performing
094: * the query.
095: */
096: public static final List executeQuery(String query, int start,
097: int count) throws InternalErrorException {
098: QueryParser parser = new QueryParser();
099: try {
100: return executeQuery(parser.parse(query), start, count);
101: } catch (ParseException e) {
102: throw new InternalErrorException(e);
103: }
104: }
105:
106: /**
107: * Executes the given query and returns the list of <code>Property</code>
108: * objects that satisfy it.
109: *
110: * @see es.udc.mypersonalizer.kernel.model.properties.Property
111: * @param q the query to perform.
112: * @return the list of <code>Property</code> objetcs.
113: * @throws InternalErrorException if some error is found while performing
114: * the query.
115: */
116: public static final List executeQuery(Query q)
117: throws InternalErrorException {
118:
119: QueryExecutorDelegate delegate = getDelegate(q);
120: return delegate.evaluate(q).getProperties();
121: }
122:
123: /**
124: * Executes the given query and returns the list of <code>Property</code>
125: * objects that satisfy it.
126: *
127: * @see QueryExecutorDelegate#evaluate(Query, int, int)
128: * @see es.udc.mypersonalizer.kernel.model.properties.Property
129: * @param q the query to execute.
130: * @param start the offset of the first property to return.
131: * @param count the number of properties to return.
132: * @return the list of <code>Property</code> objetcs.
133: * @throws InternalErrorException if some error is found while performing
134: * the query.
135: */
136: public static final List executeQuery(Query q, int start, int count)
137: throws InternalErrorException {
138: QueryExecutorDelegate delegate = getDelegate(q);
139: return delegate.evaluate(q, start, count).getProperties();
140: }
141:
142: /**
143: * Obtains the <code>QueryExecutorDelegate</code> for the given query.
144: * @param q the query.
145: * @return the query executor delegate.
146: * @throws InternalErrorException if there are errors obtaining the
147: * delegate.
148: */
149: private static final QueryExecutorDelegate getDelegate(Query q)
150: throws InternalErrorException {
151: Step firstStep = (Step) q.getReturnClause().get(0);
152: MetaProperty firstMetaProperty = firstStep.getMetaProperty();
153: String persistenceType = PersistenceTypeAnnotationHelper
154: .getPersistenceTypeAnnotation(firstMetaProperty);
155: QueryExecutorDelegate delegate = QueryExecutorDelegateFactory
156: .getExecutorForPersistenceType(persistenceType);
157: return delegate;
158: }
159:
160: }
|