Subversion Repositories eFlore/Applications.coel

Rev

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

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