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 |
Window.alert(""+preferences.size());
|
|
|
45 |
String chaine = "";
|
|
|
46 |
Iterator<String> it = preferences.keySet().iterator();
|
|
|
47 |
while (it.hasNext()) {
|
|
|
48 |
String cle = it.next();
|
|
|
49 |
if(!chaine.equals("")) {
|
|
|
50 |
chaine += "&";
|
|
|
51 |
}
|
|
|
52 |
chaine += cle+"="+preferences.get(cle);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
return chaine;
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
@Override
|
|
|
59 |
public void chargerPreferences(Utilisateur utilisateur,
|
|
|
60 |
ListePreferencesCallback callback) {
|
|
|
61 |
String idUtilisateur = utilisateur.getId();
|
|
|
62 |
String urlService = baseUrl+"utilisateurs/"+idUtilisateur+"/preferences/";
|
|
|
63 |
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, urlService);
|
|
|
64 |
|
|
|
65 |
callback.setMode(ModeRequete.LECTURE);
|
|
|
66 |
try {
|
|
|
67 |
rb.sendRequest(null, callback);
|
|
|
68 |
} catch (Exception e) {
|
|
|
69 |
//TODO: quoi faire si la requete est mal formée coté client avant d'être envoyée ?
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
}
|