Subversion Repositories eFlore/Applications.cel

Rev

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