Subversion Repositories eFlore/Applications.del

Rev

Rev 939 | Rev 952 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
500 aurelien 1
package org.tela_botanica.del.client.utils;
2
 
3
import java.util.ArrayList;
4
import java.util.Date;
5
import java.util.HashMap;
876 gduche 6
import java.util.Iterator;
500 aurelien 7
import java.util.List;
8
 
941 benjamin 9
import org.tela_botanica.del.client.cache.CacheClient;
876 gduche 10
import org.tela_botanica.del.client.modeles.Commentaire;
690 gduche 11
import org.tela_botanica.del.client.modeles.Contributeur;
500 aurelien 12
import org.tela_botanica.del.client.modeles.Image;
941 benjamin 13
import org.tela_botanica.del.client.modeles.ImageServiceResultat;
846 aurelien 14
import org.tela_botanica.del.client.modeles.InterventionForum;
500 aurelien 15
import org.tela_botanica.del.client.modeles.Observation;
941 benjamin 16
import org.tela_botanica.del.client.modeles.ObservationServiceResultat;
500 aurelien 17
import org.tela_botanica.del.client.modeles.PropositionDetermination;
941 benjamin 18
import org.tela_botanica.del.client.modeles.Protocole;
19
import org.tela_botanica.del.client.modeles.ProtocoleServiceResultat;
516 aurelien 20
import org.tela_botanica.del.client.modeles.Utilisateur;
846 aurelien 21
import org.tela_botanica.del.client.modeles.VoteDetermination;
500 aurelien 22
import org.tela_botanica.del.client.modeles.VoteProtocole;
23
 
24
import com.google.gwt.i18n.client.DateTimeFormat;
941 benjamin 25
import com.google.gwt.json.client.JSONArray;
500 aurelien 26
import com.google.gwt.json.client.JSONObject;
900 aurelien 27
import com.google.gwt.json.client.JSONParser;
516 aurelien 28
import com.google.gwt.json.client.JSONValue;
500 aurelien 29
 
941 benjamin 30
/**
31
 * Centralisation des methodes de parsing du code JSON retourné par les
32
 * webservices
33
 *
34
 * @author LIENS
35
 *
36
 */
500 aurelien 37
public class UtilitairesServiceResultat {
926 benjamin 38
 
941 benjamin 39
	/**
40
	 * Recupere un objet Image à partir du JSON
41
	 *
42
	 * @param imageJson
43
	 * @return
44
	 */
926 benjamin 45
	public static Image parserImageJSON(JSONObject imageJson) {
46
 
500 aurelien 47
		Image image = new Image();
763 aurelien 48
		String idImage = imageJson.get("id_image").isString().stringValue();
500 aurelien 49
		image.setIdImage(idImage);
926 benjamin 50
		image.setUrlFormat("http://www.tela-botanica.org/appli:cel-img:" + getIdAvecPadding(idImage) + "%s%.jpg");
51
		image.setUrl("http://www.tela-botanica.org/appli:cel-img:" + getIdAvecPadding(idImage) + "CRS.jpg");
52
		image.setMiniature("http://www.tela-botanica.org/appli:cel-img:" + getIdAvecPadding(idImage) + "XS.jpg");
53
 
500 aurelien 54
		return image;
55
	}
926 benjamin 56
 
941 benjamin 57
	/**
58
	 * Recupere un objet Observation à partir du JSON
59
	 *
60
	 * @param imageJson
61
	 * @return
62
	 */
500 aurelien 63
	public static Observation parserObservationJSON(JSONObject observationJson) {
926 benjamin 64
 
500 aurelien 65
		Observation observation = new Observation();
926 benjamin 66
		observation.setAuteur(getValeurOuVide(observationJson, "auteur.prenom") + " " + getValeurOuVide(observationJson, "auteur.nom"));
67
		observation.setNomAuteur(getValeurOuVide(observationJson, "auteur.nom"));
68
		observation.setPrenomAuteur(getValeurOuVide(observationJson, "auteur.nom"));
69
		observation.setIdAuteur(getValeurOuVide(observationJson, "auteur.id"));
70
		// TODO: renvoyer le courriel de l'auteur dans les obs
837 aurelien 71
		observation.setCourrielAuteur("");
926 benjamin 72
		observation.setDateTransmission(getValeurOuVide(observationJson, "date_observation"));
763 aurelien 73
		observation.setDateReleve(getValeurOuVide(observationJson, "date_observation"));
74
		observation.setFamille(getValeurOuVide(observationJson, "determination.famille"));
846 aurelien 75
		observation.setId(getValeurOuVide(observationJson, "id_observation"));
76
		observation.setIdLocalite(getValeurOuVide(observationJson, "id_zone_geo"));
77
		observation.setLocalite(getValeurOuVide(observationJson, "zone_geo"));
78
		observation.setNomRetenu(getValeurOuVide(observationJson, "determination.ns"));
79
		observation.setMilieu(getValeurOuVide(observationJson, "milieu"));
80
		observation.setLieuDit(getValeurOuVide(observationJson, "lieudit"));
81
		observation.setNumNomenclatural(getValeurOuVide(observationJson, "determination.nn"));
926 benjamin 82
		// TODO: renvoyer les mots clés des observations
83
		// observation.setMotsClefs(parserMotsCles(UtilitairesServiceResultat.getValeurOuVide(observationJson,
84
		// "mots_cles_texte")));
846 aurelien 85
		JSONValue propositions = observationJson.get("commentaires");
926 benjamin 86
 
87
		if (propositions != null && propositions.isObject() != null) {
939 benjamin 88
			List<InterventionForum> interventions = parserInterventions(propositions.isObject());
89
			for (InterventionForum interventionForum : interventions) {
90
				interventionForum.setObservation(observation);
91
			}
876 gduche 92
			observation.setInterventionsForum(interventions);
846 aurelien 93
		} else {
94
			observation.ajouterPropositionDetermination(creerPropositionDeterminationAPartirObservation(observation));
95
		}
500 aurelien 96
		return observation;
97
	}
926 benjamin 98
 
941 benjamin 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
126
	 */
939 benjamin 127
	public static List<Commentaire> parserCommentaires(JSONObject commentaires) {
128
		List<InterventionForum> interventionForums = parserInterventions(commentaires);
926 benjamin 129
		List<Commentaire> commentairesListe = new ArrayList<Commentaire>();
130
		for (InterventionForum interventionForum : interventionForums) {
131
			if (interventionForum instanceof Commentaire) {
132
				commentairesListe.add((Commentaire) interventionForum);
133
			}
134
		}
135
		return commentairesListe;
136
 
137
	}
138
 
941 benjamin 139
	/**
140
	 * Recupere une liste d'interventions à partir du JSON
141
	 *
142
	 * @param imageJson
143
	 * @return
144
	 */
939 benjamin 145
	public static List<InterventionForum> parserInterventions(JSONObject interventions) {
889 gduche 146
		HashMap<String, InterventionForum> interventionsNonTypees = new HashMap<String, InterventionForum>();
926 benjamin 147
 
148
		List<InterventionForum> retour = new ArrayList<InterventionForum>();
939 benjamin 149
 
926 benjamin 150
		// parcourir les interventions et en faire un tableau
939 benjamin 151
		if (interventions == null) {
926 benjamin 152
			return retour;
153
		}
888 gduche 154
		Iterator<String> itInterventions = interventions.keySet().iterator();
155
		while (itInterventions.hasNext()) {
156
			JSONObject jsonIntervention = interventions.get(itInterventions.next()).isObject();
157
			String nomSel = getValeurOuVide(jsonIntervention, "nom_sel");
926 benjamin 158
 
888 gduche 159
			String id = getValeurOuVide(jsonIntervention, "id_commentaire");
160
			String idParent = getValeurOuVide(jsonIntervention, "ce_commentaire_parent");
161
			String texte = getValeurOuVide(jsonIntervention, "texte");
926 benjamin 162
 
888 gduche 163
			String idUtilisateur = getValeurOuVide(jsonIntervention, "ce_utilisateur");
164
			String nom = getValeurOuVide(jsonIntervention, "utilisateur_nom");
165
			String prenom = getValeurOuVide(jsonIntervention, "utilisateur_prenom");
166
			String courriel = getValeurOuVide(jsonIntervention, "utilisateur_courriel");
167
			Contributeur contributeur = new Contributeur(idUtilisateur, nom, prenom, courriel);
926 benjamin 168
 
169
			// TODO : parser date
888 gduche 170
			Date date = new Date();
926 benjamin 171
 
888 gduche 172
			if (!nomSel.equals("")) {
173
				String nom_sel = getValeurOuVide(jsonIntervention, "nom_sel");
174
				String nom_sel_nn = getValeurOuVide(jsonIntervention, "nom_sel_nn");
175
				String nom_ret = getValeurOuVide(jsonIntervention, "nom_ret");
176
				String nom_ret_nn = getValeurOuVide(jsonIntervention, "nom_ret_nn");
177
				String famille = getValeurOuVide(jsonIntervention, "famille");
178
				String nom_referentiel = getValeurOuVide(jsonIntervention, "nom_referentiel");
926 benjamin 179
 
920 aurelien 180
				String nbCommentaires = getValeurOuVide(jsonIntervention, "nb_commentaires");
926 benjamin 181
 
888 gduche 182
				PropositionDetermination intervention = new PropositionDetermination(id, contributeur, texte);
939 benjamin 183
				// intervention.setObservation(observation);
888 gduche 184
				intervention.setEspece(nom_sel);
926 benjamin 185
 
186
				if (!nbCommentaires.equals("")) {
933 aurelien 187
					int nbComm = Integer.parseInt(nbCommentaires);
939 benjamin 188
					if (!texte.equals("")) {
933 aurelien 189
						nbComm++;
190
					}
191
					intervention.setNbCommentaires(nbComm);
920 aurelien 192
				}
926 benjamin 193
 
888 gduche 194
				if (!idParent.equals("")) {
195
					intervention.setIdParent(idParent);
196
				}
926 benjamin 197
 
198
				if (jsonIntervention.get("votes") != null && jsonIntervention.get("votes").isObject() != null) {
939 benjamin 199
					intervention.setVotesDeterminations(parserVotesDetermination(jsonIntervention.get("votes").isObject()));
888 gduche 200
				}
926 benjamin 201
 
888 gduche 202
				intervention.setDate(date);
889 gduche 203
				interventionsNonTypees.put(intervention.getId(), intervention);
926 benjamin 204
 
888 gduche 205
			} else {
206
				Commentaire intervention = new Commentaire(contributeur, date, texte);
893 gduche 207
				intervention.setId(id);
888 gduche 208
				intervention.setDate(date);
889 gduche 209
				interventionsNonTypees.put(intervention.getId(), intervention);
893 gduche 210
				if (!idParent.equals("")) {
211
					intervention.setIdParent(idParent);
212
				}
888 gduche 213
			}
214
		}
215
 
889 gduche 216
		Iterator<String> itIntervention = interventionsNonTypees.keySet().iterator();
217
		while (itIntervention.hasNext()) {
218
			String id = itIntervention.next();
219
			InterventionForum intervention = interventionsNonTypees.get(id);
220
			String idParent = intervention.getIdParent();
893 gduche 221
			if (idParent != null && !idParent.equals("") && !idParent.equals("0")) {
222
				InterventionForum parent = interventionsNonTypees.get(idParent);
223
				intervention.setParent(parent);
889 gduche 224
			}
225
		}
926 benjamin 226
 
889 gduche 227
		retour.addAll(interventionsNonTypees.values());
228
		return retour;
500 aurelien 229
	}
939 benjamin 230
 
941 benjamin 231
	/**
232
	 * Recupere une liste de commentaires à partir du JSON
233
	 *
234
	 * @param imageJson
235
	 * @return
236
	 */
931 aurelien 237
	public static String convertirEtParserRetourAjoutCommentaire(String retour) {
929 aurelien 238
		JSONObject retourJson = JSONParser.parseStrict(retour).isObject();
239
		return parserRetourAjoutCommentaire(retourJson);
240
	}
926 benjamin 241
 
941 benjamin 242
	/**
243
	 * Recupere une liste de commentaires à partir d'un objet JSON
244
	 *
245
	 * @param imageJson
246
	 * @return
247
	 */
884 aurelien 248
	public static String parserRetourAjoutCommentaire(JSONObject retour) {
249
		String id = "";
926 benjamin 250
		if (retour != null) {
884 aurelien 251
			id = getValeurOuVide(retour, "id_commentaire");
252
		}
253
		return id;
254
	}
926 benjamin 255
 
888 gduche 256
	public static String getValeurOuVide(JSONObject objet, String index) {
920 aurelien 257
		return (objet.get(index) != null && objet.get(index).isString() != null) ? objet.get(index).isString().stringValue() : "";
888 gduche 258
	}
926 benjamin 259
 
941 benjamin 260
	/**
261
	 * Recupere une liste de votes sur une determination à partir du JSON
262
	 *
263
	 * @param imageJson
264
	 * @return
265
	 */
939 benjamin 266
	public static HashMap<String, VoteDetermination> parserRetourListeVotesDetermination(String votesString) {
900 aurelien 267
 
268
		HashMap<String, VoteDetermination> retour = null;
926 benjamin 269
 
900 aurelien 270
		JSONObject votes = JSONParser.parseStrict(votesString).isObject();
926 benjamin 271
		if (votes != null && votes.get("resultats") != null && votes.get("resultats").isObject() != null) {
900 aurelien 272
			JSONObject resultat = votes.get("resultats").isObject();
939 benjamin 273
			retour = parserVotesDetermination(resultat);
900 aurelien 274
		}
275
		return retour;
276
	}
926 benjamin 277
 
941 benjamin 278
	/**
279
	 * Recupere une liste de votes sur une determination à partir d'un objet
280
	 * JSON
281
	 *
282
	 * @param imageJson
283
	 * @return
284
	 */
939 benjamin 285
	public static HashMap<String, VoteDetermination> parserVotesDetermination(JSONObject votes) {
846 aurelien 286
		HashMap<String, VoteDetermination> votesDetermination = new HashMap<String, VoteDetermination>();
287
		java.util.Iterator<String> itVotes = votes.keySet().iterator();
288
		while (itVotes.hasNext()) {
289
			JSONObject vote = votes.get(itVotes.next()).isObject();
290
			VoteDetermination vd = new VoteDetermination();
291
			vd.setContributeur(getValeurOuVide(vote, "auteur.id"));
292
			vd.setDate(new Date());
293
			vd.setId(getValeurOuVide(vote, "vote.id"));
294
			vd.setVote(Integer.parseInt(getValeurOuVide(vote, "vote")));
900 aurelien 295
			vd.setContributeur(getValeurOuVide(vote, "auteur.id"));
926 benjamin 296
 
297
			if (vote.get("auteur.nom") != null && vote.get("auteur.nom") != null && vote.get("auteur.courriel") != null) {
298
				Contributeur auteur = new Contributeur(getValeurOuVide(vote, "auteur.id"), getValeurOuVide(vote, "auteur.nom"), getValeurOuVide(vote, "auteur.prenom"), getValeurOuVide(vote, "auteur.courriel"));
900 aurelien 299
				vd.setAuteur(auteur);
300
			}
926 benjamin 301
 
846 aurelien 302
			votesDetermination.put(getValeurOuVide(vote, "auteur.id"), vd);
926 benjamin 303
		}
846 aurelien 304
		return votesDetermination;
305
	}
926 benjamin 306
 
941 benjamin 307
	/**
308
	 * Recupere une liste de votes sur des images à partir d'un objet JSON
309
	 *
310
	 * @param imageJson
311
	 * @return
312
	 */
926 benjamin 313
	public static HashMap<String, HashMap<String, VoteProtocole>> parserVotesProtocoles(JSONObject votes) {
314
 
315
		HashMap<String, HashMap<String, VoteProtocole>> votesProtocoles = new HashMap<String, HashMap<String, VoteProtocole>>();
777 aurelien 316
		java.util.Iterator<String> itProtocoles = votes.keySet().iterator();
926 benjamin 317
		while (itProtocoles.hasNext()) {
318
			JSONObject protocole = votes.get(itProtocoles.next()).isObject();
777 aurelien 319
			JSONObject votesPourCeProtocoles = protocole.get("votes").isObject();
320
			String idProtocoleVote = protocole.get("protocole.id").isString().stringValue();
321
			java.util.Iterator<String> itVotes = votesPourCeProtocoles.keySet().iterator();
926 benjamin 322
			while (itVotes.hasNext()) {
777 aurelien 323
				JSONObject voteEnCours = votesPourCeProtocoles.get(itVotes.next()).isObject();
324
				VoteProtocole vd = new VoteProtocole();
325
				vd.setContributeur(voteEnCours.get("auteur.id").isString().stringValue());
326
				// TODO récupérer la date du vote et la parser
327
				vd.setDate(new Date());
328
				int valeurVote = Integer.parseInt(voteEnCours.get("vote").isString().stringValue());
329
				vd.setVote(valeurVote);
926 benjamin 330
				if (!votesProtocoles.containsKey(idProtocoleVote)) {
331
					votesProtocoles.put(idProtocoleVote, new HashMap<String, VoteProtocole>());
777 aurelien 332
				}
333
				votesProtocoles.get(idProtocoleVote).put(vd.getContributeur(), vd);
500 aurelien 334
			}
335
		}
926 benjamin 336
 
500 aurelien 337
		return votesProtocoles;
338
	}
926 benjamin 339
 
941 benjamin 340
	/**
341
	 * Recupere une date à partir du JSON
342
	 *
343
	 * @param imageJson
344
	 * @return
345
	 */
500 aurelien 346
	public static Date parserDateObservation(String date) {
347
		Date dateParsee = new Date();
348
		DateTimeFormat formatDateObs = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
926 benjamin 349
		try {
350
			dateParsee = formatDateObs.parse(date);
351
		} catch (IllegalArgumentException e) {
352
			dateParsee = new java.sql.Date(0);
500 aurelien 353
		}
926 benjamin 354
		return dateParsee;
500 aurelien 355
	}
926 benjamin 356
 
941 benjamin 357
	/**
358
	 * Recupere des mots-clefs à partir du JSON
359
	 *
360
	 * @param imageJson
361
	 * @return
362
	 */
500 aurelien 363
	public static List<String> parserMotsCles(String motsClesTexte) {
364
		String[] tabMotsCle = motsClesTexte.split(",");
365
		List<String> motsClesParses = new ArrayList<String>();
366
		for (int i = 0; i < tabMotsCle.length; i++) {
367
			motsClesParses.add(tabMotsCle[i]);
368
		}
926 benjamin 369
 
500 aurelien 370
		return motsClesParses;
371
	}
926 benjamin 372
 
500 aurelien 373
	public static String getIdAvecPadding(String id) {
374
		int maxZeros = 9 - id.length();
375
		for (int i = 0; i < maxZeros; i++) {
376
			id = "0" + id;
377
		}
378
		return id;
379
	}
926 benjamin 380
 
941 benjamin 381
	/**
382
	 * Recupere un utilisateur à partir du JSON
383
	 *
384
	 * @param imageJson
385
	 * @return
386
	 */
516 aurelien 387
	public static Utilisateur parserUtilisateurJson(JSONValue valeurJson) {
926 benjamin 388
 
516 aurelien 389
		JSONObject utilisateurJson = valeurJson.isObject();
390
		boolean connecteUtilisateur = utilisateurJson.get("connecte").isBoolean().booleanValue();
391
		String idUtilisateur = utilisateurJson.get("id_utilisateur").isString().stringValue();
926 benjamin 392
 
516 aurelien 393
		Utilisateur utilisateur;
926 benjamin 394
 
395
		if (connecteUtilisateur) {
522 aurelien 396
			String courrielUtilisateur = utilisateurJson.get("courriel").isString().stringValue();
516 aurelien 397
			String nomUtilisateur = utilisateurJson.get("nom").isString().stringValue();
398
			String prenomUtilisateur = utilisateurJson.get("prenom").isString().stringValue();
926 benjamin 399
 
516 aurelien 400
			utilisateur = new Utilisateur(idUtilisateur, prenomUtilisateur, nomUtilisateur, courrielUtilisateur);
401
		} else {
402
			utilisateur = new Utilisateur(idUtilisateur);
403
		}
926 benjamin 404
 
516 aurelien 405
		return utilisateur;
406
	}
941 benjamin 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
	}
568
 
500 aurelien 569
}