View Javadoc
1   package com.gisgraphy.fulltext;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.HashSet;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Set;
9   
10  import org.apache.solr.common.SolrDocument;
11  
12  import com.gisgraphy.domain.repository.exception.RepositoryException;
13  import com.gisgraphy.street.HouseNumberDeserializer;
14  import com.gisgraphy.street.HouseNumberDto;
15  
16  public class solrResponseDtoBuilder {
17  	
18  	HouseNumberDeserializer houseNumberDeserializer = new HouseNumberDeserializer();
19  	
20  	  /**
21       * Create a {@link SolrResponseDto} from a {@link SolrDocument}
22       */
23      //TODO maybe a unit test is missing here
24      public SolrResponseDto build(SolrDocument solrDocument) {
25  	SolrResponseDto solrResponseDto = new SolrResponseDto();
26  	if (solrDocument != null) {
27  		solrResponseDto.score= getFieldAsFloat(solrDocument, "score");
28  	    solrResponseDto.name = getFieldAsString(solrDocument, FullTextFields.NAME
29  		    .getValue());
30  	    solrResponseDto.feature_id = getFieldAsLong(solrDocument,
31  		    FullTextFields.FEATUREID.getValue());
32  	    solrResponseDto.feature_class = getFieldAsString(solrDocument,
33  		    FullTextFields.FEATURECLASS.getValue());
34  	    solrResponseDto.feature_code = getFieldAsString(solrDocument,
35  		    FullTextFields.FEATURECODE.getValue());
36  	    solrResponseDto.name_ascii = getFieldAsString(solrDocument,
37  		    FullTextFields.NAMEASCII.getValue());
38  	    solrResponseDto.elevation = getFieldAsInteger(solrDocument,
39  		    FullTextFields.ELEVATION.getValue());
40  	    solrResponseDto.gtopo30 = getFieldAsInteger(solrDocument,
41  		    FullTextFields.GTOPO30.getValue());
42  	    solrResponseDto.timezone = getFieldAsString(solrDocument,
43  		    FullTextFields.TIMEZONE.getValue());
44  	    solrResponseDto.fully_qualified_name = getFieldAsString(solrDocument,
45  		    FullTextFields.FULLY_QUALIFIED_NAME.getValue());
46  	    solrResponseDto.placetype = getFieldAsString(solrDocument,
47  		    FullTextFields.PLACETYPE.getValue());
48  	    solrResponseDto.population = getFieldAsInteger(solrDocument,
49  		    FullTextFields.POPULATION.getValue());
50  	    solrResponseDto.lat = getFieldAsDouble(solrDocument, FullTextFields.LAT
51  		    .getValue());
52  	    solrResponseDto.lng = getFieldAsDouble(solrDocument, FullTextFields.LONG
53  		    .getValue());
54  	    solrResponseDto.adm1_code = getFieldAsString(solrDocument,
55  		    FullTextFields.ADM1CODE.getValue());
56  	    solrResponseDto.adm2_code = getFieldAsString(solrDocument,
57  		    FullTextFields.ADM2CODE.getValue());
58  	    solrResponseDto.adm3_code = getFieldAsString(solrDocument,
59  		    FullTextFields.ADM3CODE.getValue());
60  	    solrResponseDto.adm4_code = getFieldAsString(solrDocument,
61  		    FullTextFields.ADM4CODE.getValue());
62  	    solrResponseDto.adm1_name = getFieldAsString(solrDocument,
63  		    FullTextFields.ADM1NAME.getValue());
64  	    solrResponseDto.adm2_name = getFieldAsString(solrDocument,
65  		    FullTextFields.ADM2NAME.getValue());
66  	    solrResponseDto.adm3_name = getFieldAsString(solrDocument,
67  		    FullTextFields.ADM3NAME.getValue());
68  	    solrResponseDto.adm4_name = getFieldAsString(solrDocument,
69  		    FullTextFields.ADM4NAME.getValue());
70  	    solrResponseDto.zipcodes = getFieldsToSet(solrDocument,
71  		    FullTextFields.ZIPCODE.getValue());
72  	    solrResponseDto.country_name = getFieldAsString(solrDocument,
73  		    FullTextFields.COUNTRYNAME.getValue());
74  	    solrResponseDto.country_flag_url = getFieldAsString(solrDocument,
75  		    FullTextFields.COUNTRY_FLAG_URL.getValue());
76  	    solrResponseDto.google_map_url = getFieldAsString(solrDocument,
77  		    FullTextFields.GOOGLE_MAP_URL.getValue());
78  	    solrResponseDto.yahoo_map_url = getFieldAsString(solrDocument,
79  		    FullTextFields.YAHOO_MAP_URL.getValue());
80  	    solrResponseDto.openstreetmap_map_url = getFieldAsString(solrDocument,
81  			    FullTextFields.OPENSTREETMAP_MAP_URL.getValue());
82  	    solrResponseDto.name_alternates = getFieldsToList(solrDocument,
83  		    FullTextFields.NAME.getValue()+FullTextFields.ALTERNATE_NAME_SUFFIX.getValue());
84  	    solrResponseDto.adm1_names_alternate = getFieldsToList(solrDocument,
85  		    FullTextFields.ADM1NAME.getValue()+FullTextFields.ALTERNATE_NAME_SUFFIX.getValue());
86  	    solrResponseDto.adm2_names_alternate = getFieldsToList(solrDocument,
87  		    FullTextFields.ADM2NAME.getValue()+FullTextFields.ALTERNATE_NAME_SUFFIX.getValue());
88  	    solrResponseDto.country_names_alternate = getFieldsToList(solrDocument,
89  		    FullTextFields.COUNTRYNAME.getValue()+FullTextFields.ALTERNATE_NAME_SUFFIX.getValue());
90  
91  	    solrResponseDto.name_alternates_localized = getFieldsToMap(solrDocument,
92  		    FullTextFields.NAME.getValue()+FullTextFields.ALTERNATE_NAME_DYNA_SUFFIX.getValue());
93  	    solrResponseDto.adm1_names_alternate_localized = getFieldsToMap(solrDocument,
94  		    FullTextFields.ADM1NAME.getValue()+FullTextFields.ALTERNATE_NAME_DYNA_SUFFIX.getValue());
95  	    solrResponseDto.adm2_names_alternate_localized = getFieldsToMap(solrDocument,
96  		    FullTextFields.ADM2NAME.getValue()+FullTextFields.ALTERNATE_NAME_DYNA_SUFFIX.getValue());
97  	    solrResponseDto.country_names_alternate_localized = getFieldsToMap(
98  		    solrDocument,  FullTextFields.COUNTRYNAME.getValue()+FullTextFields.ALTERNATE_NAME_DYNA_SUFFIX.getValue());
99  	    //countryspecific
100 	    solrResponseDto.continent=getFieldAsString(solrDocument,
101 		    FullTextFields.CONTINENT.getValue());
102 	    solrResponseDto.currency_code = getFieldAsString(solrDocument,
103 		    FullTextFields.CURRENCY_CODE.getValue());
104 	    solrResponseDto.currency_name= getFieldAsString(solrDocument,
105 		    FullTextFields.CURRENCY_NAME.getValue());
106 	    solrResponseDto.fips_code= getFieldAsString(solrDocument,
107 		    FullTextFields.FIPS_CODE.getValue());
108 	    solrResponseDto.isoalpha2_country_code= getFieldAsString(solrDocument,
109 		    FullTextFields.ISOALPHA2_COUNTRY_CODE.getValue());
110 	    solrResponseDto.country_code= getFieldAsString(solrDocument,
111 		    FullTextFields.COUNTRYCODE.getValue());
112 	    solrResponseDto.isoalpha3_country_code= getFieldAsString(solrDocument,
113 		    FullTextFields.ISOALPHA3_COUNTRY_CODE.getValue());
114 	    solrResponseDto.postal_code_mask= getFieldAsString(solrDocument,
115 		    FullTextFields.POSTAL_CODE_MASK.getValue());
116 	    solrResponseDto.postal_code_regex= getFieldAsString(solrDocument,
117 		    FullTextFields.POSTAL_CODE_REGEX.getValue());
118 	    solrResponseDto.phone_prefix= getFieldAsString(solrDocument,
119 		    FullTextFields.PHONE_PREFIX.getValue());
120 	    solrResponseDto.spoken_languages=getFieldsToList(solrDocument,
121 		    FullTextFields.SPOKEN_LANGUAGES.getValue());
122 	    solrResponseDto.tld= getFieldAsString(solrDocument,
123 		    FullTextFields.TLD.getValue());
124 	    solrResponseDto.capital_name= getFieldAsString(solrDocument,
125 		    FullTextFields.CAPITAL_NAME.getValue());
126 	    solrResponseDto.area= getFieldAsDouble(solrDocument,
127 		    FullTextFields.AREA.getValue());
128 	    solrResponseDto.level= getFieldAsInteger(solrDocument,
129 		    FullTextFields.LEVEL.getValue());
130 	    solrResponseDto.amenity= getFieldAsString(solrDocument,
131 			    FullTextFields.AMENITY.getValue());
132 	    solrResponseDto.municipality= getFieldAsBoolean(solrDocument,
133 			    FullTextFields.MUNICIPALITY.getValue(),false);
134 	    
135 	    
136 	    //street specific
137 	    solrResponseDto.one_way = getFieldAsBoolean(solrDocument,
138 		    FullTextFields.ONE_WAY.getValue(),false);
139 	    solrResponseDto.length = getFieldAsDouble(solrDocument,
140 		    FullTextFields.LENGTH.getValue());
141 	    solrResponseDto.street_type = getFieldAsString(solrDocument,
142 		    FullTextFields.STREET_TYPE.getValue());
143 	    solrResponseDto.openstreetmap_id = getFieldAsLong(solrDocument, FullTextFields.OPENSTREETMAP_ID.getValue());
144 	    solrResponseDto.is_in = getFieldAsString(solrDocument, FullTextFields.IS_IN.getValue());
145 	    solrResponseDto.is_in_place = getFieldAsString(solrDocument, FullTextFields.IS_IN_PLACE.getValue());
146 	    solrResponseDto.is_in_zip = getFieldsToSet(solrDocument, FullTextFields.IS_IN_ZIP.getValue());
147 	    solrResponseDto.is_in_adm = getFieldAsString(solrDocument, FullTextFields.IS_IN_ADM.getValue());
148 	    solrResponseDto.fully_qualified_address = getFieldAsString(solrDocument, FullTextFields.FULLY_QUALIFIED_ADDRESS.getValue());
149 	    solrResponseDto.house_numbers=getHouseNumber(solrDocument);
150 	}
151 	return solrResponseDto;
152     }
153     
154     
155 	private List<HouseNumberDto> getHouseNumber(SolrDocument solrDocument) {
156 		List<HouseNumberDto> housenumbers = new ArrayList<HouseNumberDto>();
157 		String fieldname = FullTextFields.HOUSE_NUMBERS.getValue();
158 		if (solrDocument.getFieldValues(fieldname) != null) {
159 			for (Object fieldValue : solrDocument.getFieldValues(fieldname)) {
160 				if (fieldValue == null) {
161 					continue;
162 				} else if (fieldValue instanceof String) {
163 					HouseNumberDto dto = houseNumberDeserializer
164 							.deserialize((String) fieldValue);
165 					housenumbers.add(dto);
166 				} else {
167 					throw new RepositoryException(fieldname
168 							+ " is not a String but a "
169 							+ fieldValue.getClass().getSimpleName());
170 				}
171 			}
172 		}
173 		return housenumbers;
174 	}
175 	
176     private Map<String, List<String>> getFieldsToMap(SolrDocument solrDocument,
177 	    String fieldNamePrefix) {
178 	Map<String, List<String>> result = new HashMap<String, List<String>>();
179 	if (solrDocument.getFieldNames()!=null){
180 	for (String fieldName : solrDocument.getFieldNames()) {
181 	    if (fieldName.startsWith(fieldNamePrefix)) {
182 		for (Object fieldValue : solrDocument.getFieldValues(fieldName)) {
183 		    String fieldValueString = (String) fieldValue;
184 		    String languageCode = fieldName.substring(fieldName
185 			    .lastIndexOf("_") + 1);
186 		    List<String> languageList = result.get(languageCode);
187 		    if (languageList == null) {
188 			languageList = new ArrayList<String>();
189 			result.put(languageCode, languageList);
190 		    }
191 		    languageList.add(fieldValueString);
192 		}
193 	    }
194 	}
195 	}
196 	return result;
197     }
198 
199     private List<String> getFieldsToList(SolrDocument solrDocument,
200 	    String fieldname) {
201 	List<String> list = new ArrayList<String>();
202 	if (solrDocument.getFieldValues(fieldname) != null) {
203 	    for (Object o : solrDocument.getFieldValues(fieldname)) {
204 		if (o == null) {
205 		    continue;
206 		} else if (o instanceof String) {
207 		    list.add(o.toString());
208 		} else {
209 		    throw new RepositoryException(fieldname
210 			    + " is not a String but a "
211 			    + o.getClass().getSimpleName());
212 		}
213 	    }
214 	}
215 	return list;
216     }
217     
218     private Set<String> getFieldsToSet(SolrDocument solrDocument,
219     	    String fieldname) {
220     	Set<String> set = new HashSet<String>();
221     	if (solrDocument.getFieldValues(fieldname) != null) {
222     	    for (Object o : solrDocument.getFieldValues(fieldname)) {
223     		if (o == null) {
224     		    continue;
225     		} else if (o instanceof String) {
226     		    set.add(o.toString());
227     		} else {
228     		    throw new RepositoryException(fieldname
229     			    + " is not a String but a "
230     			    + o.getClass().getSimpleName());
231     		}
232     	    }
233     	}
234     	return set;
235         }
236 
237     private Integer getFieldAsInteger(SolrDocument solrDocument,
238 	    String fieldname) {
239 	Object o = solrDocument.getFieldValue(fieldname);
240 	if (o == null) {
241 	    return null;
242 	} else if (o instanceof Integer) {
243 	    return (Integer) o;
244 	} else {
245 	    throw new RepositoryException(fieldname
246 		    + " is not an Integer but a "
247 		    + o.getClass().getSimpleName());
248 	}
249     }
250     
251 
252     private boolean getFieldAsBoolean(SolrDocument solrDocument,
253 	    String fieldname,boolean defaultValue) {
254 	Object o = solrDocument.getFieldValue(fieldname);
255 	if (o == null) {
256 	    return defaultValue;
257 	} else if (o instanceof Boolean) {
258 	    return (Boolean) o;
259 	} else {
260 	    throw new RepositoryException(fieldname
261 		    + " is not an Integer but a "
262 		    + o.getClass().getSimpleName());
263 	}
264     }
265 
266     @SuppressWarnings("unused")
267     private Float getFieldAsFloat(SolrDocument solrDocument, String fieldname) {
268 	Object o = solrDocument.getFieldValue(fieldname);
269 	if (o == null) {
270 	    return null;
271 	} else if (o instanceof Float) {
272 	    return (Float) o;
273 	} else {
274 	    throw new RepositoryException(fieldname + " is not a Float but a "
275 		    + o.getClass().getSimpleName());
276 	}
277     }
278 
279     private Long getFieldAsLong(SolrDocument solrDocument, String fieldname) {
280 	Object o = solrDocument.getFieldValue(fieldname);
281 	if (o == null) {
282 	    return null;
283 	} else if (o instanceof Long) {
284 	    return (Long) o;
285 	} else {
286 	    throw new RepositoryException(fieldname + " is not a Long but a "
287 		    + o.getClass().getSimpleName());
288 	}
289     }
290 
291     private Double getFieldAsDouble(SolrDocument solrDocument, String fieldname) {
292 	Object o = solrDocument.getFieldValue(fieldname);
293 	if (o == null) {
294 	    return null;
295 	} else if (o instanceof Double) {
296 	    return (Double) o;
297 	} else {
298 	    throw new RepositoryException(fieldname + " is not a Double but a "
299 		    + o.getClass().getSimpleName());
300 	}
301     }
302 
303     private String getFieldAsString(SolrDocument solrDocument, String fieldname) {
304 	Object o = solrDocument.getFieldValue(fieldname);
305 	if (o == null) {
306 	    return null;
307 	} else if (o instanceof String) {
308 	    return (String) o;
309 	} else {
310 	    throw new RepositoryException(fieldname + " is not a String but a "
311 		    + o.getClass().getSimpleName());
312 	}
313     }
314 
315 
316 }