Subversion Repositories eFlore/Applications.coel

Rev

Rev 348 | Rev 752 | 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.modeles.Configuration;

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

public class UtilDAO {
        
        /**
         * @author greg
         * @description La classe utilDAO fournit des méthodes communes pour les outils DAO
         * */
        
        private static String baseUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl(); 
        
        /**
         * @author greg
         * @description La classe construireRequete permet de revonyer un object RequestBuilder pour le service JREST
         * @param nomService String le nom du service JREST
         * @param strParametres String le paramètre pour le service  
         */
        
        public static RequestBuilder construireRequete(String nomService, String strParametres) {
                
                String[] arrParametres = {strParametres};               
                return construireRequete(nomService, arrParametres);
        }
        
        /**
         * 
         * @param nomService
         * @param arrParametres
         * @return
         */
        public static RequestBuilder construireRequete(String nomService, String[] arrParametres)       {
                return construireRequete(nomService, arrParametres, "GET");
        }
        
        public static RequestBuilder construireRequete(String nomService, String[] arrParametres, String typeRequete)   {
                
                HashMap<String, String> hmRestrictions = null;
                return construireRequete(nomService, arrParametres, hmRestrictions, typeRequete);
                
        }
        
        public static RequestBuilder construireRequete(String nomService, HashMap<String, String> hmRestrictions)       {
                return construireRequete(nomService, hmRestrictions, "GET");
        }
                public static RequestBuilder construireRequete(String nomService, HashMap<String, String> hmRestrictions, String typeRequete)   {
                
                String[] arrParametres = null;
                return construireRequete( nomService, arrParametres, hmRestrictions, typeRequete);
                
        }
        
        public static RequestBuilder construireRequete(String nomService, String[] arrParametres, HashMap<String, String> hmRestrictions, String typeRequete)   {
                
                
                String restrictions = "";
                
                //Les restrictions sont ajoutées en paramètres GET
                if ((hmRestrictions!=null)&&(hmRestrictions.size() > 0))        {
                        
                        Iterator<String> itRestrictions = hmRestrictions.keySet().iterator();
                        while (itRestrictions.hasNext())        {
                                String cle = itRestrictions.next();
                                restrictions += cle + "=" + hmRestrictions.get(cle);
                                
                                if (itRestrictions.hasNext())   {
                                        restrictions = restrictions + "&";
                                }
                        }
                        
                        restrictions = "?" + restrictions;
                }
                
                String strParametres = "/";
                if (arrParametres != null) {
                        
                        for (int i=0; i < arrParametres.length; i++)    {
                                if (arrParametres[i] != null)   {
                                        strParametres += arrParametres[i]+ "/";
                                }
                        }
                }
                
                String wholeUrl = baseUrl + nomService + strParametres + restrictions;
                wholeUrl = URL.encode(wholeUrl);
                
                RequestBuilder rb;
                if (typeRequete.equals("GET"))  {
                        rb = new RequestBuilder(RequestBuilder.GET, wholeUrl);
                } else  {
                        rb = new RequestBuilder(RequestBuilder.POST, wholeUrl);
                }
                
                return rb;
        }
        
        /**
         * 
         * @param nomService String le nom du service
         * @return un objet RB
         */
        public static RequestBuilder construireRequete(String nomService)       {
                        
                String[] arrParametres = null;
                return construireRequete(nomService, arrParametres);
        }

}