1941 |
aurelien |
1 |
package org.tela_botanica.client.modeles.dao;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.Iterator;
|
|
|
6 |
|
2392 |
aurelien |
7 |
import org.tela_botanica.client.cel2;
|
1941 |
aurelien |
8 |
import org.tela_botanica.client.interfaces.Rafraichissable;
|
|
|
9 |
import org.tela_botanica.client.modeles.objets.Configuration;
|
2033 |
aurelien |
10 |
import org.tela_botanica.client.modeles.objets.ListeChampsEtendus;
|
|
|
11 |
import org.tela_botanica.client.modeles.objets.ListeGroupesChampsEtendus;
|
1941 |
aurelien |
12 |
import org.tela_botanica.client.modeles.objets.ListeReferentielPerso;
|
|
|
13 |
import org.tela_botanica.client.observation.ObservationModele;
|
|
|
14 |
|
2392 |
aurelien |
15 |
import com.google.gwt.core.client.Callback;
|
1941 |
aurelien |
16 |
import com.google.gwt.http.client.Request;
|
|
|
17 |
import org.tela_botanica.client.util.RequestBuilderWithCredentials;
|
2401 |
aurelien |
18 |
import org.tela_botanica.client.util.Util;
|
|
|
19 |
|
1941 |
aurelien |
20 |
import com.google.gwt.http.client.RequestCallback;
|
|
|
21 |
import com.google.gwt.http.client.RequestException;
|
|
|
22 |
import com.google.gwt.http.client.Response;
|
|
|
23 |
import com.google.gwt.http.client.URL;
|
|
|
24 |
import com.google.gwt.json.client.JSONArray;
|
|
|
25 |
import com.google.gwt.json.client.JSONObject;
|
|
|
26 |
import com.google.gwt.json.client.JSONParser;
|
|
|
27 |
import com.google.gwt.json.client.JSONValue;
|
2392 |
aurelien |
28 |
import com.google.gwt.user.client.Window;
|
1941 |
aurelien |
29 |
|
|
|
30 |
public class ListeReferentielChampsEtendusDAO {
|
|
|
31 |
|
|
|
32 |
|
2392 |
aurelien |
33 |
public static ListeGroupesChampsEtendus cacheGroupes;
|
|
|
34 |
public static HashMap<String, String> cacheClesLabels;
|
|
|
35 |
public static HashMap<String, String> cacheDescription = new HashMap<String, String>();
|
|
|
36 |
|
|
|
37 |
public static String prefixePageChampEtendu = "CE";
|
|
|
38 |
|
1941 |
aurelien |
39 |
public ListeReferentielChampsEtendusDAO(ObservationModele obs) {
|
|
|
40 |
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
public void obtenirListeNomsChampsEtendus(final Rafraichissable r, String recherche) {
|
|
|
44 |
|
|
|
45 |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +
|
|
|
46 |
"/NomsChampsEtendus/cle"+
|
2392 |
aurelien |
47 |
"?recherche="+URL.encode(recherche));
|
1941 |
aurelien |
48 |
try {
|
|
|
49 |
|
|
|
50 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
51 |
@Override
|
|
|
52 |
public void onResponseReceived(final Request request,
|
|
|
53 |
final Response response) {
|
|
|
54 |
|
2033 |
aurelien |
55 |
ListeChampsEtendus labelCles = new ListeChampsEtendus(response.getText());
|
1941 |
aurelien |
56 |
r.rafraichir(labelCles, true);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
@Override
|
|
|
60 |
public void onError(Request request, Throwable exception) {
|
2392 |
aurelien |
61 |
// TODO Auto-generated method stub
|
|
|
62 |
}
|
1941 |
aurelien |
63 |
});
|
|
|
64 |
|
|
|
65 |
} catch (RequestException e) {
|
|
|
66 |
|
|
|
67 |
e.printStackTrace();
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public void obtenirListeValeursChampEtendu(final Rafraichissable r, String cle, String recherche) {
|
|
|
72 |
|
|
|
73 |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +
|
|
|
74 |
"/NomsChampsEtendus/valeur"+
|
|
|
75 |
"?cle="+URL.encode(cle)+"&recherche="+URL.encode(recherche));
|
|
|
76 |
|
|
|
77 |
try {
|
|
|
78 |
|
|
|
79 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
80 |
@Override
|
|
|
81 |
public void onResponseReceived(final Request request,
|
|
|
82 |
final Response response) {
|
|
|
83 |
|
|
|
84 |
|
|
|
85 |
ArrayList<String> valeurs = new ArrayList<String>();
|
|
|
86 |
final JSONValue responseValue = JSONParser.parse(response.getText());
|
|
|
87 |
|
|
|
88 |
JSONArray reponse = null;
|
|
|
89 |
if ((reponse = responseValue.isArray()) != null) {
|
|
|
90 |
for(int i = 0; i < reponse.size(); i++) {
|
|
|
91 |
valeurs.add(reponse.get(i).isString().stringValue());
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
r.rafraichir(valeurs, true);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
@Override
|
|
|
99 |
public void onError(Request request, Throwable exception) {
|
|
|
100 |
// TODO Auto-generated method stub
|
2392 |
aurelien |
101 |
}
|
1941 |
aurelien |
102 |
});
|
|
|
103 |
|
|
|
104 |
} catch (RequestException e) {
|
|
|
105 |
|
|
|
106 |
e.printStackTrace();
|
|
|
107 |
}
|
|
|
108 |
}
|
2033 |
aurelien |
109 |
|
|
|
110 |
public void obtenirGroupesChampsEtendus(final Rafraichissable r) {
|
2392 |
aurelien |
111 |
|
|
|
112 |
if(cacheGroupes == null) {
|
|
|
113 |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +
|
|
|
114 |
"/CatalogueChampsEtendus/groupes");
|
|
|
115 |
try {
|
|
|
116 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
117 |
@Override
|
|
|
118 |
public void onResponseReceived(final Request request,
|
|
|
119 |
final Response response) {
|
|
|
120 |
ListeGroupesChampsEtendus valeurs = new ListeGroupesChampsEtendus(response.getText());
|
|
|
121 |
cacheGroupes = valeurs;
|
|
|
122 |
r.rafraichir(valeurs, true);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
@Override
|
|
|
126 |
public void onError(Request request, Throwable exception) {
|
|
|
127 |
// TODO Auto-generated method stub
|
|
|
128 |
}
|
|
|
129 |
});
|
|
|
130 |
|
|
|
131 |
} catch (RequestException e) {
|
|
|
132 |
e.printStackTrace();
|
2033 |
aurelien |
133 |
}
|
2392 |
aurelien |
134 |
} else {
|
|
|
135 |
r.rafraichir(cacheGroupes, true);
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
public void obtenirCatalogueChampsEtendus(final Rafraichissable r) {
|
|
|
140 |
|
|
|
141 |
if(cacheClesLabels == null) {
|
|
|
142 |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET, Configuration.getServiceBaseUrl() +
|
|
|
143 |
"/CatalogueChampsEtendus/champs");
|
|
|
144 |
try {
|
|
|
145 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
146 |
@Override
|
|
|
147 |
public void onResponseReceived(final Request request,
|
|
|
148 |
final Response response) {
|
|
|
149 |
try {
|
|
|
150 |
JSONValue champsJson = JSONParser.parseStrict(response.getText());
|
|
|
151 |
JSONArray champs = champsJson.isArray();
|
|
|
152 |
|
|
|
153 |
cacheClesLabels = new HashMap<String, String>(champs.size());
|
|
|
154 |
|
|
|
155 |
for (int i = 0; i < champs.size(); i++) {
|
|
|
156 |
JSONObject champ = champs.get(i).isObject();
|
|
|
157 |
String cle = champ.get("cle").isString().stringValue();
|
|
|
158 |
String label = champ.get("label").isString().stringValue();
|
|
|
159 |
cacheClesLabels.put(cle, label);
|
|
|
160 |
|
|
|
161 |
r.rafraichir(cacheClesLabels, true);
|
|
|
162 |
}
|
|
|
163 |
} catch (Exception e) {
|
|
|
164 |
// TODO: handle exception
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
@Override
|
|
|
169 |
public void onError(Request request, Throwable exception) {
|
|
|
170 |
// TODO Auto-generated method stub
|
|
|
171 |
}
|
|
|
172 |
});
|
2033 |
aurelien |
173 |
|
2392 |
aurelien |
174 |
} catch (RequestException e) {
|
|
|
175 |
e.printStackTrace();
|
|
|
176 |
}
|
|
|
177 |
} else {
|
|
|
178 |
r.rafraichir(cacheClesLabels, true);
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
public static void telechargerDescriptionWikini(final String champ, final Callback<String, String> callback) {
|
2401 |
aurelien |
183 |
String url = Configuration.getWikiDescriptionChampsEtendusRestUrl()+obtenirNomPageChampEtendu(champ)+"?";
|
2392 |
aurelien |
184 |
String options = Configuration.getWikiDescriptionChampsEtendusUrlOptions();
|
|
|
185 |
|
|
|
186 |
if(cacheDescription.containsKey(champ)) {
|
|
|
187 |
callback.onSuccess(cacheDescription.get(champ));
|
|
|
188 |
}
|
2033 |
aurelien |
189 |
|
2392 |
aurelien |
190 |
RequestBuilderWithCredentials rb = new RequestBuilderWithCredentials(RequestBuilderWithCredentials.GET,
|
|
|
191 |
url + options);
|
|
|
192 |
|
|
|
193 |
try {
|
|
|
194 |
rb.sendRequest(null, new RequestCallback() {
|
|
|
195 |
|
|
|
196 |
@Override
|
|
|
197 |
public void onResponseReceived(final Request request,
|
|
|
198 |
final Response response) {
|
2437 |
mathias |
199 |
try {
|
|
|
200 |
|
|
|
201 |
final JSONValue responseValue = JSONParser.parse(response.getText());
|
|
|
202 |
|
|
|
203 |
String description = "";
|
|
|
204 |
|
|
|
205 |
JSONObject reponse = responseValue.isObject();
|
|
|
206 |
if(reponse != null && reponse.get("texte") != null && reponse.get("texte").isString() != null) {
|
|
|
207 |
description = reponse.get("texte").isString().stringValue();
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
cacheDescription.put(champ, description);
|
|
|
211 |
callback.onSuccess(description);
|
|
|
212 |
} catch(Exception exception) {
|
|
|
213 |
this.onError(request, exception);
|
2392 |
aurelien |
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
@Override
|
|
|
218 |
public void onError(Request request, Throwable exception) {
|
|
|
219 |
cacheDescription.put(champ, "");
|
|
|
220 |
callback.onFailure("");
|
2437 |
mathias |
221 |
}
|
2392 |
aurelien |
222 |
});
|
|
|
223 |
|
|
|
224 |
} catch (RequestException e) {
|
2033 |
aurelien |
225 |
e.printStackTrace();
|
|
|
226 |
}
|
|
|
227 |
}
|
2392 |
aurelien |
228 |
|
|
|
229 |
// Pour obliger la page wiki a avoir été créée avant de rediriger, on utilise une requete synchrone
|
|
|
230 |
private static native String effectuerRequeteSynchrone(String url, String contenu, String typeRequete)/*-{
|
|
|
231 |
// tant pis pour les vieux navigateurs, ie6 etc...
|
|
|
232 |
// tous ceux que l'on supporte connaissent XMLHttpRequest
|
|
|
233 |
var xhReq = new XMLHttpRequest();
|
|
|
234 |
xhReq.open(typeRequete, url, false);
|
|
|
235 |
|
|
|
236 |
if(typeRequete == "POST") xhReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
|
|
|
237 |
xhReq.send(contenu);
|
|
|
238 |
|
|
|
239 |
var serverResponse = xhReq.status + xhReq.responseText;
|
|
|
240 |
return serverResponse;
|
|
|
241 |
}-*/;
|
|
|
242 |
|
2401 |
aurelien |
243 |
public static void creerPageWikini(final String cle) {
|
|
|
244 |
String url = Configuration.getWikiDescriptionChampsEtendusRestUrl()+obtenirNomPageChampEtendu(cle)+"?";
|
2392 |
aurelien |
245 |
String options = Configuration.getWikiDescriptionChampsEtendusUrlOptions();
|
|
|
246 |
String pageTpl = Configuration.getWikiDescriptionChampsEtendusUrlOptionsPageTpl();
|
|
|
247 |
effectuerRequeteSynchrone(url + options + pageTpl, "", "GET");
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
public static String obtenirUrlEditionDescriptionWiki(String cle) {
|
2401 |
aurelien |
251 |
String url = Configuration.getWikiDescriptionChampsEtendusPageUrl()+obtenirNomPageChampEtendu(cle)+"/edit";
|
2392 |
aurelien |
252 |
return url;
|
|
|
253 |
}
|
2401 |
aurelien |
254 |
|
|
|
255 |
protected static String obtenirNomPageChampEtendu(String cle) {
|
|
|
256 |
return prefixePageChampEtendu+Util.convertirEnChaMot(cle);
|
|
|
257 |
}
|
1941 |
aurelien |
258 |
}
|