989 |
aurelien |
1 |
package org.tela_botanica.client.modeles.dao;
|
728 |
aurelien |
2 |
|
|
|
3 |
import java.util.HashMap;
|
|
|
4 |
|
|
|
5 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
989 |
aurelien |
6 |
import org.tela_botanica.client.modeles.objets.Configuration;
|
|
|
7 |
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso;
|
|
|
8 |
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso.TypesReferentiels;
|
728 |
aurelien |
9 |
import org.tela_botanica.client.observation.ObservationModele;
|
|
|
10 |
|
|
|
11 |
import com.google.gwt.http.client.Request;
|
1918 |
aurelien |
12 |
import org.tela_botanica.client.util.RequestBuilderWithCredentials;
|
728 |
aurelien |
13 |
import com.google.gwt.http.client.RequestCallback;
|
|
|
14 |
import com.google.gwt.http.client.RequestException;
|
|
|
15 |
import com.google.gwt.http.client.Response;
|
|
|
16 |
import com.google.gwt.http.client.URL;
|
|
|
17 |
import com.google.gwt.json.client.JSONArray;
|
|
|
18 |
import com.google.gwt.json.client.JSONParser;
|
|
|
19 |
import com.google.gwt.json.client.JSONString;
|
|
|
20 |
import com.google.gwt.json.client.JSONValue;
|
|
|
21 |
|
|
|
22 |
public class ListeReferentielPersoAsynchroneDAO {
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Cache
|
|
|
26 |
*
|
|
|
27 |
*/
|
|
|
28 |
private HashMap<String,ListeReferentielPerso> cache = new HashMap();
|
|
|
29 |
|
|
|
30 |
private TypesReferentiels refDemande = null;
|
|
|
31 |
|
|
|
32 |
public ListeReferentielPersoAsynchroneDAO(ObservationModele obs) {
|
|
|
33 |
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public void setRefDemande(TypesReferentiels refDemande) {
|
|
|
37 |
this.refDemande = refDemande;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public void obtenirListeDonnees(final Rafraichissable r, String utilisateur, final String critere) {
|
|
|
41 |
|
1918 |
aurelien |
42 |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +
|
728 |
aurelien |
43 |
"/SelfRefList/"+
|
|
|
44 |
utilisateur+"/"+
|
|
|
45 |
refDemande.toString()+
|
|
|
46 |
"?start=0&limit=50&recherche="+URL.encode(critere));
|
|
|
47 |
|
|
|
48 |
try {
|
|
|
49 |
|
|
|
50 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
51 |
|
1293 |
aurelien |
52 |
@Override
|
728 |
aurelien |
53 |
public void onError(final Request request, final Throwable exception) {
|
|
|
54 |
// TODO Auto-generated method stub
|
|
|
55 |
|
|
|
56 |
}
|
|
|
57 |
|
1293 |
aurelien |
58 |
@Override
|
728 |
aurelien |
59 |
public void onResponseReceived(final Request request,
|
|
|
60 |
final Response response) {
|
|
|
61 |
|
|
|
62 |
final ListeReferentielPerso referentielData;
|
|
|
63 |
|
|
|
64 |
final JSONValue responseValue = JSONParser.parse(response.getText());
|
|
|
65 |
|
|
|
66 |
JSONArray reponse = null;
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
// si c'est un tableau
|
|
|
70 |
if ((reponse = responseValue.isArray()) != null) {
|
|
|
71 |
|
|
|
72 |
JSONString elementsRef;
|
|
|
73 |
final int taillemax = reponse.size();
|
|
|
74 |
|
|
|
75 |
referentielData = new ListeReferentielPerso(taillemax, refDemande);
|
|
|
76 |
|
|
|
77 |
for (int i = 0; i < taillemax; i++) {
|
|
|
78 |
if ((elementsRef = reponse.get(i).isString()) != null) {
|
|
|
79 |
|
|
|
80 |
String valeur = elementsRef.stringValue();
|
|
|
81 |
referentielData.put(i+"", valeur);
|
|
|
82 |
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
} else {
|
|
|
86 |
|
|
|
87 |
referentielData = new ListeReferentielPerso(0, refDemande);
|
|
|
88 |
|
|
|
89 |
}
|
|
|
90 |
// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
|
|
|
91 |
// le premier à les recevoir
|
|
|
92 |
r.rafraichir(referentielData, true);
|
|
|
93 |
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
});
|
|
|
97 |
|
|
|
98 |
} catch (RequestException e) {
|
|
|
99 |
|
|
|
100 |
e.printStackTrace();
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|