Subversion Repositories eFlore/Applications.del

Rev

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