Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
239 aurelien 1
package org.tela_botanica.client.modeles;
2
 
3
import org.tela_botanica.client.interfaces.Rafraichissable;
4
 
5
import com.google.gwt.http.client.Request;
6
import com.google.gwt.http.client.RequestBuilder;
7
import com.google.gwt.http.client.RequestCallback;
8
import com.google.gwt.http.client.RequestException;
9
import com.google.gwt.http.client.Response;
10
import com.google.gwt.http.client.URL;
11
import com.google.gwt.json.client.JSONArray;
12
import com.google.gwt.json.client.JSONObject;
13
import com.google.gwt.json.client.JSONParser;
14
import com.google.gwt.json.client.JSONValue;
15
import com.google.gwt.maps.client.geom.LatLng;
16
import com.google.gwt.user.client.Window;
17
 
253 aurelien 18
public class InformationCommuneDAO {
239 aurelien 19
 
20
	private final String NOM_SERVICE = "CoordSearch";
21
	private final String CODE_PAYS = "FR";
253 aurelien 22
 
23
	Rafraichissable r = null;
24
 
25
	public InformationCommuneDAO(Rafraichissable r) {
26
		this.r = r;
239 aurelien 27
	}
28
 
258 aurelien 29
	public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
30
			final double lat) {
253 aurelien 31
 
32
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
33
				+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
34
				+ URL.encode("" + lng) + "/*/*/";
35
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
36
 
239 aurelien 37
		try {
253 aurelien 38
			rb.sendRequest(null, new RequestCallback() {
39
 
40
				public void onError(Request request, Throwable exception) {
239 aurelien 41
					Window.alert(exception.getMessage());
42
				}
253 aurelien 43
 
44
				public void onResponseReceived(Request request,
45
						Response response) {
46
 
47
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
239 aurelien 48
						Window.alert("Requete mal formée");
253 aurelien 49
					} else {
50
						final JSONValue responseValue = JSONParser
51
								.parse(response.getText());
52
 
53
						if (responseValue.isObject() != null) {
266 aurelien 54
							EntiteGeographiqueObservation infos;
55
							String idLocalite = "";
56
							String nomCommune = "";
57
 
253 aurelien 58
							JSONObject resultat = responseValue.isObject();
266 aurelien 59
							if(resultat != null && resultat.containsKey("geonames")) {
60
								if(resultat.get("geonames").isArray() != null) {
61
									JSONArray tableauCommune = resultat.get("geonames")
253 aurelien 62
									.isArray();
266 aurelien 63
									if(tableauCommune.get(0) != null) {
64
										if(tableauCommune.get(0).isObject() != null) {
65
											JSONObject objetCommune = tableauCommune.get(0)
66
											.isObject();
67
 
68
											if(objetCommune.containsKey("name")) {
69
												if(objetCommune.get("name").isString() != null)  {
70
													nomCommune = objetCommune.get(
71
													"name").isString().stringValue();
72
												}
73
											}
74
 
75
											if(objetCommune.containsKey("adminCode2")) {
76
												if(objetCommune.get("adminCode2").isString() != null)  {
77
													idLocalite = objetCommune.get(
78
													"adminCode2").isString().stringValue();
79
												}
80
											}
81
										}
82
									}
83
								}
253 aurelien 84
							}
85
 
266 aurelien 86
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
87
							infos.setLat(""+lat);
88
							infos.setLon(""+lng);
253 aurelien 89
 
90
							r.rafraichir(infos, false);
239 aurelien 91
						}
92
					}
253 aurelien 93
				}
239 aurelien 94
			});
253 aurelien 95
		} catch (RequestException e) {
239 aurelien 96
			Window.alert(e.getMessage());
97
		}
98
	}
99
 
253 aurelien 100
	public void obtenirInfosCommune(final Rafraichissable r,
101
			String valeurCommune, String codePostal) {
102
 
103
		codePostal = codePostal.replaceAll("000null", "*");
104
		codePostal = codePostal.replaceAll("\"", "");
258 aurelien 105
 
106
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
253 aurelien 107
		valeurCommune = valeurCommune.replaceAll("000null", "*");
108
		valeurCommune = valeurCommune.replaceAll("\"", "");
109
 
110
 
111
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
112
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
113
				+ URL.encode(codePostal) + "/"
114
				+ URL.encode(CODE_PAYS);
115
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
239 aurelien 116
 
117
		try {
118
			rb.sendRequest(null, new RequestCallback() {
253 aurelien 119
 
239 aurelien 120
				public void onError(Request request, Throwable exception) {
253 aurelien 121
					Window.alert(exception.getMessage());
239 aurelien 122
				}
253 aurelien 123
 
124
				public void onResponseReceived(Request request,
125
						Response response) {
126
 
127
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
128
						r.rafraichir("Impossible de géolocaliser cette observation", false);
129
					} else {
130
						final JSONValue responseValue = JSONParser
131
								.parse(response.getText());
132
 
133
						if (responseValue.isObject() != null) {
266 aurelien 134
 
135
							EntiteGeographiqueObservation infos;
136
							String idLocalite = "";
137
							String nomCommune = "";
138
							Double lng = 0.0;
139
							Double lat = 0.0;
253 aurelien 140
 
141
							JSONObject resultat = responseValue.isObject();
266 aurelien 142
 
143
							if(resultat != null && resultat.containsKey("postalCodes")) {
144
								if(resultat.get("postalCodes").isArray() != null) {
145
									JSONArray tableauCommune = resultat.get("postalCodes")
146
									.isArray();
147
									if(tableauCommune.get(0) != null) {
148
										if(tableauCommune.get(0).isObject() != null) {
149
											JSONObject objetCommune = tableauCommune.get(0)
150
											.isObject();
151
 
152
											if(objetCommune.containsKey("lng")) {
153
												if(objetCommune.get("lng").isNumber() != null)  {
154
													lng = objetCommune.get(
155
													"lng").isNumber().doubleValue();
156
												}
157
											}
158
 
159
											if(objetCommune.containsKey("lat")) {
160
												if(objetCommune.get("lat").isNumber() != null)  {
161
													lat = objetCommune.get(
162
													"lat").isNumber().doubleValue();
163
												}
164
											}
165
 
166
											if(objetCommune.containsKey("adminCode2")) {
167
												if(objetCommune.get("adminCode2").isString() != null)  {
168
													idLocalite = objetCommune.get(
169
													"adminCode2").isString().stringValue();
170
												}
171
											}
172
 
173
											if(objetCommune.containsKey("placeName")) {
174
												if(objetCommune.get("placeName").isString() != null)  {
175
													nomCommune = objetCommune.get(
176
													"placeName").isString().stringValue();
177
												}
178
											}
179
										}
180
									}
181
								}
239 aurelien 182
							}
266 aurelien 183
 
184
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
185
							infos.setLat(""+lat);
186
							infos.setLon(""+lng);
187
 
188
							r.rafraichir(infos, false);
239 aurelien 189
						}
190
					}
253 aurelien 191
				}
239 aurelien 192
			});
253 aurelien 193
		} catch (RequestException e) {
239 aurelien 194
			Window.alert(e.getMessage());
195
		}
196
	}
197
}