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.geoloc;
27  
28  import java.io.ByteArrayOutputStream;
29  import java.io.OutputStream;
30  import java.io.UnsupportedEncodingException;
31  import java.util.HashMap;
32  import java.util.List;
33  import java.util.Map;
34  
35  import javax.annotation.Resource;
36  
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  import org.springframework.util.Assert;
40  
41  import com.gisgraphy.domain.geoloc.entity.GisFeature;
42  import com.gisgraphy.domain.repository.IGisDao;
43  import com.gisgraphy.domain.repository.IRepositoryStrategy;
44  import com.gisgraphy.domain.valueobject.Constants;
45  import com.gisgraphy.domain.valueobject.GisFeatureDistance;
46  import com.gisgraphy.geocoloc.IGeolocSearchEngine;
47  import com.gisgraphy.serializer.common.UniversalSerializerConstant;
48  import com.gisgraphy.service.IStatsUsageService;
49  import com.gisgraphy.service.ServiceException;
50  import com.gisgraphy.stats.StatsUsageType;
51  
52  /**
53   * Default (threadsafe) implementation of {@link IGeolocSearchEngine}.
54   * 
55   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
56   * 
57   */
58  public class GeolocSearchEngine implements IGeolocSearchEngine {
59  	
60  	/**
61  	 * very usefull when import is running
62  	 */
63  	public static boolean disableLogging=false;
64  	
65  
66      @Resource
67      IStatsUsageService statsUsageService;
68      
69      @Resource
70      IGeolocResultsDtoSerializer geolocResultsDtoSerializer;
71  
72     
73      @Resource
74      IRepositoryStrategy repositoryStrategy;
75      
76  
77      /**
78       * The logger
79       */
80      protected static final Logger logger = LoggerFactory
81  	    .getLogger(GeolocSearchEngine.class);
82  
83  
84  
85      /*
86       * (non-Javadoc)
87       * 
88       * @see com.gisgraphy.domain.geoloc.service.geoloc.IGeolocSearchEngine#executeQuery(com.gisgraphy.domain.geoloc.service.fulltextsearch.FulltextQuery)
89       */
90      public GeolocResultsDto executeQuery(GeolocQuery query)
91  	    throws ServiceException {
92  	statsUsageService.increaseUsage(StatsUsageType.GEOLOC);
93  	Assert.notNull(query, "Can not execute a null query");
94  	long start = System.currentTimeMillis();
95  	Class<?> placetype = query.getPlaceType();
96  	IGisDao<?> dao = repositoryStrategy
97  		.getDao(GisFeature.class);
98  	if (placetype != null) {
99  	    dao = repositoryStrategy.getDao(placetype);
100 	}
101 	if (dao == null) {
102 	    throw new GeolocSearchException(
103 		    "No gisFeatureDao or no placetype can be found for "
104 			    + placetype + " can be found.");
105 	}
106 	List<GisFeatureDistance> results = dao.getNearestAndDistanceFrom(query
107 		.getPoint(), query.getRadius(),
108 		query.getFirstPaginationIndex(), query.getMaxNumberOfResults(),query.hasDistanceField(), query.hasMunicipalityFilter());
109 
110 	long end = System.currentTimeMillis();
111 	long qTime = end - start;
112 	if (!disableLogging){
113 		logger.info(query + " took " + (qTime) + " ms and returns "
114 				+ results.size() + " results");
115 	}
116 	return new GeolocResultsDto(results, qTime);
117 
118     }
119 
120     /*
121      * (non-Javadoc)
122      * 
123      * @see com.gisgraphy.domain.geoloc.service.IQueryProcessor#executeAndSerialize(com.gisgraphy.domain.geoloc.service.AbstractGisQuery,
124      *      java.io.OutputStream)
125      */
126     public void executeAndSerialize(GeolocQuery query, OutputStream outputStream)
127 	    throws ServiceException {
128 	Assert.notNull(query, "Can not execute a null query");
129 	Assert.notNull(outputStream,
130 		"Can not serialize into a null outputStream");
131 	GeolocResultsDto geolocResultsDto = executeQuery(query);
132 	Map<String, Object> extraParameter = new HashMap<String, Object>();
133 	extraParameter.put(GeolocResultsDtoSerializer.START_PAGINATION_INDEX_EXTRA_PARAMETER, query.getFirstPaginationIndex());
134 	extraParameter.put(UniversalSerializerConstant.CALLBACK_METHOD_NAME, query.getCallback());
135 	geolocResultsDtoSerializer.serialize(outputStream, query.getOutputFormat(), geolocResultsDto, query.isOutputIndented(),extraParameter);
136     }
137 
138     /*
139      * (non-Javadoc)
140      * 
141      * @see com.gisgraphy.domain.geoloc.service.IQueryProcessor#executeQueryToString(com.gisgraphy.domain.geoloc.service.AbstractGisQuery)
142      */
143     public String executeQueryToString(GeolocQuery query)
144 	    throws ServiceException {
145 	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
146 	executeAndSerialize(query, outputStream);
147 	try {
148 	    return outputStream.toString(Constants.CHARSET);
149 	} catch (UnsupportedEncodingException e) {
150 	    throw new RuntimeException("unknow encoding "+Constants.CHARSET);
151 	}
152     }
153 
154 }