From c8c3409db2513b1071920d28a36f0b8cdaa5f693 Mon Sep 17 00:00:00 2001 From: SeekDaSky Date: Tue, 21 Nov 2017 08:34:05 +0100 Subject: [PATCH 1/2] =?UTF-8?q?impl=C3=A9mentation=20du=20dico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Interfaces/Observable.java | 3 + controller/RootLayout.java | 17 +++- model/DictionaryModel.java | 152 +++++++++++++++++++++++++++++++++ model/NewsListModel.java | 13 ++- model/WordTraductionModel.java | 40 +++++++++ 5 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 model/DictionaryModel.java create mode 100644 model/WordTraductionModel.java diff --git a/Interfaces/Observable.java b/Interfaces/Observable.java index a24d9de..f5e1052 100644 --- a/Interfaces/Observable.java +++ b/Interfaces/Observable.java @@ -3,12 +3,15 @@ */ package Interfaces; +import Classes.Event; + /** * @author lucas * */ public interface Observable { + public void notifyObservers(Event e); public void addObserver(String key , EventObserver o); public void removeObserver(String key); diff --git a/controller/RootLayout.java b/controller/RootLayout.java index b2d6ece..60a965d 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -24,6 +24,7 @@ import javafx.scene.control.MenuBar; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; +import model.DictionaryModel; import model.LangModel; import model.NewsListModel; @@ -139,6 +140,7 @@ public class RootLayout extends Application implements EventObserver { case "NewsQuerySuccess": System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé"); if(NewsListModel.getInstance().getNews().size() != 0) { + System.out.println("Le titre du premier article est: "+NewsListModel.getInstance().getNews().get(0).getTitle()); System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription()); } @@ -146,15 +148,24 @@ public class RootLayout extends Application implements EventObserver { case "NewsQueryFailed": System.out.println("une erreur est survenue"); break; + + case "WordQuerySuccess": + System.out.println(DictionaryModel.getInstance().getTranslations().size()+" Traductions ont été trouvés"); + System.out.println(DictionaryModel.getInstance().getUsages().size()+" Exemples ont été trouvés"); + break; } } public void handleMainLayoutChange(String layout) { + /* NewsListModel.getInstance().addObserver("MainClass", this); - NewsListModel.getInstance().setCategory(Category.business); - NewsListModel.getInstance().setSortType(SortTypes.publishedAt); - NewsListModel.getInstance().query("bitcoin"); + NewsListModel.getInstance().setCategory(Category.gaming); + NewsListModel.getInstance().setSortType(SortTypes.relevancy); + NewsListModel.getInstance().query("the evil within");*/ + DictionaryModel.getInstance().addObserver("MainClass", this); + DictionaryModel.getInstance().query("maison"); + } } diff --git a/model/DictionaryModel.java b/model/DictionaryModel.java new file mode 100644 index 0000000..8c7804e --- /dev/null +++ b/model/DictionaryModel.java @@ -0,0 +1,152 @@ +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) { + + final String encodedQuery; + try { + encodedQuery = java.net.URLEncoder.encode(search,"UTF-8"); + } catch (UnsupportedEncodingException e1) { + this.notifyObservers(new Event("NewsModel","NewsQueryFailed")); + 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) { + //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","WordQueryNoResults")); + }else{ + //Traitement des traductions principales + DictionaryModel.this.translations.addAll( + DictionaryModel.this.computeJSON( + response.getJSONObject("term0").getJSONObject("PrincipalTranslations") + ) + ); + //Traitement des traductions secondaires + 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")); + + } + + } + + @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); + } + } + + +} diff --git a/model/NewsListModel.java b/model/NewsListModel.java index 8fccc2e..e35711f 100644 --- a/model/NewsListModel.java +++ b/model/NewsListModel.java @@ -1,5 +1,6 @@ package model; +import java.io.UnsupportedEncodingException; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; @@ -124,9 +125,17 @@ public class NewsListModel implements Observable{ this.apiError = false; return; } + + final String encodedQuery; + try { + encodedQuery = java.net.URLEncoder.encode(q,"UTF-8"); + } catch (UnsupportedEncodingException e1) { + this.notifyObservers(new Event("NewsModel","NewsQueryFailed")); + return; + } //on créer l'URL de l'appel String lang = LangModel.getInstance().getToLang().name(); - String URL = "http://beta.newsapi.org/v2/everything?apiKey="+this.APIKey+"&language="+lang+"&q="+q+"&sortBy="+this.sortType.name(); + String URL = "http://beta.newsapi.org/v2/everything?apiKey="+this.APIKey+"&language="+lang+"&q="+encodedQuery+"&sortBy="+this.sortType.name(); //on ajoute la liste des sources a l'URL de la requete if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) { @@ -197,7 +206,7 @@ public class NewsListModel implements Observable{ String lang = LangModel.getInstance().getToLang().name(); String URL = "http://beta.newsapi.org/v2/everything?apiKey="+NewsListModel.this.APIKey+"&language="+lang - +"&q="+q+"&sortBy="+NewsListModel.this.sortType.name(); + +"&q="+encodedQuery+"&sortBy="+NewsListModel.this.sortType.name(); //on ajoute la liste des sources a l'URL de la requete if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) { diff --git a/model/WordTraductionModel.java b/model/WordTraductionModel.java new file mode 100644 index 0000000..d963898 --- /dev/null +++ b/model/WordTraductionModel.java @@ -0,0 +1,40 @@ +package model; + +public class WordTraductionModel { + + private String word; + private String POS; + private String sense; + private String usage; + + + public String getWord() { + return word; + } + public WordTraductionModel setWord(String word) { + this.word = word; + return this; + } + public String getPOS() { + return POS; + } + public WordTraductionModel setPOS(String pOS) { + POS = pOS; + return this; + } + public String getSense() { + return sense; + } + public WordTraductionModel setSense(String sense) { + this.sense = sense; + return this; + } + public String getUsage() { + return usage; + } + public WordTraductionModel setUsage(String usage) { + this.usage = usage; + return this; + } + +} From 5479c909c98fd211546924d03beac5562005edc2 Mon Sep 17 00:00:00 2001 From: SeekDaSky Date: Tue, 21 Nov 2017 08:38:24 +0100 Subject: [PATCH 2/2] fix merge --- controller/RootLayout.java | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/controller/RootLayout.java b/controller/RootLayout.java index d051c7e..8669df1 100644 --- a/controller/RootLayout.java +++ b/controller/RootLayout.java @@ -150,19 +150,21 @@ public class RootLayout extends Application implements EventObserver { System.out.println("Le titre du premier article est: "+NewsListModel.getInstance().getNews().get(0).getTitle()); System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription()); - // For each news - for( NewsModel news : NewsListModel.getInstance().getNews() ){ - - try{ - - this.articles.addItem( news ); - - }catch(Exception e1){ - - System.out.println("Cannot fetch article data"); - e1.printStackTrace(); + // For each news + for( NewsModel news : NewsListModel.getInstance().getNews() ){ + + try{ + + this.articles.addItem( news ); + }catch(Exception e1){ + + System.out.println("Cannot fetch article data"); + e1.printStackTrace(); + + } } + } break; @@ -179,11 +181,11 @@ public class RootLayout extends Application implements EventObserver { } public void handleMainLayoutChange(String layout) { - /* + NewsListModel.getInstance().addObserver("MainClass", this); NewsListModel.getInstance().setCategory(Category.gaming); NewsListModel.getInstance().setSortType(SortTypes.relevancy); - NewsListModel.getInstance().query("the evil within");*/ + NewsListModel.getInstance().query("the evil within"); DictionaryModel.getInstance().addObserver("MainClass", this); DictionaryModel.getInstance().query("maison");