Subversion Repositories eFlore/Applications.coel

Rev

Rev 1034 | Details | Compare with Previous | 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;
907 jpm 7
import org.tela_botanica.client.configuration.Configuration;
748 jpm 8
import org.tela_botanica.client.http.JsonRestRequestBuilder;
148 gduche 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
 
754 jpm 20
	private static String baseUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
21
	public static final String GET = "GET";
22
	public static final String POST = "POST";
23
	public static final String DELETE = "DELETE";
24
	public static final String PUT = "PUT";
25
	private static final String SEPARATEUR_CHEMIN = "/";
26
	private static final String SEPARATEUR_CLE_VALEUR = "=";
27
	private static final String SEPARATEUR_PARAMETRE = "&";
28
	private static final String SEPARATEUR_CHEMIN_PARAMETRE = "?";
29
	private static final String CONTENU_CHEMIN_VIDE = "*";
748 jpm 30
 
31
	public static JsonRestRequestBuilder construireRequetePost(String nomService)	{
754 jpm 32
		return construireRequete(nomService, null, null, POST);
748 jpm 33
	}
754 jpm 34
 
752 jpm 35
	public static JsonRestRequestBuilder construireRequetePost(String nomService, String[] parametres)	{
754 jpm 36
		return construireRequete(nomService, parametres, null, POST);
348 gduche 37
	}
754 jpm 38
 
39
	public static JsonRestRequestBuilder construireRequete(String nomService)	{
40
		return construireRequete(nomService, null, null, GET);
41
	}
348 gduche 42
 
752 jpm 43
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres)	{
754 jpm 44
		return construireRequete(nomService, parametres, null, GET);
182 gduche 45
	}
46
 
748 jpm 47
	public static JsonRestRequestBuilder construireRequete(String nomService, HashMap<String, String> restrictions)	{
754 jpm 48
		return construireRequete(nomService, null, restrictions, GET);
348 gduche 49
	}
754 jpm 50
 
51
	public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions)	{
52
		return construireRequete(nomService, parametres, restrictions, GET);
53
	}
748 jpm 54
 
754 jpm 55
	private static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions, String typeRequete)	{
748 jpm 56
		String restrictionsUrl = construireUrlParametres(restrictions);
57
		String parametresUrl = construireUrlChemin(parametres);
182 gduche 58
 
748 jpm 59
		String urlComplete = baseUrl + nomService + parametresUrl + restrictionsUrl;
60
		String urlCompleteEncodee = URL.encode(urlComplete);
182 gduche 61
 
1034 gduche 62
		System.out.println(urlCompleteEncodee);
63
 
748 jpm 64
		JsonRestRequestBuilder jrrb;
754 jpm 65
		if (typeRequete.equals(GET))	{
748 jpm 66
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee);
67
		} else	{
68
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.POST, urlCompleteEncodee);
69
		}
70
		return jrrb;
182 gduche 71
	}
72
 
748 jpm 73
	private static String construireUrlParametres(HashMap<String, String> parametres) {
74
		String parametresUrl = "";
75
		if (parametres != null && parametres.size() > 0)	{
754 jpm 76
			parametresUrl = SEPARATEUR_CHEMIN_PARAMETRE;
748 jpm 77
			Iterator<String> iterateur = parametres.keySet().iterator();
78
			while (iterateur.hasNext())	{
79
				String cle = iterateur.next();
754 jpm 80
				parametresUrl += cle + SEPARATEUR_CLE_VALEUR + parametres.get(cle);
182 gduche 81
 
748 jpm 82
				if (iterateur.hasNext())	{
754 jpm 83
					parametresUrl = parametresUrl + SEPARATEUR_PARAMETRE;
182 gduche 84
				}
85
			}
86
		}
748 jpm 87
		return parametresUrl;
88
	}
89
 
90
	private static String construireUrlChemin(String[] morceauxDuChemin) {
91
		String cheminUrl = "";
92
		if (morceauxDuChemin != null && morceauxDuChemin.length > 0)	{
754 jpm 93
			cheminUrl = SEPARATEUR_CHEMIN;
748 jpm 94
			for (int i = 0; i < morceauxDuChemin.length; i++)	{
1020 jpm 95
				cheminUrl += (UtilString.isEmpty(morceauxDuChemin[i]) ? CONTENU_CHEMIN_VIDE : morceauxDuChemin[i]) + SEPARATEUR_CHEMIN;
148 gduche 96
			}
97
		}
748 jpm 98
		return cheminUrl;
148 gduche 99
	}
100
 
748 jpm 101
}