| Line 1... |
Line 1... |
| 1 |
package org.tela_botanica.client.modeles;
|
1 |
package org.tela_botanica.client.modeles;
|
| Line -... |
Line 2... |
| - |
|
2 |
|
| - |
|
3 |
import org.tela_botanica.client.http.JsonRestRequestBuilder;
|
| 2 |
|
4 |
import org.tela_botanica.client.http.JsonRestRequestCallback;
|
| 3 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
5 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
| Line 4... |
Line -... |
| 4 |
import org.tela_botanica.client.util.UtilDAO;
|
- |
|
| 5 |
|
- |
|
| 6 |
import com.extjs.gxt.ui.client.widget.Info;
|
- |
|
| 7 |
import com.google.gwt.core.client.GWT;
|
- |
|
| 8 |
import com.google.gwt.http.client.Request;
|
- |
|
| 9 |
import com.google.gwt.http.client.RequestBuilder;
|
- |
|
| 10 |
import com.google.gwt.http.client.RequestCallback;
|
- |
|
| 11 |
import com.google.gwt.http.client.RequestException;
|
6 |
import org.tela_botanica.client.util.UtilDAO;
|
| 12 |
import com.google.gwt.http.client.Response;
|
- |
|
| 13 |
import com.google.gwt.json.client.JSONArray;
|
- |
|
| 14 |
import com.google.gwt.json.client.JSONException;
|
7 |
|
| Line 15... |
Line 8... |
| 15 |
import com.google.gwt.json.client.JSONParser;
|
8 |
import com.google.gwt.json.client.JSONArray;
|
| 16 |
import com.google.gwt.json.client.JSONValue;
|
9 |
import com.google.gwt.json.client.JSONValue;
|
| Line 17... |
Line -... |
| 17 |
|
- |
|
| 18 |
public class ProjetAsyncDao {
|
10 |
|
| Line 19... |
Line 11... |
| 19 |
private static final String SERVICE_NOM = "CoelProjet";
|
11 |
public class ProjetAsyncDao {
|
| 20 |
|
12 |
private static final String SERVICE_NOM = "CoelProjet";
|
| 21 |
private ProjetListe projets = null;
|
13 |
|
| Line 22... |
Line 14... |
| 22 |
private Rafraichissable vue = null;
|
14 |
private Rafraichissable vueARafraichir = null;
|
| 23 |
|
- |
|
| 24 |
public ProjetAsyncDao(Rafraichissable vueARafraichir) {
|
- |
|
| 25 |
vue = vueARafraichir;
|
15 |
|
| 26 |
}
|
- |
|
| 27 |
|
- |
|
| 28 |
public void selectionner() {
|
16 |
public ProjetAsyncDao(Rafraichissable vueARafraichirCourrante) {
|
| 29 |
|
- |
|
| 30 |
// Appel du service JREST pour avoir toutes les personnes
|
- |
|
| 31 |
RequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM);
|
- |
|
| 32 |
|
- |
|
| 33 |
try {
|
- |
|
| 34 |
rb.sendRequest(null, new RequestCallback() {
|
17 |
vueARafraichir = vueARafraichirCourrante;
|
| 35 |
|
- |
|
| 36 |
public void onError(Request request, Throwable exception) {
|
18 |
}
|
| 37 |
// Gestion des exceptions déclenchées par l'exécution de la requête
|
- |
|
| 38 |
GWT.log("Erreur à l'exécution du service "+SERVICE_NOM+" (selectionner)", exception);
|
- |
|
| 39 |
Info.display("Erreur de Requête", "Une erreur s'est produite lors de l'exécution de la requête.");
|
- |
|
| 40 |
}
|
- |
|
| 41 |
|
- |
|
| 42 |
public void onErrorHTTP(Request request, Response reponse) {
|
- |
|
| 43 |
// Gestion des erreurs HTTP renvoyé par Apache ou JRest
|
- |
|
| 44 |
Information info = new Information("erreur_jrest", JSONParser.parse(reponse.getText()).isArray());
|
- |
|
| 45 |
GWT.log("Erreur JREST - Code "+reponse.getStatusCode()+"\n"+info.getMessages().toString(), null);
|
- |
|
| 46 |
Info.display("Erreur JREST - Code "+reponse.getStatusCode(), info.toString());
|
19 |
|
| 47 |
}
|
- |
|
| 48 |
|
- |
|
| 49 |
public void onResponseReceived(Request request, Response response) {
|
- |
|
| 50 |
// Si le code de réponse HTTP ne vaut pas 200 OK, on lance le mécanise d'erreur HTTP
|
- |
|
| 51 |
if (response.getStatusCode() != 200) {
|
- |
|
| 52 |
onErrorHTTP(request, response);
|
- |
|
| 53 |
} else {
|
20 |
public void selectionner() {
|
| 54 |
// La requête doit renvoyer une chaîne de caratères, parsable avec JSON
|
21 |
final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM);
|
| 55 |
// Si ce n'est pas le cas, on log une erreur
|
22 |
rb.envoyerRequete(null, new JsonRestRequestCallback() {
|
| 56 |
if (!response.getText().equals("")) {
|
23 |
@Override
|
| 57 |
try {
|
24 |
public void surReponse(JSONValue responseValue) {
|
| 58 |
final JSONValue responseValue = JSONParser.parse(response.getText());
|
25 |
ProjetListe projets;
|
| 59 |
// Si la requête est un succès, reception d'un tableau
|
- |
|
| 60 |
if (responseValue.isArray() != null) {
|
- |
|
| 61 |
final JSONArray reponse = responseValue.isArray();
|
- |
|
| 62 |
// Transformation du tableau JSON réponse en ListePersonnes
|
- |
|
| 63 |
projets = new ProjetListe(reponse);
|
- |
|
| 64 |
}
|
- |
|
| 65 |
} catch (JSONException e) {
|
- |
|
| 66 |
// Cette exception est levée si la chaîne de caractère n'a pu être parsée par JSON
|
- |
|
| 67 |
GWT.log("ERREUR parsing JSON", e);
|
- |
|
| 68 |
Info.display("Erreur DAO", "Échec du traitement des données des projets.");
|
- |
|
| 69 |
}
|
26 |
// Si la requête est un succès, reception d'un tableau
|
| 70 |
}
|
- |
|
| 71 |
}
|
- |
|
| 72 |
|
- |
|
| 73 |
// Quelque soit l'issue, il faut retourner une liste vide.
|
- |
|
| 74 |
if (projets == null) {
|
27 |
if (responseValue.isArray() != null) {
|
| 75 |
projets = new ProjetListe();
|
- |
|
| 76 |
}
|
- |
|
| 77 |
|
28 |
final JSONArray reponse = responseValue.isArray();
|
| 78 |
// Mise à jour du demandeur des données
|
- |
|
| 79 |
vue.rafraichir(projets);
|
29 |
// Transformation du tableau JSON réponse en ListePersonnes
|
| 80 |
}
|
- |
|
| 81 |
|
30 |
projets = new ProjetListe(reponse);
|
| - |
|
31 |
} else {
|
| 82 |
});
|
32 |
projets = new ProjetListe();
|
| 83 |
} catch (RequestException e) {
|
33 |
}
|
| 84 |
// Gestion des exceptions déclenchées par la création de la requête (url non-valide ?)
|
34 |
// Mise à jour du demandeur des données
|