Subversion Repositories eFlore/Applications.del

Rev

Rev 1982 | Rev 1986 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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