001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc2;
023:
024: import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
025: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
026: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCJBossQLQueryMetaData;
027: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQlQueryMetaData;
028: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDeclaredQueryMetaData;
029: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCDynamicQLQueryMetaData;
030: import org.jboss.deployment.DeploymentException;
031:
032: import javax.ejb.FinderException;
033: import java.lang.reflect.Method;
034: import java.util.Map;
035: import java.util.HashMap;
036: import java.util.Iterator;
037:
038: /**
039: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
040: * @version <tt>$Revision: 57209 $</tt>
041: */
042: public class QueryFactory {
043: private final Map queriesByMethod = new HashMap();
044: private final JDBCEntityBridge2 entity;
045:
046: public QueryFactory(JDBCEntityBridge2 entity) {
047: this .entity = entity;
048: }
049:
050: public QueryCommand getQueryCommand(Method queryMethod)
051: throws FinderException {
052: QueryCommand queryCommand = (QueryCommand) queriesByMethod
053: .get(queryMethod);
054: if (queryCommand == null) {
055: throw new FinderException("Unknown query method: "
056: + queryMethod);
057: }
058: return queryCommand;
059: }
060:
061: public void init() throws DeploymentException {
062: Method findByPkMethod;
063: Class home = entity.getHomeClass();
064: if (home != null) {
065: try {
066: findByPkMethod = home.getMethod("findByPrimaryKey",
067: new Class[] { entity.getPrimaryKeyClass() });
068: } catch (NoSuchMethodException e) {
069: throw new DeploymentException("Home interface "
070: + home.getClass().getName()
071: + " does not contain findByPrimaryKey("
072: + entity.getPrimaryKeyClass().getName() + ")");
073: }
074:
075: FindByPrimaryKeyCommand findByPk = new FindByPrimaryKeyCommand(
076: entity);
077: queriesByMethod.put(findByPkMethod, findByPk);
078: }
079:
080: Class local = entity.getLocalHomeClass();
081: if (local != null) {
082: try {
083: findByPkMethod = local.getMethod("findByPrimaryKey",
084: new Class[] { entity.getPrimaryKeyClass() });
085: } catch (NoSuchMethodException e) {
086: throw new DeploymentException("Local home interface "
087: + local.getClass().getName()
088: + " does not contain findByPrimaryKey("
089: + entity.getPrimaryKeyClass().getName() + ")");
090: }
091:
092: FindByPrimaryKeyCommand findByPk = new FindByPrimaryKeyCommand(
093: entity);
094: queriesByMethod.put(findByPkMethod, findByPk);
095: }
096:
097: //
098: // Defined finders - Overrides automatic finders.
099: //
100: Iterator definedFinders = entity.getMetaData().getQueries()
101: .iterator();
102: while (definedFinders.hasNext()) {
103: JDBCQueryMetaData q = (JDBCQueryMetaData) definedFinders
104: .next();
105:
106: if (!queriesByMethod.containsKey(q.getMethod())) {
107: if (q instanceof JDBCJBossQLQueryMetaData) {
108: QueryCommand queryCommand = new JBossQLQueryCommand(
109: entity, (JDBCJBossQLQueryMetaData) q);
110: queriesByMethod.put(q.getMethod(), queryCommand);
111: } else if (q instanceof JDBCQlQueryMetaData) {
112: QueryCommand queryCommand = new EJBQLQueryCommand(
113: entity, (JDBCQlQueryMetaData) q);
114: queriesByMethod.put(q.getMethod(), queryCommand);
115: } else if (q instanceof JDBCDeclaredQueryMetaData) {
116: QueryCommand queryCommand = new DeclaredSQLQueryCommand(
117: entity, (JDBCDeclaredQueryMetaData) q);
118: queriesByMethod.put(q.getMethod(), queryCommand);
119: } else if (q instanceof JDBCDynamicQLQueryMetaData) {
120: QueryCommand queryCommand = new DynamicQueryCommand(
121: entity, (JDBCDynamicQLQueryMetaData) q);
122: queriesByMethod.put(q.getMethod(), queryCommand);
123: } else {
124: throw new DeploymentException(
125: "Unsupported query metadata: method="
126: + q.getMethod().getName()
127: + ", metadata=" + q);
128: }
129: }
130: }
131: }
132: }
|