Subversion Repositories eFlore/Applications.coel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
148 gduche 1
package org.tela_botanica.client.util;
2
 
182 gduche 3
import java.util.HashMap;
4
import java.util.Iterator;
148 gduche 5
 
6
import org.tela_botanica.client.RegistreId;
748 jpm 7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
148 gduche 8
import org.tela_botanica.client.modeles.Configuration;
9
 
10
import com.extjs.gxt.ui.client.Registry;
296 gduche 11
import com.google.gwt.http.client.URL;
148 gduche 12
 
748 jpm 13
/**
14
 * @author Gréguoire DUCHÉ <greguoire@tela-botanica.org>
15
 * @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
16
 * @description La classe utilDAO fournit des méthodes communes pour les outils DAO
17
 * */
148 gduche 18
public class UtilDAO {
19
 
20
	private static String baseUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
748 jpm 21
 
22
	public static JsonRestRequestBuilder construireRequete(String nomService)	{
23
		return construireRequete(nomService, null, null, "GET");
24
	}
148 gduche 25
 
748 jpm 26
	public static JsonRestRequestBuilder construireRequetePost(String nomService)	{
27
		return construireRequete(nomService, null, null, "POST");
28
	}
148 gduche 29
 
748 jpm 30
	public static JsonRestRequestBuilder construireRequete(String nomService, String parametre)	{
31
		String[] parametres = {parametre};
32
		return construireRequete(nomService, parametres);
148 gduche 33
	}
34
 
748 jpm 35
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres)	{
36
		return construireRequete(nomService, parametres, "GET");
348 gduche 37
	}
38
 
748 jpm 39
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, String typeRequete)	{
40
		return construireRequete(nomService, parametres, null, typeRequete);
182 gduche 41
	}
42
 
748 jpm 43
	public static JsonRestRequestBuilder construireRequete(String nomService, HashMap<String, String> restrictions)	{
44
		return construireRequete(nomService, restrictions, "GET");
348 gduche 45
	}
748 jpm 46
 
47
	public static JsonRestRequestBuilder construireRequete(String nomService, HashMap<String, String> restrictions, String typeRequete)	{
48
		return construireRequete( nomService, null, restrictions, typeRequete);
49
	}
50
 
51
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions, String typeRequete)	{
52
		String restrictionsUrl = construireUrlParametres(restrictions);
53
		String parametresUrl = construireUrlChemin(parametres);
182 gduche 54
 
748 jpm 55
		String urlComplete = baseUrl + nomService + parametresUrl + restrictionsUrl;
56
		String urlCompleteEncodee = URL.encode(urlComplete);
182 gduche 57
 
748 jpm 58
		JsonRestRequestBuilder jrrb;
59
		if (typeRequete.equals("GET"))	{
60
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee);
61
		} else	{
62
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.POST, urlCompleteEncodee);
63
		}
64
		return jrrb;
182 gduche 65
	}
66
 
748 jpm 67
	private static String construireUrlParametres(HashMap<String, String> parametres) {
68
		String parametresUrl = "";
69
		if (parametres != null && parametres.size() > 0)	{
70
			parametresUrl = "?";
71
			Iterator<String> iterateur = parametres.keySet().iterator();
72
			while (iterateur.hasNext())	{
73
				String cle = iterateur.next();
74
				parametresUrl += cle + "=" + parametres.get(cle);
182 gduche 75
 
748 jpm 76
				if (iterateur.hasNext())	{
77
					parametresUrl = parametresUrl + "&";
182 gduche 78
				}
79
			}
80
		}
748 jpm 81
		return parametresUrl;
82
	}
83
 
84
	private static String construireUrlChemin(String[] morceauxDuChemin) {
85
		String cheminUrl = "";
86
		if (morceauxDuChemin != null && morceauxDuChemin.length > 0)	{
87
			cheminUrl = "/";
88
			for (int i = 0; i < morceauxDuChemin.length; i++)	{
89
				cheminUrl += (morceauxDuChemin[i] != null ? morceauxDuChemin[i] : "*") + "/";
148 gduche 90
			}
91
		}
748 jpm 92
		return cheminUrl;
148 gduche 93
	}
94
 
748 jpm 95
}