Subversion Repositories eFlore/Applications.del

Rev

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

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