1175 |
aurelien |
1 |
package org.tela_botanica.del.client.services.rest;
|
|
|
2 |
|
|
|
3 |
import java.util.HashMap;
|
|
|
4 |
import java.util.Iterator;
|
|
|
5 |
|
|
|
6 |
import org.tela_botanica.del.client.config.Config;
|
|
|
7 |
import org.tela_botanica.del.client.modeles.Utilisateur;
|
|
|
8 |
import org.tela_botanica.del.client.services.rest.async.ListePreferencesCallback;
|
|
|
9 |
import org.tela_botanica.del.client.services.rest.async.ModificationPreferencesCallback;
|
|
|
10 |
import org.tela_botanica.del.client.services.rest.async.PHPCallback.ModeRequete;
|
|
|
11 |
|
|
|
12 |
import com.google.gwt.http.client.RequestBuilder;
|
|
|
13 |
import com.google.gwt.user.client.Window;
|
|
|
14 |
|
|
|
15 |
public class PreferencesServiceConcret implements PreferencesService {
|
|
|
16 |
|
|
|
17 |
private String baseUrl;
|
|
|
18 |
|
|
|
19 |
public PreferencesServiceConcret() {
|
|
|
20 |
Config config = new Config();
|
|
|
21 |
this.baseUrl = config.getServiceBaseUrl();
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
public PreferencesServiceConcret(Config config) {
|
|
|
25 |
this.baseUrl = config.getServiceBaseUrl();
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
@Override
|
|
|
29 |
public void modifierPreferences(Utilisateur utilisateur, HashMap<String, String> preferences, ModificationPreferencesCallback callback) {
|
|
|
30 |
String idUtilisateur = utilisateur.getId();
|
|
|
31 |
String urlService = baseUrl+"utilisateurs/"+idUtilisateur+"/preferences/";
|
|
|
32 |
RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, urlService);
|
|
|
33 |
|
|
|
34 |
callback.setMode(ModeRequete.MODIFICATION);
|
|
|
35 |
String chainePost = assemblerRequeteModification(preferences);
|
|
|
36 |
try {
|
|
|
37 |
rb.sendRequest(chainePost, callback);
|
|
|
38 |
} catch (Exception e) {
|
|
|
39 |
//TODO: quoi faire si la requete est mal formée coté client avant d'être envoyée ?
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
private String assemblerRequeteModification(HashMap<String, String> preferences) {
|
|
|
44 |
String chaine = "";
|
|
|
45 |
Iterator<String> it = preferences.keySet().iterator();
|
|
|
46 |
while (it.hasNext()) {
|
|
|
47 |
String cle = it.next();
|
|
|
48 |
if(!chaine.equals("")) {
|
|
|
49 |
chaine += "&";
|
|
|
50 |
}
|
|
|
51 |
chaine += cle+"="+preferences.get(cle);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
return chaine;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
public void chargerPreferences(Utilisateur utilisateur,
|
|
|
59 |
ListePreferencesCallback callback) {
|
|
|
60 |
String idUtilisateur = utilisateur.getId();
|
|
|
61 |
String urlService = baseUrl+"utilisateurs/"+idUtilisateur+"/preferences/";
|
|
|
62 |
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, urlService);
|
|
|
63 |
|
|
|
64 |
callback.setMode(ModeRequete.LECTURE);
|
|
|
65 |
try {
|
|
|
66 |
rb.sendRequest(null, callback);
|
|
|
67 |
} catch (Exception e) {
|
|
|
68 |
//TODO: quoi faire si la requete est mal formée coté client avant d'être envoyée ?
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}
|