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