Rev 1733 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.tela_botanica.client.modeles.structure;import java.util.HashMap;import org.tela_botanica.client.Mediateur;import org.tela_botanica.client.RegistreId;import org.tela_botanica.client.http.JsonRestRequestBuilder;import org.tela_botanica.client.http.JsonRestRequestCallback;import org.tela_botanica.client.interfaces.Rafraichissable;import org.tela_botanica.client.modeles.Information;import org.tela_botanica.client.modeles.personne.PersonneListe;import org.tela_botanica.client.modeles.projet.ProjetListe;import org.tela_botanica.client.synchronisation.Reponse;import org.tela_botanica.client.util.UtilDAO;import com.extjs.gxt.ui.client.Registry;import com.google.gwt.core.client.GWT;import com.google.gwt.http.client.URL;import com.google.gwt.json.client.JSONArray;import com.google.gwt.json.client.JSONObject;import com.google.gwt.json.client.JSONValue;import com.google.gwt.user.client.Window;public class StructureAsyncDao {private static final String SERVICE_NOM = "CoelStructure";private String utilisateurId = null;private Rafraichissable vueARafraichir = null;public StructureAsyncDao(Rafraichissable vue) {if (Mediateur.DEBUG) System.out.println("|| StructureAsyncDao > vueARafraichir = "+vue.getClass().toString());vueARafraichir = vue;utilisateurId = ((Mediateur) Registry.get(RegistreId.MEDIATEUR)).getUtilisateurId();}/**** @param paginationProgressive : définit le mode de consultation de la base de données* - True : la consultation des données est progressive, ce qui signifie que la liste est chargée (paginée) au* fur et à mesure de la consultation des données par l'utilisateur.* - False : la consultation des données est classique : un seul appel à la base de données est effectué, le retour* est renvoyé à l'appelant* // FIXME : si la taille de la liste est supérieure à la limite du JREST (150), ce deuxieme mode ne fonctionne pas*/public void selectionner(final boolean paginationProgressive, final String projetId, final String structureId, final String nomStructure, final String formatRetour, final int start, final int nbElements, final Integer seqId, final boolean searchCity) {String[] parametres = {projetId, structureId, nomStructure};HashMap<String, String> restrictions = new HashMap<String, String>();if (nbElements != -1) {restrictions.put("limit", String.valueOf(nbElements));}restrictions.put("searchCity", String.valueOf(searchCity));if ((formatRetour != null) && (formatRetour != "")) {restrictions.put("formatRetour", formatRetour);}/** GESTION DE LA REQUETE dans le cas d'une liste paginée progressive **/if (paginationProgressive) {/** DEFINITION DU TUPLE DE DEPART **/restrictions.put("start", String.valueOf(start));/** CONSTRUCTION DE LA REQUETE **/final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);/** ENVOI DE LA REQUETE **/rb.envoyerRequete(null, new JsonRestRequestCallback(){/** RECEPTION DE LA REPONSE **/public void surReponse(JSONValue responseValue){if (seqId != null) {Reponse reponseRequete = new Reponse(responseValue, seqId);vueARafraichir.rafraichir(reponseRequete);}/** Dans le cas d'une liste paginée, vueARafraichir est un objet Proxy.* On retourne l'objet JSON au proxy afin que ce soit lui qui le traite **/else vueARafraichir.rafraichir(responseValue);}});}/** GESTION DE LA REQUETE dans le cas d'une liste NON paginée progressive **/else {/** DEFINITION DU TUPLE DE DEPART **/restrictions.put("start", String.valueOf(start*nbElements));final JsonRestRequestBuilder rb = UtilDAO.construireRequete(SERVICE_NOM, parametres, restrictions);rb.envoyerRequete(null, new JsonRestRequestCallback() {public void surReponse(JSONValue responseValue) {if (responseValue != null) {Information info = new Information("selection_structure");JSONObject responseObject = responseValue.isObject();if (responseObject != null) {// Si la réponse est un tableau, alors c'est une liste de structure qui a été retournéeif (responseObject.get("structures").isArray() != null) {JSONObject reponse = responseObject;StructureListe structures;structures = new StructureListe(reponse.get("structures").isArray(), reponse.get("nbElements").isNumber(), vueARafraichir);structures.setTaillePage(nbElements);structures.setPageCourante(start);info.setDonnee(0, structures);if (seqId != null) {Reponse reponseRequete = new Reponse(info, seqId);vueARafraichir.rafraichir(reponseRequete);}else vueARafraichir.rafraichir(structures);// Si la réponse est un objet, alors c'est une unique structure qui a été retournée} else if (responseObject.get("structures").isObject() != null) {JSONObject reponse = responseObject.get("structures").isObject();Structure structure = new Structure(reponse);StructureConservation structureConservation = new StructureConservation(reponse);StructureValorisation structureValorisation = new StructureValorisation(reponse);info.setDonnee(0, structure);info.setDonnee(1, structureConservation);info.setDonnee(2, structureValorisation);if (seqId != null) {Reponse reponseRequete = new Reponse(info, seqId);vueARafraichir.rafraichir(reponseRequete);}else vueARafraichir.rafraichir(info);}} else {GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas un objet ou un talbeau JSON et vaut : "+responseValue.toString(), null);}} else {if (structureId == null) {// Dans le cas, où nous demandons toutes les institutions et qu'il n'y en a pas, nous retournons un objet videStructureListe structures = new StructureListe(0);vueARafraichir.rafraichir(structures);}}}});}}public void ajouter(final Structure str, StructureConservation conservation, StructureValorisation valorisation) {String postDonneesEncodees = construirePost(null, str, conservation, valorisation);final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM);rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {@Overridepublic void surReponse(JSONValue responseValue) {// Si la requête a impliquées des valeurs autogénérées, l'objet modifié est retournéJSONObject obj;if ((obj = responseValue.isObject()) != null) {if (obj.get("structures").isObject() != null) {Information info = new Information("ajout_structure");Structure structure = new Structure(obj.get("structures").isObject());info.setDonnee(structure);vueARafraichir.rafraichir(info);}}else if (responseValue.isString() != null) {Information info = new Information("ajout_structure");String structureIdOuMessage = responseValue.isString().stringValue();if (structureIdOuMessage.matches("^[0-9]+$")) {info.setDonnee(structureIdOuMessage);} else {info.setMessage(structureIdOuMessage);}vueARafraichir.rafraichir(info);} else {GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);}}});}public void modifier(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {String postDonneesEncodees = construirePost(structureId, str, conservation, valorisation);String[] parametres = {structureId};final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);rb.envoyerRequete(postDonneesEncodees, new JsonRestRequestCallback() {@Overridepublic void surReponse(JSONValue responseValue) {// Si la requête a impliquées des valeurs autogénérées, l'objet modifié est retournéJSONObject obj;if ((obj = responseValue.isObject()) != null) {if (obj.get("structures").isObject() != null) {Structure structure = new Structure(obj.get("structures").isObject());Information info = new Information("modif_structure");info.setDonnee(structure);vueARafraichir.rafraichir(info);}}// Si la requête est un succès, reception d'une chaineelse if (responseValue.isString() != null) {Information info = new Information("modif_structure");info.setMessage(responseValue.isString().stringValue());vueARafraichir.rafraichir(info);} else {GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);}}});}public void supprimer(String structuresId) {String[] parametres = {utilisateurId, structuresId};final JsonRestRequestBuilder rb = UtilDAO.construireRequetePost(SERVICE_NOM, parametres);rb.envoyerRequeteSuppression(new JsonRestRequestCallback() {@Overridepublic void surReponse(JSONValue responseValue) {if (responseValue.isString() != null) {Information info = new Information("suppression_structure");info.setMessage(responseValue.isString().stringValue());vueARafraichir.rafraichir(info);} else {GWT.log(rb.getUrl()+"\n\tLa réponse n'est pas une chaine JSON.", null);}}});}private String construirePost(String structureId, Structure str, StructureConservation conservation, StructureValorisation valorisation) {String postDonnees = "cmhl_ce_modifier_par=" + URL.encodeComponent(utilisateurId);if (str != null) {if (structureId != null) {postDonnees += "&cs_id_structure=" + URL.encodeComponent(structureId);}postDonnees += "&cpr_abreviation=" + URL.encodeComponent(str.getAbreviationProjet());postDonnees += "&cs_ce_projet=" + URL.encodeComponent(str.getIdProjet()) +"&cs_ce_mere=" + URL.encodeComponent(str.getIdMere()) +"&cs_guid=" + URL.encodeComponent(str.getGuid()) +"&cs_truk_identifiant_alternatif=" + URL.encodeComponent(str.getIdAlternatif()) +"&cs_nom=" + URL.encodeComponent(str.getNom()) +"&cs_truk_nom_alternatif=" + URL.encodeComponent(str.getNomAlternatif()) +"&cs_description=" + URL.encodeComponent(str.getDescription()) +"&cs_ce_type=" + URL.encodeComponent(str.getType()) +"&cs_ce_truk_type_prive=" + URL.encodeComponent(str.getTypePrive()) +"&cs_ce_truk_type_public=" + URL.encodeComponent(str.getTypePublic()) +"&cs_adresse_01=" + URL.encodeComponent(str.getAdresse()) +"&cs_adresse_02=" + URL.encodeComponent(str.getAdresseComplement()) +"&cs_date_fondation=" + URL.encodeComponent(str.getDateFondationFormatMysql()) +"&cs_code_postal=" + URL.encodeComponent(str.getCodePostal()) +"&cs_ville=" + URL.encodeComponent(str.getVille()) +"&cs_ce_truk_region=" + URL.encodeComponent(str.get("ce_truk_region").toString()) +"&cs_ce_truk_pays=" + URL.encodeComponent(str.getPays()) +"&cs_latitude=" + URL.encodeComponent(str.getLatitude()) +"&cs_longitude=" + URL.encodeComponent(str.getLongitude()) +"&cs_truk_telephone=" + URL.encodeComponent(str.getTelephone()) +"&cs_truk_url=" + URL.encodeComponent(str.getUrl()) +"&cs_nbre_personne=" + (str.getNbrePersonne() != null ? str.getNbrePersonne() : "") +"&cs_condition_acces=" + URL.encodeComponent(str.getConditionAcces())+"&cs_condition_usage=" + URL.encodeComponent(str.getConditionUsage())+"&cs_courriel=" + URL.encodeComponent(str.getCourriel());}if (conservation != null) {if (structureId != null) {postDonnees += "&csc_id_structure=" + URL.encodeComponent(structureId);}postDonnees += "&csc_mark_formation=" + (conservation.getFormation() != null ? conservation.getFormation() : "") +"&csc_formation=" + URL.encodeComponent(conservation.getFormationInfo()) +"&csc_mark_formation_interet=" + (conservation.getFormationInteret() != null ? conservation.getFormationInteret() : "") +"&csc_truk_stockage_local=" + URL.encodeComponent(conservation.getStockageLocal()) +"&csc_truk_stockage_meuble=" + URL.encodeComponent(conservation.getStockageMeuble()) +"&csc_truk_stockage_parametre=" + URL.encodeComponent(conservation.getStockageParametre()) +"&csc_mark_collection_commune=" + (conservation.getCollectionCommune() != null ? conservation.getCollectionCommune() : "") +"&csc_truk_collection_autre=" + URL.encodeComponent(conservation.getCollectionAutre()) +"&csc_mark_acces_controle=" + (conservation.getAccesControle() != null ? conservation.getAccesControle() : "") +"&csc_mark_restauration=" + (conservation.getRestauration() != null ? conservation.getRestauration() : "") +"&csc_truk_restauration_operation=" + URL.encodeComponent(conservation.getRestaurationOperation()) +"&csc_ce_materiel_conservation=" + URL.encodeComponent(conservation.getMaterielConservation()) +"&csc_truk_materiel_autre=" + URL.encodeComponent(conservation.getMaterielAutre()) +"&csc_mark_traitement=" + (conservation.getTraitement() != null ? conservation.getTraitement() : "") +"&csc_truk_traitement=" + URL.encodeComponent(conservation.getTraitements()) +"&csc_mark_acquisition_collection=" + (conservation.getAcquisitionCollection() != null ? conservation.getAcquisitionCollection() : "") +"&csc_mark_acquisition_echantillon=" + (conservation.getAcquisitionEchantillon() != null ? conservation.getAcquisitionEchantillon() : "") +"&csc_mark_acquisition_traitement=" + URL.encodeComponent(conservation.getAcquisitionTraitement()) +"&csc_truk_acquisition_traitement_poison=" + URL.encodeComponent(conservation.getAcquisitionTraitementPoison()) +"&csc_truk_acquisition_traitement_insecte=" + URL.encodeComponent(conservation.getAcquisitionTraitementInsecte());}if (valorisation != null) {if (structureId != null) {postDonnees += "&csv_id_structure=" + URL.encodeComponent(structureId);}postDonnees += "&csv_mark_action=" + (valorisation.getAction() != null ? valorisation.getAction() : "") +"&csv_truk_action=" + URL.encodeComponent(valorisation.getActionInfo()) +"&csv_publication=" + URL.encodeComponent(valorisation.getPublication()) +"&csv_collection_autre=" + URL.encodeComponent(valorisation.getCollectionAutre()) +"&csv_mark_action_future=" + (valorisation.getActionFuture() != null ? valorisation.getActionFuture() : "") +"&csv_action_future=" + URL.encodeComponent(valorisation.getActionFutureInfo()) +"&csv_mark_recherche=" + (valorisation.getRecherche() != null ? valorisation.getRecherche() : "") +"&csv_truk_recherche_provenance=" + URL.encodeComponent(valorisation.getRechercheProvenance()) +"&csv_truk_recherche_type=" + URL.encodeComponent(valorisation.getRechercheType()) +"&csv_mark_acces_ss_motif=" + (valorisation.getAccesSansMotif() != null ? valorisation.getAccesSansMotif() : "") +"&csv_acces_ss_motif=" + URL.encodeComponent(valorisation.getAccesSansMotifInfo()) +"&csv_mark_visite_avec_motif=" + (valorisation.getVisiteAvecMotif() != null ? valorisation.getVisiteAvecMotif() : "") +"&csv_visite_avec_motif=" + URL.encodeComponent(valorisation.getVisiteAvecMotifInfo());}return postDonnees;}}