Subversion Repositories eFlore/Applications.coel

Rev

Rev 1292 | Rev 1329 | 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.http.JsonRestRequestBuilder;
import org.tela_botanica.client.http.JsonRestRequestCallback;
import org.tela_botanica.client.interfaces.Rafraichissable;
import org.tela_botanica.client.synchronisation.Reponse;
import org.tela_botanica.client.synchronisation.Sequenceur;
import org.tela_botanica.client.util.Debug;
import org.tela_botanica.client.util.UtilDAO;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNumber;
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, Object> ontologieCache = new HashMap<String, Object>();
        
        private static HashMap<String, HashMap<Integer, Object>> pagedOntologieCache = new HashMap<String, HashMap<Integer, Object>>();
        
        private static final String SERVICE_NOM = "CoelValeurListe";
        private Rafraichissable vueARafraichir = null;
        private static ValeurListe tempListe = new ValeurListe();
        private int cptPage = 0;
        private int limiteJREST = 150;

        public ValeurListeAsyncDao(Rafraichissable vueCourante) {
                vueARafraichir = vueCourante;
        }
        
        public static HashMap<String, Object> getOntologieCache() {
                return ontologieCache;
        }

        public void chargerListe(boolean nextPage, String type, Integer cle, String abv, String idValeur, boolean pagination, String recherche, int start, int nbElements, final Integer seqId) {
                /** Si nextpage est VRAI, alors cela signifie que la liste est plus grande
                que la limite du JREST et l'on doit relancer une requete pour obtenir
                la page suivante **/
                if (nextPage) {
                        cptPage++;
                        selectionner(type, cle, abv, idValeur, pagination, recherche, cptPage*limiteJREST, limiteJREST, seqId);
                }
                /** Sinon cela signifie que le chargement de la liste est terminé et on peut
                la mettre en cache et retourner la réponse au demandeur **/
                else {
                        // on met en cache
                        String id = String.valueOf(cle);
                        
                        String abreviationStr = "";
                        if (abv != null) {
                                abreviationStr = abv;
                                int indexPoint = abreviationStr.indexOf(".", 0);
                                abreviationStr = abreviationStr.substring(0, indexPoint);
                                id = id+abreviationStr; 
                        }
                        
                        // réinitialiser le compteur
                        cptPage = 0;

                        // et on met à jour le demandeur des données                  
                        if (ontologieCache.get(id) != null) {
                                if (vueARafraichir instanceof Sequenceur)       {
                                        vueARafraichir = (Sequenceur) vueARafraichir;
                                        
                                        Reponse reponse = new Reponse(ontologieCache.get(id), seqId);
                                        vueARafraichir.rafraichir(reponse);
                                } else {
                                        vueARafraichir.rafraichir(ontologieCache.get(id));
                                }
                        }
                }
        }
        
        public void obtenirListe(Integer cle, Integer seqId)    {               
                selectionner("id", cle, null, null, false, null, -1, -1, seqId);
        }
        
        public void obtenirListe(String type, String identifiantValeur, Integer seqId)  {               
                selectionner(type, null, null, identifiantValeur, false, null, -1, -1, seqId);
        }
        
        public void obtenirListe(Integer cle, boolean pagination, String recherche, int start, int limit, Integer seqId)        {
                selectionner("nom", cle, null, null, pagination, recherche, start, limit, seqId);
        }
        
        public void selectionner(String type, Integer cle, String abv, String idValeur, Integer seqId) {
                selectionner(type, cle, abv, idValeur, false, null, -1, -1, seqId);
        }

        public void selectionner(final String type, final Integer cle, final String abv, final String idValeur, final boolean pagination, final String recherche, final int start, final int limit, final Integer seqId) {
                // 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 cleParentPourCache = cle + (abv == null ? "" : abv);
                final String cleParent = cle+"";
                
                String nom = "";
                if ( (recherche == null) || (recherche.equals("")) ) nom = "";
                else {
                        nom = recherche+"%";
                }

                String abreviation = "";
                String paramAbv = "";
                
                if (type.equals("ab") || type.equals("abv")) {
                        int positionPoint = abv.indexOf(".");
                        if (positionPoint != -1) abreviation = abv.substring(0, positionPoint)+"%";
                        else abreviation=abv+"%";
                }
                paramAbv = abreviation;

                String[] parametres = {type, cleParent, paramAbv, idValeur, nom};

                HashMap<String, String> restrictions = new HashMap<String, String>();

                if (pagination) {

                        restrictions.put("limit", String.valueOf(limit));
                        restrictions.put("start", String.valueOf(start));
                        restrictions.put("orderby", "cmlv_nom");
                        
                final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
                rb.envoyerRequete(null, new JsonRestRequestCallback()
                {
                        @Override
                        public void surReponse(JSONValue responseValue)
                        {                               
                                if (vueARafraichir instanceof Sequenceur)       {
                                        Reponse reponse = new Reponse(responseValue, seqId);
                                        vueARafraichir.rafraichir(reponse);
                                } else {
                                        vueARafraichir.rafraichir(responseValue);
                                }
                        }
                });
                }
                else
                {
                        boolean nextPage = (start > 0);
                        
                        if (nextPage)
                        {
                                restrictions.put("start", String.valueOf(start));
                                restrictions.put("limit", String.valueOf(limit));
                                restrictions.put("orderby", "cmlv_nom");
                        }
                        else
                        {
                                restrictions.put("orderby", "cmlv_nom");
                        }
                        
                        // si l'on est pas dans un processus de récupération d'une liste
                        // et si le cache contient déjà la liste recherchée
                        if (ontologieCache.containsKey(cleParentPourCache) && !nextPage)
                        {
                                //On est à la derniere page, on peu rafraichir
                                if (vueARafraichir instanceof Sequenceur)       {
                                        Reponse reponse = new Reponse(ontologieCache.get(cleParentPourCache), seqId);
                                        vueARafraichir.rafraichir(reponse);
                                } else {
                                        vueARafraichir.rafraichir(ontologieCache.get(cleParentPourCache));
                                }
                }
                        else
                {
                        final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
                        rb.envoyerRequete(null, new JsonRestRequestCallback()
                        {
                                @Override
                                public void surReponse(JSONValue responseValue)
                                {                       
                                        if (responseValue.isObject() != null)
                                        {
                                                /** Récuperation des différents paramètres de la réponse JSON **/
                                                        final JSONObject reponse = responseValue.isObject();
                                                        JSONString listeId = reponse.get("id").isString();
                                                        JSONArray listeValeurs = reponse.get("valeurs").isArray();
                                                        JSONNumber nbElements = reponse.get("nbElements").isNumber();
                                                        JSONBoolean getNextPage = reponse.get("getNextPage").isBoolean();
                                                        
                                                        /** Gestion de l'abreviation (pour la liste des régions) **/
                                                        String abreviationStr = "";
                                                        if (reponse.get("abreviation") != null) {
                                                                abreviationStr = reponse.get("abreviation").isString().stringValue();
                                                                int a = abreviationStr.indexOf("%", 1);
                                                                abreviationStr = abreviationStr.substring(1, a);
                                                        }
                                                        else {
                                                                abreviationStr = "";
                                                        }

                                                        /** si l'on a bien reçu une liste de valeurs **/
                                                        if (listeId != null)
                                                        {
                                                                /** Transformation du tableau JSON réponse en ValeurListe **/
                                                                String id = listeId.stringValue();
                                                                ValeurListe liste = new ValeurListe(new JSONString(id), abreviationStr, listeValeurs, nbElements);

                                                                /** Si la liste existe deja en cache **/
                                                                String identifiantCache = (abreviationStr=="") ? id : (id+abreviationStr);
                                                                if (ontologieCache.get(id) != null)
                                                                {
                                                                        /** Alors on concatène la liste existante avec celle qu'on vient de recevoir **/
                                                                        ((ValeurListe)ontologieCache.get(identifiantCache)).concatenerListe(liste);
                                                                }
                                                                /** Sinon on l'insère simplement dans le cache **/
                                                                else {
                                                                        ontologieCache.put(identifiantCache,liste);
                                                                }

                                                                /** Appel à la méthode qui gère le retour à l'appelant ou la suite du chargement **/
                                                                chargerListe(getNextPage.booleanValue(), type, liste.getId(), abv, idValeur, pagination, recherche, start, limit, seqId);
                                                        }
                                        }
                                }
                        });
                }
                }
        }
}