Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 341 Rev 358
1
package org.tela_botanica.client.modeles;
1
package org.tela_botanica.client.modeles;
2
 
2
 
3
import org.tela_botanica.client.Util;
3
import org.tela_botanica.client.Util;
4
import org.tela_botanica.client.interfaces.Rafraichissable;
4
import org.tela_botanica.client.interfaces.Rafraichissable;
5
import org.tela_botanica.client.observation.ObservationModele;
5
import org.tela_botanica.client.observation.ObservationModele;
6
 
6
 
7
import com.google.gwt.core.client.GWT;
7
import com.google.gwt.core.client.GWT;
8
import com.google.gwt.http.client.Request;
8
import com.google.gwt.http.client.Request;
9
import com.google.gwt.http.client.RequestBuilder;
9
import com.google.gwt.http.client.RequestBuilder;
10
import com.google.gwt.http.client.RequestCallback;
10
import com.google.gwt.http.client.RequestCallback;
11
import com.google.gwt.http.client.RequestException;
11
import com.google.gwt.http.client.RequestException;
12
import com.google.gwt.http.client.Response;
12
import com.google.gwt.http.client.Response;
13
import com.google.gwt.http.client.URL;
13
import com.google.gwt.http.client.URL;
14
import com.google.gwt.json.client.JSONArray;
14
import com.google.gwt.json.client.JSONArray;
15
import com.google.gwt.json.client.JSONParser;
15
import com.google.gwt.json.client.JSONParser;
16
import com.google.gwt.json.client.JSONString;
16
import com.google.gwt.json.client.JSONString;
17
import com.google.gwt.json.client.JSONValue;
17
import com.google.gwt.json.client.JSONValue;
18
import com.google.gwt.user.client.Window;
18
import com.google.gwt.user.client.Window;
19
 
19
 
20
/**
20
/**
21
 * DAO la liste des observations attachées a un observateur.
21
 * DAO la liste des observations attachées a un observateur.
22
 * @author David Delon
22
 * @author David Delon
23
 * TODO : se servir de ObservationDAO pour la lecture unitaire
23
 * TODO : se servir de ObservationDAO pour la lecture unitaire
24
 *
24
 *
25
 */
25
 */
26
public class ListeObservationAsynchroneDAO {
26
public class ListeObservationAsynchroneDAO {
27
 
27
 
28
 
28
 
29
/**
29
/**
30
 * Le modèle associé au DAO.
30
 * Le modèle associé au DAO.
31
 */
31
 */
32
 private ObservationModele observationModele = null;
32
 private ObservationModele observationModele = null;
33
 
33
 
34
 public ListeObservationAsynchroneDAO(final ObservationModele obs) {
34
 public ListeObservationAsynchroneDAO(final ObservationModele obs) {
35
  this.observationModele=obs;
35
  this.observationModele=obs;
36
 }
36
 }
37
	
37
	
38
	
38
	
39
	/**
39
	/**
40
	 * Envoie une requete au serveur jrest pour obtenir le nombre d'observation correspondant
40
	 * Envoie une requete au serveur jrest pour obtenir le nombre d'observation correspondant
41
	 * à des critères données en paramètres
41
	 * à des critères données en paramètres
42
	 * @param r le rafraichissable qui demande la mise à jour
42
	 * @param r le rafraichissable qui demande la mise à jour
43
	 * @param criteres un tableau nom/valeur des critères pour les observations
43
	 * @param criteres un tableau nom/valeur des critères pour les observations
44
	 */
44
	 */
45
	public final void obtenirListeObservation(final Rafraichissable r, final String utilisateur, final String[][] criteres)
45
	public final void obtenirListeObservation(final Rafraichissable r, final String utilisateur, final String[][] criteres)
46
	{
46
	{
47
		
47
		
48
		
48
		
49
		String requete = "" ;
49
		String requete = "" ;
50
			
50
			
51
		if(criteres != null)
51
		if(criteres != null)
52
		{
52
		{
53
			// on construit les paramètres du get avec les critères (&critere1=valeur1&critere2=valeur2 etc...)
53
			// on construit les paramètres du get avec les critères (&critere1=valeur1&critere2=valeur2 etc...)
54
			// ils contiennent limite et taille page et autres filtres (communes , lieu dit etc ...)S
54
			// ils contiennent limite et taille page et autres filtres (communes , lieu dit etc ...)S
55
			
55
			
56
			for (int i = 0; i < criteres.length; i++) {
56
			for (int i = 0; i < criteres.length; i++) {
57
				
57
				
58
				if(criteres[i][1] != null && !criteres[i][1].equals(""))
58
				if(criteres[i][1] != null && !criteres[i][1].equals(""))
59
				{
59
				{
60
					if(i!= 0)
60
					if(i!= 0)
61
					{
61
					{
62
						requete += "&";
62
						requete += "&";
63
					}
63
					}
64
					requete += criteres[i][0]+"="+URL.encodeComponent(criteres[i][1]) ;
64
					requete += criteres[i][0]+"="+URL.encodeComponent(criteres[i][1]) ;
65
				}
65
				}
66
			}
66
			}
67
		}
67
		}
68
		
68
		
69
		// on envoie le get asynchrone
69
		// on envoie le get asynchrone
70
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,Configuration.getServiceBaseUrl()
70
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET,Configuration.getServiceBaseUrl()
71
				+"/InventoryObservationList/"+utilisateur+"/"+requete) ;
71
				+"/InventoryObservationList/"+utilisateur+"/"+requete) ;
72
		
72
		
73
		try {
73
		try {
74
			rb.sendRequest(null, new RequestCallback() {
74
			rb.sendRequest(null, new RequestCallback() {
75
 
75
 
76
				public void onError(final Request request, final Throwable exception) {
76
				public void onError(final Request request, final Throwable exception) {
77
					// TODO Auto-generated method stub
77
					// TODO Auto-generated method stub
78
					
78
					
79
				}
79
				}
80
 
80
 
81
				public void onResponseReceived(final Request request,
81
				public void onResponseReceived(final Request request,
82
						final Response response) {
82
						final Response response) {
83
					
83
					
84
					final ListeObservation observationData ;
84
					final ListeObservation observationData ;
85
					final JSONValue responseValue = JSONParser.parse(response.getText());
85
					final JSONValue responseValue = JSONParser.parse(response.getText());
86
								
86
								
87
					JSONArray reponse=null;
87
					JSONArray reponse=null;
88
					
88
					
89
					// si c'est un tableau
89
					// si c'est un tableau
90
					if ((reponse=responseValue.isArray()) != null) {
90
					if ((reponse=responseValue.isArray()) != null) {
91
						
91
						
92
						JSONArray observation;
92
						JSONArray observation;
93
						final int taillemax = reponse.size();
93
						final int taillemax = reponse.size();
94
						
94
						
95
						observationData = new ListeObservation(taillemax);
95
						observationData = new ListeObservation(taillemax);
96
						
96
						
97
						for (int i = 0; i < taillemax; i++) {
97
						for (int i = 0; i < taillemax; i++) {
98
							
98
							
99
							if ((observation=reponse.get(i).isArray()) != null) {								
99
							if ((observation=reponse.get(i).isArray()) != null) {								
100
								
100
								
101
								String transmis=((JSONString) observation.get(13)).stringValue();
101
								String transmis=((JSONString) observation.get(13)).stringValue();
102
								String identifiantLocalite=((JSONString) observation.get(14)).toString();
102
								String identifiantLocalite=((JSONString) observation.get(14)).toString();
103
								String nomSaisi=Util.toCelString(((JSONString) observation.get(0)).toString());
103
								String nomSaisi=Util.toCelString(((JSONString) observation.get(0)).toString());
104
								String nomRetenu=Util.toCelString(((JSONString) observation.get(2)).toString());
104
								String nomRetenu=Util.toCelString(((JSONString) observation.get(2)).toString());
105
								String numeroNomenclaturalSaisi=((JSONString) observation.get(1)).stringValue();
105
								String numeroNomenclaturalSaisi=((JSONString) observation.get(1)).stringValue();
106
								String numeroNomenclaturalRetenu=((JSONString) observation.get(3)).stringValue();
106
								String numeroNomenclaturalRetenu=((JSONString) observation.get(3)).stringValue();
107
								String numeroTaxonomique=((JSONString) observation.get(4)).stringValue();
107
								String numeroTaxonomique=((JSONString) observation.get(4)).stringValue();
108
								String famille=Util.toCelString(((JSONString) observation .get(5)).toString());
108
								String famille=Util.toCelString(((JSONString) observation .get(5)).toString());
109
								String localite=Util.toCelString(((JSONString) observation .get(6)).toString());
109
								String localite=Util.toCelString(((JSONString) observation .get(6)).toString());
110
								String lieudit=Util.toCelString(((JSONString) observation .get(9)).toString());
110
								String lieudit=Util.toCelString(((JSONString) observation .get(9)).toString());
111
								String station=Util.toCelString(((JSONString) observation .get(10)).toString());
111
								String station=Util.toCelString(((JSONString) observation .get(10)).toString());
112
								String milieu=Util.toCelString(((JSONString) observation .get(11)).toString());
112
								String milieu=Util.toCelString(((JSONString) observation .get(11)).toString());
113
								String commentaire=Util.toCelString(((JSONString) observation .get(12)).toString());
113
								String commentaire=Util.toCelString(((JSONString) observation .get(12)).toString());
114
								String date=((JSONString) observation .get(8)).stringValue();
114
								String date=((JSONString) observation .get(8)).stringValue();
115
								String numeroOrdre=((JSONString) observation.get(7)).stringValue();
115
								String numeroOrdre=((JSONString) observation.get(7)).stringValue();
116
								String coordX=((JSONString) observation.get(15)).stringValue();
116
								String coordX=((JSONString) observation.get(15)).stringValue();
117
								String coordY=((JSONString) observation.get(16)).stringValue();
117
								String coordY=((JSONString) observation.get(16)).stringValue();
118
								String motsCles=((JSONString) observation.get(17)).stringValue();
118
								String motsCles=((JSONString) observation.get(17)).stringValue();
119
								
119
								
120
								Observation obs=new Observation(transmis, nomSaisi, nomRetenu, numeroNomenclaturalSaisi, numeroNomenclaturalRetenu ,numeroTaxonomique, famille, localite, identifiantLocalite, lieudit, station, milieu, commentaire, date, numeroOrdre/*, motsCles*/);
120
								Observation obs=new Observation(transmis, nomSaisi, nomRetenu, numeroNomenclaturalSaisi, numeroNomenclaturalRetenu ,numeroTaxonomique, famille, localite, identifiantLocalite, lieudit, station, milieu, commentaire, date, numeroOrdre/*, motsCles*/);
121
								obs.setCoordonneeX(coordX);
121
								obs.setCoordonneeX(coordX);
122
								obs.setCoordonneeY(coordY);
122
								obs.setCoordonneeY(coordY);
123
								obs.setMotsCles(motsCles);
123
								obs.setMotsCles(motsCles);
124
								observationData.put(obs.getNumeroOrdre(),obs);
124
								observationData.put(obs.getNumeroOrdre(),obs);
125
								
125
								
126
								
126
								
127
							}
127
							}
128
						}
128
						}
129
					} else {
129
					} else {
130
						
130
						
131
						observationData = new ListeObservation(0) ;
131
						observationData = new ListeObservation(0) ;
132
					}
132
					}
133
										
133
										
134
					// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
134
					// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est
135
					// le premier à les recevoir
135
					// le premier à les recevoir
136
					
136
					
137
					// TODO : ce n'est pas ici qu'on devrait le decider ..
137
					// TODO : ce n'est pas ici qu'on devrait le decider ..
138
					r.rafraichir(observationData,true);	
138
					r.rafraichir(observationData,true);	
139
				}
139
				}
140
			}) ;
140
			}) ;
141
 
141
 
142
		} catch (RequestException e) {
142
		} catch (RequestException e) {
143
			// TODO Auto-generated catch block
143
			// TODO Auto-generated catch block
144
			e.printStackTrace();
144
			e.printStackTrace();
145
		}
145
		}
146
	}
146
	}
147
	
147
	
148
	public void modifierEnMasse(final Rafraichissable r, String identifiant,
148
	public void modifierEnMasse(final Rafraichissable r, String identifiant,
149
			final Observation obs) {
149
			final Observation obs) {
150
 
150
 
151
				RequestBuilder rb = new RequestBuilder(RequestBuilder.POST,Configuration.getServiceBaseUrl()+ "/InventoryObservationList/" + identifiant + "/" + obs.getNumeroOrdre()+ "/") ;
151
				RequestBuilder rb = new RequestBuilder(RequestBuilder.POST,Configuration.getServiceBaseUrl()+ "/InventoryObservationList/" + identifiant + "/" + obs.getNumeroOrdre()+ "/") ;
152
				
152
				
153
				String postData = "identifiant="
153
				String postData = "identifiant="
154
					+ identifiant ;
154
					+ identifiant ;
155
				if(obs.getNomSaisi() != null) {
155
				if(obs.getNomSaisi() != null) {
156
					postData += "&nom_sel=" + URL.encodeComponent(obs.getNomSaisi());
156
					postData += "&nom_sel=" + URL.encodeComponent(obs.getNomSaisi());
157
				}
157
				}
158
				if(obs.getNumeroNomenclaturalSaisi() != null) {
158
				if(obs.getNumeroNomenclaturalSaisi() != null) {
159
					postData += "&num_nom_sel=" + obs.getNumeroNomenclaturalSaisi();
159
					postData += "&num_nom_sel=" + obs.getNumeroNomenclaturalSaisi();
160
				}
160
				}
161
				
161
				
162
				if(obs.getIdentifiantLocalite() != null) {
162
				if(obs.getIdentifiantLocalite() != null) {
163
					postData += "&id_location=" + obs.getIdentifiantLocalite();
163
					postData += "&id_location=" + obs.getIdentifiantLocalite();
164
				}
164
				}
165
				
165
				
166
				if(obs.getLocalite() != null) {
166
				if(obs.getLocalite() != null) {
167
					postData += "&location=" + URL.encodeComponent(obs.getLocalite()); 
167
					postData += "&location=" + URL.encodeComponent(obs.getLocalite()); 
168
				}
168
				}
169
				
169
				
170
				if(obs.getDate() != null) {
170
				if(obs.getDate() != null) {
171
					postData += "&date_observation=" +  obs.getDate();
171
					postData += "&date_observation=" +  obs.getDate();
172
				}
172
				}
173
				
173
				
174
				if(obs.getLieudit() != null) {
174
				if(obs.getLieudit() != null) {
175
					postData += "&lieudit="+ URL.encodeComponent(obs.getLieudit()); 
175
					postData += "&lieudit="+ URL.encodeComponent(obs.getLieudit()); 
176
				}
176
				}
177
				
177
				
178
				if(obs.getStation() != null) {
178
				if(obs.getStation() != null) {
179
					postData += "&station="+ URL.encodeComponent(obs.getStation());
179
					postData += "&station="+ URL.encodeComponent(obs.getStation());
180
				}
180
				}
181
				
181
				
182
				if(obs.getMilieu() != null) {
182
				if(obs.getMilieu() != null) {
183
					postData += "&milieu="+ URL.encodeComponent(obs.getMilieu());
183
					postData += "&milieu="+ URL.encodeComponent(obs.getMilieu());
184
				}
184
				}
185
				
185
				
186
				if(obs.getCommentaire() != null) {
186
				if(obs.getCommentaire() != null) {
187
					postData += "&commentaire="+URL.encodeComponent(""+obs.getCommentaire());
187
					postData += "&commentaire="+URL.encodeComponent(""+obs.getCommentaire());
188
				}
188
				}
189
				
189
				
190
				if(obs.getCoordonneeX() != null ) {
190
				if(obs.getCoordonneeX() != null ) {
191
					postData += "&coord_x="+URL.encodeComponent(""+obs.getCoordonneeX());
191
					postData += "&coord_x="+URL.encodeComponent(""+obs.getCoordonneeX());
192
				}
192
				}
193
				
193
				
194
				if(obs.getCoordonneeY() != null) {
194
				if(obs.getCoordonneeY() != null) {
195
					postData += "&coord_y="+URL.encodeComponent(""+obs.getCoordonneeY());
195
					postData += "&coord_y="+URL.encodeComponent(""+obs.getCoordonneeY());
196
				}
196
				}
-
 
197
				
-
 
198
				if(obs.getMotsCles() != null) {
197
				
199
					postData += "&mots_cles="+URL.encodeComponent(obs.getMotsCles()) ;
198
				
200
				}
199
				
-
 
200
					/*+"&mots_cles="+URL.encodeComponent(obs.getMotsCles()) */;
201
 
201
				try {
202
				try {
202
					
203
					
203
					rb.sendRequest(postData, new RequestCallback() {
204
					rb.sendRequest(postData, new RequestCallback() {
204
 
205
 
205
						public void onError(Request request, Throwable exception) {
206
						public void onError(Request request, Throwable exception) {
206
							// TODO Auto-generated method stub
207
							// TODO Auto-generated method stub
207
							
208
							
208
						}
209
						}
209
 
210
 
210
						public void onResponseReceived(Request request,
211
						public void onResponseReceived(Request request,
211
								Response response) {
212
								Response response) {
212
							
213
							
213
							observationModele.obtenirListeObservation(r);
214
							observationModele.obtenirListeObservation(r);
214
							
215
							
215
						}
216
						}
216
						
217
						
217
						
218
						
218
					}) ;
219
					}) ;
219
					
220
					
220
				} catch (RequestException e) {
221
				} catch (RequestException e) {
221
					
222
					
222
			}
223
			}
223
		
224
		
224
	}
225
	}
225
 
226
 
226
	/**
227
	/**
227
	 * Supprime les observations possédant les identifiants données
228
	 * Supprime les observations possédant les identifiants données
228
	 * @param r le rafraichissable à avertir de la mise à jour
229
	 * @param r le rafraichissable à avertir de la mise à jour
229
	 * @param identifiant l'identifiant utilisateur
230
	 * @param identifiant l'identifiant utilisateur
230
	 * @param numerosOrdre les numéros d'ordre des observations séparés par des virgules
231
	 * @param numerosOrdre les numéros d'ordre des observations séparés par des virgules
231
	 */
232
	 */
232
	public void supprimerListeObservation(Rafraichissable r, String identifiant, String numerosOrdre) {
233
	public void supprimerListeObservation(Rafraichissable r, String identifiant, String numerosOrdre) {
233
		
234
		
234
		String postData = "";
235
		String postData = "";
235
		postData += "&action=DELETE";
236
		postData += "&action=DELETE";
236
 
237
 
237
		// on envoie un post avec l'id de l'image à supprimer
238
		// on envoie un post avec l'id de l'image à supprimer
238
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, 
239
		RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, 
239
				Configuration.getServiceBaseUrl()
240
				Configuration.getServiceBaseUrl()
240
				+ "/inventoryObservationList/"
241
				+ "/inventoryObservationList/"
241
				+ identifiant
242
				+ identifiant
242
				+ "/"
243
				+ "/"
243
				+ numerosOrdre);
244
				+ numerosOrdre);
244
 
245
 
245
		try {
246
		try {
246
			rb.sendRequest(postData, new RequestCallback() {
247
			rb.sendRequest(postData, new RequestCallback() {
247
 
248
 
248
				public void onError(Request request, Throwable exception) {
249
				public void onError(Request request, Throwable exception) {
249
					// TODO Auto-generated method stub
250
					// TODO Auto-generated method stub
250
 
251
 
251
				}
252
				}
252
 
253
 
253
				public void onResponseReceived(Request request,
254
				public void onResponseReceived(Request request,
254
						Response response) {
255
						Response response) {
255
 
256
 
256
					if (response.getText().equals("OK")) {
257
					if (response.getText().equals("OK")) {
257
						
258
						
258
 
259
 
259
					} else {
260
					} else {
260
						com.google.gwt.user.client.Window
261
						com.google.gwt.user.client.Window
261
								.alert("Problème lors de la mise à jour des données");
262
								.alert("Problème lors de la mise à jour des données");
262
						return ;
263
						return ;
263
					}
264
					}
264
				}
265
				}
265
 
266
 
266
			});
267
			});
267
			
268
			
268
			r.rafraichir("OK", true) ;
269
			r.rafraichir("OK", true) ;
269
 
270
 
270
		} catch (RequestException e) {
271
		} catch (RequestException e) {
271
			// TODO Auto-generated catch block
272
			// TODO Auto-generated catch block
272
			e.printStackTrace();
273
			e.printStackTrace();
273
		}
274
		}
274
		
275
		
275
	}
276
	}
276
	
277
	
277
}
278
}