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