Subversion Repositories eFlore/Applications.coel

Rev

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

Rev Author Line No. Line
935 jpm 1
package org.tela_botanica.client.modeles.projet;
147 gduche 2
 
1045 gduche 3
import java.util.HashMap;
4
 
890 aurelien 5
import org.tela_botanica.client.Mediateur;
6
import org.tela_botanica.client.RegistreId;
751 jpm 7
import org.tela_botanica.client.http.JsonRestRequestBuilder;
8
import org.tela_botanica.client.http.JsonRestRequestCallback;
147 gduche 9
import org.tela_botanica.client.interfaces.Rafraichissable;
935 jpm 10
import org.tela_botanica.client.modeles.Information;
1045 gduche 11
import org.tela_botanica.client.modeles.structure.StructureListe;
147 gduche 12
import org.tela_botanica.client.util.UtilDAO;
13
 
890 aurelien 14
import com.extjs.gxt.ui.client.Registry;
929 jpm 15
import com.google.gwt.core.client.GWT;
147 gduche 16
import com.google.gwt.json.client.JSONArray;
879 aurelien 17
import com.google.gwt.json.client.JSONObject;
147 gduche 18
import com.google.gwt.json.client.JSONValue;
19
 
268 jp_milcent 20
public class ProjetAsyncDao {
21
	private static final String SERVICE_NOM = "CoelProjet";
147 gduche 22
 
890 aurelien 23
	String utilisateurId = null;
751 jpm 24
	private Rafraichissable vueARafraichir = null;
147 gduche 25
 
751 jpm 26
	public ProjetAsyncDao(Rafraichissable vueARafraichirCourrante) {
27
		vueARafraichir = vueARafraichirCourrante;
890 aurelien 28
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
147 gduche 29
	}
30
 
1045 gduche 31
	public void selectionner(final String projetId, final String nom, final int pageCourante, final int nbElements) {
32
		String[] param = {projetId, nom};
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
		}
39
 
40
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, param, restrictions);
751 jpm 41
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
42
			@Override
43
			public void surReponse(JSONValue responseValue) {
929 jpm 44
				if (responseValue != null) {
45
					// Si la requête est un succès, reception d'un objet ou d'un tableau
1045 gduche 46
					JSONArray responseArray = responseValue.isArray();
47
					if (responseArray.get(1).isObject() != null) {
48
						final JSONObject reponse = responseArray.get(1).isObject();
929 jpm 49
						Projet projet = new Projet(reponse);
50
 
51
						Information info = new Information("selection_projet");
52
						info.setDonnee(0, projet);
53
						vueARafraichir.rafraichir(info);
1045 gduche 54
					} else if (responseArray.get(1).isArray() != null) {
929 jpm 55
						final JSONArray reponse = responseValue.isArray();
1045 gduche 56
						ProjetListe projets;
57
						if (responseArray.get(1).isObject() != null)	{
58
							projets = new ProjetListe(reponse.get(1).isArray());
59
						} else	{
60
							projets = new ProjetListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
61
						}
62
						projets.setTaillePage(nbElements);
63
						projets.setPageCourante(pageCourante);
64
 
929 jpm 65
						vueARafraichir.rafraichir(projets);
66
					} else {
67
						GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
68
					}
751 jpm 69
				} else {
929 jpm 70
					// Dans le cas, où nous demandons tous les projets et qu'il n'y en a pas, nous retournons un objet vide
71
					if (projetId == null) {
72
						ProjetListe projets = new ProjetListe(0);
73
						vueARafraichir.rafraichir(projets);
74
					}
147 gduche 75
				}
751 jpm 76
			}
77
		});
879 aurelien 78
 
79
	}
890 aurelien 80
 
81
	public void ajouter(Projet projet) {
82
		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
83
 
84
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
85
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
86
			@Override
87
			public void surReponse(JSONValue reponseValeur) {
929 jpm 88
				traiterReponse(reponseValeur, "ajout_projet");
890 aurelien 89
			}
90
		}) ;
91
	}
92
 
93
	public void modifier(Projet projet) {
94
		String[] parametres = {projet.getId()};
95
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
96
 
97
		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
98
 
99
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
100
			@Override
101
			public void surReponse(JSONValue reponseValeur) {
929 jpm 102
				traiterReponse(reponseValeur, "modif_projet");
890 aurelien 103
			}
104
		});
105
	}
106
 
107
	public void supprimer(String projetsId) {
108
		String[] parametres = {utilisateurId, projetsId};
109
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
110
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
111
			@Override
112
			public void surReponse(JSONValue reponseValeur) {
113
				traiterReponse(reponseValeur, "suppression_projet");
114
			}
115
		});
116
	}
117
 
118
	private void traiterReponse(JSONValue reponseValeur, String type) {
119
		Information info = new Information(type);
120
		// Si la requête est un succès, réception d'une chaîne
121
		if (reponseValeur.isString() != null) {
122
			info.setDonnee(reponseValeur.isString().stringValue());
123
		} else {
124
			info.setDeboguage("La réponse n'est pas une chaine JSON.");
125
		}
126
		vueARafraichir.rafraichir(info);
127
	}
151 jpm 128
}