Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.modeles;

import java.util.HashMap;

import org.tela_botanica.client.http.JsonRestRequestBuilder;
import org.tela_botanica.client.http.JsonRestRequestCallback;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.util.UtilDAO;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

public class ValeurListeAsyncDao {
        
        private static HashMap<String, ValeurListe> ontologieCache = new HashMap<String, ValeurListe>();
        private static final String SERVICE_NOM = "CoelValeurListe";
        private Rafraichissable vueARafraichir = null;
        
        public ValeurListeAsyncDao(Rafraichissable vueCourante) {
                vueARafraichir = vueCourante;
        }
        
        public void obtenirListe(Integer cle)   {
                selectionner("id", cle, null, null);
        }
        
        public void selectionner(String type, Integer cle, String abv, String idValeur) {
                
                // La cleParent en Integer est insuffisante pour les liste valeurs comme Région qui on plusieurs sections sur leur liste
                // (ex : on ne sélectionne que les régions FR.__ puis les régions ES.__ sur la liste 1078 ....
                final String cleParent = cle + (abv == null ? "" : abv);
                
                if (ontologieCache.containsKey(cleParent)) {
                        vueARafraichir.rafraichir(ontologieCache.get(cleParent));
        } else {
                String paramAbv = (type.equals("id") ? null : abv);
                String[] parametres = {type, cleParent.toString(), paramAbv, idValeur};
                final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
                rb.envoyerRequete(null, new JsonRestRequestCallback() {
                        @Override
                        public void surReponse(JSONValue responseValue) {
                                if (responseValue.isObject() != null) {
                                                final JSONObject reponse = responseValue.isObject();
                                                JSONString listeId = reponse.get("id").isString();
                                                JSONArray listeValeurs = reponse.get("valeurs").isArray();
                                                if (listeId != null)    {
                                                        // Transformation du tableau JSON réponse en Liste
                                                        ValeurListe liste = new ValeurListe(listeId, listeValeurs);
                                                        // Stockage en cache
                                                        ontologieCache.put(cleParent, liste);
                                                        // et on met à jour le demandeur des données
                                                        vueARafraichir.rafraichir(liste);
                                                }
                                        }
                        }
                });
        }
        }
        
}