Subversion Repositories eFlore/Applications.del

Rev

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

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