Subversion Repositories eFlore/Applications.del

Rev

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