Subversion Repositories eFlore/Applications.cel

Rev

Rev 253 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles;

import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.observation.ObservationModele;

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";
        
        ObservationModele oModele = null;
        
        public InformationCommuneDAO(ObservationModele m) {
                oModele = m ;
        }

        public void obtenirCommunePlusProche(final Rafraichissable r, double lng,double lat) {
                
              String adresseAppel = Configuration.getServiceBaseUrl()+"/"+NOM_SERVICE+"/"+URL.encode(""+lat)+"/"+URL.encode(""+lng)+"/*/*/";
                RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,URL.encode(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) {
                                                        JSONObject resultat = responseValue.isObject() ;
                                                        JSONArray tableauCommune = resultat.get("geonames").isArray();
                                                        JSONObject objetCommune = tableauCommune.get(0).isObject();
                                                        
                                                        String valeurCommune = objetCommune.get("name").isString().stringValue();
                                                        r.rafraichir(valeurCommune, false);
                                                }
                                        }
                                }       
                        });
                }
                catch (RequestException e) { 
                        Window.alert(e.getMessage());
                }
        }
        


        public void obtenirInfosCommune(final Rafraichissable r, String valeurCommune) {

                String adresseAppel = Configuration.getServiceBaseUrl()+"/"+NOM_SERVICE+"/*/*/"+URL.encode(valeurCommune)+"/"+CODE_PAYS;
                RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,URL.encode(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) {
                                                        JSONObject resultat = responseValue.isObject() ;
                                                        JSONArray tableauCommune = resultat.get("postalCodes").isArray();
                                                        
                                                        if(tableauCommune.get(0) != null) {
                                                                JSONObject objetCommune = tableauCommune.get(0).isObject();
                                                                
                                                                double lng = objetCommune.get("lng").isNumber().doubleValue();
                                                                double lat = objetCommune.get("lat").isNumber().doubleValue();
                                                                String nomCommune = objetCommune.get("postalCode").isString().stringValue();
                
                                                                r.rafraichir(nomCommune, false);
                                                        }
                                                }
                                        }
                                }       
                        });
                }
                catch (RequestException e) {
                        Window.alert(e.getMessage());
                }
        }
}