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
 
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
 
748 jpm 62
		JsonRestRequestBuilder jrrb;
754 jpm 63
		if (typeRequete.equals(GET))	{
748 jpm 64
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee);
65
		} else	{
66
			jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.POST, urlCompleteEncodee);
67
		}
68
		return jrrb;
182 gduche 69
	}
70
 
748 jpm 71
	private static String construireUrlParametres(HashMap<String, String> parametres) {
72
		String parametresUrl = "";
73
		if (parametres != null && parametres.size() > 0)	{
754 jpm 74
			parametresUrl = SEPARATEUR_CHEMIN_PARAMETRE;
748 jpm 75
			Iterator<String> iterateur = parametres.keySet().iterator();
76
			while (iterateur.hasNext())	{
77
				String cle = iterateur.next();
754 jpm 78
				parametresUrl += cle + SEPARATEUR_CLE_VALEUR + parametres.get(cle);
182 gduche 79
 
748 jpm 80
				if (iterateur.hasNext())	{
754 jpm 81
					parametresUrl = parametresUrl + SEPARATEUR_PARAMETRE;
182 gduche 82
				}
83
			}
84
		}
748 jpm 85
		return parametresUrl;
86
	}
87
 
88
	private static String construireUrlChemin(String[] morceauxDuChemin) {
89
		String cheminUrl = "";
90
		if (morceauxDuChemin != null && morceauxDuChemin.length > 0)	{
754 jpm 91
			cheminUrl = SEPARATEUR_CHEMIN;
748 jpm 92
			for (int i = 0; i < morceauxDuChemin.length; i++)	{
754 jpm 93
				cheminUrl += (morceauxDuChemin[i] != null ? morceauxDuChemin[i] : CONTENU_CHEMIN_VIDE) + SEPARATEUR_CHEMIN;
148 gduche 94
			}
95
		}
748 jpm 96
		return cheminUrl;
148 gduche 97
	}
98
 
748 jpm 99
}