KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > DynamicQueryCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc2;
23
24 import java.util.Collection JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.ejb.FinderException JavaDoc;
28 import org.jboss.ejb.GenericEntityObjectFactory;
29 import org.jboss.ejb.plugins.cmp.jdbc.EJBQLToSQL92Compiler;
30 import org.jboss.ejb.plugins.cmp.jdbc.QLCompiler;
31 import org.jboss.ejb.plugins.cmp.jdbc.QueryParameter;
32 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData;
33 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2;
34 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
35 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema;
36 import org.jboss.logging.Logger;
37
38 /**
39  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
40  * @version <tt>$Revision: 58402 $</tt>
41  */

42 public class DynamicQueryCommand
43    implements QueryCommand
44 {
45    private Logger log;
46    private JDBCEntityBridge2 entity;
47    private JDBCDynamicQLQueryMetaData metadata;
48    private AbstractQueryCommand.CollectionFactory collectionFactory;
49
50    public DynamicQueryCommand(JDBCEntityBridge2 entity, JDBCDynamicQLQueryMetaData metadata)
51    {
52       log =
53          Logger.getLogger(getClass().getName() + "." + entity.getEntityName() + "#" + metadata.getMethod().getName());
54       this.entity = entity;
55       this.metadata = metadata;
56
57       Class JavaDoc returnType = metadata.getMethod().getReturnType();
58       if(Collection JavaDoc.class.isAssignableFrom(returnType))
59       {
60          if(Set JavaDoc.class.isAssignableFrom(returnType))
61          {
62             collectionFactory = AbstractQueryCommand.SET_FACTORY;
63          }
64          else
65          {
66             collectionFactory = AbstractQueryCommand.COLLECTION_FACTORY;
67          }
68       }
69    }
70
71    public JDBCStoreManager2 getStoreManager()
72    {
73       return (JDBCStoreManager2)entity.getManager();
74    }
75
76    public Collection JavaDoc fetchCollection(Schema schema, GenericEntityObjectFactory factory, Object JavaDoc[] args)
77       throws FinderException JavaDoc
78    {
79       if(log.isTraceEnabled())
80       {
81          log.trace("executing dynamic-ql: " + args[0]);
82       }
83
84       JDBCStoreManager2 manager = (JDBCStoreManager2)entity.getManager();
85       QLCompiler compiler = new EJBQLToSQL92Compiler(manager.getCatalog());
86       try
87       {
88          compiler.compileJBossQL((String JavaDoc)args[0],
89             metadata.getMethod().getReturnType(),
90             getParamTypes(args),
91             metadata
92          );
93       }
94       catch(Throwable JavaDoc t)
95       {
96          log.error("Error compiling JBossQL statement '" + args[0] + "'", t);
97          throw new FinderException JavaDoc("Error compiling JBossQL statement '" + args[0] + "'");
98       }
99
100       String JavaDoc sql = compiler.getSQL();
101
102       AbstractQueryCommand.ResultReader resultReader;
103       if(!compiler.isSelectEntity())
104       {
105          if(compiler.isSelectField())
106          {
107             resultReader = new AbstractQueryCommand.FieldReader((JDBCCMPFieldBridge2)compiler.getSelectField());
108          }
109          else
110          {
111             resultReader = new AbstractQueryCommand.FunctionReader(compiler.getSelectFunction());
112          }
113       }
114       else
115       {
116          resultReader = new AbstractQueryCommand.EntityReader((JDBCEntityBridge2)compiler.getSelectEntity(), compiler.isSelectDistinct());
117       }
118
119       return AbstractQueryCommand.fetchCollection(
120          entity, sql, toArray(compiler.getInputParameters()),
121          new AbstractQueryCommand.EagerCollectionStrategy(collectionFactory, resultReader, log),
122          schema, factory, (Object JavaDoc[])args[1], log);
123    }
124
125    public Object JavaDoc fetchOne(Schema schema, GenericEntityObjectFactory factory, Object JavaDoc[] args) throws FinderException JavaDoc
126    {
127       if(log.isTraceEnabled())
128       {
129          log.trace("executing dynamic-ql: " + args[0]);
130       }
131
132       JDBCStoreManager2 manager = (JDBCStoreManager2)entity.getManager();
133       QLCompiler compiler = new EJBQLToSQL92Compiler(manager.getCatalog());
134       try
135       {
136          compiler.compileJBossQL((String JavaDoc)args[0],
137             metadata.getMethod().getReturnType(),
138             getParamTypes(args),
139             metadata
140          );
141       }
142       catch(Throwable JavaDoc t)
143       {
144          log.error("Error compiling JBossQL statement '" + args[0] + "'", t);
145          throw new FinderException JavaDoc("Error compiling JBossQL statement '" + args[0] + "'");
146       }
147
148       String JavaDoc sql = compiler.getSQL();
149
150       AbstractQueryCommand.ResultReader resultReader;
151       if(!compiler.isSelectEntity())
152       {
153          if(compiler.isSelectField())
154          {
155             resultReader = new AbstractQueryCommand.FieldReader((JDBCCMPFieldBridge2)compiler.getSelectField());
156          }
157          else
158          {
159             resultReader = new AbstractQueryCommand.FunctionReader(compiler.getSelectFunction());
160          }
161       }
162       else
163       {
164          resultReader = new AbstractQueryCommand.EntityReader((JDBCEntityBridge2)compiler.getSelectEntity(), compiler.isSelectDistinct());
165       }
166
167       return AbstractQueryCommand.fetchOne(entity, sql, toArray(compiler.getInputParameters()),
168          resultReader, (Object JavaDoc[])args[1], factory, log
169       );
170    }
171
172    private static Class JavaDoc[] getParamTypes(Object JavaDoc[] args)
173       throws FinderException JavaDoc
174    {
175       Class JavaDoc[] parameterTypes;
176       // get the parameters
177
Object JavaDoc[] parameters = (Object JavaDoc[])args[1];
178       if(parameters == null)
179       {
180          parameterTypes = new Class JavaDoc[0];
181       }
182       else
183       {
184          // get the parameter types
185
parameterTypes = new Class JavaDoc[parameters.length];
186          for(int i = 0; i < parameters.length; i++)
187          {
188             if(parameters[i] == null)
189             {
190                throw new FinderException JavaDoc("Parameter[" + i + "] is null");
191             }
192             parameterTypes[i] = parameters[i].getClass();
193          }
194       }
195       return parameterTypes;
196    }
197
198    static QueryParameter[] toArray(List JavaDoc p)
199    {
200       QueryParameter[] params = null;
201       if(p.size() > 0)
202       {
203          params = (QueryParameter[])p.toArray(new QueryParameter[p.size()]);
204       }
205       return params;
206    }
207 }
208
Popular Tags