Subversion Repositories eFlore/Applications.del

Rev

Rev 1986 | Rev 2050 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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