Subversion Repositories eFlore/Applications.del

Rev

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

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