Subversion Repositories eFlore/Applications.coel

Rev

Rev 997 | Rev 1058 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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