Subversion Repositories eFlore/Applications.coel

Rev

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

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