| 239 |
aurelien |
1 |
package org.tela_botanica.client.modeles;
|
|
|
2 |
|
|
|
3 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
|
|
4 |
|
| 779 |
aurelien |
5 |
import com.google.gwt.core.client.GWT;
|
| 239 |
aurelien |
6 |
import com.google.gwt.http.client.Request;
|
|
|
7 |
import com.google.gwt.http.client.RequestBuilder;
|
|
|
8 |
import com.google.gwt.http.client.RequestCallback;
|
|
|
9 |
import com.google.gwt.http.client.RequestException;
|
|
|
10 |
import com.google.gwt.http.client.Response;
|
|
|
11 |
import com.google.gwt.http.client.URL;
|
|
|
12 |
import com.google.gwt.json.client.JSONArray;
|
|
|
13 |
import com.google.gwt.json.client.JSONObject;
|
|
|
14 |
import com.google.gwt.json.client.JSONParser;
|
|
|
15 |
import com.google.gwt.json.client.JSONValue;
|
|
|
16 |
import com.google.gwt.maps.client.geom.LatLng;
|
|
|
17 |
import com.google.gwt.user.client.Window;
|
|
|
18 |
|
| 253 |
aurelien |
19 |
public class InformationCommuneDAO {
|
| 239 |
aurelien |
20 |
|
|
|
21 |
private final String NOM_SERVICE = "CoordSearch";
|
|
|
22 |
private final String CODE_PAYS = "FR";
|
| 253 |
aurelien |
23 |
|
|
|
24 |
Rafraichissable r = null;
|
|
|
25 |
|
|
|
26 |
public InformationCommuneDAO(Rafraichissable r) {
|
|
|
27 |
this.r = r;
|
| 239 |
aurelien |
28 |
}
|
|
|
29 |
|
| 258 |
aurelien |
30 |
public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
|
|
|
31 |
final double lat) {
|
| 253 |
aurelien |
32 |
|
|
|
33 |
String adresseAppel = Configuration.getServiceBaseUrl() + "/"
|
|
|
34 |
+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
|
|
|
35 |
+ URL.encode("" + lng) + "/*/*/";
|
|
|
36 |
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
|
|
|
37 |
|
| 239 |
aurelien |
38 |
try {
|
| 253 |
aurelien |
39 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
40 |
|
|
|
41 |
public void onError(Request request, Throwable exception) {
|
| 239 |
aurelien |
42 |
Window.alert(exception.getMessage());
|
|
|
43 |
}
|
| 253 |
aurelien |
44 |
|
|
|
45 |
public void onResponseReceived(Request request,
|
|
|
46 |
Response response) {
|
| 751 |
aurelien |
47 |
|
|
|
48 |
EntiteGeographiqueObservation infos;
|
|
|
49 |
String idLocalite = "";
|
|
|
50 |
String nomCommune = "";
|
| 253 |
aurelien |
51 |
|
|
|
52 |
if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
|
| 239 |
aurelien |
53 |
Window.alert("Requete mal formée");
|
| 253 |
aurelien |
54 |
} else {
|
| 646 |
aurelien |
55 |
|
|
|
56 |
if(response.getText().equals("")) {
|
|
|
57 |
|
|
|
58 |
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
|
|
|
59 |
infos.setLat(""+lat);
|
|
|
60 |
infos.setLon(""+lng);
|
|
|
61 |
|
|
|
62 |
r.rafraichir(infos, false);
|
|
|
63 |
}
|
|
|
64 |
|
| 253 |
aurelien |
65 |
final JSONValue responseValue = JSONParser
|
|
|
66 |
.parse(response.getText());
|
|
|
67 |
|
|
|
68 |
if (responseValue.isObject() != null) {
|
| 266 |
aurelien |
69 |
|
| 751 |
aurelien |
70 |
JSONObject objectRetour = responseValue.isObject();
|
|
|
71 |
|
|
|
72 |
if(objectRetour.get("nom").isString() != null) {
|
|
|
73 |
nomCommune = objectRetour.get("nom").isString().stringValue();
|
| 253 |
aurelien |
74 |
}
|
|
|
75 |
|
| 751 |
aurelien |
76 |
if(objectRetour.get("code_insee").isString() != null) {
|
|
|
77 |
idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
|
|
|
78 |
}
|
| 239 |
aurelien |
79 |
}
|
|
|
80 |
}
|
| 751 |
aurelien |
81 |
|
|
|
82 |
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
|
|
|
83 |
infos.setLat(""+lat);
|
|
|
84 |
infos.setLon(""+lng);
|
|
|
85 |
|
|
|
86 |
r.rafraichir(infos, false);
|
| 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 {
|
| 646 |
aurelien |
124 |
|
|
|
125 |
if(response.getText().equals("")) {
|
|
|
126 |
EntiteGeographiqueObservation infos;
|
|
|
127 |
String idLocalite = "";
|
|
|
128 |
String nomCommune = "";
|
|
|
129 |
|
|
|
130 |
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
|
|
|
131 |
infos.setLat("");
|
|
|
132 |
infos.setLon("");
|
|
|
133 |
|
|
|
134 |
r.rafraichir(infos, false);
|
|
|
135 |
}
|
| 779 |
aurelien |
136 |
|
| 253 |
aurelien |
137 |
final JSONValue responseValue = JSONParser
|
|
|
138 |
.parse(response.getText());
|
| 779 |
aurelien |
139 |
|
| 253 |
aurelien |
140 |
if (responseValue.isObject() != null) {
|
| 266 |
aurelien |
141 |
|
|
|
142 |
EntiteGeographiqueObservation infos;
|
|
|
143 |
String idLocalite = "";
|
|
|
144 |
String nomCommune = "";
|
|
|
145 |
Double lng = 0.0;
|
|
|
146 |
Double lat = 0.0;
|
| 779 |
aurelien |
147 |
|
|
|
148 |
JSONObject objectRetour = responseValue.isObject();
|
| 266 |
aurelien |
149 |
|
| 779 |
aurelien |
150 |
if(objectRetour.get("nom").isString() != null) {
|
|
|
151 |
nomCommune = objectRetour.get("nom").isString().stringValue();
|
| 239 |
aurelien |
152 |
}
|
| 266 |
aurelien |
153 |
|
| 779 |
aurelien |
154 |
if(objectRetour.get("code_insee").isString() != null) {
|
|
|
155 |
idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if(objectRetour.get("lat").isNumber() != null) {
|
|
|
159 |
lat = objectRetour.get("lat").isNumber().doubleValue();
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
if(objectRetour.get("lng").isNumber() != null) {
|
|
|
163 |
lng = objectRetour.get("lng").isNumber().doubleValue();
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
|
| 266 |
aurelien |
167 |
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
|
|
|
168 |
infos.setLat(""+lat);
|
|
|
169 |
infos.setLon(""+lng);
|
| 779 |
aurelien |
170 |
|
| 266 |
aurelien |
171 |
r.rafraichir(infos, false);
|
| 239 |
aurelien |
172 |
}
|
|
|
173 |
}
|
| 253 |
aurelien |
174 |
}
|
| 239 |
aurelien |
175 |
});
|
| 253 |
aurelien |
176 |
} catch (RequestException e) {
|
| 239 |
aurelien |
177 |
Window.alert(e.getMessage());
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
}
|