KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
26 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCJBossQLQueryMetaData;
28 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQlQueryMetaData;
29 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDeclaredQueryMetaData;
30 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData;
31 import org.jboss.deployment.DeploymentException;
32
33 import javax.ejb.FinderException JavaDoc;
34 import java.lang.reflect.Method JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38
39 /**
40  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
41  * @version <tt>$Revision: 37459 $</tt>
42  */

43 public class QueryFactory
44 {
45    private final Map JavaDoc queriesByMethod = new HashMap JavaDoc();
46    private final JDBCEntityBridge2 entity;
47
48    public QueryFactory(JDBCEntityBridge2 entity)
49    {
50       this.entity = entity;
51    }
52
53    public QueryCommand getQueryCommand(Method JavaDoc queryMethod) throws FinderException JavaDoc
54    {
55       QueryCommand queryCommand = (QueryCommand)queriesByMethod.get(queryMethod);
56       if(queryCommand == null)
57       {
58          throw new FinderException JavaDoc("Unknown query method: " + queryMethod);
59       }
60       return queryCommand;
61    }
62
63    public void init() throws DeploymentException
64    {
65       Method JavaDoc findByPkMethod;
66       Class JavaDoc home = entity.getHomeClass();
67       if(home != null)
68       {
69          try
70          {
71             findByPkMethod = home.getMethod("findByPrimaryKey", new Class JavaDoc[]{entity.getPrimaryKeyClass()});
72          }
73          catch(NoSuchMethodException JavaDoc e)
74          {
75             throw new DeploymentException("Home interface " + home.getClass().getName() +
76                " does not contain findByPrimaryKey(" + entity.getPrimaryKeyClass().getName() + ")");
77          }
78
79          FindByPrimaryKeyCommand findByPk = new FindByPrimaryKeyCommand(entity);
80          queriesByMethod.put(findByPkMethod, findByPk);
81       }
82
83       Class JavaDoc local = entity.getLocalHomeClass();
84       if(local != null)
85       {
86          try
87          {
88             findByPkMethod = local.getMethod("findByPrimaryKey", new Class JavaDoc[]{entity.getPrimaryKeyClass()});
89          }
90          catch(NoSuchMethodException JavaDoc e)
91          {
92             throw new DeploymentException("Local home interface " + local.getClass().getName() +
93                " does not contain findByPrimaryKey(" + entity.getPrimaryKeyClass().getName() + ")");
94          }
95
96          FindByPrimaryKeyCommand findByPk = new FindByPrimaryKeyCommand(entity);
97          queriesByMethod.put(findByPkMethod, findByPk);
98       }
99
100       //
101
// Defined finders - Overrides automatic finders.
102
//
103
Iterator JavaDoc definedFinders = entity.getMetaData().getQueries().iterator();
104       while(definedFinders.hasNext())
105       {
106          JDBCQueryMetaData q = (JDBCQueryMetaData) definedFinders.next();
107
108          if(!queriesByMethod.containsKey(q.getMethod()))
109          {
110             if(q instanceof JDBCJBossQLQueryMetaData)
111             {
112                QueryCommand queryCommand = new JBossQLQueryCommand(entity, (JDBCJBossQLQueryMetaData)q);
113                queriesByMethod.put(q.getMethod(), queryCommand);
114             }
115             else if(q instanceof JDBCQlQueryMetaData)
116             {
117                QueryCommand queryCommand = new EJBQLQueryCommand(entity, (JDBCQlQueryMetaData)q);
118                queriesByMethod.put(q.getMethod(), queryCommand);
119             }
120             else if(q instanceof JDBCDeclaredQueryMetaData)
121             {
122                QueryCommand queryCommand = new DeclaredSQLQueryCommand(entity, (JDBCDeclaredQueryMetaData)q);
123                queriesByMethod.put(q.getMethod(), queryCommand);
124             }
125             else if(q instanceof JDBCDynamicQLQueryMetaData)
126             {
127                QueryCommand queryCommand = new DynamicQueryCommand(entity, (JDBCDynamicQLQueryMetaData)q);
128                queriesByMethod.put(q.getMethod(), queryCommand);
129             }
130             else
131             {
132                throw new DeploymentException("Unsupported query metadata: method=" + q.getMethod().getName() +
133                   ", metadata=" + q);
134             }
135          }
136       }
137    }
138 }
139
Popular Tags