Subversion Repositories eFlore/Applications.coel

Rev

Rev 1136 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1136 Rev 1327
1
package org.tela_botanica.client.modeles.projet;
1
package org.tela_botanica.client.modeles.projet;
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.Debug;
12
import org.tela_botanica.client.util.UtilDAO;
13
import org.tela_botanica.client.util.UtilDAO;
13
 
14
 
14
import com.extjs.gxt.ui.client.Registry;
15
import com.extjs.gxt.ui.client.Registry;
15
import com.extjs.gxt.ui.client.widget.Info;
16
import com.extjs.gxt.ui.client.widget.Info;
16
import com.google.gwt.core.client.GWT;
17
import com.google.gwt.core.client.GWT;
17
import com.google.gwt.json.client.JSONArray;
18
import com.google.gwt.json.client.JSONArray;
18
import com.google.gwt.json.client.JSONObject;
19
import com.google.gwt.json.client.JSONObject;
19
import com.google.gwt.json.client.JSONValue;
20
import com.google.gwt.json.client.JSONValue;
20
 
21
 
21
public class ProjetAsyncDao {
22
public class ProjetAsyncDao {
22
	private static final String SERVICE_NOM = "CoelProjet";
23
	private static final String SERVICE_NOM = "CoelProjet";
23
	
24
	
24
	String utilisateurId = null;
25
	String utilisateurId = null;
25
	private Rafraichissable vueARafraichir = null;
26
	private Rafraichissable vueARafraichir = null;
26
	
27
	
27
	public ProjetAsyncDao(Rafraichissable vueARafraichirCourrante) {
28
	public ProjetAsyncDao(Rafraichissable vueARafraichirCourrante) {
28
		vueARafraichir = vueARafraichirCourrante;
29
		vueARafraichir = vueARafraichirCourrante;
29
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
30
		utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();
30
	}
31
	}
-
 
32
	
-
 
33
	/**
-
 
34
	 * @param paginationProgressive : définit le mode de consultation de la base de données
-
 
35
	 * 			- True :	la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au
-
 
36
	 * 						fur et à mesure de la consultation des données par l'utilisateur.
-
 
37
	 * 			- False :	la consultation des données est classique : un seul appel à la base de données est effectué, le retour
-
 
38
	 * 						est renvoyé à l'appelant
-
 
39
	 * 			// FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas
31
	
40
	 */
-
 
41
	public void selectionner(final boolean paginationProgressive, final String projetId, final String nomProjet, final int start, final int nbElements) {
-
 
42
		
32
	public void selectionner(final String projetId, final String nom, final int pageCourante, final int nbElements) {
43
		String nom = (nomProjet == null) ? "%" : nomProjet+"%";
33
		String[] param = {projetId, nom};
44
		String[] param = {projetId, nom};
34
		
45
		
35
		HashMap<String, String> restrictions = new HashMap<String, String>();
46
		HashMap<String, String> restrictions = new HashMap<String, String>();
36
		restrictions.put("start", String.valueOf(pageCourante*nbElements));
-
 
-
 
47
		
37
		if (nbElements != -1)	{
48
		if (nbElements != -1)	{
38
			restrictions.put("limit", String.valueOf(nbElements));
49
			restrictions.put("limit", String.valueOf(nbElements));
39
		}
50
		}
-
 
51
		
-
 
52
		/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/
-
 
53
		if (paginationProgressive) {
-
 
54
 
-
 
55
			/** DEFINITION DU TUPLE DE DEPART **/
-
 
56
			restrictions.put("start", String.valueOf(start));
-
 
57
			
40
		
58
			/** CONSTRUCTION DE LA REQUETE **/
-
 
59
    		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, param, restrictions);
-
 
60
 
41
		final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, param, restrictions);
61
    		/** ENVOI DE LA REQUETE **/
42
		rb.envoyerRequete(null, new JsonRestRequestCallback() {
62
    		rb.envoyerRequete(null, new JsonRestRequestCallback()
-
 
63
    		{
43
			@Override
64
    			/** RECEPTION DE LA REPONSE **/
-
 
65
    			public void surReponse(JSONValue responseValue)
-
 
66
    			{
-
 
67
    				/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.
44
			public void surReponse(JSONValue responseValue) {
68
    				 * On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/
-
 
69
    				vueARafraichir.rafraichir(responseValue);
-
 
70
    			}
-
 
71
    		});
45
				if (responseValue != null) {
72
		}
-
 
73
		/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/
-
 
74
		else {
-
 
75
			
46
					// Si la requête est un succès, reception d'un objet ou d'un tableau
76
			/** DEFINITION DU TUPLE DE DEPART **/
-
 
77
			restrictions.put("start", String.valueOf(start*nbElements));			
-
 
78
			
-
 
79
			final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, param, restrictions);
47
					JSONArray responseArray = responseValue.isArray();
80
			
-
 
81
			rb.envoyerRequete(null, new JsonRestRequestCallback() {
48
					if (responseArray.get(1).isObject() != null) {
82
				
-
 
83
				public void surReponse(JSONValue responseValue) {
49
						final JSONObject reponse = responseArray.get(1).isObject();
84
					
50
						Projet projet = new Projet(reponse);
-
 
51
						
-
 
52
						Information info = new Information("selection_projet");
-
 
53
						info.setDonnee(0, projet);
-
 
54
						vueARafraichir.rafraichir(info);
85
					if (responseValue != null) {
55
					} else if (responseArray.get(1).isArray() != null) {
-
 
56
						final JSONArray reponse = responseValue.isArray();
-
 
57
						ProjetListe projets;
-
 
58
						if (responseArray.get(1).isObject() != null)	{
-
 
59
							projets = new ProjetListe(reponse.get(1).isArray());
-
 
60
						} else	{
-
 
61
							projets = new ProjetListe(reponse.get(1).isArray(), reponse.get(0).isNumber(), vueARafraichir);
-
 
62
						}
-
 
-
 
86
						
-
 
87
						JSONObject responseObject = responseValue.isObject();
-
 
88
						
-
 
89
						if (responseObject != null) {
-
 
90
							
-
 
91
							// Si la réponse est un tableau, alors c'est une liste de projets qui a été retournée
-
 
92
							if (responseObject.get("projets").isArray() != null) {
-
 
93
								
-
 
94
								JSONArray reponse = responseObject.get("projets").isArray();
-
 
95
								ProjetListe projets;
-
 
96
								projets = new ProjetListe(reponse, responseObject.get("nbElements").isNumber(), vueARafraichir);
63
						projets.setTaillePage(nbElements);
97
								projets.setTaillePage(nbElements);
-
 
98
								projets.setPageCourante(start);							
-
 
99
								
-
 
100
								vueARafraichir.rafraichir(projets);
-
 
101
							
-
 
102
							// Si la réponse est un objet, alors c'est un unique projet qui a été retournée
-
 
103
							} else if (responseObject.get("projets").isObject() != null) {
-
 
104
								
-
 
105
								JSONObject reponse = responseObject.get("projets").isObject();
-
 
106
								Projet projet = new Projet(reponse);
-
 
107
								
-
 
108
								Information info = new Information("selection_projet");
-
 
109
								info.setDonnee(0, projet);
-
 
110
								vueARafraichir.rafraichir(info);
-
 
111
							}
64
						projets.setPageCourante(pageCourante);							
112
						} else {
65
						
-
 
66
						vueARafraichir.rafraichir(projets);
-
 
67
					} else {
-
 
68
						GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);
113
							GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un tableau JSON et vaut : "+responseValue.toString(), null);
69
					}
114
						}
70
				} else {
115
					} else {
71
					// Dans le cas, où nous demandons tous les projets et qu'il n'y en a pas, nous retournons un objet vide
116
						// Dans le cas, où nous demandons tous les projets et qu'il n'y en a pas, nous retournons un objet vide
-
 
117
						if (projetId == null) {
72
					if (projetId == null) {
118
							ProjetListe projets = new ProjetListe(0);
73
						ProjetListe projets = new ProjetListe(0);
119
							vueARafraichir.rafraichir(projets);								
74
						vueARafraichir.rafraichir(projets);								
120
						}
75
					}
121
					}
76
				}
-
 
77
			}
122
				}
78
		});
123
			});
79
		
124
		}		
80
	}
125
	}
81
	
126
	
82
	public void ajouter(Projet projet) {
127
	public void ajouter(Projet projet) {
83
		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
128
		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
84
		
129
		
85
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
130
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);
86
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
131
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
87
			@Override
132
			@Override
88
			public void surReponse(JSONValue reponseValeur) {
133
			public void surReponse(JSONValue reponseValeur) {
89
				traiterReponse(reponseValeur, "ajout_projet");
134
				traiterReponse(reponseValeur, "ajout_projet");
90
			}
135
			}
91
		}) ;
136
		}) ;
92
	}
137
	}
93
	
138
	
94
	public void modifier(Projet projet) {
139
	public void modifier(Projet projet) {
95
		String[] parametres = {projet.getId()};
140
		String[] parametres = {projet.getId()};
96
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
141
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
97
		
142
		
98
		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
143
		String postDonneesEncodees = projet.obtenirChainePOST()+"&cmhl_ce_modifier_par="+utilisateurId;
99
		
144
		
100
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
145
		rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {
101
			@Override
146
			@Override
102
			public void surReponse(JSONValue reponseValeur) {
147
			public void surReponse(JSONValue reponseValeur) {
103
				traiterReponse(reponseValeur, "modif_projet");
148
				traiterReponse(reponseValeur, "modif_projet");
104
			}
149
			}
105
		});
150
		});
106
	}
151
	}
107
 
152
 
108
	public void supprimer(String projetsId) {
153
	public void supprimer(String projetsId) {
109
		String[] parametres = {utilisateurId, projetsId};
154
		String[] parametres = {utilisateurId, projetsId};
110
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
155
		final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);
111
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
156
		rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {
112
			@Override
157
			@Override
113
			public void surReponse(JSONValue reponseValeur) {
158
			public void surReponse(JSONValue reponseValeur) {
114
				traiterReponse(reponseValeur, "suppression_projet");
159
				traiterReponse(reponseValeur, "suppression_projet");
115
			}
160
			}
116
		});
161
		});
117
	}
162
	}
118
	
163
	
119
	private void traiterReponse(JSONValue reponseValeur, String type) {
164
	private void traiterReponse(JSONValue reponseValeur, String type) {
120
		Information info = new Information(type);
165
		Information info = new Information(type);
121
		String idsNonSuppr = "";
166
		String idsNonSuppr = "";
122
		
167
		
123
		// Si la requête est un succès, réception d'une chaîne
168
		// Si la requête est un succès, réception d'une chaîne
124
		if (type.equals("suppression_projet") && reponseValeur.isArray() != null)	{
169
		if (type.equals("suppression_projet") && reponseValeur.isArray() != null)	{
125
			JSONArray tableauInfo = reponseValeur.isArray();
170
			JSONArray tableauInfo = reponseValeur.isArray();
126
			idsNonSuppr = tableauInfo.get(0).isString().stringValue();
171
			idsNonSuppr = tableauInfo.get(0).isString().stringValue();
127
		} else if (reponseValeur.isString() != null) {
172
		} else if (reponseValeur.isString() != null) {
128
			String idOuMessage = reponseValeur.isString().stringValue();
173
			String idOuMessage = reponseValeur.isString().stringValue();
129
			if (idOuMessage.matches("^[0-9]+$")) {
174
			if (idOuMessage.matches("^[0-9]+$")) {
130
				info.setDonnee(idOuMessage);
175
				info.setDonnee(idOuMessage);
131
			} else {
176
			} else {
132
				info.setDonnee("");
177
				info.setDonnee("");
133
				info.setMessage(idOuMessage);
178
				info.setMessage(idOuMessage);
134
			}
179
			}
135
		} else {
180
		} else {
136
			info.setDeboguage("La réponse n'est pas une chaine JSON.");
181
			info.setDeboguage("La réponse n'est pas une chaine JSON.");
137
		}
182
		}
138
		info.setDonnee(1, idsNonSuppr);
183
		info.setDonnee(1, idsNonSuppr);
139
		vueARafraichir.rafraichir(info);
184
		vueARafraichir.rafraichir(info);
140
	}
185
	}
141
}
186
}