Subversion Repositories eFlore/Applications.cel

Rev

Rev 253 | Go to most recent revision | Details | Compare with Previous | 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) {
54
							JSONObject resultat = responseValue.isObject();
55
							JSONArray tableauCommune = resultat.get("geonames")
56
									.isArray();
57
							JSONObject objetCommune = tableauCommune.get(0)
58
									.isObject();
59
 
60
							/*String valeurCommune = objetCommune.get("name")
61
									.isString().stringValue();
62
							r.rafraichir(valeurCommune, false);*/
239 aurelien 63
 
253 aurelien 64
							String pays = objetCommune.get("countryName").isString()
65
							.stringValue();
66
 
67
							if(!pays.equals("France")){
68
								r.rafraichir("Aucune commune française n'a été trouvée à cet emplacement", false);
69
								return;
70
							}
71
 
258 aurelien 72
							Double newLng = objetCommune.get("lng").isNumber()
253 aurelien 73
							.doubleValue();
258 aurelien 74
							Double newLat = objetCommune.get("lat").isNumber()
253 aurelien 75
									.doubleValue();
76
							String nomCommune = objetCommune.get(
77
									"name").isString().stringValue();
78
 
79
							String idLocalite = objetCommune.get(
80
							"adminCode2").isString().stringValue();
81
 
258 aurelien 82
							Object[] infos = { lat, lng, idLocalite, nomCommune, newLat, newLng};
253 aurelien 83
 
84
							r.rafraichir(infos, false);
239 aurelien 85
						}
86
					}
253 aurelien 87
				}
239 aurelien 88
			});
253 aurelien 89
		} catch (RequestException e) {
239 aurelien 90
			Window.alert(e.getMessage());
91
		}
92
	}
93
 
253 aurelien 94
	public void obtenirInfosCommune(final Rafraichissable r,
95
			String valeurCommune, String codePostal) {
96
 
97
		codePostal = codePostal.replaceAll("000null", "*");
98
		codePostal = codePostal.replaceAll("\"", "");
258 aurelien 99
 
100
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
253 aurelien 101
		valeurCommune = valeurCommune.replaceAll("000null", "*");
102
		valeurCommune = valeurCommune.replaceAll("\"", "");
103
 
104
 
105
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
106
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
107
				+ URL.encode(codePostal) + "/"
108
				+ URL.encode(CODE_PAYS);
109
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
239 aurelien 110
 
111
		try {
112
			rb.sendRequest(null, new RequestCallback() {
253 aurelien 113
 
239 aurelien 114
				public void onError(Request request, Throwable exception) {
253 aurelien 115
					Window.alert(exception.getMessage());
239 aurelien 116
				}
253 aurelien 117
 
118
				public void onResponseReceived(Request request,
119
						Response response) {
120
 
121
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
122
						r.rafraichir("Impossible de géolocaliser cette observation", false);
123
					} else {
124
						final JSONValue responseValue = JSONParser
125
								.parse(response.getText());
126
 
127
						if (responseValue.isObject() != null) {
128
 
129
							JSONObject resultat = responseValue.isObject();
130
							JSONArray tableauCommune = resultat.get(
131
									"postalCodes").isArray();
132
 
133
							if (tableauCommune.get(0) != null) {
134
								JSONObject objetCommune = tableauCommune.get(0)
135
										.isObject();
136
 
137
								Double lng = objetCommune.get("lng").isNumber()
138
										.doubleValue();
139
								Double lat = objetCommune.get("lat").isNumber()
140
										.doubleValue();
141
								String nomCommune = objetCommune.get(
142
										"placeName").isString().stringValue();
239 aurelien 143
 
253 aurelien 144
								String idLocalite = objetCommune.get(
145
								"adminCode2").isString().stringValue();
146
 
147
								Object[] infos = { lat, lng, idLocalite, nomCommune};
148
 
149
 
150
								r.rafraichir(infos, false);
239 aurelien 151
							}
152
						}
153
					}
253 aurelien 154
				}
239 aurelien 155
			});
253 aurelien 156
		} catch (RequestException e) {
239 aurelien 157
			Window.alert(e.getMessage());
158
		}
159
	}
160
}