Subversion Repositories eFlore/Applications.del

Rev

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

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