Subversion Repositories eFlore/Applications.del

Rev

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

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