Subversion Repositories eFlore/Applications.cel

Rev

Rev 253 | Rev 266 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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