wam         2003/01/13 02:29:55

  Added:       src/stores/org/apache/slide/search/basic/sample package.html
                        BasicQuerySample.java BasicExpressionSample.java
                        BasicExpressionFactorySample.java
  Log:
  initial
  
  Revision  Changes    Path
  1.1                  
jakarta-slide/src/stores/org/apache/slide/search/basic/sample/package.html
  
  Index: package.html
  ===================================================================
  <!-- package.html, Created by Omnicore CodeGuide -->
  <HTML>
  <BODY>
  <p>This package is meant as an example for a store specific implementation of 
    SEARCH. It consist mainly of three classes, BasicQuerySample, 
BasicExpressionFactorySample 
    and BasicExpressionSample. </p>
  <h3>How to integrate the sample to your store</h3>
  <p>The easiest way is to add a parameter to your store in domain.xml. Check for</p>
  <p><code>/slide/namespace/definition/store [@name=&quot;yourStore&quot;]</code> 
  </p>
  <p>and add following parameter element:</p>
  <p> &lt;parameter 
name=&quot;basicQueryClass&quot;&gt;org.apache.slide.search.basic.sample.BasicQuerySample&lt;/parameter&gt;<br>
  </p>
  <p>Now for queries in that scope the BasicQuerySample class is called. For real 
    implementations you could hard code that parameter within the store 
implementation. 
    Add something like the following code into the initialize() method of your parent 
    store. </p>
  <pre><code>
          String queryClass = (String)parameters.get
              (BasicSearchLanguage.BASIC_QUERY_CLASS);
          
          if (queryClass == null) {
              parameters.put (BasicSearchLanguage.BASIC_QUERY_CLASS,
                              
&quot;org.apache.slide.search.basic.sample.BasicQuerySample&quot;);
          }</code></pre>
  
  <h3>How it works</h3>
  
  Consider a query with following WHERE:
  <pre>
  <code>
      &lt;where&gt;
        &lt;or&gt;
          &lt;eq&gt;
            &lt;prop&gt;&lt;displayname/&gt;&lt;/prop&gt;
            &lt;literal&gt;cathgirls.mp3&lt;/literal&gt;
          &lt;/eq&gt;
          &lt;lt&gt;
            &lt;prop&gt;&lt;getcontentlength/&gt;&lt;/prop&gt;
            &lt;literal&gt;100000&lt;/literal&gt;
          &lt;/lt&gt;
        &lt;/or&gt;
      &lt;/where&gt;</code>
  </pre>
  <p>The expression factory will first be called for each leave expression (<i>eq</i> 
    and <i>lt</i>). The factory may return a BasicExpression. Each of these 
expressions 
    must be executable for themselves. Later the factory will be called for the 
    <i>or</i> expression with both the <i>eq</i> and <i>lt</i> expression. The factory 
    now may create an expression, that executes this at once. An SQL implementation 
    for example would create a string like: </p>
  <p><code>WHERE prop.displayname = &quot;cathgirls.mp3&quot; or prop.getcontentlength 
    &lt; 10000</code></p>
  <p>For expressions that are not implemented by the store specific SEARCH, the 
    factory must return null. If you look to ExpressionFactorySample, you will see, 
    that <i>gt</i> is not implemented. If you pose the example query with <i>gt</i> 
    instead of <i>lt</i>, the sample factory will return null for <i>gt</i>. This 
    expression is now delegated to the generic factory. The call to the example 
    factory for the <i>or</i> expresssion will return null as well, as a merge of 
    a generic and an example expression is not possible. So in this case only the 
    <i>eq</i> expression is executed by the example implementation. </p>
  <p>The BasicExpression class is really store specific. It must know how to query 
    the store and how to create slide objects (NodeRevisionDescriptor...) from the 
    result set. </p>
  
  </BODY>
  </HTML>
  
  
  
  
  1.1                  
jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicQuerySample.java
  
  Index: BasicQuerySample.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicQuerySample.java,v
 1.1 2003/01/13 10:29:54 wam Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/13 10:29:54 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Slide", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.slide.search.basic.sample;
  
  import org.apache.slide.common.ServiceAccessException;
  import org.apache.slide.search.SearchQueryResult;
  import org.apache.slide.search.SearchToken;
  import org.apache.slide.search.basic.BasicQueryImpl;
  import org.apache.slide.search.basic.IBasicExpressionFactory;
  import org.apache.slide.search.basic.IBasicQuery;
  
  
  
  /**
   * This class is meant as an example for a store specific implementation
   * for Basic SEARCH.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Wallmer</a>
   * @version $Revision: 1.1 $
   */
  public class BasicQuerySample extends BasicQueryImpl implements IBasicQuery {
      
      /**
         * Constructor
         */
        public BasicQuerySample () {
      }
      
        /**
         * Constructor
         */
      public BasicQuerySample (SearchToken searchToken) {
          super (searchToken);
      }
   
          
        /**
         * Method getExpressionFactory
         *
         * @return   an IBasicExpressionFactory
         *
         */
      public IBasicExpressionFactory getExpressionFactory() {
          return new BasicExpressionFactorySample ();
      }
  }
  
  
  
  
  
  1.1                  
jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicExpressionSample.java
  
  Index: BasicExpressionSample.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicExpressionSample.java,v
 1.1 2003/01/13 10:29:54 wam Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/13 10:29:54 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Slide", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  
  package org.apache.slide.search.basic.sample;
  
  import java.util.Collection;
  import java.util.Hashtable;
  import java.util.Iterator;
  import org.apache.slide.content.NodeRevisionDescriptor;
  import org.apache.slide.content.NodeRevisionDescriptors;
  import org.apache.slide.content.NodeRevisionNumber;
  import org.apache.slide.search.BadQueryException;
  import org.apache.slide.search.RequestedResource;
  import org.apache.slide.search.SearchException;
  import org.apache.slide.search.basic.BasicResultSetImpl;
  import org.apache.slide.search.basic.IBasicExpression;
  import org.apache.slide.search.basic.IBasicExpressionFactory;
  import org.apache.slide.search.basic.IBasicResultSet;
  import org.apache.slide.webdav.util.ResourceWithProvidedProperties;
  
  /**
   * A very basic sample for a store specific Expression. Depending on the
   * complexity of the concrete store specific implementation, iut might be
   * a good idea to have an Expression class for each DAV: expression
   * (SQLEqExpression, SQLOrExpression, ...)
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Wallmer</a>
   * @version $Revision: 1.1 $
   */
  public class BasicExpressionSample implements IBasicExpression {
      
      
      /** an example for an executable command */
      String theExecutableCommand;
      
      /** backptr to the factory */
      IBasicExpressionFactory factory;
      
      /**
       * constructor for a compare expression like gt, eq, ...
       * For your concrete implementation you are free, which parameters have to
       * be passed, let the factory give you everything you need.
       */
      BasicExpressionSample (String command, IBasicExpressionFactory factory){
          theExecutableCommand = command;
          this.factory = factory;
      }
      
      /**
       * contructor for a merge expression
       */
      BasicExpressionSample (String mergeOperator,
                        Collection children,
                        IBasicExpressionFactory factory)
          throws BadQueryException
      {
          this.factory = factory;
          Iterator it = children.iterator();
          BasicExpressionSample firstChild = (BasicExpressionSample)it.next();
          
          if (firstChild == null)
              throw new BadQueryException (mergeOperator + " needs at least one nested 
element");
          
          theExecutableCommand = firstChild.theExecutableCommand;
          
          // create the executable command
          while (it.hasNext()) {
              BasicExpressionSample exp = (BasicExpressionSample)it.next();
              theExecutableCommand += " " + mergeOperator + " " + 
exp.theExecutableCommand;
          }
      }
      
      /**
       * fake executer. The executable command is printed and a fake result is created.
       *
       * @return   an IBasicResultSet
       *
       * @throws   SearchException
       *
       */
      public IBasicResultSet execute() throws SearchException {
          
          IBasicResultSet result = new BasicResultSetImpl (true);
          
          // here the miracle happens. The command is executed, and slide objects
          // are created from all results, that match the query.
          System.out.println("now execute: " + theExecutableCommand);
          
          // fake one result
          NodeRevisionDescriptors nrds =
              new NodeRevisionDescriptors (
              "/", new NodeRevisionNumber (1,0),
              new Hashtable(), new Hashtable(), new Hashtable(), false);
          
          NodeRevisionDescriptor nrd = new NodeRevisionDescriptor ();
          
          RequestedResource resource = new ResourceWithProvidedProperties
              (nrds, nrd, factory.getPropertyProvider());
          
          result.add (resource);
          
          return  result;
      }
  }
  
  
  
  
  1.1                  
jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicExpressionFactorySample.java
  
  Index: BasicExpressionFactorySample.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/stores/org/apache/slide/search/basic/sample/BasicExpressionFactorySample.java,v
 1.1 2003/01/13 10:29:54 wam Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/13 10:29:54 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Slide", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  
  package org.apache.slide.search.basic.sample;
  import org.apache.slide.search.basic.sample.*;
  
  import java.util.Collection;
  import org.apache.slide.content.NodeProperty.NamespaceCache;
  import org.apache.slide.search.BadQueryException;
  import org.apache.slide.search.PropertyProvider;
  import org.apache.slide.search.basic.IBasicExpression;
  import org.apache.slide.search.basic.IBasicExpressionFactory;
  import org.apache.slide.search.basic.IBasicQuery;
  import org.jdom.Element;
  
  
  /**
   * This factory creates executable BasicExpressions. An instance is created for
   * each SEARCH request.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Wallmer</a>
   * @version $Revision: 1.1 $
   */
  public class BasicExpressionFactorySample implements IBasicExpressionFactory {
      
      
      private BasicQuerySample query;
      protected PropertyProvider propertyProvider;
      
      /**
       * called for merge expressions (or, and).
       *
       * @param    mergeOperator       and, or
       * @param    namespace           the namespace of this expression
       * @param    expressionsToMerge  all expressions, that shall be merged
       *
       * @return   an IBasicExpression
       *
       * @throws   BadQueryException
       *
       */
      public IBasicExpression createMergeExpression (String mergeOperator,
                                                     String namespace,
                                                     Collection expressionsToMerge)
          throws BadQueryException
      {
          // you might want to check for the namespace
          BasicExpressionSample result = null;
          try {
              result = new BasicExpressionSample (mergeOperator, expressionsToMerge, 
this);
          }
          
          // if one of the expressions is for example a generic expression,
          // a ClassCastException is thrown, merge is not possible, so return null.
          catch (ClassCastException e) {
              System.out.println("one of the the expressions is not an 
ExpressionSample");
          }
          
          return result;
      }
      
      /**
       * Called by the expression compiler for each leave expression.
       *
       * @param    element             an Element discribing the expression
       *
       * @return   an IBasicExpression
       *
       * @throws   BadQueryException
       *
       */
      public IBasicExpression createExpression (Element element)
          throws BadQueryException
      {
          BasicExpressionSample result = null;
          
          if (element == null) {
              result = new BasicExpressionSample ("(no WHERE specified)", this);
          }
          else {
              String namespace = element.getNamespace().getURI();
              if (namespace.equals (NamespaceCache.DEFAULT_URI))
                  result = createDAVExpression (element);
              
              // allow store specific extensions
              //  else if (namespace.equals (MyNamespace))
              //      result = createMyExpression (element);
          }
          
          return result;
      }
      
      
      /**
       * Called, when the expression is in the default (DAV:) namespace.
       *
       *
       * @param    e                   an Element
       *
       * @return   a BasicExpressionTemplate
       *
       */
      private BasicExpressionSample createDAVExpression (Element e) {
          String name = e.getName();
          BasicExpressionSample result = null;
          
          if (name.equals ("eq")) {
              String prop = propName (e);
              String literal = e.getChild ("literal", e.getNamespace()).getText();
              result = new BasicExpressionSample ("(" + prop + " equals " + literal + 
")", this);
          }
              
          else if (name.equals ("lt")) {
              String prop = propName (e);
              String literal = e.getChildText ("literal", e.getNamespace());
              
              result = new BasicExpressionSample ("(" + prop + " lower_than " + 
literal + ")", this);
          }
          // ...
          
          return result;
      }
          
      /**
       * called by BasicExpressionCompiler after construction.
       *
       * @param    query               the associated BasicQuery
       * @param    propertyProvider    the PropertyProvider for this expression.
       *
       * @throws   BadQueryException
       *
       */
      public void init(IBasicQuery query, PropertyProvider propertyProvider)
          throws BadQueryException
      {
          this.query = (BasicQuerySample) query;
          this.propertyProvider = propertyProvider;
      }
      
      /**
       * Method getPropertyProvider
       *
       * @return   the PropertyProvider
       *
       */
      public PropertyProvider getPropertyProvider() {
          return propertyProvider;
      }
      
      /**
       * Method getQuery
       *
       * @return   the IBasicQuery
       *
       */
      public IBasicQuery getQuery() {
          return query;
      }
      
      
      private String propName (Element e) {
          Element propElem = e.getChild ("prop", e.getNamespace());
          Element el = (Element) propElem.getChildren().get(0);
          return el.getName();
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to