You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by gr...@apache.org on 2005/03/01 03:41:02 UTC

svn commit: r155739 - in lenya/trunk/src/java/org/apache/lenya/cms: search/usecases/Search.java usecase/AbstractUsecase.java

Author: gregor
Date: Mon Feb 28 18:41:01 2005
New Revision: 155739

URL: http://svn.apache.org/viewcvs?view=rev&rev=155739
Log:
Tried to refactor the spaghetti code that is the search usecase

Modified:
    lenya/trunk/src/java/org/apache/lenya/cms/search/usecases/Search.java
    lenya/trunk/src/java/org/apache/lenya/cms/usecase/AbstractUsecase.java

Modified: lenya/trunk/src/java/org/apache/lenya/cms/search/usecases/Search.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/search/usecases/Search.java?view=diff&r1=155738&r2=155739
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/search/usecases/Search.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/search/usecases/Search.java Mon Feb 28 18:41:01 2005
@@ -19,13 +19,11 @@
 import org.apache.lenya.cms.usecase.DocumentUsecase;
 import org.apache.lenya.cms.usecase.UsecaseException;
 
-import org.apache.avalon.framework.context.ContextException;
-import org.apache.avalon.framework.component.ComponentException;
-
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
+import org.apache.lucene.queryParser.ParseException;
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.queryParser.MultiFieldQueryParser;
 import org.apache.lucene.search.Hits;
@@ -39,6 +37,8 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -55,11 +55,31 @@
     
     // File workDir = (File)super.getContext().get(Constants.CONTEXT_WORK_DIR);
     
-    File indexDir=null;
-    File excerptDir=null;
+    private File indexDir=null;
+    private File excerptDir=null;
+    
+    private String[] fields={"contents","title"};
+    private String field = "contents";
+    private String queryString = "";
+    private String publication_id = "";
+    private String sortBy = "";
+    private String sortReverse = "";
+    
+    private String startString = "";
+    private String endString = "";
+    
+    private String[] words=new String[0];
     
-    String[] fields={"contents","title"};
-    String field = "contents";
+    private Hits hits = null;
+    private int hits_length=-1;
+    
+    private int hitsPerPage = 10;
+    private int maxPages = 10;
+    private int excerptOffset = 100;
+    private int start = 10;
+    private int end = 10; 
+    private String sitemapPath = null;
+    private int numberOfPubs = 1;
     
     /**
      * Ctor.
@@ -92,24 +112,24 @@
     
     /**
      * Search index
+     * @param query_string The query string
+     * @param publication_id The id of the publication to search
+     * @param sortField The field to sort by
+     * @param sortReverse Whether to sort in reverse
+     * @return the hits
      */
-    public Hits search(String query_string, String publication_id, String sortField, boolean sortReverse) 
-        throws IOException{
+    private Hits search(String query_string, String publication_id, String sortField, boolean sortReverse) {
         Hits hits =null;
         
-        try{
-            Searcher searcher=new IndexSearcher(indexDir.getAbsolutePath());
+        try {
+            Searcher searcher=new IndexSearcher(this.indexDir.getAbsolutePath());
             Analyzer l_analyzer=new StandardAnalyzer();
-            
-            QueryParser l_queryParser = new QueryParser(field,l_analyzer); // Single field
+            QueryParser l_queryParser = new QueryParser(this.field,l_analyzer); // Single field
             //MultiFieldQueryParser l_queryParser = new MultiFieldQueryParser(fields[0], l_analyzer); // Multiple fields
-            
             l_queryParser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
-            
             getLogger().debug(query_string);
             Query l_query = l_queryParser.parse(query_string); // Single field
             //Query l_query = l_queryParser.parse(query_string,fields,l_analyzer); // Multiple fields
-            
             if (sortField != null) {
                 Sort sort = new Sort(sortField, sortReverse);
                 hits = searcher.search(l_query, sort);
@@ -119,46 +139,22 @@
             if(hits != null){
                 return hits;
             }
-        }
-        catch(IOException e){
-            getLogger().error(".search(): EXCEPTION: "+e);
-            throw e;
-        }
-        catch(Exception e){
-            getLogger().error(".search(): EXCEPTION: "+e);
+        } catch (final IOException e) {
+            addErrorMessage("IO error occured: " +e.toString());
+        } catch (final ParseException e) {
+            addErrorMessage("Parse error occured: " +e.toString());
         }
         
         return null;
     }
     
     /**
-     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#initParameters()
      */
-    protected void doExecute() throws Exception {
-        super.doExecute();
-
-        Hits hits = null;
-        int hits_length=-1;
-        
-        String[] words=new String[0];
-        
-        int hitsPerPage = 10;
-        int maxPages = 10;
-        int excerptOffset = 100;
-        int start = 10;
-        int end = 10; 
-        String sitemapPath = null;
-        int numberOfPubs = 1;
-        
-        // Get sitemap path
-/*        Source input_source=this.getResolver().resolveURI("");
-        String sitemapPath=input_source.getURI();
-        sitemapPath=sitemapPath.substring(5); // Remove "file:" protocol
-*/        
-        
-        // Read parameters from sitemap
-        numberOfPubs = Integer.parseInt(getParameterAsString("number-of-pubs"));
-        Publication[] pubs = new Publication[numberOfPubs];
+    protected void initParameters() {
+        super.initParameters();
+        this.numberOfPubs = Integer.parseInt(getParameterAsString("number-of-pubs"));
+        Publication[] pubs = new Publication[this.numberOfPubs];
         for(int i = 0;i < pubs.length;i++) {
             pubs[i] = new Publication();
             pubs[i].id = getParameterAsString("pub"+i+"-id");
@@ -169,297 +165,279 @@
             pubs[i].prefix = getParameterAsString("pub"+i+"-prefix");
         }
         
-        hitsPerPage = Integer.parseInt(getParameterAsString("max-hits-per-page"));
-        maxPages = Integer.parseInt(getParameterAsString("max-pages"));
-        excerptOffset = Integer.parseInt(getParameterAsString("excerpt-offset"));
-
+        this.hitsPerPage = Integer.parseInt(getParameterAsString("max-hits-per-page"));
+        this.maxPages = Integer.parseInt(getParameterAsString("max-pages"));
+        this.excerptOffset = Integer.parseInt(getParameterAsString("excerpt-offset"));
+        
         // Read parameters from query string
-        String queryString = getParameterAsString("queryString");
-        String publication_id = getParameterAsString("publication-id");
-        String sortBy = getParameterAsString("sortBy");
-        String sortReverse = getParameterAsString("sortReverse");
-        
-        String startString = getParameterAsString("start");
-        String endString = getParameterAsString("end");
-        start=new Integer(startString).intValue();
-        if(endString == null){
-            end=hitsPerPage;
+        this.queryString = getParameterAsString("queryString");
+        this.publication_id = getParameterAsString("publication-id");
+        this.sortBy = getParameterAsString("sortBy");
+        this.sortReverse = getParameterAsString("sortReverse");
+        
+        this.startString = getParameterAsString("start");
+        this.endString = getParameterAsString("end");
+        this.start=new Integer(this.startString).intValue();
+        if(this.endString == null){
+            this.end=this.hitsPerPage;
         }
         else{
-            end=new Integer(endString).intValue();
+            this.end=new Integer(this.endString).intValue();
         }
         
         // Find the number of the selected publication
         int whichPublication=0;
         for (int i = 0;i < pubs.length;i++) {
-            if (pubs[i].id.equals(publication_id)) {
+            if (pubs[i].id.equals(this.publication_id)) {
                 whichPublication = i;
             }
         }
         
         
         // Get all search fields
-/*        Vector myFields = new Vector();
-        Enumeration parameterNames = request.getParameterNames();
-        while(parameterNames.hasMoreElements()){
-            String parameterName=(String)parameterNames.nextElement();
-            String value=request.getParameter(parameterName);
+        Vector myFields = new Vector();
+        Set keys = getParameters().keySet();
+        
+        for (Iterator i = keys.iterator(); i.hasNext();) {
+            Object key = i.next();
             
-            if (parameterName.indexOf(".fields") > 0) { // looking for field parameters
-                StringTokenizer st = new StringTokenizer(parameterName, ".");
-                int length = st.countTokens();
-                String fieldPublicationId = st.nextToken();
-                if (fieldPublicationId.equals(publication_id) || fieldPublicationId.equals("dummy-index-id")) {
-                    st.nextToken(); // Ignore "fields" token
-                    if (length == 2) { // radio or select
-                        myFields.addElement(value);
-                    } else if (length == 3) { // checkbox
-                        myFields.addElement(st.nextToken());
-                    } else {
-                        // something is wrong
+            if (key instanceof String) {
+                String keyString = (String) key;
+                
+                if (keyString.indexOf(".fields") > 0) {// looking for field parameters
+                    StringTokenizer st = new StringTokenizer(keyString, ".");
+                    int length = st.countTokens();
+                    String fieldPublicationId = st.nextToken();
+                    if (fieldPublicationId.equals(this.publication_id) || fieldPublicationId.equals("dummy-index-id")) {
+                        st.nextToken(); // Ignore "fields" token
+                        if (length == 2) { // radio or select
+                            myFields.addElement(getParameterAsString(keyString));
+                        } else if (length == 3) { // checkbox
+                            myFields.addElement(st.nextToken());
+                        } else {
+                            // something is wrong
+                        }
                     }
                 }
             }
         }
-        
         if (myFields.size() > 0) {
-            field = (String)myFields.elementAt(0);
-            fields = new String[myFields.size()];
+            this.field = (String)myFields.elementAt(0);
+            this.fields = new String[myFields.size()];
             for (int i = 0; i < myFields.size(); i++) {
-                fields[i] = (String)myFields.elementAt(i);
+                this.fields[i] = (String)myFields.elementAt(i);
             }
         }
-*/        
+        
         // Set index and excerpt dir
         String param_index_dir=pubs[whichPublication].indexDir;
         if(param_index_dir.charAt(0) == '/'){
-            indexDir=new File(param_index_dir);
+            this.indexDir=new File(param_index_dir);
         }
         else{
-            indexDir=new File(sitemapPath+File.separator+param_index_dir);
+            this.indexDir=new File(this.sitemapPath+File.separator+param_index_dir);
         }
         String param_excerpt_dir=pubs[whichPublication].excerptDir;
         if(param_excerpt_dir.charAt(0) == '/'){
             this.excerptDir=new File(param_excerpt_dir);
         }
         else{
-            excerptDir=new File(sitemapPath+File.separator+param_excerpt_dir);
-        }
-        
-        for(int i = 0;i < pubs.length;i++) {
-            // pubs[i].id
-            // pubs[i].name
-            // pubs[i].indexDir
-            
-            String[] searchFields = pubs[i].getFields();
-            if (searchFields != null) {
-                for (int k = 0; k < searchFields.length; k++) {
-                    // searchFields[k];
-                }
-            } else {
-                // pubs[i].searchFields;
-            }
-            
-            // pubs[i].excerptDir;
-            // pubs[i].prefix;
+            this.excerptDir=new File(this.sitemapPath+File.separator+param_excerpt_dir);
         }
         
         // hitsPerPage
         // maxPages
         // excerptOffset
         
+        // Get sitemap path
+        /*        Source input_source=this.getResolver().resolveURI("");
+         String sitemapPath=input_source.getURI();
+         sitemapPath=sitemapPath.substring(5); // Remove "file:" protocol
+         */        
+        
+        
+        
+    }
+    
+    
+    /**
+     * @see org.apache.lenya.cms.usecase.AbstractUsecase#doExecute()
+     */
+    protected void doExecute() throws Exception {
+        super.doExecute();
         
-        if(queryString != null && queryString.length() != 0 && publication_id != null 
-                && publication_id.length() > 0){
+        if(this.queryString != null && this.queryString.length() != 0 && this.publication_id != null 
+                && this.publication_id.length() > 0){
             
-            try {
-                if (sortBy.equals("score")) {
-                    hits = search(queryString, publication_id, null, false);
+            if (this.sortBy.equals("score")) {
+                this.hits = search(this.queryString, this.publication_id, null, false);
+            } else {
+                if (this.sortReverse.equals("true")) {
+                    this.hits = search(this.queryString, this.publication_id, this.sortBy, true);
                 } else {
-                    if (sortReverse.equals("true")) {
-                        hits = search(queryString, publication_id, sortBy, true);
-                    } else {
-                        hits = search(queryString, publication_id, sortBy, false);
-                    }
+                    this.hits = search(this.queryString, this.publication_id, this.sortBy, false);
                 }
-            } catch(Exception e) {
             }
             
-            if(hits != null){
-                hits_length=hits.length();
-            }
-            else{
-                hits_length=-1;
-                hits=null;
+            if(this.hits != null){
+                this.hits_length=this.hits.length();
+            } else{
+                this.hits_length=-1;
             }
             //publication_id
             // pubs[whichPublication].name
             // pubs[whichPublication].prefix
             //sortBy
             //queryString
-            if(queryString != null){
-                Vector twords=new Vector();
-                
-                StringTokenizer st=new StringTokenizer(queryString," ");
-                while(st.hasMoreTokens()){
-                    String word=(String)st.nextElement();
-                    if(!(word.equals("OR") || word.equals("AND"))){
-                        //word
-                        twords.addElement(word);
-                    }
-                }
-                words=new String[twords.size()];
-                for(int i=0;i < twords.size();i++){
-                    words[i]=(String)twords.elementAt(i);
+            Vector twords=new Vector();
+            
+            StringTokenizer st=new StringTokenizer(this.queryString," ");
+            while(st.hasMoreTokens()){
+                String word=(String)st.nextElement();
+                if(!(word.equals("OR") || word.equals("AND"))){
+                    //word
+                    twords.addElement(word);
                 }
             }
+            this.words=new String[twords.size()];
+            for(int i=0;i < twords.size();i++){
+                this.words[i]=(String)twords.elementAt(i);
+            }
             //start
             //end
             
-            for (int i = 0; i < fields.length; i++) {
-                // fields[i];
-            }
-            
-            try{
+            try {
                 Analyzer ll_analyzer=new StandardAnalyzer();
-                QueryParser queryParser = new QueryParser(field,ll_analyzer);
+                QueryParser queryParser = new QueryParser(this.field,ll_analyzer);
                 //MultiFieldQueryParser queryParser = new MultiFieldQueryParser("contents",ll_analyzer);
                 queryParser.setOperator(QueryParser.DEFAULT_OPERATOR_AND);
                 Query ll_query = queryParser.parse(queryString);
                 //Query ll_query = queryParser.parse(queryString,fields,ll_analyzer);
                 // ll_query.toString("contents");
-            }
-            catch(Exception e){
+            } catch (final ParseException e) {
+                addErrorMessage("Parse error occured: " +e.toString());
             }
             
         }
         else{
-            hits_length=-1;
-            hits=null;
+            this.hits_length=-1;
+            this.hits=null;
         }
         
-        if(hits != null){
-            //  hits_length
-            if(hits_length > 0){
-                for (int i=start-1;i <= end-1;i++) {
-                    if((i < 0) || (i >= hits.length())){
-                        continue;
+        if(hits != null)
+            generateExtracts();
+    }
+
+    /**
+     * @throws IOException
+     */
+    private void generateExtracts() throws IOException {
+        //  hits_length
+        if(hits_length > 0){
+            for (int i=start-1;i <= end-1;i++) {
+                if((i < 0) || (i >= hits.length())){
+                    continue;
+                }
+                Document ldoc=hits.doc(i);
+                Enumeration lfields = ldoc.fields();
+                String lpath=ldoc.get("path");
+                
+                String lurl=ldoc.get("url");
+                String ltitle=ldoc.get("title");
+                String mime_type=ldoc.get("mime-type");
+                
+                if(lpath != null){
+                    //getPercent(hits.score(i);
+                }
+                else if(lurl != null){
+                    while (lfields.hasMoreElements()) {
+                        Field lfield = (Field)lfields.nextElement();
+                        String slfield = lfield.toString();
+                        // lfield.name()
+                        // slfield.substring(0, slfield.indexOf("&lt;"))
+                        // lfield.stringValue()
                     }
-                    Document ldoc=hits.doc(i);
-                    Enumeration lfields = ldoc.fields();
-                    String lpath=ldoc.get("path");
-                    
-                    String lurl=ldoc.get("url");
-                    String ltitle=ldoc.get("title");
-                    String mime_type=ldoc.get("mime-type");
-                    
-                    if(lpath != null){
-                        //getPercent(hits.score(i);
+                    //getPercent(hits.score(i))
+                    //hits.score(i)
+                    String parent = "";
+                    String filename = "";
+                    String querystring = "";
+                    if(lurl.lastIndexOf("/") > -1) {
+                        parent = lurl.substring(0,lurl.lastIndexOf("/"));
+                        filename = lurl.substring(lurl.lastIndexOf("/")+1);
                     }
-                    else if(lurl != null){
-                        while (lfields.hasMoreElements()) {
-                            Field lfield = (Field)lfields.nextElement();
-                            String slfield = lfield.toString();
-                            // lfield.name()
-                            // slfield.substring(0, slfield.indexOf("&lt;"))
-                            // lfield.stringValue()
-                        }
-                        //getPercent(hits.score(i))
-                        //hits.score(i)
-                        String parent = "";
-                        String filename = "";
-                        String querystring = "";
-                        if(lurl.lastIndexOf("/") > -1) {
-                            parent = lurl.substring(0,lurl.lastIndexOf("/"));
-                            filename = lurl.substring(lurl.lastIndexOf("/")+1);
-                        }
-                        if(lurl.indexOf("?") > -1) {
-                            querystring = lurl.substring(lurl.indexOf("?"));
-                        }
-                        // parent
-                        // filename
-                        // querystring
-                        // lurl
-                        
-                        File excerptFile=new File(excerptDir+File.separator+lurl);
-                        if((ltitle != null) && (ltitle.length() > 0)){
-                        }
-                        else{
-                        }
-                        if((mime_type != null) && (mime_type.length() > 0)){
-                        }
-                        else{
-                        }
-                        
-                        //String[] words={"funds","bonds"};
-                        try{
-                            ReTokenizeFile rtf=new ReTokenizeFile();
-                            rtf.setOffset(excerptOffset);
-                            String excerpt=rtf.getExcerpt(excerptFile,words);
-                            if(excerpt != null){
-                                excerpt=rtf.emphasizeAsXML(rtf.tidy(excerpt),words);
-                                
-                            }
-                            else{
-                                throw new Exception("excerpt == null. Maybe file does not contain the words!");
-                            }
-                        }
-                        catch(FileNotFoundException e){
-                        }
-                        catch(Exception e){
-                        }
-                        
+                    if(lurl.indexOf("?") > -1) {
+                        querystring = lurl.substring(lurl.indexOf("?"));
                     }
-                }
-            }
-            else{
-            }
-            int number_of_pages=(hits.length()/hitsPerPage);
-            if(number_of_pages*hitsPerPage != hits.length()){
-                number_of_pages=number_of_pages+1;
-            }
-            if(number_of_pages > maxPages){
-                number_of_pages=maxPages;
-            }
-            if(hits.length() == 0){
-                number_of_pages=0;
-            }
-            else{
-                for(int i=0;i < number_of_pages;i++){
-                    int pstart=i*hitsPerPage+1;
-                    int pend=(i+1)*hitsPerPage;
-                    if(hits_length < pend){
-                        pend=hits_length;
+                    // parent
+                    // filename
+                    // querystring
+                    // lurl
+                    
+                    File excerptFile=new File(this.excerptDir+File.separator+lurl);
+                    if((ltitle != null) && (ltitle.length() > 0)){
                     }
-                    String type="other";
-                    if(pstart == start){
-                        type="current";
+                    else{
                     }
-                    else if(pstart == start-hitsPerPage){
-                        type="previous";
+                    if((mime_type != null) && (mime_type.length() > 0)){
                     }
-                    else if(pstart == start+hitsPerPage){
-                        type="next";
+                    else{
+                    }
+                    
+                    try {
+                        ReTokenizeFile rtf=new ReTokenizeFile();
+                        rtf.setOffset(excerptOffset);
+                        String excerpt=rtf.getExcerpt(excerptFile,words);
+                        if(excerpt != null){
+                            excerpt=rtf.emphasizeAsXML(rtf.tidy(excerpt),words);                                
+                        }
+                    } catch (final FileNotFoundException e) {
+                        addErrorMessage("File not found error: " +e.toString());
+                    } catch (final IOException e) {
+                        addErrorMessage("IO error occured: " +e.toString());
                     }
+                    
+                }
+            }
+        }
+        else{
+        }
+        int number_of_pages=(hits.length()/hitsPerPage);
+        if(number_of_pages*hitsPerPage != hits.length()){
+            number_of_pages=number_of_pages+1;
+        }
+        if(number_of_pages > maxPages){
+            number_of_pages=maxPages;
+        }
+        if(hits.length() == 0){
+            number_of_pages=0;
+        }
+        else{
+            for(int i=0;i < number_of_pages;i++){
+                int pstart=i*hitsPerPage+1;
+                int pend=(i+1)*hitsPerPage;
+                if(hits_length < pend){
+                    pend=hits_length;
+                }
+                String type="other";
+                if(pstart == start){
+                    type="current";
+                }
+                else if(pstart == start-hitsPerPage){
+                    type="previous";
+                }
+                else if(pstart == start+hitsPerPage){
+                    type="next";
                 }
             }
         }
-        
     }
     
     /**
-     * @see org.apache.lenya.cms.usecase.Usecase#setParameter(java.lang.String, java.lang.Object)
+     *
      */
-    public void setParameter(String name, Object value) {
-        super.setParameter(name, value);
-        
+    String getPercent(float score){
+        return ""+java.lang.Math.round(score*100.0);
     }
-
-    /**
-    *
-    */
-   String getPercent(float score){
-       return ""+java.lang.Math.round(score*100.0);
-   }
-
+    
 }

Modified: lenya/trunk/src/java/org/apache/lenya/cms/usecase/AbstractUsecase.java
URL: http://svn.apache.org/viewcvs/lenya/trunk/src/java/org/apache/lenya/cms/usecase/AbstractUsecase.java?view=diff&r1=155738&r2=155739
==============================================================================
--- lenya/trunk/src/java/org/apache/lenya/cms/usecase/AbstractUsecase.java (original)
+++ lenya/trunk/src/java/org/apache/lenya/cms/usecase/AbstractUsecase.java Mon Feb 28 18:41:01 2005
@@ -282,6 +282,14 @@
     }
 
     /**
+     * Return a map of all parameters
+     * @return the map
+     */
+    public Map getParameters() {
+        return Collections.unmodifiableMap(this.parameters);
+    }
+
+    /**
      * Returns one of the strings "true" or "false" depending on whether the
      * corresponding checkbox was checked.
      * @param name The parameter name.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org