Coverage Report - org.apache.commons.scaffold.lucene.SearchUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchUtils
0%
0/23
0%
0/4
4
 
 1  
  /*
 2  
  * Copyright 2001,2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.scaffold.lucene;
 18  
 
 19  
 
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Enumeration;
 23  
 import java.util.HashMap;
 24  
 
 25  
 import org.apache.commons.beanutils.BeanUtils;
 26  
 import org.apache.commons.scaffold.lang.PopulateException;
 27  
 
 28  
 import org.apache.lucene.document.Document;
 29  
 import org.apache.lucene.document.Field;
 30  
 import org.apache.lucene.search.Hits;
 31  
 
 32  
 
 33  
  /**
 34  
   * General purpose utility methods related to Hits
 35  
   *
 36  
   * @author Craig R. McClanahan
 37  
   * @author Ted Husted
 38  
   * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
 39  
   */
 40  0
  public class SearchUtils {
 41  
 
 42  
     /**
 43  
      * Populate the properties of the specified JavaBean from the specified
 44  
      * Lucene document, based on matching each parameter name against the
 45  
      * corresponding JavaBeans "property setter" methods in the bean's class.
 46  
      * See <code>BeanUtils.CopyProperites</code> for more about automatic 
 47  
      * conversion between types.
 48  
      *
 49  
      * @param bean The JavaBean whose properties are to be set
 50  
      * @param document The Lucene document whose parameters are to be used
 51  
      * to populate bean properties
 52  
      * @exception PopulateException if an exception is thrown while setting
 53  
      * property values
 54  
      */
 55  
     public static void populate(
 56  
             Object bean,
 57  
             Document document)
 58  
         throws PopulateException {
 59  
 
 60  
         // Build a list of relevant fields and values
 61  0
         HashMap properties = new HashMap();
 62  
 
 63  
         // Iterator of field names
 64  0
         Enumeration fields = document.fields();
 65  
 
 66  0
         while (fields.hasMoreElements()) {
 67  0
             Field field = (Field) fields.nextElement();
 68  0
             properties.put(field.name(),field.stringValue());
 69  0
         }
 70  
 
 71  
         // Set the corresponding properties of our bean
 72  
         try {
 73  0
             BeanUtils.copyProperties(bean, properties);
 74  0
         } catch (Throwable t) {
 75  0
             throw new PopulateException(t);
 76  0
         }
 77  
 
 78  0
     } // end populate()
 79  
 
 80  
 
 81  
     /**
 82  
       * Return a ArrayList of hits by looping and calling populate.
 83  
       * No assumptions are made about fields in document. Each is
 84  
       * processed with a separate call to populate. Whatever fields
 85  
       * in each document that match the target bean will be
 86  
       * transferred.
 87  
       *
 88  
       * @param hits The Hits whose documents are to be used
 89  
       * to populate bean properties
 90  
       * @param target An instance of the bean to populate
 91  
       * @exception PopulateException if an exception is thrown while setting
 92  
       * property values, populating the bean, or accessing the Hits
 93  
       */
 94  
      public static Collection getCollection(
 95  
             Object target,
 96  
             Hits hits)
 97  
         throws PopulateException {
 98  
 
 99  
         // Use ArrayList to maintain sequence
 100  0
         ArrayList list = new ArrayList();
 101  
 
 102  
         // Acquire target class
 103  0
         Class factory = target.getClass();
 104  
 
 105  
         try {
 106  
             // Scroll to each document; populate bean, and add to list
 107  0
             for (int i=0; i<hits.length(); ++i) {
 108  0
                 Object bean = factory.newInstance();
 109  0
                 Document doc = hits.doc(i);
 110  0
                 populate(bean,doc);
 111  0
                 list.add(bean);
 112  
             }
 113  0
         } catch (Throwable t) {
 114  0
             throw new PopulateException(t);
 115  0
         }
 116  0
         return ((Collection) list);
 117  
 
 118  
     } // end getCollection()
 119  
 
 120  
 } // end SearchUtils
 121