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