User: dsundstrom
  Date: 01/11/01 14:10:46

  Modified:    src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata
                        JDBCDeclaredQueryMetaData.java
                        JDBCQueryMetaDataFactory.java
  Log:
  Modified declared-sql to support ejbSelect queries. Cleaned up code.
  
  Revision  Changes    Path
  1.6       +142 -20   
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCDeclaredQueryMetaData.java
  
  Index: JDBCDeclaredQueryMetaData.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCDeclaredQueryMetaData.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JDBCDeclaredQueryMetaData.java    2001/09/01 22:03:17     1.5
  +++ JDBCDeclaredQueryMetaData.java    2001/11/01 22:10:46     1.6
  @@ -8,8 +8,15 @@
   
   import java.lang.reflect.Method;
   
  +import java.util.Collection;
  +import java.util.Set;
  +
  +import javax.ejb.EJBObject;
  +import javax.ejb.EJBLocalObject;
  +
   import org.jboss.ejb.DeploymentException;
   import org.jboss.metadata.MetaData;
  +import org.jboss.metadata.QueryMetaData;
   
   import org.w3c.dom.Element;
   
  @@ -17,28 +24,73 @@
    * Imutable class contains information about a declated query.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Dain Sundstrom</a>
  - *   @version $Revision: 1.5 $
  + *   @version $Revision: 1.6 $
    */
   public final class JDBCDeclaredQueryMetaData implements JDBCQueryMetaData {
  +   /**
  +    * The method to which this query is bound.
  +    */
      private final Method method;
  +
  +   /**
  +    * The user specified from clause.
  +    */
      private final String from;
  +
  +   /**
  +    * The user specified where clause.
  +    */
      private final String where;
  +
  +   /**
  +    * The user specified order clause.
  +    */
      private final String order;
  +
  +   /**
  +    * The other clause is appended to the end of the sql.  This is useful for
  +    * hints to the query engine.
  +    */
      private final String other;
   
      /**
  +    * Should the select be DISTINCT?
  +    */
  +   private final boolean distinct;
  +   
  +   /**
  +    * The name of the ejb from which the field will be selected.
  +    */
  +   private final String ejbName;
  +
  +   /**
  +    * The name of the cmp-field to be selected.
  +    */
  +   private final String fieldName;
  +
  +   /**
       * Read ahead meta data.
       */
      private final JDBCReadAheadMetaData readAhead;
   
  +   /**
  +    * Should the query return Local or Remote beans.
  +    */
  +   private final boolean resultTypeMappingLocal;
   
      /**
  -    * Constructs a JDBCDeclaredQueryMetaData which is defined by the declared-sql 
xml element
  -    * and is invoked by the specified method.
  -    * @param queryElement the xml Element which contains the metadata about this 
query
  +    * Constructs a JDBCDeclaredQueryMetaData which is defined by the 
  +    * declared-sql xml element and is invoked by the specified method.
  +    * @param queryElement the xml Element which contains the metadata about 
  +    *       this query
       * @param method the method which invokes this query
       */
  -   public JDBCDeclaredQueryMetaData(Element queryElement, Method method, 
JDBCReadAheadMetaData readAhead) throws DeploymentException {
  +   public JDBCDeclaredQueryMetaData(
  +         JDBCQueryMetaData jdbcQueryMetaData,
  +         Element queryElement,
  +         Method method,
  +         JDBCReadAheadMetaData readAhead) throws DeploymentException {
  +
         this.method = method;
         this.readAhead = readAhead;
   
  @@ -46,14 +98,56 @@
         where = MetaData.getOptionalChildContent(queryElement, "where");
         order = MetaData.getOptionalChildContent(queryElement, "order");
         other = MetaData.getOptionalChildContent(queryElement, "other");
  +
  +
  +             String resultTypeMappingString = MetaData.getOptionalChildContent(
  +            queryElement,
  +            "result-type-mapping");
  +            
  +             if(QueryMetaData.LOCAL.equals(resultTypeMappingString)) {
  +         resultTypeMappingLocal = true;
  +      } else if(QueryMetaData.REMOTE.equals(resultTypeMappingString)) {
  +         resultTypeMappingLocal = false;
  +      } else if(resultTypeMappingString == null) {
  +         resultTypeMappingLocal = jdbcQueryMetaData.isResultTypeMappingLocal();
  +      } else {
  +                     throw new DeploymentException("result-type-mapping, if 
specified, " +
  +               "must be '" + QueryMetaData.REMOTE + "' or '" +
  +               QueryMetaData.LOCAL + "', and it is case sensitive.");
  +             }
  +
  +      // load ejbSelect info
  +      if(method.getName().startsWith("ejbSelect")) {
  +         Element selectElement = 
  +               MetaData.getUniqueChild(queryElement, "select");
  +         
  +         // should select use distinct?
  +         distinct = 
  +               (MetaData.getOptionalChild(selectElement, "distinct") != null);
  +         
  +         ejbName = MetaData.getUniqueChildContent(selectElement, "ejb-name");
  +         fieldName = 
  +               MetaData.getOptionalChildContent(selectElement, "field-name");
  +      } else {
  +         // the select element is not allowed for finders
  +         if(MetaData.getOptionalChild(queryElement, "select") != null) {
  +            throw new DeploymentException("The select element of " +
  +                  "declared-sql is only allowed for ejbSelect queries.");
  +         }
  +         distinct = false;
  +         ejbName = null;
  +         fieldName = null;
  +      }
      }
   
  +   // javadoc in parent class
      public Method getMethod() {
         return method;
      }
   
  +   // javadoc in parent class
      public boolean isResultTypeMappingLocal() {
  -      return false;
  +      return resultTypeMappingLocal;
      }
   
      /**
  @@ -66,7 +160,7 @@
   
      /**
       * Gets the sql FROM clause of this query.
  -    * @returns a String which contains the sql FROM clause
  +    * @return a String which contains the sql FROM clause
       */
      public String getFrom() {
         return from;
  @@ -74,7 +168,7 @@
   
      /**
       * Gets the sql WHERE clause of this query.
  -    * @returns a String which contains the sql WHERE clause
  +    * @return a String which contains the sql WHERE clause
       */
      public String getWhere() {
         return where;
  @@ -82,7 +176,7 @@
   
      /**
       * Gets the sql ORDER BY clause of this query.
  -    * @returns a String which contains the sql ORDER BY clause
  +    * @return a String which contains the sql ORDER BY clause
       */
      public String getOrder() {
         return order;
  @@ -91,19 +185,46 @@
      /**
       * Gets other sql code which is appended to the end of the query.
       * This is userful for supplying hints to the query engine.
  -    * @returns a String which contains additional sql code which is 
  +    * @return a String which contains additional sql code which is 
       *         appended to the end of the query
       */
      public String getOther() {
         return other;
      }
   
  +   /**
  +    * Should the select be DISTINCT?
  +    * @return true if the select clause should contain distinct
  +    */
  +   public boolean isSelectDistinct() {
  +      return distinct;
  +   }
  +
  +   /**
  +    * The name of the ejb from which the field will be selected.
  +    * @return the name of the ejb from which a field will be selected, or null
  +    * if returning a whole ejb
  +    */
  +   public String getEJBName() {
  +      return ejbName;
  +   }
  +
  +   /**
  +    * The name of the cmp-field to be selected.
  +    * @return the name of the cmp-field to be selected or null if returning a 
  +    * whole ejb
  +    */
  +   public String getFieldName() {
  +      return fieldName;
  +   }
  +   
      /**
  -    * Compares this JDBCDeclaredQueryMetaData against the specified object. Returns
  -    * true if the objects are the same. Two JDBCDeclaredQueryMetaData are the same 
  -    * if they are both invoked by the same method.
  +    * Compares this JDBCDeclaredQueryMetaData against the specified object.
  +    * Returns true if the objects are the same. Two JDBCDeclaredQueryMetaData
  +    * are the same if they are both invoked by the same method.
       * @param o the reference object with which to compare
  -    * @return true if this object is the same as the object argument; false 
otherwise
  +    * @return true if this object is the same as the object argument; false
  +    * otherwise
       */
      public boolean equals(Object o) {
         if(o instanceof JDBCDeclaredQueryMetaData) {
  @@ -113,19 +234,20 @@
      }
      
      /**
  -    * Returns a hashcode for this JDBCDeclaredQueryMetaData. The hashcode is 
computed
  -    * by the method which invokes this query.
  +    * Returns a hashcode for this JDBCDeclaredQueryMetaData. The hashcode is
  +    * computed by the method which invokes this query.
       * @return a hash code value for this object
       */
      public int hashCode() {
         return method.hashCode();
      }
      /**
  -    * Returns a string describing this JDBCDeclaredQueryMetaData. The exact details
  -    * of the representation are unspecified and subject to change, but the following
  -    * may be regarded as typical:
  +    * Returns a string describing this JDBCDeclaredQueryMetaData. The exact 
  +    * details of the representation are unspecified and subject to change, 
  +    * but the following may be regarded as typical:
       * 
  -    * "[JDBCDeclaredQueryMetaData: method=public org.foo.User 
findByName(java.lang.String)]"
  +    * "[JDBCDeclaredQueryMetaData: method=public org.foo.User findByName(
  +    *    java.lang.String)]"
       *
       * @return a string representation of the object
       */
  
  
  
  1.6       +71 -26    
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCQueryMetaDataFactory.java
  
  Index: JDBCQueryMetaDataFactory.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCQueryMetaDataFactory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- JDBCQueryMetaDataFactory.java     2001/09/01 22:03:17     1.5
  +++ JDBCQueryMetaDataFactory.java     2001/11/01 22:10:46     1.6
  @@ -22,7 +22,7 @@
    * on the query specifiection type.
    *    
    * @author <a href="mailto:[EMAIL PROTECTED]";>Dain Sundstrom</a>
  - *   @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    */
   public class JDBCQueryMetaDataFactory {
      private JDBCEntityMetaData entity;
  @@ -31,41 +31,64 @@
         this.entity = entity;
      }
   
  -   public JDBCQueryMetaData createJDBCQueryMetaData(QueryMetaData queryMetaData, 
Method method) throws DeploymentException  {
  +   public JDBCQueryMetaData createJDBCQueryMetaData(
  +         QueryMetaData queryMetaData,
  +         Method method) throws DeploymentException  {
  +
         return new JDBCQlQueryMetaData(queryMetaData, method);
      }
  +
  +   public JDBCQueryMetaData createJDBCQueryMetaData(
  +         JDBCQueryMetaData jdbcQueryMetaData,
  +         Element queryElement,
  +         Method method,
  +         JDBCReadAheadMetaData readAhead) throws DeploymentException {
   
  -   public JDBCQueryMetaData createJDBCQueryMetaData(JDBCQueryMetaData 
jdbcQueryMetaData, Element queryElement, Method method, JDBCReadAheadMetaData 
readAhead) throws DeploymentException {
  +      // RAW-SQL
         Element rawSql = MetaData.getOptionalChild(queryElement, "raw-sql");
         if(rawSql != null) {
            return new JDBCRawSqlQueryMetaData(method);
         }
   
  -      Element delcaredSql = MetaData.getOptionalChild(queryElement, "declared-sql");
  +      // DECLARED-SQL
  +      Element delcaredSql = 
  +            MetaData.getOptionalChild(queryElement, "declared-sql");
         if(delcaredSql != null) {
  -         return new JDBCDeclaredQueryMetaData(delcaredSql, method, readAhead);
  +         return new JDBCDeclaredQueryMetaData(
  +               jdbcQueryMetaData,
  +               delcaredSql,
  +               method,
  +               readAhead);
         }
   
  +      // EJB-QL
         Element ejbQl = MetaData.getOptionalChild(queryElement, "ejb-ql");
         if(ejbQl != null) {
  -         return new JDBCQlQueryMetaData((JDBCQlQueryMetaData)jdbcQueryMetaData, 
ejbQl, method, readAhead);
  +         return new JDBCQlQueryMetaData(
  +               (JDBCQlQueryMetaData)jdbcQueryMetaData,
  +               ejbQl,
  +               method,
  +               readAhead);
         }
   
  -      if(jdbcQueryMetaData == null) {
  -         throw new DeploymentException("Error in query spedification for method " + 
method.getName());
  -      }
  -      return jdbcQueryMetaData;
  +      throw new DeploymentException(
  +            "Error in query spedification for method " + method.getName());
      }
   
  -   public Method[] getQueryMethods(Element queryElement) throws DeploymentException 
{
  +   public Method[] getQueryMethods(Element queryElement)
  +         throws DeploymentException {
  +
         // query-method sub-element
  -      Element queryMethod = MetaData.getUniqueChild(queryElement, "query-method");
  +      Element queryMethod = 
  +            MetaData.getUniqueChild(queryElement, "query-method");
         
         // method name
  -      String methodName = 
MetaData.getElementContent(MetaData.getUniqueChild(queryMethod, "method-name"));
  +      String methodName =
  +            MetaData.getUniqueChildContent(queryMethod, "method-name");
         
         // method interface
  -      String methodIntf = 
MetaData.getElementContent(MetaData.getOptionalChild(queryMethod, "method-intf"));
  +      String methodIntf =
  +            MetaData.getOptionalChildContent(queryMethod, "method-intf");
         if(methodIntf!=null && !QueryMetaData.HOME.equals(methodIntf) &&
               !QueryMetaData.LOCAL_HOME.equals(methodIntf)) {
            throw new DeploymentException("result-type-mapping must be '" + 
  @@ -75,8 +98,10 @@
   
         // method params
         ArrayList methodParams = new ArrayList();
  -      Element methodParamsElement = MetaData.getUniqueChild(queryMethod, 
"method-params");
  -      Iterator iterator = MetaData.getChildrenByTagName(methodParamsElement, 
"method-param");         
  +      Element methodParamsElement =
  +            MetaData.getUniqueChild(queryMethod, "method-params");
  +      Iterator iterator =
  +            MetaData.getChildrenByTagName(methodParamsElement, "method-param");
         while (iterator.hasNext()) {
            methodParams.add(MetaData.getElementContent((Element)iterator.next()));
         }
  @@ -85,21 +110,28 @@
         return getQueryMethods(methodName, parameters, methodIntf);
      }
   
  -   public Method[] getQueryMethods(QueryMetaData queryData) throws 
DeploymentException {
  +   public Method[] getQueryMethods(QueryMetaData queryData)
  +         throws DeploymentException {
         String methodName = queryData.getMethodName();
         Class[] parameters = convertToJavaClasses(queryData.getMethodParams());
         String methodIntf = queryData.getMethodIntf();
         return getQueryMethods(methodName, parameters, methodIntf);
      }
   
  -   public Method[] getQueryMethods(String methodName, Class parameters[], String 
methodIntf) 
  +   public Method[] getQueryMethods(
  +         String methodName,
  +         Class parameters[],
  +         String methodIntf) 
            throws DeploymentException {
  -            
  +
         // find the query and load the xml
         ArrayList methods = new ArrayList(2);
         if(methodName.startsWith("ejbSelect")) {
            // bean method
  -         methods.add(getQueryMethod(methodName, parameters, 
entity.getEntityClass()));
  +         methods.add(getQueryMethod(
  +                  methodName,
  +                  parameters,
  +                  entity.getEntityClass()));
         } else {
            // interface element
            if(methodIntf == null || methodIntf.equals(QueryMetaData.HOME)) {
  @@ -107,10 +139,16 @@
               if(homeClass != null) {
                  methods.add(getQueryMethod(methodName, parameters, homeClass));
               }
  -         } else if(methodIntf == null || 
methodIntf.equals(QueryMetaData.LOCAL_HOME)) {
  -            Class localHomeClass = entity.getLocalHomeClass(); // optional, might 
be null
  +         } else if(methodIntf == null ||
  +               methodIntf.equals(QueryMetaData.LOCAL_HOME)) {
  +
  +            // optional, might be null
  +            Class localHomeClass = entity.getLocalHomeClass();
               if(localHomeClass != null) {
  -               methods.add(getQueryMethod(methodName, parameters, localHomeClass));
  +               methods.add(getQueryMethod(
  +                        methodName,
  +                        parameters,
  +                        localHomeClass));
               }
            }
         }          
  @@ -121,11 +159,16 @@
         return (Method[])methods.toArray(new Method[methods.size()]);
      }
         
  -   private Method getQueryMethod(String queryName, Class[] parameters, Class clazz) 
{
  +   private Method getQueryMethod(
  +         String queryName,
  +         Class[] parameters,
  +         Class clazz) {
  +
         try {
            Method method  = clazz.getMethod(queryName, parameters);
   
  -         // is the method abstract?  (remember interface methods are always 
abstract)
  +         // is the method abstract?
  +         // (remember interface methods are always abstract)
            if(Modifier.isAbstract(method.getModifiers())) {
               return method;
            }
  @@ -135,7 +178,9 @@
         return null;
      }
      
  -   private Class[] convertToJavaClasses(Iterator iter) throws DeploymentException {
  +   private Class[] convertToJavaClasses(Iterator iter)
  +         throws DeploymentException {
  +
         ArrayList classes = new ArrayList();
         while(iter.hasNext()) {
            classes.add(convertToJavaClass((String)iter.next()));
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to