Rev 2602 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package org.tela_botanica.client.modeles.dao;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.tela_botanica.client.cel2;import org.tela_botanica.client.interfaces.Rafraichissable;import org.tela_botanica.client.modeles.objets.Configuration;import org.tela_botanica.client.modeles.objets.ListeChampsEtendus;import org.tela_botanica.client.modeles.objets.ListeGroupesChampsEtendus;import org.tela_botanica.client.modeles.objets.ListeReferentielPerso;import org.tela_botanica.client.observation.ObservationModele;import com.google.gwt.core.client.Callback;import com.google.gwt.http.client.Request;import org.tela_botanica.client.util.RequestBuilderWithCredentials;import org.tela_botanica.client.util.Util;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.JSONObject;import com.google.gwt.json.client.JSONParser;import com.google.gwt.json.client.JSONValue;import com.google.gwt.user.client.Window;public class ListeReferentielChampsEtendusDAO {public static ListeGroupesChampsEtendus cacheGroupes;public static HashMap<String, String> cacheClesLabels;public static HashMap<String, String> cacheDescription = new HashMap<String, String>();public static String prefixePageChampEtendu = "CE";public ListeReferentielChampsEtendusDAO(ObservationModele obs) {}public void obtenirListeNomsChampsEtendus(final Rafraichissable r, String recherche) {RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +"/NomsChampsEtendus/cle"+"?recherche="+URL.encode(recherche));try {rb.sendRequest(null, new RequestCallback() {@Overridepublic void onResponseReceived(final Request request,final Response response) {ListeChampsEtendus labelCles = new ListeChampsEtendus(response.getText());r.rafraichir(labelCles, true);}@Overridepublic void onError(Request request, Throwable exception) {// TODO Auto-generated method stub}});} catch (RequestException e) {e.printStackTrace();}}public void obtenirListeValeursChampEtendu(final Rafraichissable r, String cle, String recherche) {RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +"/NomsChampsEtendus/valeur"+"?cle="+URL.encode(cle)+"&recherche="+URL.encode(recherche));try {rb.sendRequest(null, new RequestCallback() {@Overridepublic void onResponseReceived(final Request request,final Response response) {ArrayList<String> valeurs = new ArrayList<String>();final JSONValue responseValue = JSONParser.parse(response.getText());JSONArray reponse = null;if ((reponse = responseValue.isArray()) != null) {for(int i = 0; i < reponse.size(); i++) {valeurs.add(reponse.get(i).isString().stringValue());}}r.rafraichir(valeurs, true);}@Overridepublic void onError(Request request, Throwable exception) {// TODO Auto-generated method stub}});} catch (RequestException e) {e.printStackTrace();}}public void obtenirGroupesChampsEtendus(final Rafraichissable r) {if(cacheGroupes == null) {RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +"/CatalogueChampsEtendus/groupes");try {rb.sendRequest(null, new RequestCallback() {@Overridepublic void onResponseReceived(final Request request,final Response response) {ListeGroupesChampsEtendus valeurs = new ListeGroupesChampsEtendus(response.getText());cacheGroupes = valeurs;r.rafraichir(valeurs, true);}@Overridepublic void onError(Request request, Throwable exception) {// TODO Auto-generated method stub}});} catch (RequestException e) {e.printStackTrace();}} else {r.rafraichir(cacheGroupes, true);}}public void obtenirCatalogueChampsEtendus(final Rafraichissable r) {if(cacheClesLabels == null) {RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +"/CatalogueChampsEtendus/champs");try {rb.sendRequest(null, new RequestCallback() {@Overridepublic void onResponseReceived(final Request request,final Response response) {try {JSONValue champsJson = JSONParser.parseStrict(response.getText());JSONArray champs = champsJson.isArray();cacheClesLabels = new HashMap<String, String>(champs.size());for (int i = 0; i < champs.size(); i++) {JSONObject champ = champs.get(i).isObject();String cle = champ.get("cle").isString().stringValue();String label = champ.get("label").isString().stringValue();cacheClesLabels.put(cle, label);r.rafraichir(cacheClesLabels, true);}} catch (Exception e) {// TODO: handle exception}}@Overridepublic void onError(Request request, Throwable exception) {// TODO Auto-generated method stub}});} catch (RequestException e) {e.printStackTrace();}} else {r.rafraichir(cacheClesLabels, true);}}public static void telechargerDescriptionWikini(final String champ, final Callback<String, String> callback) {String url = Configuration.getWikiDescriptionChampsEtendusRestUrl()+obtenirNomPageChampEtendu(champ)+"?";String options = Configuration.getWikiDescriptionChampsEtendusUrlOptions();if(cacheDescription.containsKey(champ)) {callback.onSuccess(cacheDescription.get(champ));}RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET,url + options);try {rb.sendRequest(null, new RequestCallback() {@Overridepublic void onResponseReceived(final Request request,final Response response) {try {final JSONValue responseValue = JSONParser.parse(response.getText());String description = "";JSONObject reponse = responseValue.isObject();if(reponse != null && reponse.get("texte") != null && reponse.get("texte").isString() != null) {description = reponse.get("texte").isString().stringValue();}cacheDescription.put(champ, description);callback.onSuccess(description);} catch(Exception exception) {this.onError(request, exception);}}@Overridepublic void onError(Request request, Throwable exception) {cacheDescription.put(champ, "");callback.onFailure("");}});} catch (RequestException e) {e.printStackTrace();}}// Pour obliger la page wiki a avoir été créée avant de rediriger, on utilise une requete synchroneprivate static native String effectuerRequeteSynchrone(String url, String contenu, String typeRequete)/*-{// tant pis pour les vieux navigateurs, ie6 etc...// tous ceux que l'on supporte connaissent XMLHttpRequestvar xhReq = new XMLHttpRequest();xhReq.open(typeRequete, url, false);if(typeRequete == "POST") xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xhReq.send(contenu);var serverResponse = xhReq.status + xhReq.responseText;return serverResponse;}-*/;public static void creerPageWikini(final String cle) {String url = Configuration.getWikiDescriptionChampsEtendusRestUrl()+obtenirNomPageChampEtendu(cle)+"?";String options = Configuration.getWikiDescriptionChampsEtendusUrlOptions();String pageTpl = Configuration.getWikiDescriptionChampsEtendusUrlOptionsPageTpl();effectuerRequeteSynchrone(url + options + pageTpl, "", "GET");}public static String obtenirUrlEditionDescriptionWiki(String cle) {String url = Configuration.getWikiDescriptionChampsEtendusPageUrl()+obtenirNomPageChampEtendu(cle)+"/edit";return url;}protected static String obtenirNomPageChampEtendu(String cle) {return prefixePageChampEtendu+Util.convertirEnChaMot(cle);}public static String getUrlRequeteValeursChampEtendu(String cle) {return Configuration.getServiceBaseUrl() +"/NomsChampsEtendus/valeur"+"?cle="+URL.encode(cle)+"&recherche=";}public static String getUrlRequeteNomsChampsEtendus() {return Configuration.getServiceBaseUrl()+"/NomsChampsEtendus/cle?recherche=";}public static Map<String, String> parserRetourListeValeursChampsEtendus(Response response) {Map<String, String> valeurs = new HashMap<String, String>();final JSONValue responseValue = JSONParser.parse(response.getText());JSONArray reponse = null;if ((reponse = responseValue.isArray()) != null) {for(int i = 0; i < reponse.size(); i++) {valeurs.put(reponse.get(i).isString().stringValue(), reponse.get(i).isString().stringValue());}}return valeurs;}public static Map<String, String> parserRetourListeNomsChampsEtendus(Response response) {final JSONValue responseValue = JSONParser.parse(response.getText());JSONObject reponse = null;cacheClesLabels = new HashMap<String, String>();if ((reponse = responseValue.isObject()) != null) {Iterator<String> it = reponse.keySet().iterator();while(it.hasNext()) {String cle = it.next();String valeur = reponse.get(cle).isString().stringValue();cacheClesLabels.put(cle, valeur);}}return cacheClesLabels;}}