Subversion Repositories eFlore/Applications.cel

Rev

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

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