Subversion Repositories eFlore/Applications.del

Rev

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

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