Subversion Repositories eFlore/Applications.del

Rev

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