Subversion Repositories eFlore/Applications.del

Rev

Rev 802 | Rev 941 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.tela_botanica.del.client.modeles;

import java.util.ArrayList;
import java.util.List;

import org.tela_botanica.del.client.cache.CacheClient;
import org.tela_botanica.del.client.utils.UtilitairesServiceResultat;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

public class ObservationServiceResultat {
        
        private List<Observation> observations;
        
        private int nbTotalObservationsPourLaRecherche;

        public ObservationServiceResultat(JSONValue retourJson) {
                
                observations = new ArrayList<Observation>();
                
                if(retourJson.isObject().get("entete") != null) {
                        //TODO ajouter vérifications plus précises
                        double total = retourJson.isObject().get("entete").isObject().get("total").isNumber().doubleValue();
                        nbTotalObservationsPourLaRecherche = (int) total;
                        JSONObject tableauObs = retourJson.isObject().get("resultats").isObject();
                                                
                        if(tableauObs != null) {
                                java.util.Iterator<String> it = tableauObs.keySet().iterator();
                                while (it.hasNext()) {                                  
                                        JSONObject observationJson = tableauObs.get(it.next()).isObject();
                                        Observation observation = analyserObservation(observationJson);
                                        observations.add(observation);                  
                                }
                        }
                } else {
                        JSONObject observationJson = retourJson.isObject();
                        Observation observation = analyserObservation(observationJson);
                        observations.add(observation);
                        CacheClient.getInstance().setObservationCourante(observation);
                }
        }
        
        private Observation analyserObservation(JSONObject observationJson) {
                Observation observation = UtilitairesServiceResultat.parserObservationEtCreerPropositionDetermination(observationJson);
                JSONArray tableauImagesObs = observationJson.get("images").isArray();
                List<Image> imagesPourObs = new ArrayList<Image>();
                
                int nbImages = tableauImagesObs.size();
                for (int j = 0; j < nbImages; j++) {
                        JSONObject imageJson = tableauImagesObs.get(j).isObject();
                        
                        Image image = UtilitairesServiceResultat.parserImageJSON(imageJson);
                        if (imageJson.get("protocoles_votes") != null && imageJson.get("protocoles_votes").isObject() != null) {
                                JSONObject votes = imageJson.get("protocoles_votes").isObject();
                                image.setVoteProtocoles(UtilitairesServiceResultat.parserVotesProtocoles(votes));
                        }
                        image.setObservation(observation);
                        imagesPourObs.add(image);
                }
                
                observation.setImages(imagesPourObs);
                
                return observation;
        }

        public List<Observation> getObservations() {
                return observations;
        }

        public void setObservations(List<Observation> observations) {
                this.observations = observations;
        }

        public int getNbTotalObservationsPourLaRecherche() {
                return nbTotalObservationsPourLaRecherche;
        }

        public void setNbTotalObservationsPourLaRecherche(int nbTotalObservationsPourLaRecherche) {
                this.nbTotalObservationsPourLaRecherche = nbTotalObservationsPourLaRecherche;
        }
}