Subversion Repositories eFlore/Applications.del

Rev

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