Subversion Repositories eFlore/Applications.del

Rev

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