Subversion Repositories eFlore/Applications.del

Rev

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