Subversion Repositories eFlore/Applications.coel

Rev

Rev 748 | Rev 754 | Go to most recent revision | 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.http.JsonRestRequestBuilder;
import org.tela_botanica.client.modeles.Configuration;

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 JsonRestRequestBuilder construireRequete(String nomService)       {
                return construireRequete(nomService, null, null, "GET");
        }
        
        public static JsonRestRequestBuilder construireRequetePost(String nomService)   {
                return construireRequete(nomService, null, null, "POST");
        }
        
        public static JsonRestRequestBuilder construireRequete(String nomService, String parametre)     {
                String[] parametres = {parametre};
                return construireRequete(nomService, parametres, null, "GET");
        }
        
        public static JsonRestRequestBuilder construireRequetePost(String nomService, String[] parametres)      {
                return construireRequete(nomService, parametres, null, "POST");
        }
        
        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, 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 = "?";
                        Iterator<String> iterateur = parametres.keySet().iterator();
                        while (iterateur.hasNext())     {
                                String cle = iterateur.next();
                                parametresUrl += cle + "=" + parametres.get(cle);
                                
                                if (iterateur.hasNext())        {
                                        parametresUrl = parametresUrl + "&";
                                }
                        }
                }
                return parametresUrl;
        }
        
        private static String construireUrlChemin(String[] morceauxDuChemin) {
                String cheminUrl = "";
                if (morceauxDuChemin != null && morceauxDuChemin.length > 0)    {
                        cheminUrl = "/";
                        for (int i = 0; i < morceauxDuChemin.length; i++)       {
                                cheminUrl += (morceauxDuChemin[i] != null ? morceauxDuChemin[i] : "*") + "/";
                        }
                }
                return cheminUrl;
        }
        
}