Subversion Repositories eFlore/Applications.coel

Rev

Rev 303 | Rev 581 | Go to most recent revision | 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.interfaces.Rafraichissable;
import org.tela_botanica.client.util.UtilDAO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.GwtEvent;
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.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

public class ValeurListeAsyncDao {
        
        private static HashMap<Integer, ValeurListe> ontologieCache = new HashMap<Integer, ValeurListe>();
        private static final String SERVICE_NOM = "CoelValeurListe";
        private Rafraichissable vueARafraichir = null;
        
        public ValeurListeAsyncDao() {
                // TODO Auto-generated constructor stub
        }
        
        public ValeurListeAsyncDao(Rafraichissable vueCourante) {
                vueARafraichir = vueCourante;
        }
        
        public void obtenirListe(Integer cle)   {
                selectionner("id", cle, "*", "*");
        }
        
        public void selectionner(String type, final Integer cleParent, String abv, String idValeur) {
        if (ontologieCache.containsKey(cleParent)) {
                vueARafraichir.rafraichir((ValeurListe) ontologieCache.get(cleParent));
                        GWT.log("Ontologie cache :"+cleParent, null);
        } else {
                // Ajout des paramètres et données à selectionner dans l'URL
                        String[] parametres = new String[4];
                        parametres[0] = type;
                        parametres[1] = cleParent.toString();
                        parametres[2] = "*";
                        
                if (!type.equals("id")) {
                        parametres[2] = abv;
                }
                
                parametres[3] = idValeur;
                
                
                
                RequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres);
                        try {
                                rb.sendRequest(null, new RequestCallback() {
        
                                        public void onError(Request request, Throwable exception) {
                                                // TODO Auto-generated method stub
        
                                        }
        
                                        public void onResponseReceived(Request request, Response response) {
                                                
                                                final JSONValue responseValue = JSONParser.parse(response.getText());
        
                                                // Si la requête est un succès, reception d'un tableau
                                                if (responseValue.isObject() != null) {
                                                        
                                                        try {
                                                                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);
                                                                }
                                                        } catch (NullPointerException e) {
                                                                e.printStackTrace();
                                                        }
                                                }
        
                                        }
                                });
                        } catch (RequestException e) {
                                e.printStackTrace();
                        }
        }
        }
        
        
}