navigation with hot topics
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Thu, 2 Nov 2017 22:41:22 +0000 (23:41 +0100)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Thu, 2 Nov 2017 22:41:22 +0000 (23:41 +0100)
war/src/main/java/pnews/Article.java
war/src/main/java/pnews/servlet/ArticleProvider.java
war/src/main/java/pnews/servlet/HTML.java
war/src/main/java/pnews/servlet/JSON.java
war/src/main/java/pnews/servlet/Pnews.java
war/src/main/resources/feeds.json

index f38f792..97e8b76 100644 (file)
@@ -27,6 +27,14 @@ public class Article {
                 return entities;
         }
         
+        public boolean hasEntity(String entity) {
+                for (String e: entities)
+                        if (e.equals(entity))
+                                return true;
+                
+                return false;
+        }
+        
         public Date getPublicationDate() {
                 return publicationDate;
         }
index 4e85e83..c2d8f59 100644 (file)
@@ -181,16 +181,24 @@ public class ArticleProvider {
         /**
          * Returns a copy.
          */
-        public List<Article> getArticles(Category cat)
+        public List<Article> getArticles(Category cat, String entity)
                         throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
-                List<Article> articles;
+                List<Article> articles, result;                
                 
                 synchronized (articlesByCategory) {
                         articles = getArticlesForUpdate(cat);
                 }
                 
-                synchronized (articles) {
-                        return new ArrayList<>(articles);
+                synchronized (articles) {                       
+                        if (entity == null)
+                                return new ArrayList<>(articles);
+                        
+                        result = new ArrayList<>(articles.size());
+                        for (Article a: articles)
+                                if (a.hasEntity(entity))
+                                        result.add(a);
+                        
+                        return result;
                 }
         }
         
@@ -203,7 +211,7 @@ public class ArticleProvider {
                 
                 LOG.entering(CLASS_NAME, FUNCTION_NAME, cat);
                 
-                articles = getArticles(cat);
+                articles = getArticles(cat, null);
                 
                 entities = new HashMap<>();
                 for (Article a: articles) 
index 42564e8..72f0b4b 100644 (file)
@@ -105,11 +105,20 @@ public class HTML {
                buf.append("</nav>\n");
        }
        
-       public static String toHTML(List<Article> articles, Category catActive, Config cfg, ArticleProvider provider) {
+       private static String toURL(Category catActive, String entity) {
+               try {
+                       return catActive.getURL() + "?entity=" + URLEncoder.encode(entity, "UTF-8");
+               } catch (UnsupportedEncodingException e) {
+                       LOG.log(Level.SEVERE, "Failed to generate link to entity " + entity, e);
+                       return catActive.getURL();
+               }
+       }
+       
+       public static String toHTML(List<Article> articles, Category catActive, String entityActive, Config cfg, ArticleProvider provider) {
                StringBuffer buf;
                int i;
-               Category[] cats;
                List<EntityStat> entities;
+               String cl;
                
                buf = new StringBuffer();
                buf.append("<!DOCTYPE html>\n");
@@ -123,8 +132,6 @@ public class HTML {
                buf.append("</head>\n");
                buf.append("<body>\n");
                
-               cats = cfg.getCategories();
-               
                appendMenu(buf, catActive, cfg);
                
                try {
@@ -136,7 +143,11 @@ public class HTML {
                                i = 0;
                                for (EntityStat s: entities) {
                                        buf.append("<li>");
-                                       buf.append(s.getEntity());
+                                       if (entityActive != null && s.getEntity().equals(entityActive))
+                                               cl = "active";
+                                       else
+                                               cl = null;
+                                       appendA(buf, s.getEntity(), toURL(catActive, s.getEntity()), cl);
                                        buf.append("</li>\n");
                                        i++;
                                        if (i > 10)
index 13509ed..bf6dcae 100644 (file)
@@ -38,7 +38,7 @@ public class JSON {
                 
                 for (Category cat: config.getCategories())
                         try {
-                                articles = provider.getArticles(cat);
+                                articles = provider.getArticles(cat, null);
                                 jcategories.addProperty(cat.getLabel(),
                                                         articles.size());
                         } catch (IllegalArgumentException | FeedException | IOException e) {
index 777f7fd..69ad1bc 100644 (file)
@@ -105,20 +105,20 @@ public class Pnews extends HttpServlet {
         }
 
         
-        private void writeArticles(Category cat, HttpServletResponse rp) {
+        private void writeArticles(Category cat, String entity, HttpServletResponse rp) {
                 String html;
                 List<Article> articles;
 
                 try {
-                        articles = provider.getArticles(cat);
+                        articles = provider.getArticles(cat, entity);
                         if (articles != null) {
-                                html = HTML.toHTML(articles, cat, config, provider);
+                                html = HTML.toHTML(articles, cat, entity, config, provider);
                                 rp.setContentType("text/html;charset=utf-8");
                                 rp.getWriter().write(html);
                                 rp.setCharacterEncoding("utf-8");
                         } else {
                                 LOG.severe("writeArticles cannot retrieve any articles");
-                                html = HTML.toHTML(new ArrayList<Article>(), cat, config, provider);
+                                html = HTML.toHTML(new ArrayList<Article>(), cat, entity, config, provider);
                                 rp.setContentType("text/html");
                                 rp.getWriter().write(html);
                         }
@@ -186,7 +186,7 @@ public class Pnews extends HttpServlet {
                 
                         for (Category cat: config.getCategories()) {
                                 if (path.equals(cat.getURL())) {
-                                        writeArticles(cat, resp);
+                                        writeArticles(cat, getQueryParameter(req, "entity"), resp);
                                         return ;
                                 }
                         }
index aae327a..91e2a1e 100644 (file)
@@ -93,6 +93,9 @@
                 "http://www.futura-sciences.com/rss/actualites.xml": { "categories": ["technologie"] },
                 "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all": { "categories": ["technologie"] },
                 "https://korben.info/feed": { "categories": ["technologie"]},
+                "https://www.techhive.com/index.rss": { "categories": ["en_technologie"]},
+                "https://www.gnome.org/feed/": { "categories": ["en_technologie"]},
+                "http://www.markshuttleworth.com/feed": { "categories": ["en_technologie"]},
                 "https://insights.ubuntu.com/feed/": { "categories": ["en_technologie"]},
                 "https://www-03.ibm.com/press/us/en/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all": { "categories": ["en_technologie"]},
                 "https://www.cnet.com/rss/news/": { "categories": ["en_technologie"]},
                 "https://www.androidheadlines.com/feed": { "categories": ["en_technologie"]},
                 "https://www.nasa.gov/rss/dyn/breaking_news.rss": { "categories": ["en_technologie"]},
                 "http://www.computerweekly.com/rss/RSS-Feed.xml": { "categories": ["en_technologie"]},
-                "https://www.debian.org/News/news": { "categories": ["en_technologie"]}
+                "https://www.debian.org/News/news": { "categories": ["en_technologie"]},
+                "https://www.theverge.com/rss/index.xml": { "categories": ["en_technologie"]}
         }
 }