View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  /**
24   * 
25   */
26  package com.gisgraphy.fulltext;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import org.apache.solr.client.solrj.response.QueryResponse;
32  import org.apache.solr.common.SolrDocument;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  
38  /**
39   * SolrUnmarshaller for solr Query response to a DTO
40   * 
41   * @see SolrResponseDto
42   * 
43   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
44   */
45  public class SolrUnmarshaller {
46  
47      /**
48       * The Logger
49       */
50      protected static final Logger logger = LoggerFactory
51  	    .getLogger(FullTextSearchEngine.class);
52      
53      private static solrResponseDtoBuilder builder = new solrResponseDtoBuilder();
54  
55      /**
56       * @param solrDocument
57       *                the solrDocument To Marshall into a SolrResponseDto
58       * @return a SolrResponseDto
59       */
60      private static SolrResponseDto unmarshall(SolrDocument solrDocument) {
61  	SolrResponseDto dto =builder.build(solrDocument);
62  	return dto;
63  
64      }
65  
66      /**
67       * @param queryResponse
68       *                the Solr QueryResponse we want to transform
69       * @return a list of SolrResponseDto or an empty list. never return null.
70       */
71      public static List<SolrResponseDto> unmarshall(QueryResponse queryResponse) {
72  	List<SolrResponseDto> results = new ArrayList<SolrResponseDto>();
73  	try {
74  	    for (SolrDocument doc : queryResponse.getResults()) {
75  		results.add(unmarshall(doc));
76  	    }
77  	} catch (RuntimeException e) {
78  	    logger
79  		    .warn("an error has occured during unmarshaling of SolrResponseDto : "
80  			    + e.getMessage());
81  	    throw e;
82  	}
83  	return results;
84  
85      }
86  
87  }