New file |
0,0 → 1,201 |
package org.tela_botanica.client.modeles.dao; |
|
import org.tela_botanica.client.i18n.Msg; |
import org.tela_botanica.client.interfaces.Rafraichissable; |
import org.tela_botanica.client.modeles.objets.Configuration; |
import org.tela_botanica.client.modeles.objets.EntiteGeographiqueObservation; |
import org.tela_botanica.client.util.Util; |
|
import com.google.gwt.http.client.Request; |
import org.tela_botanica.client.util.RequestBuilderWithCredentials; |
import com.google.gwt.http.client.RequestCallback; |
import com.google.gwt.http.client.RequestException; |
import com.google.gwt.http.client.Response; |
import com.google.gwt.http.client.URL; |
import com.google.gwt.json.client.JSONObject; |
import com.google.gwt.json.client.JSONParser; |
import com.google.gwt.json.client.JSONValue; |
import com.google.gwt.user.client.Window; |
|
public class InformationLocaliteDAO { |
|
private final String NOM_SERVICE = "CoordSearch"; |
private final String CODE_PAYS = "FR"; |
|
// Ce DAO peut être fréquemment sollicité lors de l'utilisation de la carte |
// et peut empiler beaucoup de longues requête, ceci permet donc de les annuler facilement |
private static Request requeteEnCours = null; |
|
Rafraichissable r = null; |
|
public InformationLocaliteDAO(Rafraichissable r) { |
this.r = r; |
} |
|
public void obtenirLocalitePlusProche(final Rafraichissable r, final double lng, |
final double lat) { |
|
annulerRequeteEnCours(); |
|
String adresseAppel = Configuration.getServiceBaseUrl() + "/" |
+ NOM_SERVICE + "?lat="+URL.encode("" + lat) + "&lon="+URL.encode("" + lng); |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel); |
|
try { |
requeteEnCours = rb.sendRequest(null, new RequestCallback() { |
|
@Override |
public void onError(Request request, Throwable exception) { |
Window.alert(exception.getMessage()); |
requeteEnCours = null; |
} |
|
@Override |
public void onResponseReceived(Request request, |
Response response) { |
|
EntiteGeographiqueObservation infos; |
String idLocalite = ""; |
String nomLocalite = ""; |
String pays = ""; |
|
if (response.getStatusCode() == Response.SC_BAD_REQUEST) { |
Window.alert(Msg.get("requete-mal-formee")); |
} else { |
|
if(response.getText().equals("")) { |
|
infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null); |
infos.setLat(""+lat); |
infos.setLon(""+lng); |
|
r.rafraichir(infos, false); |
} |
|
final JSONValue responseValue = JSONParser |
.parse(response.getText()); |
|
if (responseValue.isObject() != null) { |
|
JSONObject objectRetour = responseValue.isObject(); |
|
if(objectRetour.get("nom").isString() != null) { |
nomLocalite = objectRetour.get("nom").isString().stringValue(); |
} |
|
if(objectRetour.get("code_zone").isString() != null) { |
idLocalite = objectRetour.get("code_zone").isString().stringValue().substring(0, 2); |
} |
|
if(objectRetour.get("code_pays").isString() != null) { |
pays = objectRetour.get("code_pays").isString().stringValue(); |
} |
} |
} |
|
infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null); |
infos.setLat(""+lat); |
infos.setLon(""+lng); |
infos.setPays(pays); |
|
r.rafraichir(infos, false); |
} |
}); |
} catch (RequestException e) { |
Window.alert(e.getMessage()); |
} |
} |
|
public void obtenirInfosLocalite(final Rafraichissable r, |
String valeurLocalite, String codeLoc) { |
|
annulerRequeteEnCours(); |
|
codeLoc = codeLoc.replaceAll("000null", "*"); |
codeLoc = codeLoc.replaceAll("\"", ""); |
|
valeurLocalite = valeurLocalite.split(" \\([0-9][0-9]\\)")[0]; |
valeurLocalite = valeurLocalite.replaceAll("000null", "*"); |
valeurLocalite = valeurLocalite.replaceAll("\"", ""); |
|
|
String adresseAppel = Configuration.getServiceBaseUrl() + "/" |
+ NOM_SERVICE+"?zone="+ URL.encode(valeurLocalite); |
|
if(!codeLoc.trim().isEmpty()) { |
// Cas du code de département ou postal |
if(Util.estUnNombre(codeLoc)) { |
adresseAppel += "&code="+URL.encode(codeLoc)+"&pays="+URL.encode(CODE_PAYS); |
} else { |
//cas du code pays |
adresseAppel += "&pays="+URL.encode(codeLoc); |
} |
|
} |
|
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, adresseAppel); |
|
try { |
requeteEnCours = rb.sendRequest(null, new RequestCallback() { |
|
@Override |
public void onError(Request request, Throwable exception) { |
Window.alert(exception.getMessage()); |
} |
|
@Override |
public void onResponseReceived(Request request, |
Response response) { |
|
if (response.getStatusCode() == Response.SC_BAD_REQUEST) { |
r.rafraichir(Msg.get("impossible-geolocaliser-obs"), false); |
} else { |
final JSONValue responseValue = JSONParser |
.parse(response.getText()); |
|
if (responseValue.isObject() != null) { |
|
EntiteGeographiqueObservation infos; |
Double lng = 0.0; |
Double lat = 0.0; |
|
JSONObject objectRetour = responseValue.isObject(); |
|
String nomLocalite = Util.getValeurJsonOuVide(objectRetour, "nom"); |
String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_zone"); |
lat = Util.jsonNonNull(objectRetour,"lat") ? objectRetour.get("lat").isNumber().doubleValue(): 0.0; |
lng = Util.jsonNonNull(objectRetour,"lng") ? objectRetour.get("lng").isNumber().doubleValue(): 0.0; |
String pays = Util.getValeurJsonOuVide(objectRetour, "code_pays"); |
|
infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null); |
infos.setLat(""+lat); |
infos.setLon(""+lng); |
infos.setPays(pays); |
|
r.rafraichir(infos, false); |
} else { |
EntiteGeographiqueObservation infos; |
String idLocalite = ""; |
String nomLocalite = ""; |
|
infos = new EntiteGeographiqueObservation(idLocalite, nomLocalite, null, null); |
infos.setLat(""); |
infos.setLon(""); |
|
r.rafraichir(infos, false); |
} |
} |
} |
}); |
} catch (RequestException e) { |
Window.alert(e.getMessage()); |
} |
} |
|
public static void annulerRequeteEnCours() { |
if(requeteEnCours != null) { |
requeteEnCours.cancel(); |
requeteEnCours = null; |
} |
} |
} |