fixed stats of articles per category
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Sun, 29 Oct 2017 20:39:40 +0000 (21:39 +0100)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Sun, 29 Oct 2017 20:39:40 +0000 (21:39 +0100)
war/src/main/java/pnews/servlet/JSON.java
war/src/main/java/pnews/servlet/Pnews.java

index a36367e..13509ed 100644 (file)
@@ -1,10 +1,13 @@
 package pnews.servlet;
 
+import java.io.IOException;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
+import com.rometools.rome.io.FeedException;
 
 import pnews.Article;
 import pnews.Category;
@@ -12,10 +15,11 @@ import pnews.Category;
 public class JSON {
         private static final Logger LOG = Logger.getLogger(JSON.class.getName());
         
-        public static String getStats(Category[] cats) {
+        public static String getStats(ArticleProvider provider, Config config) {
                 JsonObject jstats, jreadcounts, jcategories, jmemory;
                 Runtime runtime;
-                Article[] articles;
+                List<Article> articles;
+                Article[] allArticles;
                 
                 jstats = new JsonObject();
                                 
@@ -24,19 +28,20 @@ public class JSON {
                 jreadcounts = new JsonObject();
                 jstats.add("read-counts", jreadcounts);
                 
-                articles = ArticleStore.singleton.getArticles();
-                for (Article a: articles)
+                allArticles = ArticleStore.singleton.getArticles();
+                for (Article a: allArticles)
                         if (a.readCount.get() > 0)
                                 jreadcounts.addProperty(a.link, a.readCount);         
                 
                 jcategories = new JsonObject();
                 jstats.add("categories", jcategories);
                 
-                for (Category cat: cats)
+                for (Category cat: config.getCategories())
                         try {
+                                articles = provider.getArticles(cat);
                                 jcategories.addProperty(cat.getLabel(),
-                                                        articles.length);
-                        } catch (IllegalArgumentException e) {
+                                                        articles.size());
+                        } catch (IllegalArgumentException | FeedException | IOException e) {
                                 LOG.log(Level.SEVERE, "Fail to retrieve articles", e);
                         }
                 
index fc08d13..57ea4be 100644 (file)
@@ -100,7 +100,7 @@ public class Pnews extends HttpServlet {
                 rp.setContentType("application/json;charset=utf-8");
                 rp.setCharacterEncoding("utf-8");
 
-                rp.getWriter().write(JSON.getStats(config.getCategories()));
+                rp.getWriter().write(JSON.getStats(provider, config));
         }