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; public class DictionaryModel implements Observable { private HashMap observers; private static DictionaryModel instance; private ArrayList> translations; private ArrayList> usages; private DictionaryModel() { this.observers = new HashMap(); } public static DictionaryModel getInstance() { if(DictionaryModel.instance == null) { DictionaryModel.instance = new DictionaryModel(); } return DictionaryModel.instance; } public void query(String search) { //si recherche vide if(search.isEmpty()) { this.translations = new ArrayList>(); this.usages = new ArrayList>(); this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); return; } final String encodedQuery; try { encodedQuery = java.net.URLEncoder.encode(search,"UTF-8"); } catch (UnsupportedEncodingException e1) { this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); return; } HashMap headers = new HashMap(); //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) { try { //on reset le contenu du modele DictionaryModel.this.translations = new ArrayList>(); DictionaryModel.this.usages = new ArrayList>(); if(response.has("Error")) { //pas de résultat DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); }else{ //Traitement des traductions principales DictionaryModel.this.translations.addAll( DictionaryModel.this.computeJSON( response.getJSONObject("term0").getJSONObject("PrincipalTranslations") ) ); //Traitement des traductions secondaires if(response.getJSONObject("term0").has("AdditionalTranslations")) { DictionaryModel.this.translations.addAll( DictionaryModel.this.computeJSON( response.getJSONObject("term0").getJSONObject("AdditionalTranslations") ) ); } //traitement des usages DictionaryModel.this.usages.addAll( DictionaryModel.this.computeJSON( response.getJSONObject("original").getJSONObject("Compounds") ) ); DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQuerySuccess")); } }catch(Exception e) { e.printStackTrace(); } } @Override public void onError() { DictionaryModel.this.notifyObservers(new Event("DictionaryModel","WordQueryFailed")); } }); call.addHeaders(headers); call.send(); } private ArrayList> computeJSON(JSONObject container){ ArrayList> returned = new ArrayList>(); 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(term,trad)); } return returned; } public ArrayList> getTranslations(){ return this.translations; } public ArrayList> 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); } } }