Subversion Repositories eFlore/Applications.cel

Rev

Rev 1941 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.client.modeles.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.modeles.objets.Configuration;
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso;
import org.tela_botanica.client.observation.ObservationModele;

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.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;

public class ListeReferentielChampsEtendusDAO {
        
        /**
         * Cache
         * 
         */     
        private HashMap<String,ListeReferentielPerso> cache = new HashMap();
        
        public ListeReferentielChampsEtendusDAO(ObservationModele obs) {
                
        }
        
        public void obtenirListeNomsChampsEtendus(final Rafraichissable r, String recherche) {
                                
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() + 
                                                                                                "/NomsChampsEtendus/cle"+
                                                                                                "?recherche="+URL.encode(recherche));
                
                try {
                        
                        rb.sendRequest(null, new RequestCallback() {

                                @Override
                                public void onResponseReceived(final Request request,
                                                final Response response) {


                                        HashMap<String, String> labelCles = new HashMap<String, String>();
                                        final JSONValue responseValue = JSONParser.parse(response.getText());
                                        
                                        JSONObject reponse = null;
                                                        
                                        // si c'est un tableau
                                        if ((reponse = responseValue.isObject()) != null) {
                                                Iterator<String> it = reponse.keySet().iterator();
                                                
                                                while(it.hasNext()) {
                                                        String cle = it.next();                                                         
                                                        String valeur = reponse.get(cle).isString().stringValue();
                                                        labelCles.put(cle, valeur);
                                                }
                                        }
                                                
                                        r.rafraichir(labelCles, true);                          
                                }

                                @Override
                                public void onError(Request request, Throwable exception) {
                                        // TODO Auto-generated method stub
                                        
                                }       
                                
                        });
                        
                } catch (RequestException e) {
                        
                        e.printStackTrace();
                }
        }
        
        public void obtenirListeValeursChampEtendu(final Rafraichissable r, String cle, String recherche) {
                
                RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() + 
                                                                                                "/NomsChampsEtendus/valeur"+
                                                                                                "?cle="+URL.encode(cle)+"&recherche="+URL.encode(recherche));
                
                try {
                        
                        rb.sendRequest(null, new RequestCallback() {

                                @Override
                                public void onResponseReceived(final Request request,
                                                final Response response) {


                                        ArrayList<String> valeurs = new ArrayList<String>();
                                        final JSONValue responseValue = JSONParser.parse(response.getText());
                                        
                                        JSONArray reponse = null;
                                        if ((reponse = responseValue.isArray()) != null) {
                                                for(int i = 0; i < reponse.size(); i++) {                                                               
                                                        valeurs.add(reponse.get(i).isString().stringValue());
                                                }
                                        }
                                                
                                        r.rafraichir(valeurs, true);                            
                                }

                                @Override
                                public void onError(Request request, Throwable exception) {
                                        // TODO Auto-generated method stub
                                        
                                }       
                                
                        });
                        
                } catch (RequestException e) {
                        
                        e.printStackTrace();
                }
        }
}