Rev 730 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.tela_botanica.client.modeles;import org.tela_botanica.client.image.ImageModele;import org.tela_botanica.client.interfaces.Rafraichissable;import org.tela_botanica.client.observation.ObservationModele;import org.tela_botanica.client.util.Util;import com.google.gwt.core.client.GWT;import com.google.gwt.http.client.Request;import com.google.gwt.http.client.RequestBuilder;import com.google.gwt.http.client.RequestCallback;import com.google.gwt.http.client.RequestException;import com.google.gwt.http.client.Response;import com.google.gwt.http.client.URL;import com.google.gwt.json.client.JSONArray;import com.google.gwt.json.client.JSONParser;import com.google.gwt.json.client.JSONString;import com.google.gwt.json.client.JSONValue;import com.google.gwt.user.client.Window;/*** DAO la liste des observations attachées a un observateur.* @author David Delon* TODO : se servir de ObservationDAO pour la lecture unitaire**/public class ListeObservationAsynchroneDAO {/*** Le modèle associé au DAO.*/private ObservationModele observationModele = null;private ImageModele imageModele = null;public ListeObservationAsynchroneDAO(final ObservationModele obs) {this.observationModele=obs;}/*** Le modèle associé au DAO.*/public ListeObservationAsynchroneDAO(final ImageModele img) {this.imageModele = img;}/*** Envoie une requete au serveur jrest pour obtenir le nombre d'observation correspondant* à des critères données en paramètres* @param r le rafraichissable qui demande la mise à jour* @param criteres un tableau nom/valeur des critères pour les observations*/public final void obtenirListeObservation(final Rafraichissable r, final String utilisateur, final String[][] criteres){String requete = "" ;if(criteres != null){// on construit les paramètres du get avec les critères (&critere1=valeur1&critere2=valeur2 etc...)// ils contiennent limite et taille page et autres filtres (communes , lieu dit etc ...)Sfor (int i = 0; i < criteres.length; i++) {if(criteres[i][1] != null && !criteres[i][1].equals("")){if(i!= 0){requete += "&";}requete += criteres[i][0]+"="+URL.encodeComponent(criteres[i][1]) ;}}}// on envoie le get asynchroneRequestBuilder rb = new RequestBuilder(RequestBuilder.GET,Configuration.getServiceBaseUrl()+"/InventoryObservationList/"+utilisateur+"/"+requete) ;try {rb.sendRequest(null, new RequestCallback() {public void onError(final Request request, final Throwable exception) {// TODO Auto-generated method stub}public void onResponseReceived(final Request request,final Response response) {final ListeObservation observationData ;final JSONValue responseValue = JSONParser.parse(response.getText());JSONArray reponse=null;// si c'est un tableauif ((reponse=responseValue.isArray()) != null) {JSONArray observation;final int taillemax = reponse.size();observationData = new ListeObservation(taillemax);for (int i = 0; i < taillemax; i++) {if ((observation=reponse.get(i).isArray()) != null) {String transmis=((JSONString) observation.get(13)).stringValue();String identifiantLocalite=((JSONString) observation.get(14)).toString();String nomSaisi=Util.toCelString(((JSONString) observation.get(0)).toString());String nomRetenu=Util.toCelString(((JSONString) observation.get(2)).toString());String numeroNomenclaturalSaisi=((JSONString) observation.get(1)).stringValue();String numeroNomenclaturalRetenu=((JSONString) observation.get(3)).stringValue();String numeroTaxonomique=((JSONString) observation.get(4)).stringValue();String famille=Util.toCelString(((JSONString) observation .get(5)).toString());String localite=Util.toCelString(((JSONString) observation .get(6)).toString());String lieudit=Util.toCelString(((JSONString) observation .get(9)).toString());String station=Util.toCelString(((JSONString) observation .get(10)).toString());String milieu=Util.toCelString(((JSONString) observation .get(11)).toString());String commentaire=Util.toCelString(((JSONString) observation .get(12)).toString());String date=((JSONString) observation .get(8)).stringValue();String numeroOrdre=((JSONString) observation.get(7)).stringValue();String coordX=((JSONString) observation.get(15)).stringValue();String coordY=((JSONString) observation.get(16)).stringValue();String motsCles=((JSONString) observation.get(17)).stringValue();Observation obs=new Observation(transmis, nomSaisi, nomRetenu, numeroNomenclaturalSaisi, numeroNomenclaturalRetenu ,numeroTaxonomique, famille, localite, identifiantLocalite, lieudit, station, milieu, commentaire, date, numeroOrdre/*, motsCles*/);obs.setCoordonneeX(coordX);obs.setCoordonneeY(coordY);obs.setMotsCles(motsCles);observationData.put(obs.getNumeroOrdre(),obs);}}} else {observationData = new ListeObservation(0) ;}// dans tous les cas on transmet la liste crée au rafraichissable en lui demandant de répandre les données car il est// le premier à les recevoir// TODO : ce n'est pas ici qu'on devrait le decider ..r.rafraichir(observationData,true);}}) ;} catch (RequestException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void modifierEnMasse(final Rafraichissable r, String identifiant,final Observation obs) {RequestBuilder rb = new RequestBuilder(RequestBuilder.POST,Configuration.getServiceBaseUrl()+ "/InventoryObservationList/" + identifiant + "/" + obs.getNumeroOrdre()+ "/") ;String postData = "identifiant="+ identifiant ;if(obs.getNomSaisi() != null) {postData += "&nom_sel=" + URL.encodeComponent(obs.getNomSaisi());}if(obs.getNumeroNomenclaturalSaisi() != null) {postData += "&num_nom_sel=" + obs.getNumeroNomenclaturalSaisi();}if(obs.getIdentifiantLocalite() != null) {postData += "&id_location=" + obs.getIdentifiantLocalite();}if(obs.getLocalite() != null) {postData += "&location=" + URL.encodeComponent(obs.getLocalite());}if(obs.getDate() != null) {postData += "&date_observation=" + obs.getDate();}if(obs.getLieudit() != null) {postData += "&lieudit="+ URL.encodeComponent(obs.getLieudit());}if(obs.getStation() != null) {postData += "&station="+ URL.encodeComponent(obs.getStation());}if(obs.getMilieu() != null) {postData += "&milieu="+ URL.encodeComponent(obs.getMilieu());}if(obs.getCommentaire() != null) {postData += "&commentaire="+obs.getCommentaire();}if(obs.getCoordonneeX() != null ) {postData += "&coord_x="+URL.encodeComponent(""+obs.getCoordonneeX());}if(obs.getCoordonneeY() != null) {postData += "&coord_y="+URL.encodeComponent(""+obs.getCoordonneeY());}try {rb.sendRequest(postData, new RequestCallback() {public void onError(Request request, Throwable exception) {// TODO Auto-generated method stub}public void onResponseReceived(Request request,Response response) {if(observationModele != null) {observationModele.obtenirListeObservation(r);}}}) ;} catch (RequestException e) {}}/*** Supprime les observations possédant les identifiants données* @param r le rafraichissable à avertir de la mise à jour* @param identifiant l'identifiant utilisateur* @param numerosOrdre les numéros d'ordre des observations séparés par des virgules*/public void supprimerListeObservation(Rafraichissable r, String identifiant, String numerosOrdre) {String postData = "";postData += "&action=DELETE";// on envoie un post avec l'id de l'image à supprimerRequestBuilder rb = new RequestBuilder(RequestBuilder.POST,Configuration.getServiceBaseUrl()+ "/inventoryObservationList/"+ identifiant+ "/"+ numerosOrdre);try {rb.sendRequest(postData, new RequestCallback() {public void onError(Request request, Throwable exception) {// TODO Auto-generated method stub}public void onResponseReceived(Request request,Response response) {if (response.getText().equals("OK")) {} else {com.google.gwt.user.client.Window.alert("Problème lors de la mise à jour des données");return ;}}});r.rafraichir("OK", true) ;} catch (RequestException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}