Subversion Repositories eFlore/Applications.del

Rev

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

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