Subversion Repositories eFlore/Applications.del

Rev

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

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