Subversion Repositories eFlore/Applications.coel

Rev

Rev 300 | Rev 378 | Go to most recent revision | 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;
7
import org.tela_botanica.client.modeles.Configuration;
8
 
9
import com.extjs.gxt.ui.client.Registry;
296 gduche 10
import com.google.gwt.core.client.GWT;
148 gduche 11
import com.google.gwt.http.client.RequestBuilder;
296 gduche 12
import com.google.gwt.http.client.URL;
148 gduche 13
 
14
public class UtilDAO {
15
 
16
	/**
17
	 * @author greg
18
	 * @description La classe utilDAO fournit des méthodes communes pour les outils DAO
19
	 * */
20
 
21
	private static String baseUrl = ((Configuration) Registry.get(RegistreId.CONFIG)).getServiceBaseUrl();
22
 
23
	/**
24
	 * @author greg
25
	 * @description La classe construireRequete permet de revonyer un object RequestBuilder pour le service JREST
26
	 * @param nomService String le nom du service JREST
27
	 * @param strParametres String le paramètre pour le service
28
	 */
29
 
30
	public static RequestBuilder construireRequete(String nomService, String strParametres)	{
31
 
32
		String[] arrParametres = {strParametres};
33
		return construireRequete(nomService, arrParametres);
34
	}
35
 
36
	/**
37
	 *
38
	 * @param nomService
39
	 * @param arrParametres
40
	 * @return
41
	 */
42
	public static RequestBuilder construireRequete(String nomService, String[] arrParametres)	{
348 gduche 43
		return construireRequete(nomService, arrParametres, "GET");
44
	}
45
 
46
	public static RequestBuilder construireRequete(String nomService, String[] arrParametres, String typeRequete)	{
148 gduche 47
 
182 gduche 48
		HashMap<String, String> hmRestrictions = null;
348 gduche 49
		return construireRequete(nomService, arrParametres, hmRestrictions, typeRequete);
182 gduche 50
 
51
	}
52
 
53
	public static RequestBuilder construireRequete(String nomService, HashMap<String, String> hmRestrictions)	{
348 gduche 54
		return construireRequete(nomService, hmRestrictions, "GET");
55
	}
56
		public static RequestBuilder construireRequete(String nomService, HashMap<String, String> hmRestrictions, String typeRequete)	{
182 gduche 57
 
58
		String[] arrParametres = null;
348 gduche 59
		return construireRequete( nomService, arrParametres, hmRestrictions, typeRequete);
182 gduche 60
 
61
	}
62
 
348 gduche 63
	public static RequestBuilder construireRequete(String nomService, String[] arrParametres, HashMap<String, String> hmRestrictions, String typeRequete)	{
182 gduche 64
 
65
 
66
		String restrictions = "";
67
 
68
		//Les restrictions sont ajoutées en paramètres GET
69
		if ((hmRestrictions!=null)&&(hmRestrictions.size() > 0))	{
70
 
71
			Iterator<String> itRestrictions = hmRestrictions.keySet().iterator();
72
			while (itRestrictions.hasNext())	{
73
				String cle = itRestrictions.next();
74
				restrictions += cle + "=" + hmRestrictions.get(cle);
75
 
76
				if (itRestrictions.hasNext())	{
77
					restrictions = restrictions + "&";
78
				}
79
			}
80
 
81
			restrictions = "?" + restrictions;
82
		}
83
 
148 gduche 84
		String strParametres = "/";
85
		if (arrParametres != null) {
86
 
87
			for (int i=0; i < arrParametres.length; i++)	{
300 gduche 88
				if (arrParametres[i] != null)	{
89
					strParametres += arrParametres[i]+ "/";
90
				}
148 gduche 91
			}
92
		}
93
 
296 gduche 94
		String wholeUrl = baseUrl + nomService + strParametres + restrictions;
95
		wholeUrl = URL.encode(wholeUrl);
184 gduche 96
 
296 gduche 97
		GWT.log("Envoi d'une requête: " + wholeUrl, null);
348 gduche 98
		RequestBuilder rb;
99
		if (typeRequete.equals("GET"))	{
100
			rb = new RequestBuilder(RequestBuilder.GET, wholeUrl);
101
		} else	{
102
			rb = new RequestBuilder(RequestBuilder.POST, wholeUrl);
103
		}
296 gduche 104
 
148 gduche 105
		return rb;
106
	}
107
 
108
	/**
109
	 *
110
	 * @param nomService String le nom du service
111
	 * @return un objet RB
112
	 */
113
	public static RequestBuilder construireRequete(String nomService)	{
114
 
115
		String[] arrParametres = null;
116
		return construireRequete(nomService, arrParametres);
117
	}
118
 
119
}