Subversion Repositories eFlore/Applications.coel

Rev

Rev 1049 | Rev 1097 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
985 jpm 1
package org.tela_botanica.client.modeles.commentaire;
2
 
1049 gduche 3
import java.util.HashMap;
4
 
985 jpm 5
import org.tela_botanica.client.Mediateur;
6
import org.tela_botanica.client.RegistreId;
7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
8
import org.tela_botanica.client.http.JsonRestRequestCallback;
9
import org.tela_botanica.client.interfaces.Rafraichissable;
10
import org.tela_botanica.client.modeles.Information;
1049 gduche 11
import org.tela_botanica.client.modeles.structure.StructureListe;
985 jpm 12
import org.tela_botanica.client.util.UtilDAO;
13
 
14
import com.extjs.gxt.ui.client.Registry;
15
import com.google.gwt.core.client.GWT;
16
import com.google.gwt.json.client.JSONArray;
17
import com.google.gwt.json.client.JSONObject;
18
import com.google.gwt.json.client.JSONValue;
19
 
20
public class CommentaireAsyncDao {
21
	private static final String SERVICE_NOM = "CoelCommentaire";
22
 
23
	private String utilisateurId = null;
24
	private Rafraichissable vueARafraichir = null;
25
 
26
	public CommentaireAsyncDao(Rafraichissable vueARafraichirCourrante) {
27
		vueARafraichir = vueARafraichirCourrante ;
28
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
29
	}
30
 
1049 gduche 31
	public void selectionner(final String commentaireId, String projetId, String titre, final int pageCourante, final int nbElements) {
997 jpm 32
		String[] parametres = {projetId, commentaireId, titre};
1049 gduche 33
 
34
		HashMap<String, String> restrictions = new HashMap<String, String>();
1058 gduche 35
		restrictions.put("start", String.valueOf(pageCourante*nbElements));
1049 gduche 36
		if (nbElements != -1)	{
37
			restrictions.put("limit", String.valueOf(nbElements));
38
		}
39
 
40
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);
985 jpm 41
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
42
			@Override
43
			public void surReponse(JSONValue responseValue) {
44
				if (responseValue != null) {
45
					// Si la requête est un succès, réception d'un objet ou d'un tableau
1049 gduche 46
					JSONArray responseArray = responseValue.isArray();
47
					if (responseArray.get(1).isObject() != null) {
48
						final JSONObject reponse = responseArray.get(1).isObject();
985 jpm 49
						// Transformation du tableau JSON réponse en ListeInstitution
50
						Commentaire commentaire = new Commentaire(reponse);
51
						// et on met à jour le demandeur des données
52
						vueARafraichir.rafraichir(commentaire);
53
					} else if (responseValue.isArray() != null) {
54
						final JSONArray reponse = responseValue.isArray();
1049 gduche 55
						CommentaireListe commentaires;
56
						if (reponse.get(1).isObject() != null)	{
57
							commentaires = new CommentaireListe(reponse.get(1).isArray());
58
						} else	{
59
							commentaires = new CommentaireListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
60
						}
61
						commentaires.setTaillePage(nbElements);
62
						commentaires.setPageCourante(pageCourante);
63
 
985 jpm 64
						vueARafraichir.rafraichir(commentaires);
65
					} else {
66
						GWT.log("La réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
67
					}
68
				} else {
69
					// Dans le cas, où nous demandons toutes les publication et qu'il n'y en a pas, nous retournons un objet vide
70
					if (commentaireId == null) {
71
						CommentaireListe commentaires = new CommentaireListe(0);
72
						vueARafraichir.rafraichir(commentaires);
73
					}
74
				}
75
			}
76
		});
77
	}
78
 
79
	public void ajouter(Commentaire commentaire) {
80
		String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
81
 
82
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
83
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
84
			@Override
85
			public void surReponse(JSONValue reponseValeur) {
86
				traiterReponse(reponseValeur, "ajout_commentaire");
87
			}
88
		}) ;
89
	}
90
 
91
	public void modifier(Commentaire commentaire) {
92
		String[] parametres = {commentaire.getId()};
93
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
94
 
95
		String postDonneesEncodees = commentaire.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
96
 
97
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
98
			@Override
99
			public void surReponse(JSONValue reponseValeur) {
100
				traiterReponse(reponseValeur, "modif_commentaire");
101
			}
102
		});
103
	}
104
 
105
	public void supprimer(String commentairesId) {
106
		String[] parametres = {utilisateurId, commentairesId};
107
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
108
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
109
			@Override
110
			public void surReponse(JSONValue reponseValeur) {
111
				traiterReponse(reponseValeur, "suppression_commentaire");
112
			}
113
		});
114
	}
115
 
116
	private void traiterReponse(JSONValue reponseValeur, String type) {
117
		Information info = new Information(type);
118
		// Si la requête est un succès, réception d'une chaîne
119
		if (reponseValeur.isString() != null) {
120
			info.setDonnee(reponseValeur.isString().stringValue());
121
			String idOuMessage = reponseValeur.isString().stringValue();
122
			if (idOuMessage.matches("^[0-9]+$")) {
123
				info.setDonnee(idOuMessage);
124
			} else {
125
				info.setMessage(idOuMessage);
126
			}
127
		} else {
128
			info.setDeboguage("La réponse n'est pas une chaine JSON.");
129
		}
130
		vueARafraichir.rafraichir(info);
131
	}
132
 
133
}