Subversion Repositories eFlore/Archives.cel-v1

Rev

Rev 29 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12 ddelon 1
package org.tela_botanica.client;
2
 
28 ddelon 3
import com.google.gwt.http.client.URL;
4
import com.google.gwt.json.client.JSONArray;
5
import com.google.gwt.json.client.JSONParser;
6
import com.google.gwt.json.client.JSONString;
7
import com.google.gwt.json.client.JSONValue;
8
import com.google.gwt.user.client.HTTPRequest;
9
import com.google.gwt.user.client.ResponseTextHandler;
10
 
12 ddelon 11
public class InventoryItem {
12
 
27 ddelon 13
	// TODO : Singleton ?
12 ddelon 14
 
15
	private String name=null;
16
	private String nomenclaturalNumber=null;
17
	private String location=null;
18
	private String location_id=null;
19
	private String date=null;
29 ddelon 20
	private String lieudit=null;
21
	private String station=null;
30 ddelon 22
	private String milieu=null;
12 ddelon 23
	private String comment=null;
13 ddelon 24
	private String ordre=null;
12 ddelon 25
 
28 ddelon 26
	private Mediator mediator = null;
27
	private String serviceBaseUrl = null;
27 ddelon 28
 
12 ddelon 29
 
28 ddelon 30
 
27 ddelon 31
//	 Constructeur
32
 
33
 
34
	// Constructeur sur saisie
35
 
28 ddelon 36
	public InventoryItem(Mediator med) {
12 ddelon 37
 
28 ddelon 38
			mediator=med;
39
 
40
			serviceBaseUrl = mediator.getServiceBaseUrl();
41
 
42
 
27 ddelon 43
	}
44
 
45
 
46
	// Alimentation contenu depuis saisie
47
 
30 ddelon 48
	public void setContent(String name,String nomenclaturalNumber, String location, String location_id, String date, String lieudit, String station, String milieu, String comment,String ordre) {
27 ddelon 49
 
12 ddelon 50
 
51
		this.name=name;
52
		this.nomenclaturalNumber=nomenclaturalNumber;
53
 
54
	 // Suppresion indication departementale (on pourrait faire mieux !!)
55
		int pos=location.indexOf(" (" );
56
	    if (pos>=0) {
57
	    	this.location=location.substring(0,pos);
58
	    }
59
		else {
60
			 this.location=location;
61
		}
62
 
63
		this.location_id=location_id;
29 ddelon 64
		if (location.compareTo("")==0) this.location_id="";
65
		if (location_id==null) this.location_id="";
12 ddelon 66
		this.date=date;
29 ddelon 67
		this.lieudit=lieudit;
68
		this.station=station;
30 ddelon 69
		this.milieu=milieu;
12 ddelon 70
		this.comment=comment;
13 ddelon 71
		this.ordre=ordre;
12 ddelon 72
 
73
	}
74
 
28 ddelon 75
 
76
	public boolean isValid() {
77
		// TODO : controle date
78
 
79
	    if (name.compareTo("")==0) {
80
	    	return false;
81
	    }
82
	    else {
83
	    	return true;
84
	    }
85
 
86
	}
12 ddelon 87
 
28 ddelon 88
	/**
89
	 *  Lancement de la creation d'une ligne d'inventaire (appele par Mediator.onAddInventoryItem())
90
	 *
91
	 */
92
 
93
 
94
	public void addelement() {
95
 
96
 
97
	if (isValid()) {
98
 
99
 
100
				// Creation d'un nom faisant parti du referentiel : recherche du nom valide correspondant
27 ddelon 101
 
28 ddelon 102
				if (nomenclaturalNumber !=null) {
103
 
104
					HTTPRequest.asyncGet(serviceBaseUrl + "/NameValid/" + nomenclaturalNumber,
105
							new ResponseTextHandler() {
106
 
107
								public void onCompletion(String strcomplete) {
108
 
109
									JSONValue jsonValue = JSONParser.parse(strcomplete);
110
									JSONArray jsonArray;
111
 
112
									if ((jsonArray = jsonValue.isArray()) != null) {
113
										// Nom retenu, Num Nomen nom retenu, Num Taxon,
114
										// Famille
115
										addElement(name, nomenclaturalNumber,
116
												Util.toCelString(((JSONString) jsonArray.get(0))
117
														.toString()),
118
												((JSONString) jsonArray.get(1))
119
														.stringValue(),
120
												((JSONString) jsonArray.get(2))
121
														.stringValue(),
122
												Util.toCelString(((JSONString) jsonArray.get(3))
123
														.toString()),
30 ddelon 124
														location,location_id,date,lieudit,station,milieu,comment);
28 ddelon 125
									}
126
								}
127
 
128
							});
129
				}
130
				// Saisie libre
131
				else {
30 ddelon 132
					addElement(name, " ", " ", " ", " ", " ",location,location_id,date,lieudit,station,milieu,comment);
28 ddelon 133
				}
134
 
135
		}
136
	else {
137
		// TODO : message d'erreur
138
		return;
12 ddelon 139
	}
28 ddelon 140
	}
141
 
142
 
143
 
144
	/**
145
	 * Ajoute effectif d'un element a l'inventaire (appel interne)
146
	 *
147
	 * @param nom_sel :
148
	 *            nom selectionne
149
	 * @param num_nom_sel :
150
	 *            numero nomenclatural nom selectionne
151
	 * @param nom_ret :
152
	 *            nom retenu
153
	 * @param num_nom_ret :
154
	 *            numero nomenclaturel nom retenu
155
	 * @param num_taxon :
156
	 *            numero taxonomique
157
	 * @param famille :
158
	 *            famille
159
	 */
12 ddelon 160
 
28 ddelon 161
	private void addElement(String nom_sel, String num_nom_sel, String nom_ret,
30 ddelon 162
			String num_nom_ret, String num_taxon, String famille,final String loc, String id_location,String dat, String lieu, String sta,  String mil, String comment) {
12 ddelon 163
 
164
 
28 ddelon 165
		HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/", "identifiant="
166
				+ mediator.getUser() + "&nom_sel=" + URL.encodeComponent(nom_sel) + "&num_nom_sel=" + num_nom_sel
167
				+ "&nom_ret=" + URL.encodeComponent(nom_ret) + "&num_nom_ret=" + num_nom_ret
168
				+ "&num_taxon=" + num_taxon + "&famille=" + URL.encodeComponent(famille) + "&location=" + URL.encodeComponent(loc) + "&id_location=" + id_location + "&date_observation=" + dat
30 ddelon 169
				+ "&lieudit="+ URL.encodeComponent(lieu) + "&station="+ URL.encodeComponent(sta) +"&milieu="+ URL.encodeComponent(mil) + "&commentaire="+ URL.encodeComponent(comment),
12 ddelon 170
 
28 ddelon 171
		new ResponseTextHandler() {
172
 
173
			public void onCompletion(String str) {
29 ddelon 174
					mediator.onInventoryUpdated(location_id,location,"all");
28 ddelon 175
			}
176
		});
12 ddelon 177
	}
178
 
28 ddelon 179
 
12 ddelon 180
 
28 ddelon 181
	/**
182
	 *  Lancement de la mise a jour d'une ligne d'inventaire (appele par Mediator.onModifyInventoryItem())
183
	 *
184
	 */
185
 
186
	public void updateElement() {
187
 
188
 
189
	if (isValid()) {
190
 
191
 
192
		// Modification d'un nom faisant parti du referentiel : recherche du nom valide correspondant
193
 
194
				if (nomenclaturalNumber !=null) {
195
 
196
					HTTPRequest.asyncGet(serviceBaseUrl + "/NameValid/" + nomenclaturalNumber,
197
							new ResponseTextHandler() {
198
 
199
								public void onCompletion(String strcomplete) {
200
 
201
									JSONValue jsonValue = JSONParser.parse(strcomplete);
202
									JSONArray jsonArray;
203
 
204
									if ((jsonArray = jsonValue.isArray()) != null) {
205
										// Nom retenu, Num Nomen nom retenu, Num Taxon,  Famille
206
										updateElement(ordre,name, nomenclaturalNumber,
207
												Util.toCelString(((JSONString) jsonArray.get(0))
208
														.toString()),
209
												((JSONString) jsonArray.get(1))
210
														.stringValue(),
211
												((JSONString) jsonArray.get(2))
212
														.stringValue(),
213
												Util.toCelString(((JSONString) jsonArray.get(3))
214
														.toString()),
30 ddelon 215
														location,location_id,date,lieudit,station, milieu,comment);
28 ddelon 216
									}
217
								}
218
 
219
							});
220
				}
221
				//  Modification d'un nom ne faisant pas parti du referentiel (saisie libre)
222
				else {
30 ddelon 223
					updateElement(ordre,name, " ", " ", " ", " ", " ",location,location_id,date,lieudit,station, milieu, comment);
28 ddelon 224
				}
225
 
226
		}
227
	else {
228
		// TODO : message d'erreur
229
		return;
12 ddelon 230
	}
28 ddelon 231
	}
12 ddelon 232
 
28 ddelon 233
 
12 ddelon 234
 
28 ddelon 235
 
12 ddelon 236
 
28 ddelon 237
	/**
238
	 * Modification effective d'un element de l'inventaire (appel interne)
239
	 *
240
	 * @param ordre : numero d'ordre
241
	 * @param nom_sel :
242
	 *            nom selectionne
243
	 * @param num_nom_sel :
244
	 *            numero nomenclatural nom selectionne
245
	 * @param nom_ret :
246
	 *            nom retenu
247
	 * @param num_nom_ret :
248
	 *            numero nomenclaturel nom retenu
249
	 * @param num_taxon :
250
	 *            numero taxonomique
251
	 * @param famille :
252
	 *            famille
253
	 */
12 ddelon 254
 
28 ddelon 255
	private void updateElement(String ordre, String nom_sel, String num_nom_sel, String nom_ret,
30 ddelon 256
			String num_nom_ret, String num_taxon, String famille,final String loc, String id_location,String dat, String lieu, String sta, String mil, String comment) {
12 ddelon 257
 
258
 
28 ddelon 259
		HTTPRequest.asyncPost(serviceBaseUrl + "/Inventory/" + mediator.getUser() + "/" +ordre + "/",
260
				 "&nom_sel=" + URL.encodeComponent(nom_sel) + "&num_nom_sel=" + num_nom_sel
261
				+ "&nom_ret=" + URL.encodeComponent(nom_ret) + "&num_nom_ret=" + num_nom_ret
262
				+ "&num_taxon=" + num_taxon + "&famille=" + URL.encodeComponent(famille) + "&location=" + URL.encodeComponent(loc) + "&id_location=" + id_location + "&date_observation=" + dat
30 ddelon 263
				+ "&lieudit="+ URL.encodeComponent(lieu) + "&station="+ URL.encodeComponent(sta) + "&milieu="+ URL.encodeComponent(mil) + "&commentaire="+ URL.encodeComponent(comment),
13 ddelon 264
 
28 ddelon 265
		new ResponseTextHandler() {
13 ddelon 266
 
28 ddelon 267
			public void onCompletion(String str) {
29 ddelon 268
				mediator.onInventoryUpdated(location_id,location,"all");
28 ddelon 269
			}
270
		});
13 ddelon 271
	}
12 ddelon 272
 
28 ddelon 273
 
12 ddelon 274
}