JavaFX/model/TraductionModel.java

171 lines
5.0 KiB
Java
Raw Normal View History

2017-11-21 07:34:05 +00:00
package model;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONObject;
import Classes.ApiCall;
import Classes.Event;
import Interfaces.Callback;
import Interfaces.EventObserver;
import Interfaces.Observable;
import javafx.util.Pair;
2017-12-14 13:50:00 +00:00
public class TraductionModel implements Observable {
2017-11-21 07:34:05 +00:00
private HashMap<String,EventObserver> observers;
2017-12-14 13:50:00 +00:00
private static TraductionModel instance;
2017-11-21 07:34:05 +00:00
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> translations;
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> usages;
2017-12-14 13:50:00 +00:00
private TraductionModel() {
2017-11-21 07:34:05 +00:00
this.observers = new HashMap<String,EventObserver>();
}
2017-12-14 13:50:00 +00:00
public static TraductionModel getInstance() {
if(TraductionModel.instance == null) {
TraductionModel.instance = new TraductionModel();
2017-11-21 07:34:05 +00:00
}
2017-12-14 13:50:00 +00:00
return TraductionModel.instance;
2017-11-21 07:34:05 +00:00
}
public void query(String search) {
2017-12-13 15:55:34 +00:00
//si recherche vide
if(search.isEmpty()) {
this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess"));
return;
}
2017-11-21 07:34:05 +00:00
final String encodedQuery;
try {
encodedQuery = java.net.URLEncoder.encode(search,"UTF-8");
} catch (UnsupportedEncodingException e1) {
2017-12-13 15:55:34 +00:00
this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
2017-11-21 07:34:05 +00:00
return;
}
HashMap<String,String> headers = new HashMap<String,String>();
//si on ne met pas ce header l'API refuse de répondre
headers.put("Referer", "http://www.wordreference.com");
String dic = LangModel.getInstance().getFromLang().name()+LangModel.getInstance().getToLang().name();
ApiCall call = new ApiCall("http://api.wordreference.com/1/json/"+dic+"/"+encodedQuery,"GET",new Callback() {
@Override
public void onSuccess(JSONObject response) {
2017-12-13 15:55:34 +00:00
try {
//on reset le contenu du modele
2017-12-14 13:50:00 +00:00
TraductionModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
TraductionModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
2017-11-21 07:34:05 +00:00
2017-12-13 15:55:34 +00:00
if(response.has("Error")) {
//pas de résultat
2017-12-14 13:50:00 +00:00
TraductionModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
2017-12-13 15:55:34 +00:00
}else{
//Traitement des traductions principales
2017-12-14 13:50:00 +00:00
TraductionModel.this.translations.addAll(
TraductionModel.this.computeJSON(
2017-12-13 15:55:34 +00:00
response.getJSONObject("term0").getJSONObject("PrincipalTranslations")
)
);
//Traitement des traductions secondaires
if(response.getJSONObject("term0").has("AdditionalTranslations")) {
2017-12-14 13:50:00 +00:00
TraductionModel.this.translations.addAll(
TraductionModel.this.computeJSON(
2017-12-13 15:55:34 +00:00
response.getJSONObject("term0").getJSONObject("AdditionalTranslations")
)
);
}
//traitement des usages
2017-12-14 13:50:00 +00:00
TraductionModel.this.usages.addAll(
TraductionModel.this.computeJSON(
2017-12-13 15:55:34 +00:00
response.getJSONObject("original").getJSONObject("Compounds")
)
);
2017-12-14 13:50:00 +00:00
TraductionModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess"));
2017-12-13 15:55:34 +00:00
}
2017-11-21 07:34:05 +00:00
2017-12-13 15:55:34 +00:00
}catch(Exception e) {
e.printStackTrace();
2017-11-21 07:34:05 +00:00
}
}
@Override
public void onError() {
2017-12-14 13:50:00 +00:00
TraductionModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed"));
2017-11-21 07:34:05 +00:00
}
});
call.addHeaders(headers);
call.send();
}
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> computeJSON(JSONObject container){
ArrayList<Pair<WordTraductionModel,WordTraductionModel>> returned = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
for (Object key : container.keySet()) {
//get original term
WordTraductionModel term = new WordTraductionModel();
JSONObject jterm = container.getJSONObject((String)key).getJSONObject("OriginalTerm");
term.setWord(jterm.getString("term"))
.setPOS(jterm.getString("POS"))
.setSense(jterm.getString("sense"))
.setUsage(jterm.getString("term"));
//get traduction
WordTraductionModel trad = new WordTraductionModel();
jterm = container.getJSONObject((String)key).getJSONObject("FirstTranslation");
term.setWord(jterm.getString("term"))
.setPOS(jterm.getString("POS"))
.setSense(jterm.getString("sense"))
.setUsage(jterm.getString("term"));
returned.add(new Pair<WordTraductionModel,WordTraductionModel>(term,trad));
}
return returned;
}
public ArrayList<Pair<WordTraductionModel,WordTraductionModel>> getTranslations(){
return this.translations;
}
public ArrayList<Pair<WordTraductionModel,WordTraductionModel>> getUsages(){
return this.usages;
}
@Override
public void addObserver(String key, EventObserver o) {
this.observers.put(key, o);
}
@Override
public void removeObserver(String key) {
this.observers.remove(key);
}
@Override
public void notifyObservers(Event e) {
for(Object key : this.observers.keySet().toArray()) {
this.observers.get(key).handleEvent(e);
}
}
}