Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 258 Rev 266
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, final double lng,
29
	public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
30
			final 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
							EntiteGeographiqueObservation infos;
-
 
55
							String idLocalite = "";
-
 
56
							String nomCommune = "";
-
 
57
							
54
							JSONObject resultat = responseValue.isObject();
58
							JSONObject resultat = responseValue.isObject();
-
 
59
							if(resultat != null && resultat.containsKey("geonames")) {
-
 
60
								if(resultat.get("geonames").isArray() != null) {
55
							JSONArray tableauCommune = resultat.get("geonames")
61
									JSONArray tableauCommune = resultat.get("geonames")
56
									.isArray();
62
									.isArray();
-
 
63
									if(tableauCommune.get(0) != null) {
-
 
64
										if(tableauCommune.get(0).isObject() != null) {
57
							JSONObject objetCommune = tableauCommune.get(0)
65
											JSONObject objetCommune = tableauCommune.get(0)
58
									.isObject();
66
											.isObject();
59
 
67
											
60
							/*String valeurCommune = objetCommune.get("name")
68
											if(objetCommune.containsKey("name")) {
61
									.isString().stringValue();
69
												if(objetCommune.get("name").isString() != null)  {
62
							r.rafraichir(valeurCommune, false);*/
70
													nomCommune = objetCommune.get(
63
							
-
 
64
							String pays = objetCommune.get("countryName").isString()
71
													"name").isString().stringValue();
65
							.stringValue();
72
												}
-
 
73
											}
66
							
74
											
67
							if(!pays.equals("France")){
75
											if(objetCommune.containsKey("adminCode2")) {
68
								r.rafraichir("Aucune commune française n'a été trouvée à cet emplacement", false);
76
												if(objetCommune.get("adminCode2").isString() != null)  {
-
 
77
													idLocalite = objetCommune.get(
-
 
78
													"adminCode2").isString().stringValue();
69
								return;
79
												}
-
 
80
											}
-
 
81
										}
-
 
82
									}
-
 
83
								}
70
							}
84
							}
71
								
-
 
72
							Double newLng = objetCommune.get("lng").isNumber()
-
 
73
							.doubleValue();
-
 
74
							Double newLat = objetCommune.get("lat").isNumber()
-
 
75
									.doubleValue();
-
 
76
							String nomCommune = objetCommune.get(
-
 
77
									"name").isString().stringValue();
-
 
78
							
85
							
79
							String idLocalite = objetCommune.get(
86
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
80
							"adminCode2").isString().stringValue();
-
 
81
		
87
							infos.setLat(""+lat);
82
							Object[] infos = { lat, lng, idLocalite, nomCommune, newLat, newLng};
88
							infos.setLon(""+lng);
83
		
89
		
84
							r.rafraichir(infos, false);
90
							r.rafraichir(infos, false);
85
						}
91
						}
86
					}
92
					}
87
				}
93
				}
88
			});
94
			});
89
		} catch (RequestException e) {
95
		} catch (RequestException e) {
90
			Window.alert(e.getMessage());
96
			Window.alert(e.getMessage());
91
		}
97
		}
92
	}
98
	}
93
 
99
 
94
	public void obtenirInfosCommune(final Rafraichissable r,
100
	public void obtenirInfosCommune(final Rafraichissable r,
95
			String valeurCommune, String codePostal) {
101
			String valeurCommune, String codePostal) {
96
		
102
		
97
		codePostal = codePostal.replaceAll("000null", "*");
103
		codePostal = codePostal.replaceAll("000null", "*");
98
		codePostal = codePostal.replaceAll("\"", "");
104
		codePostal = codePostal.replaceAll("\"", "");
99
		
105
		
100
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
106
		valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
101
		valeurCommune = valeurCommune.replaceAll("000null", "*");
107
		valeurCommune = valeurCommune.replaceAll("000null", "*");
102
		valeurCommune = valeurCommune.replaceAll("\"", "");
108
		valeurCommune = valeurCommune.replaceAll("\"", "");
103
		
109
		
104
		
110
		
105
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
111
		String adresseAppel = Configuration.getServiceBaseUrl() + "/"
106
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
112
				+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
107
				+ URL.encode(codePostal) + "/"
113
				+ URL.encode(codePostal) + "/"
108
				+ URL.encode(CODE_PAYS);
114
				+ URL.encode(CODE_PAYS);
109
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
115
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
110
 
116
 
111
		try {
117
		try {
112
			rb.sendRequest(null, new RequestCallback() {
118
			rb.sendRequest(null, new RequestCallback() {
113
 
119
 
114
				public void onError(Request request, Throwable exception) {
120
				public void onError(Request request, Throwable exception) {
115
					Window.alert(exception.getMessage());
121
					Window.alert(exception.getMessage());
116
				}
122
				}
117
 
123
 
118
				public void onResponseReceived(Request request,
124
				public void onResponseReceived(Request request,
119
						Response response) {
125
						Response response) {
120
 
126
 
121
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
127
					if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
122
						r.rafraichir("Impossible de géolocaliser cette observation", false);
128
						r.rafraichir("Impossible de géolocaliser cette observation", false);
123
					} else {
129
					} else {
124
						final JSONValue responseValue = JSONParser
130
						final JSONValue responseValue = JSONParser
125
								.parse(response.getText());
131
								.parse(response.getText());
126
 
132
 
127
						if (responseValue.isObject() != null) {
133
						if (responseValue.isObject() != null) {
-
 
134
							
-
 
135
							EntiteGeographiqueObservation infos;
-
 
136
							String idLocalite = "";
-
 
137
							String nomCommune = "";
-
 
138
							Double lng = 0.0;
-
 
139
							Double lat = 0.0;
128
 
140
 
-
 
141
							JSONObject resultat = responseValue.isObject();
-
 
142
							
-
 
143
							if(resultat != null && resultat.containsKey("postalCodes")) {
129
							JSONObject resultat = responseValue.isObject();
144
								if(resultat.get("postalCodes").isArray() != null) {
130
							JSONArray tableauCommune = resultat.get(
145
									JSONArray tableauCommune = resultat.get("postalCodes")
131
									"postalCodes").isArray();
-
 
132
 
146
									.isArray();
-
 
147
									if(tableauCommune.get(0) != null) {
133
							if (tableauCommune.get(0) != null) {
148
										if(tableauCommune.get(0).isObject() != null) {
134
								JSONObject objetCommune = tableauCommune.get(0)
149
											JSONObject objetCommune = tableauCommune.get(0)
135
										.isObject();
150
											.isObject();
-
 
151
											
136
 
152
											if(objetCommune.containsKey("lng")) {
-
 
153
												if(objetCommune.get("lng").isNumber() != null)  {
137
								Double lng = objetCommune.get("lng").isNumber()
154
													lng = objetCommune.get(
-
 
155
													"lng").isNumber().doubleValue();
-
 
156
												}
-
 
157
											}
138
										.doubleValue();
158
											
139
								Double lat = objetCommune.get("lat").isNumber()
159
											if(objetCommune.containsKey("lat")) {
140
										.doubleValue();
160
												if(objetCommune.get("lat").isNumber() != null)  {
141
								String nomCommune = objetCommune.get(
161
													lat = objetCommune.get(
-
 
162
													"lat").isNumber().doubleValue();
-
 
163
												}
142
										"placeName").isString().stringValue();
164
											}
-
 
165
											
-
 
166
											if(objetCommune.containsKey("adminCode2")) {
143
								
167
												if(objetCommune.get("adminCode2").isString() != null)  {
144
								String idLocalite = objetCommune.get(
168
													idLocalite = objetCommune.get(
-
 
169
													"adminCode2").isString().stringValue();
-
 
170
												}
145
								"adminCode2").isString().stringValue();
171
											}
-
 
172
											
-
 
173
											if(objetCommune.containsKey("placeName")) {
146
 
174
												if(objetCommune.get("placeName").isString() != null)  {
147
								Object[] infos = { lat, lng, idLocalite, nomCommune};
-
 
148
 
-
 
149
 
175
													nomCommune = objetCommune.get(
-
 
176
													"placeName").isString().stringValue();
-
 
177
												}
-
 
178
											}
-
 
179
										}
-
 
180
									}
150
								r.rafraichir(infos, false);
181
								}
-
 
182
							}
-
 
183
							
-
 
184
							infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
-
 
185
							infos.setLat(""+lat);
-
 
186
							infos.setLon(""+lng);
-
 
187
		
151
							}
188
							r.rafraichir(infos, false);
152
						}
189
						}
153
					}
190
					}
154
				}
191
				}
155
			});
192
			});
156
		} catch (RequestException e) {
193
		} catch (RequestException e) {
157
			Window.alert(e.getMessage());
194
			Window.alert(e.getMessage());
158
		}
195
		}
159
	}
196
	}
160
}
197
}