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.street;
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.repository.IOpenStreetMapDao;
42  import com.gisgraphy.domain.valueobject.Constants;
43  import com.gisgraphy.domain.valueobject.StreetDistance;
44  import com.gisgraphy.domain.valueobject.StreetSearchResultsDto;
45  import com.gisgraphy.serializer.common.UniversalSerializerConstant;
46  import com.gisgraphy.service.IStatsUsageService;
47  import com.gisgraphy.service.ServiceException;
48  import com.gisgraphy.stats.StatsUsageType;
49  
50  /**
51   * Default (threadsafe) implementation of {@link IStreetSearchEngine}.
52   * 
53   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
54   * 
55   */
56  public class StreetSearchEngine implements IStreetSearchEngine {
57  
58      @Resource
59      IOpenStreetMapDao openStreetMapDao;
60  
61      @Resource
62      IStreetSearchResultsDtoSerializer streetSearchResultsDtoSerializer;
63  
64      @Resource
65      IStatsUsageService statsUsageService;
66  
67      /**
68       * The logger
69       */
70      protected static final Logger logger = LoggerFactory
71  	    .getLogger(StreetSearchEngine.class);
72  
73      public StreetSearchResultsDto executeQuery(StreetSearchQuery query)
74  	    throws ServiceException {
75  	statsUsageService.increaseUsage(StatsUsageType.STREET);
76  	Assert.notNull(query, "Can not execute a null query");
77  	long start = System.currentTimeMillis();
78  
79  	List<StreetDistance> results = openStreetMapDao
80  		.getNearestAndDistanceFrom(query.getPoint(), query.getRadius(),
81  			query.getFirstPaginationIndex(), query
82  				.getMaxNumberOfResults(),
83  			query.getStreetType(), query.getOneWay(), query
84  				.getName(),query.getStreetSearchMode(), query.hasDistanceField());
85  
86  	long end = System.currentTimeMillis();
87  	long qTime = end - start;
88  	logger.info(query + " took " + (qTime) + " ms and returns "
89  		+ results.size() + " results");
90  	return new StreetSearchResultsDto(results, qTime,query.getName());
91  
92      }
93  
94      /*
95       * (non-Javadoc)
96       * 
97       * @see com.gisgraphy.domain.geoloc.service.IQueryProcessor#executeAndSerialize(com.gisgraphy.domain.geoloc.service.AbstractGisQuery,
98       *      java.io.OutputStream)
99       */
100     public void executeAndSerialize(StreetSearchQuery query, OutputStream outputStream)
101 	    throws ServiceException {
102 	Assert.notNull(query, "Can not execute a null query");
103 	Assert.notNull(outputStream,
104 		"Can not serialize into a null outputStream");
105 	StreetSearchResultsDto streetSearchResultsDto = executeQuery(query);
106 	Map<String, Object> extraParameter = new HashMap<String, Object>();
107 	extraParameter.put(StreetSearchResultsDtoSerializer.START_PAGINATION_INDEX_EXTRA_PARAMETER, query.getFirstPaginationIndex());
108 	extraParameter.put(UniversalSerializerConstant.CALLBACK_METHOD_NAME, query.getCallback());
109 	streetSearchResultsDtoSerializer.serialize(outputStream, query
110 		.getOutputFormat(), streetSearchResultsDto, query.isOutputIndented(),
111 		extraParameter);
112 	}
113 
114     /*
115      * (non-Javadoc)
116      * 
117      * @see com.gisgraphy.domain.geoloc.service.IQueryProcessor#executeQueryToString(com.gisgraphy.domain.geoloc.service.AbstractGisQuery)
118      */
119     public String executeQueryToString(StreetSearchQuery query)
120 	    throws ServiceException {
121 	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
122 	executeAndSerialize(query, outputStream);
123 	try {
124 	    return outputStream.toString(Constants.CHARSET);
125 	} catch (UnsupportedEncodingException e) {
126 	   throw new StreetSearchException("unsupported encoding "+Constants.CHARSET);
127 	}
128     }
129 
130 }