Subversion Repositories eFlore/Applications.del

Rev

Rev 954 | Rev 977 | 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
 
888 gduche 178
			String idUtilisateur = getValeurOuVide(jsonIntervention, "ce_utilisateur");
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);
889 gduche 238
			}
239
		}
926 benjamin 240
 
889 gduche 241
		retour.addAll(interventionsNonTypees.values());
242
		return retour;
500 aurelien 243
	}
939 benjamin 244
 
941 benjamin 245
	/**
246
	 * Recupere une liste de commentaires à partir du JSON
247
	 *
248
	 * @param imageJson
249
	 * @return
250
	 */
931 aurelien 251
	public static String convertirEtParserRetourAjoutCommentaire(String retour) {
929 aurelien 252
		JSONObject retourJson = JSONParser.parseStrict(retour).isObject();
253
		return parserRetourAjoutCommentaire(retourJson);
254
	}
926 benjamin 255
 
941 benjamin 256
	/**
257
	 * Recupere une liste de commentaires à partir d'un objet JSON
258
	 *
259
	 * @param imageJson
260
	 * @return
261
	 */
884 aurelien 262
	public static String parserRetourAjoutCommentaire(JSONObject retour) {
263
		String id = "";
926 benjamin 264
		if (retour != null) {
884 aurelien 265
			id = getValeurOuVide(retour, "id_commentaire");
266
		}
267
		return id;
268
	}
926 benjamin 269
 
888 gduche 270
	public static String getValeurOuVide(JSONObject objet, String index) {
920 aurelien 271
		return (objet.get(index) != null && objet.get(index).isString() != null) ? objet.get(index).isString().stringValue() : "";
888 gduche 272
	}
926 benjamin 273
 
941 benjamin 274
	/**
275
	 * Recupere une liste de votes sur une determination à partir du JSON
276
	 *
277
	 * @param imageJson
278
	 * @return
279
	 */
939 benjamin 280
	public static HashMap<String, VoteDetermination> parserRetourListeVotesDetermination(String votesString) {
900 aurelien 281
 
282
		HashMap<String, VoteDetermination> retour = null;
926 benjamin 283
 
900 aurelien 284
		JSONObject votes = JSONParser.parseStrict(votesString).isObject();
926 benjamin 285
		if (votes != null && votes.get("resultats") != null && votes.get("resultats").isObject() != null) {
900 aurelien 286
			JSONObject resultat = votes.get("resultats").isObject();
939 benjamin 287
			retour = parserVotesDetermination(resultat);
900 aurelien 288
		}
289
		return retour;
290
	}
926 benjamin 291
 
941 benjamin 292
	/**
293
	 * Recupere une liste de votes sur une determination à partir d'un objet
294
	 * JSON
295
	 *
296
	 * @param imageJson
297
	 * @return
298
	 */
939 benjamin 299
	public static HashMap<String, VoteDetermination> parserVotesDetermination(JSONObject votes) {
846 aurelien 300
		HashMap<String, VoteDetermination> votesDetermination = new HashMap<String, VoteDetermination>();
301
		java.util.Iterator<String> itVotes = votes.keySet().iterator();
302
		while (itVotes.hasNext()) {
303
			JSONObject vote = votes.get(itVotes.next()).isObject();
304
			VoteDetermination vd = new VoteDetermination();
305
			vd.setContributeur(getValeurOuVide(vote, "auteur.id"));
965 aurelien 306
			vd.setDate(parserDateObservation(getValeurOuVide(vote, "date")));
846 aurelien 307
			vd.setId(getValeurOuVide(vote, "vote.id"));
308
			vd.setVote(Integer.parseInt(getValeurOuVide(vote, "vote")));
900 aurelien 309
			vd.setContributeur(getValeurOuVide(vote, "auteur.id"));
926 benjamin 310
 
311
			if (vote.get("auteur.nom") != null && vote.get("auteur.nom") != null && vote.get("auteur.courriel") != null) {
312
				Contributeur auteur = new Contributeur(getValeurOuVide(vote, "auteur.id"), getValeurOuVide(vote, "auteur.nom"), getValeurOuVide(vote, "auteur.prenom"), getValeurOuVide(vote, "auteur.courriel"));
900 aurelien 313
				vd.setAuteur(auteur);
314
			}
926 benjamin 315
 
846 aurelien 316
			votesDetermination.put(getValeurOuVide(vote, "auteur.id"), vd);
926 benjamin 317
		}
846 aurelien 318
		return votesDetermination;
319
	}
926 benjamin 320
 
941 benjamin 321
	/**
322
	 * Recupere une liste de votes sur des images à partir d'un objet JSON
323
	 *
324
	 * @param imageJson
325
	 * @return
326
	 */
926 benjamin 327
	public static HashMap<String, HashMap<String, VoteProtocole>> parserVotesProtocoles(JSONObject votes) {
328
 
329
		HashMap<String, HashMap<String, VoteProtocole>> votesProtocoles = new HashMap<String, HashMap<String, VoteProtocole>>();
777 aurelien 330
		java.util.Iterator<String> itProtocoles = votes.keySet().iterator();
926 benjamin 331
		while (itProtocoles.hasNext()) {
332
			JSONObject protocole = votes.get(itProtocoles.next()).isObject();
777 aurelien 333
			JSONObject votesPourCeProtocoles = protocole.get("votes").isObject();
334
			String idProtocoleVote = protocole.get("protocole.id").isString().stringValue();
335
			java.util.Iterator<String> itVotes = votesPourCeProtocoles.keySet().iterator();
926 benjamin 336
			while (itVotes.hasNext()) {
777 aurelien 337
				JSONObject voteEnCours = votesPourCeProtocoles.get(itVotes.next()).isObject();
338
				VoteProtocole vd = new VoteProtocole();
339
				vd.setContributeur(voteEnCours.get("auteur.id").isString().stringValue());
340
				// TODO récupérer la date du vote et la parser
965 aurelien 341
				vd.setDate(parserDateObservation(getValeurOuVide(voteEnCours, "date")));
777 aurelien 342
				int valeurVote = Integer.parseInt(voteEnCours.get("vote").isString().stringValue());
343
				vd.setVote(valeurVote);
926 benjamin 344
				if (!votesProtocoles.containsKey(idProtocoleVote)) {
345
					votesProtocoles.put(idProtocoleVote, new HashMap<String, VoteProtocole>());
777 aurelien 346
				}
347
				votesProtocoles.get(idProtocoleVote).put(vd.getContributeur(), vd);
500 aurelien 348
			}
349
		}
926 benjamin 350
 
500 aurelien 351
		return votesProtocoles;
352
	}
926 benjamin 353
 
941 benjamin 354
	/**
355
	 * Recupere une date à partir du JSON
356
	 *
357
	 * @param imageJson
358
	 * @return
359
	 */
500 aurelien 360
	public static Date parserDateObservation(String date) {
361
		Date dateParsee = new Date();
965 aurelien 362
		DateTimeFormat formatDateObs = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
926 benjamin 363
		try {
364
			dateParsee = formatDateObs.parse(date);
365
		} catch (IllegalArgumentException e) {
366
			dateParsee = new java.sql.Date(0);
500 aurelien 367
		}
926 benjamin 368
		return dateParsee;
500 aurelien 369
	}
926 benjamin 370
 
941 benjamin 371
	/**
372
	 * Recupere des mots-clefs à partir du JSON
373
	 *
374
	 * @param imageJson
375
	 * @return
376
	 */
500 aurelien 377
	public static List<String> parserMotsCles(String motsClesTexte) {
378
		String[] tabMotsCle = motsClesTexte.split(",");
379
		List<String> motsClesParses = new ArrayList<String>();
380
		for (int i = 0; i < tabMotsCle.length; i++) {
381
			motsClesParses.add(tabMotsCle[i]);
382
		}
926 benjamin 383
 
500 aurelien 384
		return motsClesParses;
385
	}
926 benjamin 386
 
500 aurelien 387
	public static String getIdAvecPadding(String id) {
388
		int maxZeros = 9 - id.length();
389
		for (int i = 0; i < maxZeros; i++) {
390
			id = "0" + id;
391
		}
392
		return id;
393
	}
926 benjamin 394
 
941 benjamin 395
	/**
396
	 * Recupere un utilisateur à partir du JSON
397
	 *
398
	 * @param imageJson
399
	 * @return
400
	 */
516 aurelien 401
	public static Utilisateur parserUtilisateurJson(JSONValue valeurJson) {
926 benjamin 402
 
516 aurelien 403
		JSONObject utilisateurJson = valeurJson.isObject();
404
		boolean connecteUtilisateur = utilisateurJson.get("connecte").isBoolean().booleanValue();
405
		String idUtilisateur = utilisateurJson.get("id_utilisateur").isString().stringValue();
926 benjamin 406
 
516 aurelien 407
		Utilisateur utilisateur;
926 benjamin 408
 
409
		if (connecteUtilisateur) {
522 aurelien 410
			String courrielUtilisateur = utilisateurJson.get("courriel").isString().stringValue();
516 aurelien 411
			String nomUtilisateur = utilisateurJson.get("nom").isString().stringValue();
412
			String prenomUtilisateur = utilisateurJson.get("prenom").isString().stringValue();
926 benjamin 413
 
516 aurelien 414
			utilisateur = new Utilisateur(idUtilisateur, prenomUtilisateur, nomUtilisateur, courrielUtilisateur);
415
		} else {
416
			utilisateur = new Utilisateur(idUtilisateur);
417
		}
926 benjamin 418
 
516 aurelien 419
		return utilisateur;
420
	}
941 benjamin 421
 
422
	/**
423
	 * Retourne un objet {@link ProtocoleServiceResultat} à partir d'un objet
424
	 * JSON
425
	 *
426
	 * @param retourJson
427
	 * @return
428
	 */
429
	public static ProtocoleServiceResultat parserProtocoleServiceResultat(JSONValue retourJson) {
430
		List<Protocole> protocoles = new ArrayList<Protocole>();
431
		JSONObject tableauProto = retourJson.isObject().get("resultats").isObject();
432
 
433
		if (tableauProto != null) {
434
			java.util.Iterator<String> it = tableauProto.keySet().iterator();
435
			while (it.hasNext()) {
436
 
437
				JSONObject protocoleJSON = tableauProto.get(it.next()).isObject();
438
				Protocole protocole = new Protocole();
439
				String idProtocole = UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.id");
440
				protocole.setId(Integer.parseInt(idProtocole));
441
				protocole.setNom(UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.intitule"));
442
				protocole.setDescription(UtilitairesServiceResultat.getValeurOuVide(protocoleJSON, "protocole.descriptif"));
443
				protocoles.add(protocole);
444
			}
445
		}
446
 
447
		return new ProtocoleServiceResultat(protocoles);
448
	}
449
 
450
	/**
451
	 * Retourne un objet {@link ImageServiceResultat} à partir du JSON
452
	 *
453
	 * @param retourJson
454
	 * @return
455
	 */
456
	public static ImageServiceResultat parserImageServiceResultat(JSONValue retourJson) {
457
 
458
		ImageServiceResultat imageServiceResultat = new ImageServiceResultat();
459
		int nbTotalImagesPourLaRecherche;
460
 
461
		List<Image> images = new ArrayList<Image>();
462
		// TODO ajouter vérifications plus précises
463
		if (retourJson.isObject().get("entete") != null) {
464
			double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
465
			nbTotalImagesPourLaRecherche = (int) total;
466
			JSONObject tableauImg = retourJson.isObject().get("resultats").isObject();
467
			java.util.Iterator<String> it = tableauImg.keySet().iterator();
468
			while (it.hasNext()) {
469
				JSONObject imageJson = tableauImg.get(it.next()).isObject();
470
				Image image = parserRetourImage(imageJson);
471
				images.add(image);
472
			}
473
 
474
		} else {
475
			JSONArray tableauImg = retourJson.isObject().get("images").isArray();
476
			nbTotalImagesPourLaRecherche = (int) tableauImg.size();
477
			for (int i = 0; i < nbTotalImagesPourLaRecherche; i++) {
478
				JSONObject imageJson = tableauImg.get(i).isObject();
479
				Image image = parserRetourImage(imageJson);
480
				images.add(image);
481
			}
482
		}
483
 
484
		imageServiceResultat.setImages(images);
485
		imageServiceResultat.setNbTotalImagesPourLaRecherche(nbTotalImagesPourLaRecherche);
486
 
487
		return imageServiceResultat;
488
 
489
	}
490
 
491
	/**
492
	 * Retourne un objet {@link Image} à partir du JSON
493
	 *
494
	 * @param retourJson
495
	 * @return
496
	 */
497
	public static Image parserRetourImage(JSONObject imageJson) {
498
		Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
499
 
500
		if (imageJson.get("observation") != null && imageJson.get("observation").isObject() != null) {
501
			JSONObject observationJson = imageJson.get("observation").isObject();
502
			image.setObservation(UtilitairesServiceResultat.parserObservationJSON(observationJson));
503
		}
504
 
505
		if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
506
			JSONObject votes = imageJson.get("protocoles_votes").isObject();
507
			image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
508
		}
509
 
510
		return image;
511
	}
512
 
513
	/**
514
	 * Retourne un objet {@link ObservationServiceResultat} à partir du JSON
515
	 *
516
	 * @param retourJson
517
	 * @return
518
	 */
519
	public static ObservationServiceResultat parserObservationServiceResultat(JSONValue retourJson) {
520
 
521
		ObservationServiceResultat observationServiceResultat = new ObservationServiceResultat();
522
 
523
		List<Observation> observations = new ArrayList<Observation>();
524
		int nbTotalObservationsPourLaRecherche = 0;
525
 
526
		if (retourJson.isObject().get("entete") != null) {
527
			// TODO ajouter vérifications plus précises
528
			double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
529
			nbTotalObservationsPourLaRecherche = (int) total;
530
			JSONObject tableauObs = retourJson.isObject().get("resultats").isObject();
531
 
532
			if (tableauObs != null) {
533
				java.util.Iterator<String> it = tableauObs.keySet().iterator();
534
				while (it.hasNext()) {
535
					JSONObject observationJson = tableauObs.get(it.next()).isObject();
536
					Observation observation = analyserObservation(observationJson);
537
					observations.add(observation);
538
				}
539
			}
540
		} else {
541
			JSONObject observationJson = retourJson.isObject();
542
			Observation observation = analyserObservation(observationJson);
543
			observations.add(observation);
544
			CacheClient.getInstance().setObservationCourante(observation);
545
		}
546
 
547
		observationServiceResultat.setObservations(observations);
548
		observationServiceResultat.setNbTotalObservationsPourLaRecherche(nbTotalObservationsPourLaRecherche);
549
 
550
		return observationServiceResultat;
551
	}
552
 
553
	/**
554
	 * Retourne un objet {@link Observation} avec ses {@link Image} associées à
555
	 * partir du JSON
556
	 *
557
	 * @param retourJson
558
	 * @return
559
	 */
560
	private static Observation analyserObservation(JSONObject observationJson) {
561
		Observation observation = UtilitairesServiceResultat.parserObservationJSON(observationJson);
562
		JSONArray tableauImagesObs = observationJson.get("images").isArray();
563
		List<Image> imagesPourObs = new ArrayList<Image>();
564
 
565
		int nbImages = tableauImagesObs.size();
566
		for (int j = 0; j < nbImages; j++) {
567
			JSONObject imageJson = tableauImagesObs.get(j).isObject();
568
 
569
			Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
570
			if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
571
				JSONObject votes = imageJson.get("protocoles_votes").isObject();
572
				image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
573
			}
574
			image.setObservation(observation);
575
			imagesPourObs.add(image);
576
		}
577
 
578
		observation.setImages(imagesPourObs);
579
 
580
		return observation;
581
	}
582
 
500 aurelien 583
}