Subversion Repositories eFlore/Applications.coel

Rev

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

package org.tela_botanica.client.util;

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

import org.tela_botanica.client.RegistreId;
import org.tela_botanica.client.configuration.Configuration;
import org.tela_botanica.client.http.JsonRestRequestBuilder;

import com.extjs.gxt.ui.client.Registry;
import com.google.gwt.http.client.URL;

/**
 * @author Gréguoire DUCHÉ <greguoire@tela-botanica.org>
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @description La classe utilDAO fournit des méthodes communes pour les outils DAO
 * */
public class UtilDAO {
        
        private static String baseUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
        public static final String GET = "GET";
        public static final String POST = "POST";
        public static final String DELETE = "DELETE";
        public static final String PUT = "PUT";
        private static final String SEPARATEUR_CHEMIN = "/";
        private static final String SEPARATEUR_CLE_VALEUR = "=";
        private static final String SEPARATEUR_PARAMETRE = "&";
        private static final String SEPARATEUR_CHEMIN_PARAMETRE = "?";
        private static final String CONTENU_CHEMIN_VIDE = "*";

        public static JsonRestRequestBuilder construireRequetePost(String nomService)   {
                return construireRequete(nomService, null, null, POST);
        }

        public static JsonRestRequestBuilder construireRequetePost(String nomService, String[] parametres)      {
                return construireRequete(nomService, parametres, null, POST);
        }

        public static JsonRestRequestBuilder construireRequete(String nomService)       {
                return construireRequete(nomService, null, null, GET);
        }
        
        public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres)  {
                return construireRequete(nomService, parametres, null, GET);
        }
        
        public static JsonRestRequestBuilder construireRequete(String nomService, HashMap<String, String> restrictions) {
                return construireRequete(nomService, null, restrictions, GET);
        }
        
        public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions)    {
                return construireRequete(nomService, parametres, restrictions, GET);
        }

        private static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions, String typeRequete)       {
                String restrictionsUrl = construireUrlParametres(restrictions);
                String parametresUrl = construireUrlChemin(parametres);
                
                String urlComplete = baseUrl + nomService + parametresUrl + restrictionsUrl;
                String urlCompleteEncodee = URL.encode(urlComplete);
                
                JsonRestRequestBuilder jrrb;
                if (typeRequete.equals(GET))    {
                        jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee);
                } else  {
                        jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.POST, urlCompleteEncodee);
                }
                return jrrb;
        }
        
        private static String construireUrlParametres(HashMap<String, String> parametres) {
                String parametresUrl = "";
                if (parametres != null && parametres.size() > 0)        {
                        parametresUrl = SEPARATEUR_CHEMIN_PARAMETRE;
                        Iterator<String> iterateur = parametres.keySet().iterator();
                        while (iterateur.hasNext())     {
                                String cle = iterateur.next();
                                parametresUrl += cle + SEPARATEUR_CLE_VALEUR + parametres.get(cle);
                                
                                if (iterateur.hasNext())        {
                                        parametresUrl = parametresUrl + SEPARATEUR_PARAMETRE;
                                }
                        }
                }
                return parametresUrl;
        }
        
        private static String construireUrlChemin(String[] morceauxDuChemin) {
                String cheminUrl = "";
                if (morceauxDuChemin != null && morceauxDuChemin.length > 0)    {
                        cheminUrl = SEPARATEUR_CHEMIN;
                        for (int i = 0; i < morceauxDuChemin.length; i++)       {
                                cheminUrl += (UtilString.isEmpty(morceauxDuChemin[i]) ? CONTENU_CHEMIN_VIDE : morceauxDuChemin[i]) + SEPARATEUR_CHEMIN;
                        }
                }
                return cheminUrl;
        }
        
}