Subversion Repositories eFlore/Applications.del

Rev

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