This commit is contained in:
xdrm-brackets 2017-12-12 08:23:54 +01:00
commit 832fb73ab8
5 changed files with 240 additions and 22 deletions

View File

@ -3,12 +3,15 @@
*/ */
package Interfaces; package Interfaces;
import Classes.Event;
/** /**
* @author lucas * @author lucas
* *
*/ */
public interface Observable { public interface Observable {
public void notifyObservers(Event e);
public void addObserver(String key , EventObserver o); public void addObserver(String key , EventObserver o);
public void removeObserver(String key); public void removeObserver(String key);

View File

@ -18,6 +18,10 @@ import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.FlowPane; import javafx.scene.layout.FlowPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import model.DictionaryModel;
import model.LangModel;
import model.NewsListModel; import model.NewsListModel;
import model.NewsModel; import model.NewsModel;
@ -142,39 +146,49 @@ public class RootLayout extends Application implements EventObserver {
case "NewsQuerySuccess": case "NewsQuerySuccess":
System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé"); System.out.println(NewsListModel.getInstance().getNews().size()+" News ont été trouvé");
if(NewsListModel.getInstance().getNews().size() != 0) {
// For each news System.out.println("Le titre du premier article est: "+NewsListModel.getInstance().getNews().get(0).getTitle());
for( NewsModel news : NewsListModel.getInstance().getNews() ){
try{
this.articles.addItem( news );
}catch(Exception e1){
System.out.println("Cannot fetch article data");
e1.printStackTrace();
}
}
if( NewsListModel.getInstance().getNews().size() != 0 )
System.out.println("La description du premier article est: "+NewsListModel.getInstance().getNews().get(0).getDescription()); 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();
}
}
}
break; break;
case "NewsQueryFailed": case "NewsQueryFailed":
System.out.println("une erreur est survenue"); System.out.println("une erreur est survenue");
break; 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) { public void handleMainLayoutChange(String layout) {
NewsListModel.getInstance().addObserver("MainClass", this); NewsListModel.getInstance().addObserver("MainClass", this);
NewsListModel.getInstance().setCategory(Category.business); NewsListModel.getInstance().setCategory(Category.gaming);
NewsListModel.getInstance().setSortType(SortTypes.publishedAt); NewsListModel.getInstance().setSortType(SortTypes.relevancy);
NewsListModel.getInstance().query("bitcoin"); NewsListModel.getInstance().query("the evil within");
DictionaryModel.getInstance().addObserver("MainClass", this);
DictionaryModel.getInstance().query("maison");
} }
} }

152
model/DictionaryModel.java Normal file
View File

@ -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<String,EventObserver> observers;
private static DictionaryModel instance;
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> translations;
private ArrayList<Pair<WordTraductionModel,WordTraductionModel>> usages;
private DictionaryModel() {
this.observers = new HashMap<String,EventObserver>();
}
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<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) {
//on reset le contenu du modele
DictionaryModel.this.translations = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
DictionaryModel.this.usages = new ArrayList<Pair<WordTraductionModel,WordTraductionModel>>();
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<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);
}
}
}

View File

@ -1,5 +1,6 @@
package model; package model;
import java.io.UnsupportedEncodingException;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -124,9 +125,17 @@ public class NewsListModel implements Observable{
this.apiError = false; this.apiError = false;
return; 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 //on créer l'URL de l'appel
String lang = LangModel.getInstance().getToLang().name(); 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 //on ajoute la liste des sources a l'URL de la requete
if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) { if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) {
@ -199,7 +208,7 @@ public class NewsListModel implements Observable{
String lang = LangModel.getInstance().getToLang().name(); String lang = LangModel.getInstance().getToLang().name();
String URL = "http://beta.newsapi.org/v2/everything?apiKey="+NewsListModel.this.APIKey+"&language="+lang 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 //on ajoute la liste des sources a l'URL de la requete
if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) { if(NewsListModel.this.sources != null && NewsListModel.this.sources.size() != 0) {

View File

@ -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;
}
}