Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | 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
import org.tela_botanica.client.observation.ObservationModele;
5
 
6
import com.google.gwt.http.client.Request;
7
import com.google.gwt.http.client.RequestBuilder;
8
import com.google.gwt.http.client.RequestCallback;
9
import com.google.gwt.http.client.RequestException;
10
import com.google.gwt.http.client.Response;
11
import com.google.gwt.http.client.URL;
12
import com.google.gwt.json.client.JSONArray;
13
import com.google.gwt.json.client.JSONObject;
14
import com.google.gwt.json.client.JSONParser;
15
import com.google.gwt.json.client.JSONValue;
16
import com.google.gwt.maps.client.geom.LatLng;
17
import com.google.gwt.user.client.Window;
18
 
19
 
20
 
21
public class InformationCommuneDAO {
22
 
23
	private final String NOM_SERVICE = "CoordSearch";
24
	private final String CODE_PAYS = "FR";
25
 
26
	ObservationModele oModele = null;
27
 
28
	public InformationCommuneDAO(ObservationModele m) {
29
		oModele = m ;
30
	}
31
 
32
	public void obtenirCommunePlusProche(final Rafraichissable r, double lng,double lat) {
33
 
34
	      String adresseAppel = Configuration.getServiceBaseUrl()+"/"+NOM_SERVICE+"/"+URL.encode(""+lat)+"/"+URL.encode(""+lng)+"/*/*/";
35
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,URL.encode(adresseAppel));
36
 
37
		try {
38
				rb.sendRequest(null, new RequestCallback() {
39
 
40
				public void onError(Request request, Throwable exception) {
41
					Window.alert(exception.getMessage());
42
				}
43
 
44
				public void onResponseReceived(Request request, Response response) {
45
 
46
					if(response.getStatusCode() == Response.SC_BAD_REQUEST) {
47
						Window.alert("Requete mal formée");
48
					}
49
					else {
50
						final JSONValue responseValue = JSONParser.parse(response.getText());
51
 
52
						if(responseValue.isObject() != null) {
53
							JSONObject resultat = responseValue.isObject() ;
54
							JSONArray tableauCommune = resultat.get("geonames").isArray();
55
							JSONObject objetCommune = tableauCommune.get(0).isObject();
56
 
57
							String valeurCommune = objetCommune.get("name").isString().stringValue();
58
							r.rafraichir(valeurCommune, false);
59
						}
60
					}
61
				}
62
			});
63
		}
64
		catch (RequestException e) {
65
			Window.alert(e.getMessage());
66
		}
67
	}
68
 
69
 
70
 
71
	public void obtenirInfosCommune(final Rafraichissable r, String valeurCommune) {
72
 
73
		String adresseAppel = Configuration.getServiceBaseUrl()+"/"+NOM_SERVICE+"/*/*/"+URL.encode(valeurCommune)+"/"+CODE_PAYS;
74
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,URL.encode(adresseAppel));
75
 
76
		try {
77
			rb.sendRequest(null, new RequestCallback() {
78
 
79
				public void onError(Request request, Throwable exception) {
80
					Window.alert(exception.getMessage());
81
				}
82
 
83
				public void onResponseReceived(Request request, Response response) {
84
 
85
					if(response.getStatusCode() == Response.SC_BAD_REQUEST) {
86
						Window.alert("Requete mal formée");
87
					}
88
					else {
89
						final JSONValue responseValue = JSONParser.parse(response.getText());
90
 
91
						if(responseValue.isObject() != null) {
92
							JSONObject resultat = responseValue.isObject() ;
93
							JSONArray tableauCommune = resultat.get("postalCodes").isArray();
94
 
95
							if(tableauCommune.get(0) != null) {
96
								JSONObject objetCommune = tableauCommune.get(0).isObject();
97
 
98
								double lng = objetCommune.get("lng").isNumber().doubleValue();
99
								double lat = objetCommune.get("lat").isNumber().doubleValue();
100
								String nomCommune = objetCommune.get("postalCode").isString().stringValue();
101
 
102
								r.rafraichir(nomCommune, false);
103
							}
104
						}
105
					}
106
				}
107
			});
108
		}
109
		catch (RequestException e) {
110
			Window.alert(e.getMessage());
111
		}
112
	}
113
}
114