Subversion Repositories eFlore/Applications.cel

Rev

Rev 258 | Blame | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles;

import org.tela_botanica.client.interfaces.Rafraichissable;

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.JSONArray;
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.maps.client.geom.LatLng;
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() {

                                public void onError(Request request, Throwable exception) {
                                        Window.alert(exception.getMessage());
                                }

                                public void onResponseReceived(Request request,
                                                Response response) {

                                        if (response.getStatusCode() == Response.SC_BAD_REQUEST) {
                                                Window.alert("Requete mal formée");
                                        } else {
                                                final JSONValue responseValue = JSONParser
                                                                .parse(response.getText());

                                                if (responseValue.isObject() != null) {
                                                        EntiteGeographiqueObservation infos;
                                                        String idLocalite = "";
                                                        String nomCommune = "";
                                                        
                                                        JSONObject resultat = responseValue.isObject();
                                                        if(resultat != null && resultat.containsKey("geonames")) {
                                                                if(resultat.get("geonames").isArray() != null) {
                                                                        JSONArray tableauCommune = resultat.get("geonames")
                                                                        .isArray();
                                                                        if(tableauCommune.get(0) != null) {
                                                                                if(tableauCommune.get(0).isObject() != null) {
                                                                                        JSONObject objetCommune = tableauCommune.get(0)
                                                                                        .isObject();
                                                                                        
                                                                                        if(objetCommune.containsKey("name")) {
                                                                                                if(objetCommune.get("name").isString() != null)  {
                                                                                                        nomCommune = objetCommune.get(
                                                                                                        "name").isString().stringValue();
                                                                                                }
                                                                                        }
                                                                                        
                                                                                        if(objetCommune.containsKey("adminCode2")) {
                                                                                                if(objetCommune.get("adminCode2").isString() != null)  {
                                                                                                        idLocalite = objetCommune.get(
                                                                                                        "adminCode2").isString().stringValue();
                                                                                                }
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        
                                                        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() {

                                public void onError(Request request, Throwable exception) {
                                        Window.alert(exception.getMessage());
                                }

                                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;
                                                        String idLocalite = "";
                                                        String nomCommune = "";
                                                        Double lng = 0.0;
                                                        Double lat = 0.0;

                                                        JSONObject resultat = responseValue.isObject();
                                                        
                                                        if(resultat != null && resultat.containsKey("postalCodes")) {
                                                                if(resultat.get("postalCodes").isArray() != null) {
                                                                        JSONArray tableauCommune = resultat.get("postalCodes")
                                                                        .isArray();
                                                                        if(tableauCommune.get(0) != null) {
                                                                                if(tableauCommune.get(0).isObject() != null) {
                                                                                        JSONObject objetCommune = tableauCommune.get(0)
                                                                                        .isObject();
                                                                                        
                                                                                        if(objetCommune.containsKey("lng")) {
                                                                                                if(objetCommune.get("lng").isNumber() != null)  {
                                                                                                        lng = objetCommune.get(
                                                                                                        "lng").isNumber().doubleValue();
                                                                                                }
                                                                                        }
                                                                                        
                                                                                        if(objetCommune.containsKey("lat")) {
                                                                                                if(objetCommune.get("lat").isNumber() != null)  {
                                                                                                        lat = objetCommune.get(
                                                                                                        "lat").isNumber().doubleValue();
                                                                                                }
                                                                                        }
                                                                                        
                                                                                        if(objetCommune.containsKey("adminCode2")) {
                                                                                                if(objetCommune.get("adminCode2").isString() != null)  {
                                                                                                        idLocalite = objetCommune.get(
                                                                                                        "adminCode2").isString().stringValue();
                                                                                                }
                                                                                        }
                                                                                        
                                                                                        if(objetCommune.containsKey("placeName")) {
                                                                                                if(objetCommune.get("placeName").isString() != null)  {
                                                                                                        nomCommune = objetCommune.get(
                                                                                                        "placeName").isString().stringValue();
                                                                                                }
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        }
                                                        
                                                        infos = new EntiteGeographiqueObservation(idLocalite, nomCommune, null, null);
                                                        infos.setLat(""+lat);
                                                        infos.setLon(""+lng);
                
                                                        r.rafraichir(infos, false);
                                                }
                                        }
                                }
                        });
                } catch (RequestException e) {
                        Window.alert(e.getMessage());
                }
        }
}