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  package com.gisgraphy.street;
24  
25  import java.io.IOException;
26  import java.io.OutputStream;
27  import java.io.OutputStreamWriter;
28  import java.io.UnsupportedEncodingException;
29  import java.io.Writer;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import com.gisgraphy.domain.valueobject.Constants;
38  import com.gisgraphy.domain.valueobject.GisgraphyServiceType;
39  import com.gisgraphy.domain.valueobject.Pagination;
40  import com.gisgraphy.domain.valueobject.StreetDistance;
41  import com.gisgraphy.domain.valueobject.StreetSearchResultsDto;
42  import com.gisgraphy.geoloc.GeolocResultsDto;
43  import com.gisgraphy.helper.OutputFormatHelper;
44  import com.gisgraphy.serializer.UniversalSerializer;
45  import com.gisgraphy.serializer.common.OutputFormat;
46  import com.gisgraphy.serializer.exception.UnsupportedFormatException;
47  import com.gisgraphy.service.ServiceException;
48  import com.sun.syndication.feed.module.georss.GeoRSSModule;
49  import com.sun.syndication.feed.module.georss.gml.GMLModuleImpl;
50  import com.sun.syndication.feed.module.opensearch.OpenSearchModule;
51  import com.sun.syndication.feed.module.opensearch.impl.OpenSearchModuleImpl;
52  import com.sun.syndication.feed.synd.SyndContent;
53  import com.sun.syndication.feed.synd.SyndContentImpl;
54  import com.sun.syndication.feed.synd.SyndEntry;
55  import com.sun.syndication.feed.synd.SyndEntryImpl;
56  import com.sun.syndication.feed.synd.SyndFeed;
57  import com.sun.syndication.feed.synd.SyndFeedImpl;
58  import com.sun.syndication.io.SyndFeedOutput;
59  
60  /**
61   * serialize @link {@link GeolocResultsDto} into several formats
62   * 
63   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
64   */
65  public class StreetSearchResultsDtoSerializer implements
66  	IStreetSearchResultsDtoSerializer {
67      
68      public final static String START_PAGINATION_INDEX_EXTRA_PARAMETER = "startPaginationIndex";
69      
70      /**
71       * The logger
72       */
73      protected static final Logger logger = LoggerFactory
74  	    .getLogger(StreetSearchResultsDtoSerializer.class);
75  
76      
77  
78     
79  
80      public void serialize(OutputStream outputStream, OutputFormat outputFormat,
81  	    StreetSearchResultsDto streetSearchResultsDto, boolean indent,Map<String,Object> extraParameters) {
82  	if (!OutputFormatHelper.isFormatSupported(outputFormat,GisgraphyServiceType.STREET)) {
83  	    throw new UnsupportedFormatException(outputFormat
84  		    + " is not applicable for street search");
85  	} 
86  	   
87  	if (outputFormat == OutputFormat.JSON || outputFormat == OutputFormat.PHP || outputFormat == OutputFormat.PYTHON  || outputFormat == OutputFormat.RUBY || outputFormat == OutputFormat.XML || outputFormat == OutputFormat.YAML) {
88  		serializeWithUniveraslSerializer(outputStream, streetSearchResultsDto,  indent, outputFormat,extraParameters);
89  	}else 	if (outputFormat==OutputFormat.ATOM){
90  	    int  startPaginationIndex = getStartPaginationIndex(extraParameters);
91  	    serializeToFeed(outputStream,streetSearchResultsDto,OutputFormat.ATOM_VERSION, startPaginationIndex);
92  	}
93  	else if (outputFormat==OutputFormat.GEORSS) {
94  	    int  startPaginationIndex = getStartPaginationIndex(extraParameters);
95  	    serializeToFeed(outputStream,streetSearchResultsDto,OutputFormat.RSS_VERSION, startPaginationIndex);
96  	}
97  	else {
98  	    //default
99  		serializeWithUniveraslSerializer(outputStream, streetSearchResultsDto,  indent, OutputFormat.XML,extraParameters);
100 	}
101     }
102     
103     private int getStartPaginationIndex(Map<String, Object> extraParameters) {
104 	if (extraParameters!= null){
105 	Object startPaginationIndexObject = extraParameters.get(START_PAGINATION_INDEX_EXTRA_PARAMETER);
106 	if (startPaginationIndexObject != null && startPaginationIndexObject instanceof Integer){
107 	    return (Integer) startPaginationIndexObject;
108 	}
109 	}
110 	return 1;
111     }    
112     private void serializeWithUniveraslSerializer(OutputStream outputStream, StreetSearchResultsDto streetSearchResultsDto,boolean indent, OutputFormat format,Map<String,Object> extraParameters) {
113    	 try {
114    	     UniversalSerializer.getInstance().write(outputStream, streetSearchResultsDto,  indent,extraParameters, format);
115    	    } catch (Exception e) {
116    		throw new ServiceException(e);
117    	    }
118    	
119        }
120     
121    
122 
123     @SuppressWarnings("unchecked")
124     private void serializeToFeed(OutputStream outputStream,
125 	    StreetSearchResultsDto streetSearchResultsDto,String feedVersion, int startPaginationIndex) {
126 	SyndFeed synFeed = new SyndFeedImpl();
127 	Writer oWriter = null;
128 	try {
129 
130 	    synFeed.setFeedType(feedVersion);
131 	    
132 
133 	    synFeed.setTitle(Constants.FEED_TITLE);
134 	    synFeed.setLink(Constants.FEED_LINK);
135 	    synFeed.setDescription(Constants.FEED_DESCRIPTION);
136 	    List<SyndEntry> entries = new ArrayList<SyndEntry>();
137 
138 	    for (StreetDistance gisFeatureDistance : streetSearchResultsDto
139 		    .getResult()) {
140 
141 		SyndEntry entry = new SyndEntryImpl();
142 		GeoRSSModule geoRSSModuleGML = new GMLModuleImpl();
143 		OpenSearchModule openSearchModule = new OpenSearchModuleImpl();
144 
145 		geoRSSModuleGML.setLatitude(gisFeatureDistance.getLat());
146 		geoRSSModuleGML.setLongitude(gisFeatureDistance.getLng());
147 
148 		openSearchModule
149 			.setItemsPerPage(Pagination.DEFAULT_MAX_RESULTS);
150 		openSearchModule
151 			.setTotalResults(streetSearchResultsDto.getNumFound());
152 		openSearchModule.setStartIndex(startPaginationIndex);
153 
154 		entry.getModules().add(openSearchModule);
155 		entry.getModules().add(geoRSSModuleGML);
156 		entry.setTitle(gisFeatureDistance.getName());
157 		entry.setAuthor(com.gisgraphy.domain.Constants.MAIL_ADDRESS);
158 		entry
159 			.setLink(Constants.STREET_BASE_URL+
160 				+ gisFeatureDistance.getGid());
161 		SyndContent description = new SyndContentImpl();
162 		description.setType(OutputFormat.ATOM.getContentType());
163 		description.setValue(gisFeatureDistance.getName());
164 		entry.setDescription(description);
165 		entries.add(entry);
166 	    }
167 	    
168 	    synFeed.setEntries(entries);
169 
170 	    try {
171 		oWriter = new OutputStreamWriter(outputStream, Constants.CHARSET);
172 	    } catch (UnsupportedEncodingException e) {
173 		throw new RuntimeException("unknow encoding "+Constants.CHARSET);
174 	    }
175 
176 	    // Copy synfeed to output
177 	    SyndFeedOutput output = new SyndFeedOutput();
178 	    try {
179 		output.output(synFeed, oWriter);
180 		 // Flush
181 		    oWriter.flush();
182 	    } catch (Exception e) {
183 		throw new ServiceException(e);
184 	    }
185 
186 	   
187 	} finally {
188 	    if (oWriter != null)
189 		try {
190 		    oWriter.close();
191 		} catch (IOException e) {
192 		    throw new RuntimeException(e);
193 		}
194 	    if (outputStream != null)
195 		try {
196 		    outputStream.close();
197 		} catch (IOException e) {
198 		    throw new RuntimeException(e);
199 		}
200 	}
201 
202     }
203 
204 }