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