Subversion Repositories eFlore/Applications.del

Rev

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

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