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