Subversion Repositories eFlore/Applications.del

Rev

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