Subversion Repositories eFlore/Applications.cel

Rev

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

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