Subversion Repositories eFlore/Applications.del

Rev

Rev 939 | Rev 952 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 939 Rev 941
Line 4... Line 4...
4
import java.util.Date;
4
import java.util.Date;
5
import java.util.HashMap;
5
import java.util.HashMap;
6
import java.util.Iterator;
6
import java.util.Iterator;
7
import java.util.List;
7
import java.util.List;
Line -... Line 8...
-
 
8
 
8
 
9
import org.tela_botanica.del.client.cache.CacheClient;
9
import org.tela_botanica.del.client.modeles.Commentaire;
10
import org.tela_botanica.del.client.modeles.Commentaire;
10
import org.tela_botanica.del.client.modeles.Contributeur;
11
import org.tela_botanica.del.client.modeles.Contributeur;
-
 
12
import org.tela_botanica.del.client.modeles.Image;
11
import org.tela_botanica.del.client.modeles.Image;
13
import org.tela_botanica.del.client.modeles.ImageServiceResultat;
12
import org.tela_botanica.del.client.modeles.InterventionForum;
14
import org.tela_botanica.del.client.modeles.InterventionForum;
-
 
15
import org.tela_botanica.del.client.modeles.Observation;
13
import org.tela_botanica.del.client.modeles.Observation;
16
import org.tela_botanica.del.client.modeles.ObservationServiceResultat;
-
 
17
import org.tela_botanica.del.client.modeles.PropositionDetermination;
-
 
18
import org.tela_botanica.del.client.modeles.Protocole;
14
import org.tela_botanica.del.client.modeles.PropositionDetermination;
19
import org.tela_botanica.del.client.modeles.ProtocoleServiceResultat;
15
import org.tela_botanica.del.client.modeles.Utilisateur;
20
import org.tela_botanica.del.client.modeles.Utilisateur;
16
import org.tela_botanica.del.client.modeles.VoteDetermination;
21
import org.tela_botanica.del.client.modeles.VoteDetermination;
Line 17... Line 22...
17
import org.tela_botanica.del.client.modeles.VoteProtocole;
22
import org.tela_botanica.del.client.modeles.VoteProtocole;
-
 
23
 
18
 
24
import com.google.gwt.i18n.client.DateTimeFormat;
19
import com.google.gwt.i18n.client.DateTimeFormat;
25
import com.google.gwt.json.client.JSONArray;
20
import com.google.gwt.json.client.JSONObject;
26
import com.google.gwt.json.client.JSONObject;
Line -... Line 27...
-
 
27
import com.google.gwt.json.client.JSONParser;
-
 
28
import com.google.gwt.json.client.JSONValue;
-
 
29
 
-
 
30
/**
-
 
31
 * Centralisation des methodes de parsing du code JSON retourné par les
-
 
32
 * webservices
-
 
33
 * 
21
import com.google.gwt.json.client.JSONParser;
34
 * @author LIENS
Line -... Line 35...
-
 
35
 * 
-
 
36
 */
-
 
37
public class UtilitairesServiceResultat {
-
 
38
 
-
 
39
	/**
-
 
40
	 * Recupere un objet Image à partir du JSON
22
import com.google.gwt.json.client.JSONValue;
41
	 * 
Line 23... Line 42...
23
 
42
	 * @param imageJson
24
public class UtilitairesServiceResultat {
43
	 * @return
25
 
44
	 */
Line 33... Line 52...
33
		image.setMiniature("http://www.tela-botanica.org/appli:cel-img:" + getIdAvecPadding(idImage) + "XS.jpg");
52
		image.setMiniature("http://www.tela-botanica.org/appli:cel-img:" + getIdAvecPadding(idImage) + "XS.jpg");
Line 34... Line 53...
34
 
53
 
35
		return image;
54
		return image;
Line -... Line 55...
-
 
55
	}
36
	}
56
 
-
 
57
	/**
37
 
58
	 * Recupere un objet Observation à partir du JSON
38
	public static Observation parserObservationEtCreerPropositionDetermination(JSONObject observationJson) {
59
	 * 
39
		Observation observation = parserObservationJSON(observationJson);
60
	 * @param imageJson
40
		return observation;
-
 
41
	}
61
	 * @return
Line 42... Line 62...
42
 
62
	 */
43
	public static Observation parserObservationJSON(JSONObject observationJson) {
63
	public static Observation parserObservationJSON(JSONObject observationJson) {
44
 
64
 
Line 74... Line 94...
74
			observation.ajouterPropositionDetermination(creerPropositionDeterminationAPartirObservation(observation));
94
			observation.ajouterPropositionDetermination(creerPropositionDeterminationAPartirObservation(observation));
75
		}
95
		}
76
		return observation;
96
		return observation;
77
	}
97
	}
Line -... Line 98...
-
 
98
 
-
 
99
	/**
-
 
100
	 * Créée une proposition de determination à partir d'une observation
-
 
101
	 * 
-
 
102
	 * @param observation
-
 
103
	 * @return
-
 
104
	 */
-
 
105
	private static PropositionDetermination creerPropositionDeterminationAPartirObservation(Observation observation) {
-
 
106
 
-
 
107
		String utilisateurNom = observation.getNomAuteur();
-
 
108
		String utilisateurPrenom = observation.getPrenomAuteur();
-
 
109
		String utilisateurCourriel = observation.getCourrielAuteur();
-
 
110
		String utilisateurId = observation.getIdAuteur();
-
 
111
		PropositionDetermination propositionDetermination = new PropositionDetermination(observation);
-
 
112
		Contributeur contributeur = new Contributeur(utilisateurId, utilisateurNom, utilisateurPrenom, utilisateurCourriel);
-
 
113
		propositionDetermination.setContributeur(contributeur);
-
 
114
		java.util.Date datePropDeter = parserDateObservation(observation.getDateReleve());
-
 
115
		propositionDetermination.setDate(datePropDeter);
-
 
116
		propositionDetermination.setEspece(observation.getNomRetenu());
-
 
117
 
-
 
118
		return propositionDetermination;
-
 
119
	}
-
 
120
 
-
 
121
	/**
-
 
122
	 * Recupere une liste de commentaires à partir du JSON
-
 
123
	 * 
-
 
124
	 * @param imageJson
-
 
125
	 * @return
78
 
126
	 */
79
	public static List<Commentaire> parserCommentaires(JSONObject commentaires) {
127
	public static List<Commentaire> parserCommentaires(JSONObject commentaires) {
80
		List<InterventionForum> interventionForums = parserInterventions(commentaires);
128
		List<InterventionForum> interventionForums = parserInterventions(commentaires);
81
		List<Commentaire> commentairesListe = new ArrayList<Commentaire>();
129
		List<Commentaire> commentairesListe = new ArrayList<Commentaire>();
82
		for (InterventionForum interventionForum : interventionForums) {
130
		for (InterventionForum interventionForum : interventionForums) {
Line 86... Line 134...
86
		}
134
		}
87
		return commentairesListe;
135
		return commentairesListe;
Line 88... Line 136...
88
 
136
 
Line -... Line 137...
-
 
137
	}
-
 
138
 
-
 
139
	/**
-
 
140
	 * Recupere une liste d'interventions à partir du JSON
-
 
141
	 * 
-
 
142
	 * @param imageJson
89
	}
143
	 * @return
90
 
144
	 */
Line 91... Line 145...
91
	public static List<InterventionForum> parserInterventions(JSONObject interventions) {
145
	public static List<InterventionForum> parserInterventions(JSONObject interventions) {
Line 172... Line 226...
172
 
226
 
173
		retour.addAll(interventionsNonTypees.values());
227
		retour.addAll(interventionsNonTypees.values());
174
		return retour;
228
		return retour;
Line -... Line 229...
-
 
229
	}
-
 
230
 
-
 
231
	/**
-
 
232
	 * Recupere une liste de commentaires à partir du JSON
-
 
233
	 * 
-
 
234
	 * @param imageJson
175
	}
235
	 * @return
176
 
236
	 */
177
	public static String convertirEtParserRetourAjoutCommentaire(String retour) {
237
	public static String convertirEtParserRetourAjoutCommentaire(String retour) {
178
		JSONObject retourJson = JSONParser.parseStrict(retour).isObject();
238
		JSONObject retourJson = JSONParser.parseStrict(retour).isObject();
Line -... Line 239...
-
 
239
		return parserRetourAjoutCommentaire(retourJson);
-
 
240
	}
-
 
241
 
-
 
242
	/**
-
 
243
	 * Recupere une liste de commentaires à partir d'un objet JSON
-
 
244
	 * 
179
		return parserRetourAjoutCommentaire(retourJson);
245
	 * @param imageJson
180
	}
246
	 * @return
181
 
247
	 */
182
	public static String parserRetourAjoutCommentaire(JSONObject retour) {
248
	public static String parserRetourAjoutCommentaire(JSONObject retour) {
183
		String id = "";
249
		String id = "";
Line 189... Line 255...
189
 
255
 
190
	public static String getValeurOuVide(JSONObject objet, String index) {
256
	public static String getValeurOuVide(JSONObject objet, String index) {
191
		return (objet.get(index) != null && objet.get(index).isString() != null) ? objet.get(index).isString().stringValue() : "";
257
		return (objet.get(index) != null && objet.get(index).isString() != null) ? objet.get(index).isString().stringValue() : "";
Line 192... Line -...
192
	}
-
 
193
 
258
	}
194
	public static PropositionDetermination creerPropositionDeterminationAPartirObservation(Observation observation) {
-
 
195
 
-
 
196
		String utilisateurNom = observation.getNomAuteur();
259
 
197
		String utilisateurPrenom = observation.getPrenomAuteur();
-
 
198
		String utilisateurCourriel = observation.getCourrielAuteur();
-
 
199
		String utilisateurId = observation.getIdAuteur();
-
 
200
		PropositionDetermination propositionDetermination = new PropositionDetermination(observation);
-
 
201
		Contributeur contributeur = new Contributeur(utilisateurId, utilisateurNom, utilisateurPrenom, utilisateurCourriel);
-
 
202
		propositionDetermination.setContributeur(contributeur);
-
 
203
		java.util.Date datePropDeter = parserDateObservation(observation.getDateReleve());
-
 
204
		propositionDetermination.setDate(datePropDeter);
260
	/**
205
		propositionDetermination.setEspece(observation.getNomRetenu());
261
	 * Recupere une liste de votes sur une determination à partir du JSON
206
 
262
	 * 
207
		return propositionDetermination;
263
	 * @param imageJson
208
	}
264
	 * @return
Line 209... Line 265...
209
 
265
	 */
Line 210... Line 266...
210
	public static HashMap<String, VoteDetermination> parserRetourListeVotesDetermination(String votesString) {
266
	public static HashMap<String, VoteDetermination> parserRetourListeVotesDetermination(String votesString) {
Line 217... Line 273...
217
			retour = parserVotesDetermination(resultat);
273
			retour = parserVotesDetermination(resultat);
218
		}
274
		}
219
		return retour;
275
		return retour;
220
	}
276
	}
Line -... Line 277...
-
 
277
 
-
 
278
	/**
-
 
279
	 * Recupere une liste de votes sur une determination à partir d'un objet
-
 
280
	 * JSON
-
 
281
	 * 
-
 
282
	 * @param imageJson
-
 
283
	 * @return
221
 
284
	 */
222
	public static HashMap<String, VoteDetermination> parserVotesDetermination(JSONObject votes) {
285
	public static HashMap<String, VoteDetermination> parserVotesDetermination(JSONObject votes) {
223
		HashMap<String, VoteDetermination> votesDetermination = new HashMap<String, VoteDetermination>();
286
		HashMap<String, VoteDetermination> votesDetermination = new HashMap<String, VoteDetermination>();
224
		java.util.Iterator<String> itVotes = votes.keySet().iterator();
287
		java.util.Iterator<String> itVotes = votes.keySet().iterator();
225
		while (itVotes.hasNext()) {
288
		while (itVotes.hasNext()) {
Line 239... Line 302...
239
			votesDetermination.put(getValeurOuVide(vote, "auteur.id"), vd);
302
			votesDetermination.put(getValeurOuVide(vote, "auteur.id"), vd);
240
		}
303
		}
241
		return votesDetermination;
304
		return votesDetermination;
242
	}
305
	}
Line -... Line 306...
-
 
306
 
-
 
307
	/**
-
 
308
	 * Recupere une liste de votes sur des images à partir d'un objet JSON
-
 
309
	 * 
-
 
310
	 * @param imageJson
-
 
311
	 * @return
243
 
312
	 */
Line 244... Line 313...
244
	public static HashMap<String, HashMap<String, VoteProtocole>> parserVotesProtocoles(JSONObject votes) {
313
	public static HashMap<String, HashMap<String, VoteProtocole>> parserVotesProtocoles(JSONObject votes) {
245
 
314
 
246
		HashMap<String, HashMap<String, VoteProtocole>> votesProtocoles = new HashMap<String, HashMap<String, VoteProtocole>>();
315
		HashMap<String, HashMap<String, VoteProtocole>> votesProtocoles = new HashMap<String, HashMap<String, VoteProtocole>>();
Line 266... Line 335...
266
		}
335
		}
Line 267... Line 336...
267
 
336
 
268
		return votesProtocoles;
337
		return votesProtocoles;
Line -... Line 338...
-
 
338
	}
-
 
339
 
-
 
340
	/**
-
 
341
	 * Recupere une date à partir du JSON
-
 
342
	 * 
-
 
343
	 * @param imageJson
269
	}
344
	 * @return
270
 
345
	 */
271
	public static Date parserDateObservation(String date) {
346
	public static Date parserDateObservation(String date) {
272
		Date dateParsee = new Date();
347
		Date dateParsee = new Date();
273
		DateTimeFormat formatDateObs = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
348
		DateTimeFormat formatDateObs = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
Line 277... Line 352...
277
			dateParsee = new java.sql.Date(0);
352
			dateParsee = new java.sql.Date(0);
278
		}
353
		}
279
		return dateParsee;
354
		return dateParsee;
280
	}
355
	}
Line -... Line 356...
-
 
356
 
-
 
357
	/**
-
 
358
	 * Recupere des mots-clefs à partir du JSON
-
 
359
	 * 
-
 
360
	 * @param imageJson
-
 
361
	 * @return
281
 
362
	 */
282
	public static List<String> parserMotsCles(String motsClesTexte) {
363
	public static List<String> parserMotsCles(String motsClesTexte) {
283
		String[] tabMotsCle = motsClesTexte.split(",");
364
		String[] tabMotsCle = motsClesTexte.split(",");
284
		List<String> motsClesParses = new ArrayList<String>();
365
		List<String> motsClesParses = new ArrayList<String>();
285
		for (int i = 0; i < tabMotsCle.length; i++) {
366
		for (int i = 0; i < tabMotsCle.length; i++) {
Line 295... Line 376...
295
			id = "0" + id;
376
			id = "0" + id;
296
		}
377
		}
297
		return id;
378
		return id;
298
	}
379
	}
Line -... Line 380...
-
 
380
 
-
 
381
	/**
-
 
382
	 * Recupere un utilisateur à partir du JSON
-
 
383
	 * 
-
 
384
	 * @param imageJson
-
 
385
	 * @return
299
 
386
	 */
Line 300... Line 387...
300
	public static Utilisateur parserUtilisateurJson(JSONValue valeurJson) {
387
	public static Utilisateur parserUtilisateurJson(JSONValue valeurJson) {
301
 
388
 
302
		JSONObject utilisateurJson = valeurJson.isObject();
389
		JSONObject utilisateurJson = valeurJson.isObject();
Line 315... Line 402...
315
			utilisateur = new Utilisateur(idUtilisateur);
402
			utilisateur = new Utilisateur(idUtilisateur);
316
		}
403
		}
Line 317... Line 404...
317
 
404
 
318
		return utilisateur;
405
		return utilisateur;
-
 
406
	}
-
 
407
 
-
 
408
	/**
-
 
409
	 * Retourne un objet {@link ProtocoleServiceResultat} à partir d'un objet
-
 
410
	 * JSON
-
 
411
	 * 
-
 
412
	 * @param retourJson
-
 
413
	 * @return
-
 
414
	 */
-
 
415
	public static ProtocoleServiceResultat parserProtocoleServiceResultat(JSONValue retourJson) {
-
 
416
		List<Protocole> protocoles = new ArrayList<Protocole>();
-
 
417
		JSONObject tableauProto = retourJson.isObject().get("resultats").isObject();
-
 
418
 
-
 
419
		if (tableauProto != null) {
-
 
420
			java.util.Iterator<String> it = tableauProto.keySet().iterator();
-
 
421
			while (it.hasNext()) {
-
 
422
 
-
 
423
				JSONObject protocoleJSON = tableauProto.get(it.next()).isObject();
-
 
424
				Protocole protocole = new Protocole();
-
 
425
				String idProtocole = UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.id");
-
 
426
				protocole.setId(Integer.parseInt(idProtocole));
-
 
427
				protocole.setNom(UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.intitule"));
-
 
428
				protocole.setDescription(UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.descriptif"));
-
 
429
				protocoles.add(protocole);
-
 
430
			}
-
 
431
		}
-
 
432
 
-
 
433
		return new ProtocoleServiceResultat(protocoles);
-
 
434
	}
-
 
435
 
-
 
436
	/**
-
 
437
	 * Retourne un objet {@link ImageServiceResultat} à partir du JSON
-
 
438
	 * 
-
 
439
	 * @param retourJson
-
 
440
	 * @return
-
 
441
	 */
-
 
442
	public static ImageServiceResultat parserImageServiceResultat(JSONValue retourJson) {
-
 
443
 
-
 
444
		ImageServiceResultat imageServiceResultat = new ImageServiceResultat();
-
 
445
		int nbTotalImagesPourLaRecherche;
-
 
446
 
-
 
447
		List<Image> images = new ArrayList<Image>();
-
 
448
		// TODO ajouter vérifications plus précises
-
 
449
		if (retourJson.isObject().get("entete") != null) {
-
 
450
			double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
-
 
451
			nbTotalImagesPourLaRecherche = (int) total;
-
 
452
			JSONObject tableauImg = retourJson.isObject().get("resultats").isObject();
-
 
453
			java.util.Iterator<String> it = tableauImg.keySet().iterator();
-
 
454
			while (it.hasNext()) {
-
 
455
				JSONObject imageJson = tableauImg.get(it.next()).isObject();
-
 
456
				Image image = parserRetourImage(imageJson);
-
 
457
				images.add(image);
-
 
458
			}
-
 
459
 
-
 
460
		} else {
-
 
461
			JSONArray tableauImg = retourJson.isObject().get("images").isArray();
-
 
462
			nbTotalImagesPourLaRecherche = (int) tableauImg.size();
-
 
463
			for (int i = 0; i < nbTotalImagesPourLaRecherche; i++) {
-
 
464
				JSONObject imageJson = tableauImg.get(i).isObject();
-
 
465
				Image image = parserRetourImage(imageJson);
-
 
466
				images.add(image);
-
 
467
			}
-
 
468
		}
-
 
469
 
-
 
470
		imageServiceResultat.setImages(images);
-
 
471
		imageServiceResultat.setNbTotalImagesPourLaRecherche(nbTotalImagesPourLaRecherche);
-
 
472
 
-
 
473
		return imageServiceResultat;
-
 
474
 
-
 
475
	}
-
 
476
 
-
 
477
	/**
-
 
478
	 * Retourne un objet {@link Image} à partir du JSON
-
 
479
	 * 
-
 
480
	 * @param retourJson
-
 
481
	 * @return
-
 
482
	 */
-
 
483
	public static Image parserRetourImage(JSONObject imageJson) {
-
 
484
		Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
-
 
485
 
-
 
486
		if (imageJson.get("observation") != null && imageJson.get("observation").isObject() != null) {
-
 
487
			JSONObject observationJson = imageJson.get("observation").isObject();
-
 
488
			image.setObservation(UtilitairesServiceResultat.parserObservationJSON(observationJson));
-
 
489
		}
-
 
490
 
-
 
491
		if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
-
 
492
			JSONObject votes = imageJson.get("protocoles_votes").isObject();
-
 
493
			image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
-
 
494
		}
-
 
495
 
-
 
496
		return image;
-
 
497
	}
-
 
498
 
-
 
499
	/**
-
 
500
	 * Retourne un objet {@link ObservationServiceResultat} à partir du JSON
-
 
501
	 * 
-
 
502
	 * @param retourJson
-
 
503
	 * @return
-
 
504
	 */
-
 
505
	public static ObservationServiceResultat parserObservationServiceResultat(JSONValue retourJson) {
-
 
506
 
-
 
507
		ObservationServiceResultat observationServiceResultat = new ObservationServiceResultat();
-
 
508
 
-
 
509
		List<Observation> observations = new ArrayList<Observation>();
-
 
510
		int nbTotalObservationsPourLaRecherche = 0;
-
 
511
 
-
 
512
		if (retourJson.isObject().get("entete") != null) {
-
 
513
			// TODO ajouter vérifications plus précises
-
 
514
			double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
-
 
515
			nbTotalObservationsPourLaRecherche = (int) total;
-
 
516
			JSONObject tableauObs = retourJson.isObject().get("resultats").isObject();
-
 
517
 
-
 
518
			if (tableauObs != null) {
-
 
519
				java.util.Iterator<String> it = tableauObs.keySet().iterator();
-
 
520
				while (it.hasNext()) {
-
 
521
					JSONObject observationJson = tableauObs.get(it.next()).isObject();
-
 
522
					Observation observation = analyserObservation(observationJson);
-
 
523
					observations.add(observation);
-
 
524
				}
-
 
525
			}
-
 
526
		} else {
-
 
527
			JSONObject observationJson = retourJson.isObject();
-
 
528
			Observation observation = analyserObservation(observationJson);
-
 
529
			observations.add(observation);
-
 
530
			CacheClient.getInstance().setObservationCourante(observation);
-
 
531
		}
-
 
532
 
-
 
533
		observationServiceResultat.setObservations(observations);
-
 
534
		observationServiceResultat.setNbTotalObservationsPourLaRecherche(nbTotalObservationsPourLaRecherche);
-
 
535
 
-
 
536
		return observationServiceResultat;
-
 
537
	}
-
 
538
 
-
 
539
	/**
-
 
540
	 * Retourne un objet {@link Observation} avec ses {@link Image} associées à
-
 
541
	 * partir du JSON
-
 
542
	 * 
-
 
543
	 * @param retourJson
-
 
544
	 * @return
-
 
545
	 */
-
 
546
	private static Observation analyserObservation(JSONObject observationJson) {
-
 
547
		Observation observation = UtilitairesServiceResultat.parserObservationJSON(observationJson);
-
 
548
		JSONArray tableauImagesObs = observationJson.get("images").isArray();
-
 
549
		List<Image> imagesPourObs = new ArrayList<Image>();
-
 
550
 
-
 
551
		int nbImages = tableauImagesObs.size();
-
 
552
		for (int j = 0; j < nbImages; j++) {
-
 
553
			JSONObject imageJson = tableauImagesObs.get(j).isObject();
-
 
554
 
-
 
555
			Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
-
 
556
			if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
-
 
557
				JSONObject votes = imageJson.get("protocoles_votes").isObject();
-
 
558
				image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
-
 
559
			}
-
 
560
			image.setObservation(observation);
-
 
561
			imagesPourObs.add(image);
-
 
562
		}
-
 
563
 
-
 
564
		observation.setImages(imagesPourObs);
-
 
565
 
-
 
566
		return observation;
-
 
567
	}
319
	}
568