Subversion Repositories eFlore/Applications.coel

Rev

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

Rev Author Line No. Line
126 gduche 1
package org.tela_botanica.client.modeles;
351 gduche 2
 
593 gduche 3
import java.util.HashMap;
4
 
317 gduche 5
import org.tela_botanica.client.Mediateur;
126 gduche 6
import org.tela_botanica.client.RegistreId;
748 jpm 7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
8
import org.tela_botanica.client.http.JsonRestRequestCallback;
126 gduche 9
import org.tela_botanica.client.interfaces.Rafraichissable;
146 gduche 10
import org.tela_botanica.client.util.UtilDAO;
126 gduche 11
import com.extjs.gxt.ui.client.Registry;
12
import com.google.gwt.core.client.GWT;
13
import com.google.gwt.json.client.JSONArray;
14
import com.google.gwt.json.client.JSONValue;
15
 
268 jp_milcent 16
public class PersonneAsyncDao {
17
	private static final String SERVICE_NOM = "CoelPersonne";
126 gduche 18
 
748 jpm 19
	private String utilisateurId = null;
277 jp_milcent 20
	private Rafraichissable vueARafraichir = null;
126 gduche 21
 
277 jp_milcent 22
	public PersonneAsyncDao(Rafraichissable vue) {
23
		vueARafraichir = vue;
748 jpm 24
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
126 gduche 25
	}
268 jp_milcent 26
 
654 gduche 27
	public void selectionner(String personneId, String projetId, String nomComplet, final int pageCourante, final int nbElements) {
748 jpm 28
		String[] parametres = {personneId, projetId, nomComplet};
415 gduche 29
 
748 jpm 30
		HashMap<String, String> restrictions = new HashMap<String, String>();
31
		restrictions.put("start", String.valueOf(pageCourante));
593 gduche 32
		if (nbElements != -1)	{
748 jpm 33
			restrictions.put("limit", String.valueOf(nbElements));
593 gduche 34
		}
35
 
748 jpm 36
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions, "GET");
37
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
38
			@Override
39
			public void surReponse(JSONValue responseValue) {
40
				// Si la requête est un succès, réception d'un tableau
41
				if (responseValue.isArray() != null) {
42
					final JSONArray reponse = responseValue.isArray();
43
 
44
					// Transformation du tableau JSON réponse en ListeInstitution
45
					Information info = new Information("liste_personne");
46
					PersonneListe personnes;
47
					if (reponse.get(0).isArray() != null)	{
48
						personnes = new PersonneListe(reponse);
241 jp_milcent 49
					} else {
748 jpm 50
						personnes = new PersonneListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
241 jp_milcent 51
					}
748 jpm 52
					personnes.setTaillePage(nbElements);
53
					personnes.setPageCourante(pageCourante);
54
					info.setDonnee(0, personnes);
55
 
56
					// et on met à jour le demandeur des données
57
					vueARafraichir.rafraichir(info);
58
				} else {
59
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un tableau JSON et vaut : "+responseValue.toString(), null);
241 jp_milcent 60
				}
748 jpm 61
			}
62
		});
126 gduche 63
	}
241 jp_milcent 64
 
748 jpm 65
	public void supprimer(String structureId) {
66
		String[] parametres = {utilisateurId, structureId};
67
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, "POST");
68
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
69
			@Override
70
			public void surReponse(JSONValue responseValue) {
71
				if (responseValue.isString() != null) {
72
					Information info = new Information("suppression_personne", responseValue.isString().stringValue());
73
					vueARafraichir.rafraichir(info);
74
				} else {
75
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
220 aurelien 76
				}
748 jpm 77
			}
78
		});
220 aurelien 79
	}
387 gduche 80
 
748 jpm 81
	public void ajouter(Personne personne) {
82
		String postDonneesEncodees = personne.obtenirChainePOST();
83
		postDonneesEncodees += "&cmhl_ce_modifier_par=" + utilisateurId;
432 gduche 84
 
748 jpm 85
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
86
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
87
			@Override
88
			public void surReponse(JSONValue responseValue) {
89
				if (responseValue.isString() != null) {
90
					Information info = new Information("ajout_personne", responseValue.isString().stringValue());
91
					vueARafraichir.rafraichir(info);
92
				} else {
93
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
432 gduche 94
				}
748 jpm 95
			}
96
		});
432 gduche 97
	}
98
 
748 jpm 99
	public void modifier(Personne personne) {
100
		String postDonneesEncodees = personne.obtenirChainePOST();
101
		postDonneesEncodees += "&cmhl_ce_modifier_par=" + utilisateurId;
432 gduche 102
 
748 jpm 103
		String[] parametres = {personne.getId()};
104
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, "POST");
105
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
106
			@Override
107
			public void surReponse(JSONValue responseValue) {
108
				// Si la requête est un succès, reception d'une chaine
109
				if (responseValue.isString() != null) {
110
					Information info = new Information("modification_personne", responseValue.isString().stringValue());
111
					vueARafraichir.rafraichir(info);
112
				} else {
113
					GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);
387 gduche 114
				}
748 jpm 115
			}
116
		});
387 gduche 117
	}
126 gduche 118
 
119
}