Subversion Repositories eFlore/Applications.del

Rev

Rev 965 | Rev 981 | 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"));
954 aurelien 78
 
79
		String nomRetenu = getValeurOuVide(observationJson, "determination.ns");
846 aurelien 80
		observation.setNomRetenu(getValeurOuVide(observationJson, "determination.ns"));
81
		observation.setMilieu(getValeurOuVide(observationJson, "milieu"));
82
		observation.setLieuDit(getValeurOuVide(observationJson, "lieudit"));
83
		observation.setNumNomenclatural(getValeurOuVide(observationJson, "determination.nn"));
926 benjamin 84
		// TODO: renvoyer les mots clés des observations
85
		// observation.setMotsClefs(parserMotsCles(UtilitairesServiceResultat.getValeurOuVide(observationJson,
86
		// "mots_cles_texte")));
846 aurelien 87
		JSONValue propositions = observationJson.get("commentaires");
954 aurelien 88
 
89
		boolean creerPropositionAPartirObs = true;
926 benjamin 90
 
91
		if (propositions != null && propositions.isObject() != null) {
939 benjamin 92
			List<InterventionForum> interventions = parserInterventions(propositions.isObject());
93
			for (InterventionForum interventionForum : interventions) {
954 aurelien 94
				interventionForum.setObservation(observation);
95
				// Si une proposition avec le même nom retenu que l'observation est déjà présente,
96
				// alors il n'est pas nécessaire de créer la proposition "factice"
97
				if(interventionForum instanceof PropositionDetermination) {
98
					PropositionDetermination proposition = (PropositionDetermination)interventionForum;
99
					if(!nomRetenu.equals("") && proposition.getEspece().equals(nomRetenu)) {
100
						creerPropositionAPartirObs = false;
101
					}
102
				}
939 benjamin 103
			}
876 gduche 104
			observation.setInterventionsForum(interventions);
846 aurelien 105
		}
954 aurelien 106
 
107
		if(creerPropositionAPartirObs) {
108
			// Si elle est nécessaire, la proposition factice est ajoutée au début
109
			observation.getInterventionsForum().add(0, creerPropositionDeterminationAPartirObservation(observation));
110
		}
500 aurelien 111
		return observation;
112
	}
926 benjamin 113
 
941 benjamin 114
	/**
115
	 * Créée une proposition de determination à partir d'une observation
116
	 *
117
	 * @param observation
118
	 * @return
119
	 */
120
	private static PropositionDetermination creerPropositionDeterminationAPartirObservation(Observation observation) {
121
 
122
		String utilisateurNom = observation.getNomAuteur();
123
		String utilisateurPrenom = observation.getPrenomAuteur();
124
		String utilisateurCourriel = observation.getCourrielAuteur();
125
		String utilisateurId = observation.getIdAuteur();
126
		PropositionDetermination propositionDetermination = new PropositionDetermination(observation);
127
		Contributeur contributeur = new Contributeur(utilisateurId, utilisateurNom, utilisateurPrenom, utilisateurCourriel);
128
		propositionDetermination.setContributeur(contributeur);
129
		java.util.Date datePropDeter = parserDateObservation(observation.getDateReleve());
130
		propositionDetermination.setDate(datePropDeter);
131
		propositionDetermination.setEspece(observation.getNomRetenu());
132
 
133
		return propositionDetermination;
134
	}
135
 
136
	/**
137
	 * Recupere une liste de commentaires à partir du JSON
138
	 *
139
	 * @param imageJson
140
	 * @return
141
	 */
939 benjamin 142
	public static List<Commentaire> parserCommentaires(JSONObject commentaires) {
143
		List<InterventionForum> interventionForums = parserInterventions(commentaires);
926 benjamin 144
		List<Commentaire> commentairesListe = new ArrayList<Commentaire>();
145
		for (InterventionForum interventionForum : interventionForums) {
146
			if (interventionForum instanceof Commentaire) {
147
				commentairesListe.add((Commentaire) interventionForum);
148
			}
149
		}
150
		return commentairesListe;
151
 
152
	}
153
 
941 benjamin 154
	/**
155
	 * Recupere une liste d'interventions à partir du JSON
156
	 *
157
	 * @param imageJson
158
	 * @return
159
	 */
939 benjamin 160
	public static List<InterventionForum> parserInterventions(JSONObject interventions) {
889 gduche 161
		HashMap<String, InterventionForum> interventionsNonTypees = new HashMap<String, InterventionForum>();
926 benjamin 162
 
163
		List<InterventionForum> retour = new ArrayList<InterventionForum>();
939 benjamin 164
 
926 benjamin 165
		// parcourir les interventions et en faire un tableau
939 benjamin 166
		if (interventions == null) {
926 benjamin 167
			return retour;
168
		}
888 gduche 169
		Iterator<String> itInterventions = interventions.keySet().iterator();
170
		while (itInterventions.hasNext()) {
171
			JSONObject jsonIntervention = interventions.get(itInterventions.next()).isObject();
172
			String nomSel = getValeurOuVide(jsonIntervention, "nom_sel");
926 benjamin 173
 
888 gduche 174
			String id = getValeurOuVide(jsonIntervention, "id_commentaire");
175
			String idParent = getValeurOuVide(jsonIntervention, "ce_commentaire_parent");
176
			String texte = getValeurOuVide(jsonIntervention, "texte");
926 benjamin 177
 
977 aurelien 178
			String idUtilisateur = getValeurOuVide(jsonIntervention, "auteur.id");
952 aurelien 179
			String nom = getValeurOuVide(jsonIntervention, "auteur.nom");
180
			String prenom = getValeurOuVide(jsonIntervention, "auteur.prenom");
181
			String courriel = getValeurOuVide(jsonIntervention, "auteur.courriel");
888 gduche 182
			Contributeur contributeur = new Contributeur(idUtilisateur, nom, prenom, courriel);
926 benjamin 183
 
965 aurelien 184
			Date date = parserDateObservation(getValeurOuVide(jsonIntervention, "date"));
926 benjamin 185
 
888 gduche 186
			if (!nomSel.equals("")) {
187
				String nom_sel = getValeurOuVide(jsonIntervention, "nom_sel");
188
				String nom_sel_nn = getValeurOuVide(jsonIntervention, "nom_sel_nn");
189
				String nom_ret = getValeurOuVide(jsonIntervention, "nom_ret");
190
				String nom_ret_nn = getValeurOuVide(jsonIntervention, "nom_ret_nn");
191
				String famille = getValeurOuVide(jsonIntervention, "famille");
192
				String nom_referentiel = getValeurOuVide(jsonIntervention, "nom_referentiel");
926 benjamin 193
 
920 aurelien 194
				String nbCommentaires = getValeurOuVide(jsonIntervention, "nb_commentaires");
926 benjamin 195
 
888 gduche 196
				PropositionDetermination intervention = new PropositionDetermination(id, contributeur, texte);
939 benjamin 197
				// intervention.setObservation(observation);
888 gduche 198
				intervention.setEspece(nom_sel);
926 benjamin 199
 
200
				if (!nbCommentaires.equals("")) {
933 aurelien 201
					int nbComm = Integer.parseInt(nbCommentaires);
939 benjamin 202
					if (!texte.equals("")) {
933 aurelien 203
						nbComm++;
204
					}
205
					intervention.setNbCommentaires(nbComm);
920 aurelien 206
				}
926 benjamin 207
 
888 gduche 208
				if (!idParent.equals("")) {
209
					intervention.setIdParent(idParent);
210
				}
926 benjamin 211
 
212
				if (jsonIntervention.get("votes") != null && jsonIntervention.get("votes").isObject() != null) {
939 benjamin 213
					intervention.setVotesDeterminations(parserVotesDetermination(jsonIntervention.get("votes").isObject()));
888 gduche 214
				}
926 benjamin 215
 
888 gduche 216
				intervention.setDate(date);
889 gduche 217
				interventionsNonTypees.put(intervention.getId(), intervention);
926 benjamin 218
 
888 gduche 219
			} else {
220
				Commentaire intervention = new Commentaire(contributeur, date, texte);
893 gduche 221
				intervention.setId(id);
888 gduche 222
				intervention.setDate(date);
889 gduche 223
				interventionsNonTypees.put(intervention.getId(), intervention);
893 gduche 224
				if (!idParent.equals("")) {
225
					intervention.setIdParent(idParent);
226
				}
888 gduche 227
			}
228
		}
229
 
889 gduche 230
		Iterator<String> itIntervention = interventionsNonTypees.keySet().iterator();
231
		while (itIntervention.hasNext()) {
232
			String id = itIntervention.next();
233
			InterventionForum intervention = interventionsNonTypees.get(id);
234
			String idParent = intervention.getIdParent();
893 gduche 235
			if (idParent != null && !idParent.equals("") && !idParent.equals("0")) {
236
				InterventionForum parent = interventionsNonTypees.get(idParent);
237
				intervention.setParent(parent);
977 aurelien 238
				parent.ajouterCommentaire((Commentaire)intervention);
889 gduche 239
			}
240
		}
926 benjamin 241
 
889 gduche 242
		retour.addAll(interventionsNonTypees.values());
243
		return retour;
500 aurelien 244
	}
939 benjamin 245
 
941 benjamin 246
	/**
247
	 * Recupere une liste de commentaires à partir du JSON
248
	 *
249
	 * @param imageJson
250
	 * @return
251
	 */
931 aurelien 252
	public static String convertirEtParserRetourAjoutCommentaire(String retour) {
929 aurelien 253
		JSONObject retourJson = JSONParser.parseStrict(retour).isObject();
254
		return parserRetourAjoutCommentaire(retourJson);
255
	}
926 benjamin 256
 
941 benjamin 257
	/**
258
	 * Recupere une liste de commentaires à partir d'un objet JSON
259
	 *
260
	 * @param imageJson
261
	 * @return
262
	 */
884 aurelien 263
	public static String parserRetourAjoutCommentaire(JSONObject retour) {
264
		String id = "";
926 benjamin 265
		if (retour != null) {
884 aurelien 266
			id = getValeurOuVide(retour, "id_commentaire");
267
		}
268
		return id;
269
	}
926 benjamin 270
 
888 gduche 271
	public static String getValeurOuVide(JSONObject objet, String index) {
920 aurelien 272
		return (objet.get(index) != null && objet.get(index).isString() != null) ? objet.get(index).isString().stringValue() : "";
888 gduche 273
	}
926 benjamin 274
 
941 benjamin 275
	/**
276
	 * Recupere une liste de votes sur une determination à partir du JSON
277
	 *
278
	 * @param imageJson
279
	 * @return
280
	 */
939 benjamin 281
	public static HashMap<String, VoteDetermination> parserRetourListeVotesDetermination(String votesString) {
900 aurelien 282
 
283
		HashMap<String, VoteDetermination> retour = null;
926 benjamin 284
 
900 aurelien 285
		JSONObject votes = JSONParser.parseStrict(votesString).isObject();
926 benjamin 286
		if (votes != null && votes.get("resultats") != null && votes.get("resultats").isObject() != null) {
900 aurelien 287
			JSONObject resultat = votes.get("resultats").isObject();
939 benjamin 288
			retour = parserVotesDetermination(resultat);
900 aurelien 289
		}
290
		return retour;
291
	}
926 benjamin 292
 
941 benjamin 293
	/**
294
	 * Recupere une liste de votes sur une determination à partir d'un objet
295
	 * JSON
296
	 *
297
	 * @param imageJson
298
	 * @return
299
	 */
939 benjamin 300
	public static HashMap<String, VoteDetermination> parserVotesDetermination(JSONObject votes) {
846 aurelien 301
		HashMap<String, VoteDetermination> votesDetermination = new HashMap<String, VoteDetermination>();
302
		java.util.Iterator<String> itVotes = votes.keySet().iterator();
303
		while (itVotes.hasNext()) {
304
			JSONObject vote = votes.get(itVotes.next()).isObject();
305
			VoteDetermination vd = new VoteDetermination();
306
			vd.setContributeur(getValeurOuVide(vote, "auteur.id"));
965 aurelien 307
			vd.setDate(parserDateObservation(getValeurOuVide(vote, "date")));
846 aurelien 308
			vd.setId(getValeurOuVide(vote, "vote.id"));
309
			vd.setVote(Integer.parseInt(getValeurOuVide(vote, "vote")));
900 aurelien 310
			vd.setContributeur(getValeurOuVide(vote, "auteur.id"));
926 benjamin 311
 
312
			if (vote.get("auteur.nom") != null && vote.get("auteur.nom") != null && vote.get("auteur.courriel") != null) {
313
				Contributeur auteur = new Contributeur(getValeurOuVide(vote, "auteur.id"), getValeurOuVide(vote, "auteur.nom"), getValeurOuVide(vote, "auteur.prenom"), getValeurOuVide(vote, "auteur.courriel"));
900 aurelien 314
				vd.setAuteur(auteur);
315
			}
926 benjamin 316
 
846 aurelien 317
			votesDetermination.put(getValeurOuVide(vote, "auteur.id"), vd);
926 benjamin 318
		}
846 aurelien 319
		return votesDetermination;
320
	}
926 benjamin 321
 
941 benjamin 322
	/**
323
	 * Recupere une liste de votes sur des images à partir d'un objet JSON
324
	 *
325
	 * @param imageJson
326
	 * @return
327
	 */
926 benjamin 328
	public static HashMap<String, HashMap<String, VoteProtocole>> parserVotesProtocoles(JSONObject votes) {
329
 
330
		HashMap<String, HashMap<String, VoteProtocole>> votesProtocoles = new HashMap<String, HashMap<String, VoteProtocole>>();
777 aurelien 331
		java.util.Iterator<String> itProtocoles = votes.keySet().iterator();
926 benjamin 332
		while (itProtocoles.hasNext()) {
333
			JSONObject protocole = votes.get(itProtocoles.next()).isObject();
777 aurelien 334
			JSONObject votesPourCeProtocoles = protocole.get("votes").isObject();
335
			String idProtocoleVote = protocole.get("protocole.id").isString().stringValue();
336
			java.util.Iterator<String> itVotes = votesPourCeProtocoles.keySet().iterator();
926 benjamin 337
			while (itVotes.hasNext()) {
777 aurelien 338
				JSONObject voteEnCours = votesPourCeProtocoles.get(itVotes.next()).isObject();
339
				VoteProtocole vd = new VoteProtocole();
340
				vd.setContributeur(voteEnCours.get("auteur.id").isString().stringValue());
341
				// TODO récupérer la date du vote et la parser
965 aurelien 342
				vd.setDate(parserDateObservation(getValeurOuVide(voteEnCours, "date")));
777 aurelien 343
				int valeurVote = Integer.parseInt(voteEnCours.get("vote").isString().stringValue());
344
				vd.setVote(valeurVote);
926 benjamin 345
				if (!votesProtocoles.containsKey(idProtocoleVote)) {
346
					votesProtocoles.put(idProtocoleVote, new HashMap<String, VoteProtocole>());
777 aurelien 347
				}
348
				votesProtocoles.get(idProtocoleVote).put(vd.getContributeur(), vd);
500 aurelien 349
			}
350
		}
926 benjamin 351
 
500 aurelien 352
		return votesProtocoles;
353
	}
926 benjamin 354
 
941 benjamin 355
	/**
356
	 * Recupere une date à partir du JSON
357
	 *
358
	 * @param imageJson
359
	 * @return
360
	 */
500 aurelien 361
	public static Date parserDateObservation(String date) {
362
		Date dateParsee = new Date();
965 aurelien 363
		DateTimeFormat formatDateObs = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
926 benjamin 364
		try {
365
			dateParsee = formatDateObs.parse(date);
366
		} catch (IllegalArgumentException e) {
367
			dateParsee = new java.sql.Date(0);
500 aurelien 368
		}
926 benjamin 369
		return dateParsee;
500 aurelien 370
	}
926 benjamin 371
 
941 benjamin 372
	/**
373
	 * Recupere des mots-clefs à partir du JSON
374
	 *
375
	 * @param imageJson
376
	 * @return
377
	 */
500 aurelien 378
	public static List<String> parserMotsCles(String motsClesTexte) {
379
		String[] tabMotsCle = motsClesTexte.split(",");
380
		List<String> motsClesParses = new ArrayList<String>();
381
		for (int i = 0; i < tabMotsCle.length; i++) {
382
			motsClesParses.add(tabMotsCle[i]);
383
		}
926 benjamin 384
 
500 aurelien 385
		return motsClesParses;
386
	}
926 benjamin 387
 
500 aurelien 388
	public static String getIdAvecPadding(String id) {
389
		int maxZeros = 9 - id.length();
390
		for (int i = 0; i < maxZeros; i++) {
391
			id = "0" + id;
392
		}
393
		return id;
394
	}
926 benjamin 395
 
941 benjamin 396
	/**
397
	 * Recupere un utilisateur à partir du JSON
398
	 *
399
	 * @param imageJson
400
	 * @return
401
	 */
516 aurelien 402
	public static Utilisateur parserUtilisateurJson(JSONValue valeurJson) {
926 benjamin 403
 
516 aurelien 404
		JSONObject utilisateurJson = valeurJson.isObject();
405
		boolean connecteUtilisateur = utilisateurJson.get("connecte").isBoolean().booleanValue();
406
		String idUtilisateur = utilisateurJson.get("id_utilisateur").isString().stringValue();
926 benjamin 407
 
516 aurelien 408
		Utilisateur utilisateur;
926 benjamin 409
 
410
		if (connecteUtilisateur) {
522 aurelien 411
			String courrielUtilisateur = utilisateurJson.get("courriel").isString().stringValue();
516 aurelien 412
			String nomUtilisateur = utilisateurJson.get("nom").isString().stringValue();
413
			String prenomUtilisateur = utilisateurJson.get("prenom").isString().stringValue();
926 benjamin 414
 
516 aurelien 415
			utilisateur = new Utilisateur(idUtilisateur, prenomUtilisateur, nomUtilisateur, courrielUtilisateur);
416
		} else {
417
			utilisateur = new Utilisateur(idUtilisateur);
418
		}
926 benjamin 419
 
516 aurelien 420
		return utilisateur;
421
	}
941 benjamin 422
 
423
	/**
424
	 * Retourne un objet {@link ProtocoleServiceResultat} à partir d'un objet
425
	 * JSON
426
	 *
427
	 * @param retourJson
428
	 * @return
429
	 */
430
	public static ProtocoleServiceResultat parserProtocoleServiceResultat(JSONValue retourJson) {
431
		List<Protocole> protocoles = new ArrayList<Protocole>();
432
		JSONObject tableauProto = retourJson.isObject().get("resultats").isObject();
433
 
434
		if (tableauProto != null) {
435
			java.util.Iterator<String> it = tableauProto.keySet().iterator();
436
			while (it.hasNext()) {
437
 
438
				JSONObject protocoleJSON = tableauProto.get(it.next()).isObject();
439
				Protocole protocole = new Protocole();
440
				String idProtocole = UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.id");
441
				protocole.setId(Integer.parseInt(idProtocole));
442
				protocole.setNom(UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.intitule"));
443
				protocole.setDescription(UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.descriptif"));
444
				protocoles.add(protocole);
445
			}
446
		}
447
 
448
		return new ProtocoleServiceResultat(protocoles);
449
	}
450
 
451
	/**
452
	 * Retourne un objet {@link ImageServiceResultat} à partir du JSON
453
	 *
454
	 * @param retourJson
455
	 * @return
456
	 */
457
	public static ImageServiceResultat parserImageServiceResultat(JSONValue retourJson) {
458
 
459
		ImageServiceResultat imageServiceResultat = new ImageServiceResultat();
460
		int nbTotalImagesPourLaRecherche;
461
 
462
		List<Image> images = new ArrayList<Image>();
463
		// TODO ajouter vérifications plus précises
464
		if (retourJson.isObject().get("entete") != null) {
465
			double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
466
			nbTotalImagesPourLaRecherche = (int) total;
467
			JSONObject tableauImg = retourJson.isObject().get("resultats").isObject();
468
			java.util.Iterator<String> it = tableauImg.keySet().iterator();
469
			while (it.hasNext()) {
470
				JSONObject imageJson = tableauImg.get(it.next()).isObject();
471
				Image image = parserRetourImage(imageJson);
472
				images.add(image);
473
			}
474
 
475
		} else {
476
			JSONArray tableauImg = retourJson.isObject().get("images").isArray();
477
			nbTotalImagesPourLaRecherche = (int) tableauImg.size();
478
			for (int i = 0; i < nbTotalImagesPourLaRecherche; i++) {
479
				JSONObject imageJson = tableauImg.get(i).isObject();
480
				Image image = parserRetourImage(imageJson);
481
				images.add(image);
482
			}
483
		}
484
 
485
		imageServiceResultat.setImages(images);
486
		imageServiceResultat.setNbTotalImagesPourLaRecherche(nbTotalImagesPourLaRecherche);
487
 
488
		return imageServiceResultat;
489
 
490
	}
491
 
492
	/**
493
	 * Retourne un objet {@link Image} à partir du JSON
494
	 *
495
	 * @param retourJson
496
	 * @return
497
	 */
498
	public static Image parserRetourImage(JSONObject imageJson) {
499
		Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
500
 
501
		if (imageJson.get("observation") != null && imageJson.get("observation").isObject() != null) {
502
			JSONObject observationJson = imageJson.get("observation").isObject();
503
			image.setObservation(UtilitairesServiceResultat.parserObservationJSON(observationJson));
504
		}
505
 
506
		if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
507
			JSONObject votes = imageJson.get("protocoles_votes").isObject();
508
			image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
509
		}
510
 
511
		return image;
512
	}
513
 
514
	/**
515
	 * Retourne un objet {@link ObservationServiceResultat} à partir du JSON
516
	 *
517
	 * @param retourJson
518
	 * @return
519
	 */
520
	public static ObservationServiceResultat parserObservationServiceResultat(JSONValue retourJson) {
521
 
522
		ObservationServiceResultat observationServiceResultat = new ObservationServiceResultat();
523
 
524
		List<Observation> observations = new ArrayList<Observation>();
525
		int nbTotalObservationsPourLaRecherche = 0;
526
 
527
		if (retourJson.isObject().get("entete") != null) {
528
			// TODO ajouter vérifications plus précises
529
			double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
530
			nbTotalObservationsPourLaRecherche = (int) total;
531
			JSONObject tableauObs = retourJson.isObject().get("resultats").isObject();
532
 
533
			if (tableauObs != null) {
534
				java.util.Iterator<String> it = tableauObs.keySet().iterator();
535
				while (it.hasNext()) {
536
					JSONObject observationJson = tableauObs.get(it.next()).isObject();
537
					Observation observation = analyserObservation(observationJson);
538
					observations.add(observation);
539
				}
540
			}
541
		} else {
542
			JSONObject observationJson = retourJson.isObject();
543
			Observation observation = analyserObservation(observationJson);
544
			observations.add(observation);
545
			CacheClient.getInstance().setObservationCourante(observation);
546
		}
547
 
548
		observationServiceResultat.setObservations(observations);
549
		observationServiceResultat.setNbTotalObservationsPourLaRecherche(nbTotalObservationsPourLaRecherche);
550
 
551
		return observationServiceResultat;
552
	}
553
 
554
	/**
555
	 * Retourne un objet {@link Observation} avec ses {@link Image} associées à
556
	 * partir du JSON
557
	 *
558
	 * @param retourJson
559
	 * @return
560
	 */
561
	private static Observation analyserObservation(JSONObject observationJson) {
562
		Observation observation = UtilitairesServiceResultat.parserObservationJSON(observationJson);
563
		JSONArray tableauImagesObs = observationJson.get("images").isArray();
564
		List<Image> imagesPourObs = new ArrayList<Image>();
565
 
566
		int nbImages = tableauImagesObs.size();
567
		for (int j = 0; j < nbImages; j++) {
568
			JSONObject imageJson = tableauImagesObs.get(j).isObject();
569
 
570
			Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
571
			if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
572
				JSONObject votes = imageJson.get("protocoles_votes").isObject();
573
				image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
574
			}
575
			image.setObservation(observation);
576
			imagesPourObs.add(image);
577
		}
578
 
579
		observation.setImages(imagesPourObs);
580
 
581
		return observation;
582
	}
583
 
500 aurelien 584
}