Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 1546 → Rev 1547

/branches/v1.6-croc/src/org/tela_botanica/client/modeles/dao/InformationCommuneDAO.java
New file
0,0 → 1,167
package org.tela_botanica.client.modeles.dao;
 
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 com.google.gwt.http.client.RequestBuilder;
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 InformationCommuneDAO {
 
private final String NOM_SERVICE = "CoordSearch";
private final String CODE_PAYS = "FR";
 
Rafraichissable r = null;
 
public InformationCommuneDAO(Rafraichissable r) {
this.r = r;
}
 
public void obtenirCommunePlusProche(final Rafraichissable r, final double lng,
final double lat) {
 
String adresseAppel = Configuration.getServiceBaseUrl() + "/"
+ NOM_SERVICE + "/" + URL.encode("" + lat) + "/"
+ URL.encode("" + lng) + "/*/*/";
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
 
try {
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) {
EntiteGeographiqueObservation infos;
String idLocalite = "";
String nomCommune = "";
 
if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
Window.alert("Requete mal formée");
} else {
if(response.getText().equals("")) {
 
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, 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) {
nomCommune = objectRetour.get("nom").isString().stringValue();
}
if(objectRetour.get("code_insee").isString() != null) {
idLocalite = objectRetour.get("code_insee").isString().stringValue().substring(0, 2);
}
}
}
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
infos.setLat(""+lat);
infos.setLon(""+lng);
 
r.rafraichir(infos, false);
}
});
} catch (RequestException e) {
Window.alert(e.getMessage());
}
}
 
public void obtenirInfosCommune(final Rafraichissable r,
String valeurCommune, String codePostal) {
codePostal = codePostal.replaceAll("000null", "*");
codePostal = codePostal.replaceAll("\"", "");
valeurCommune = valeurCommune.split(" \\([0-9][0-9]\\)")[0];
valeurCommune = valeurCommune.replaceAll("000null", "*");
valeurCommune = valeurCommune.replaceAll("\"", "");
String adresseAppel = Configuration.getServiceBaseUrl() + "/"
+ NOM_SERVICE + "/*/*/" + URL.encode(valeurCommune) + "/"
+ URL.encode(codePostal) + "/"
+ URL.encode(CODE_PAYS);
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, adresseAppel);
 
try {
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("Impossible de géolocaliser cette observation", 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 nomCommune = Util.getValeurJsonOuVide(objectRetour, "nom");
String idLocalite = Util.getValeurJsonOuVide(objectRetour, "code_insee");
lat = Util.jsonNonNull(objectRetour,"lat") ? objectRetour.get("lat").isNumber().doubleValue(): 0.0;
lng = Util.jsonNonNull(objectRetour,"lng") ? objectRetour.get("lng").isNumber().doubleValue(): 0.0;
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
infos.setLat(""+lat);
infos.setLon(""+lng);
r.rafraichir(infos, false);
} else {
EntiteGeographiqueObservation infos;
String idLocalite = "";
String nomCommune = "";
 
infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
infos.setLat("");
infos.setLon("");
r.rafraichir(infos, false);
}
}
}
});
} catch (RequestException e) {
Window.alert(e.getMessage());
}
}
}