Subversion Repositories eFlore/Applications.cel

Rev

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

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