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) {
|
752 |
jpm |
31 |
String[] parametres = {parametre};
|
|
|
32 |
return construireRequete(nomService, parametres, null, "GET");
|
148 |
gduche |
33 |
}
|
|
|
34 |
|
752 |
jpm |
35 |
public static JsonRestRequestBuilder construireRequetePost(String nomService, String[] parametres) {
|
|
|
36 |
return construireRequete(nomService, parametres, null, "POST");
|
348 |
gduche |
37 |
}
|
|
|
38 |
|
752 |
jpm |
39 |
public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres) {
|
|
|
40 |
return construireRequete(nomService, parametres, null, "GET");
|
182 |
gduche |
41 |
}
|
|
|
42 |
|
748 |
jpm |
43 |
public static JsonRestRequestBuilder construireRequete(String nomService, HashMap<String, String> restrictions) {
|
752 |
jpm |
44 |
return construireRequete(nomService, null, restrictions, "GET");
|
348 |
gduche |
45 |
}
|
748 |
jpm |
46 |
|
|
|
47 |
public static JsonRestRequestBuilder construireRequete(String nomService, String[] parametres, HashMap<String, String> restrictions, String typeRequete) {
|
|
|
48 |
String restrictionsUrl = construireUrlParametres(restrictions);
|
|
|
49 |
String parametresUrl = construireUrlChemin(parametres);
|
182 |
gduche |
50 |
|
748 |
jpm |
51 |
String urlComplete = baseUrl + nomService + parametresUrl + restrictionsUrl;
|
|
|
52 |
String urlCompleteEncodee = URL.encode(urlComplete);
|
182 |
gduche |
53 |
|
748 |
jpm |
54 |
JsonRestRequestBuilder jrrb;
|
|
|
55 |
if (typeRequete.equals("GET")) {
|
|
|
56 |
jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.GET, urlCompleteEncodee);
|
|
|
57 |
} else {
|
|
|
58 |
jrrb = new JsonRestRequestBuilder(JsonRestRequestBuilder.POST, urlCompleteEncodee);
|
|
|
59 |
}
|
|
|
60 |
return jrrb;
|
182 |
gduche |
61 |
}
|
|
|
62 |
|
748 |
jpm |
63 |
private static String construireUrlParametres(HashMap<String, String> parametres) {
|
|
|
64 |
String parametresUrl = "";
|
|
|
65 |
if (parametres != null && parametres.size() > 0) {
|
|
|
66 |
parametresUrl = "?";
|
|
|
67 |
Iterator<String> iterateur = parametres.keySet().iterator();
|
|
|
68 |
while (iterateur.hasNext()) {
|
|
|
69 |
String cle = iterateur.next();
|
|
|
70 |
parametresUrl += cle + "=" + parametres.get(cle);
|
182 |
gduche |
71 |
|
748 |
jpm |
72 |
if (iterateur.hasNext()) {
|
|
|
73 |
parametresUrl = parametresUrl + "&";
|
182 |
gduche |
74 |
}
|
|
|
75 |
}
|
|
|
76 |
}
|
748 |
jpm |
77 |
return parametresUrl;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private static String construireUrlChemin(String[] morceauxDuChemin) {
|
|
|
81 |
String cheminUrl = "";
|
|
|
82 |
if (morceauxDuChemin != null && morceauxDuChemin.length > 0) {
|
|
|
83 |
cheminUrl = "/";
|
|
|
84 |
for (int i = 0; i < morceauxDuChemin.length; i++) {
|
|
|
85 |
cheminUrl += (morceauxDuChemin[i] != null ? morceauxDuChemin[i] : "*") + "/";
|
148 |
gduche |
86 |
}
|
|
|
87 |
}
|
748 |
jpm |
88 |
return cheminUrl;
|
148 |
gduche |
89 |
}
|
|
|
90 |
|
748 |
jpm |
91 |
}
|