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