1 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 ; 34 import java.lang.reflect.Method ; 35 import java.util.Map ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 43 public class QueryFactory 44 { 45 private final Map queriesByMethod = new HashMap (); 46 private final JDBCEntityBridge2 entity; 47 48 public QueryFactory(JDBCEntityBridge2 entity) 49 { 50 this.entity = entity; 51 } 52 53 public QueryCommand getQueryCommand(Method queryMethod) throws FinderException 54 { 55 QueryCommand queryCommand = (QueryCommand)queriesByMethod.get(queryMethod); 56 if(queryCommand == null) 57 { 58 throw new FinderException ("Unknown query method: " + queryMethod); 59 } 60 return queryCommand; 61 } 62 63 public void init() throws DeploymentException 64 { 65 Method findByPkMethod; 66 Class home = entity.getHomeClass(); 67 if(home != null) 68 { 69 try 70 { 71 findByPkMethod = home.getMethod("findByPrimaryKey", new Class []{entity.getPrimaryKeyClass()}); 72 } 73 catch(NoSuchMethodException 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 local = entity.getLocalHomeClass(); 84 if(local != null) 85 { 86 try 87 { 88 findByPkMethod = local.getMethod("findByPrimaryKey", new Class []{entity.getPrimaryKeyClass()}); 89 } 90 catch(NoSuchMethodException 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 Iterator 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 |