01: /*
02: * Created on 17-Jul-2005
03: *
04: * TODO To change the template for this generated file go to
05: * Window - Preferences - Java - Code Style - Code Templates
06: */
07: package com.jofti.parser;
08:
09: import com.jofti.api.IndexQuery;
10: import com.jofti.core.IParsedQuery;
11: import com.jofti.exception.JoftiException;
12: import com.jofti.introspect.ClassIntrospector;
13: import com.jofti.query.EJBQuery;
14: import com.jofti.query.SQLQuery;
15:
16: /**
17: * @author xenephon (xenephon@jofti.com)
18: *
19: */
20: public class ParserManager {
21:
22: private IQueryParser nativeQueryParser = null;
23: private IQueryParser ejb3QueryParser = null;
24:
25: public ParserManager(ClassIntrospector introspector) {
26: nativeQueryParser = new NativeQueryParser(introspector);
27: ejb3QueryParser = new EJBQueryParser(introspector);
28: }
29:
30: /**
31: * Parses a String representation of a query. Uses the SimpleQuery Parser.
32: *
33: * @param originalQuery
34: * @return the parsed query
35: * @throws JoftiException
36: */
37: public IParsedQuery parseQuery(IndexQuery originalQuery)
38: throws JoftiException {
39: if (originalQuery instanceof EJBQuery) {
40: return ejb3QueryParser.parseQuery(originalQuery);
41: } else if (originalQuery instanceof SQLQuery) {
42: return nativeQueryParser.parseQuery(originalQuery);
43:
44: } else {
45: return new ConvenienceQueryWrapper(originalQuery);
46: }
47:
48: }
49:
50: public IndexQuery addQuery(String name, IndexQuery originalQuery)
51: throws JoftiException {
52:
53: if (originalQuery instanceof EJBQuery) {
54: return ejb3QueryParser.parseQuery(name, originalQuery);
55: } else if (originalQuery instanceof SQLQuery) {
56: return nativeQueryParser.parseQuery(name, originalQuery);
57: } else {
58: throw new JoftiException("Only " + EJBQuery.class + " or "
59: + SQLQuery.class + " are allowed to be added");
60:
61: }
62:
63: }
64:
65: public IndexQuery addQuery(String name, String query)
66: throws JoftiException {
67:
68: return addQuery(name, new EJBQuery(query));
69:
70: }
71:
72: public IndexQuery getQuery(String name) {
73: IndexQuery query = ejb3QueryParser.getQuery(name);
74: if (query == null) {
75: query = nativeQueryParser.getQuery(name);
76: }
77: return query;
78: }
79:
80: }
|