Subversion Repositories eFlore/Applications.del

Rev

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