Subversion Repositories eFlore/Applications.cel

Rev

Rev 1918 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

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.observation.ObservationModele;

import com.google.gwt.core.client.Callback;
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.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

/**
 * DAO d'accès a une observation
 * 
 * @author aurelien
 * 
 */
public class ImageInformationRepartitionAsynchroneDAO {

        /**
         * Le modele associé au DAO
         */
        private ObservationModele observationModele = null;

        public ImageInformationRepartitionAsynchroneDAO(ObservationModele obs) {
                observationModele = obs;
        }



        /**
         * Recherche Image repartition associee a un nom 
         * @param r
         * @param identifiant
         * @param numeroNomenclaturalSaisiObservation
         */
        
        public void obtenirURLImage(final Rafraichissable r, String referentielTaxo, String numeroNomenclaturalSaisiObservation) {
        
                String referentielDefaut =  Configuration.getReferentielsDispos().get(0).getCode();
                referentielTaxo = (referentielTaxo != null && !referentielTaxo.isEmpty()) ? referentielTaxo : referentielDefaut;
                
                // on envoie le get asynchrone
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET,Configuration.getServiceBaseUrl()
                                +"/NameMap/"+referentielTaxo+"/"+numeroNomenclaturalSaisiObservation) ;
                
                try {
                        rb.sendRequest(null, new RequestCallback() {

                                @Override
                                public void onError(Request request, Throwable exception) {
                                        // TODO Auto-generated method stub
                                        
                                }

                                @Override
                                public void onResponseReceived(Request request,
                                                Response response) {
                                        
                                        JSONValue responseValue = JSONParser.parse(response.getText());
                                        JSONArray reponse;
                                        String urlImage=null;

                                        if ((reponse = responseValue.isArray()) != null) {
                                                // Url Image
                                                urlImage= ((JSONString)reponse.get(0)).stringValue();
                                        }
                                        else {
                                                urlImage="";
                                        }
                                        
                                        r.rafraichir(urlImage,true);                            
                                }
                        }) ;

                } catch (RequestException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
        
        public static void taxonEstPresentDansZoneGeo(String referentielTaxo, String numeroNomenclaturalSaisiObservation, String pays, String zoneGeo, final Callback<String, String> callback) {
                String referentielDefaut =  Configuration.getReferentielsDispos().get(0).getCode();
                referentielTaxo = (referentielTaxo != null && !referentielTaxo.isEmpty()) ? referentielTaxo : referentielDefaut;
                
                String url = Configuration.getServiceBaseUrl()+"/InventoryTaxonPresent/"+referentielTaxo+"/"+numeroNomenclaturalSaisiObservation+"?pays="+URL.encode(pays)+"&ce_zone_geo="+URL.encode(zoneGeo);
                
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, url) ;
                try {
                        rb.sendRequest(null, new RequestCallback() {

                                @Override
                                public void onError(Request request, Throwable exception) {
                                        callback.onFailure(exception.getMessage());
                                }

                                @Override
                                public void onResponseReceived(Request request,
                                                Response response) {
                                        final JSONValue responseValue = JSONParser
                                                        .parse(response.getText());
                                        
                                        if (responseValue.isString() != null) {
                                                //Window.alert(response.getText()+responseValue.isString().stringValue());
                                                callback.onSuccess(responseValue.isString().stringValue());
                                        } else {
                                                callback.onFailure(response.getText());
                                        }
                                }
                        }) ;

                } catch (RequestException e) {
                        callback.onFailure(e.getMessage());
                }
        }
                        
}