Subversion Repositories eFlore/Applications.del

Rev

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