Subversion Repositories eFlore/Applications.del

Rev

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