code cleanup
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Mon, 16 Oct 2017 20:51:50 +0000 (22:51 +0200)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Mon, 16 Oct 2017 20:51:50 +0000 (22:51 +0200)
war/src/main/java/pnews/servlet/ArticleProvider.java

index 55e098a..b28bbf5 100644 (file)
@@ -98,12 +98,68 @@ public class ArticleProvider {
                 return result;
         }
         
+        private List<Article> getArticlesForUpdate(Category cat) {
+                List<Article> result;
+                
+                synchronized (articlesByCategory) {
+                        result = articlesByCategory.get(cat);
+                        if (result == null) {
+                                result = new ArrayList<>();
+                                articlesByCategory.put(cat, result);
+                        }
+                        return result;
+                }                
+        }
+        
+        private boolean exists(String articleLink, List<Article> articles) {
+                synchronized (articles) {
+                        for (Article a: articles)
+                                if (a.link.equals(articleLink))
+                                        return true;
+                }
+                return false;
+        }
+        
+        private Article toArticle(String link, Category cat, SyndEntry entry, SyndFeed feed) {
+                String desc, title, thumbnail;
+                Date date;
+
+                thumbnail = null;
+                for (SyndEnclosure e: entry.getEnclosures()) {
+                        if (e.getType().startsWith("image/"))
+                                thumbnail = e.getUrl();    
+                        break;
+                }
+                
+                if (thumbnail == null && feed.getImage() != null)
+                        thumbnail = feed.getImage().getUrl();
+                                                
+                title = entry.getTitle().trim();
+                
+                if (entry.getDescription() != null) {                                      
+                        desc = Jsoup.parse(entry.getDescription().getValue()).text();
+                } else {       
+                        desc = null;
+                        LOG.severe("No description for " + feed.getTitle() + " - " + title);
+                }
+                
+                date = entry.getPublishedDate();
+                if (date == null)
+                        date = entry.getUpdatedDate();
+                if (date == null)
+                        LOG.severe("The article " + feed.getTitle() + " - " + title + " does not have a date");
+                                     
+                return new Article(link, cat, title, desc, thumbnail, date, title);
+        }
+        
+        
+        
         private void addArticles(Category cat, SyndFeed feed) {
                 String thumbnail;
                 String desc, link, title, feedTitle, feedImage;
                 Date date;
                 List<Article> articles;
-                boolean exist;
+                Article a;
                 
                 feedTitle = feed.getTitle().trim();
                 
@@ -115,64 +171,31 @@ public class ArticleProvider {
                 LOG.info("addArticles " + cat.getId() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
                 
                 for (SyndEntry entry: feed.getEntries()) {
-                        thumbnail = null;
-                        for (SyndEnclosure e: entry.getEnclosures()) {
-                                if (e.getType().startsWith("image/"))
-                                        thumbnail = e.getUrl();    
-                                break;
+                        link = entry.getLink().trim();
+                        articles = getArticlesForUpdate(cat);
+                        if (exists(link, articles)) {
+                                LOG.fine("addArticles " + link + " is already present");
+                                continue ;
                         }
-                        if (thumbnail == null)
-                                thumbnail = feedImage;
-                                                        
-                        title = entry.getTitle().trim();
                         
-                        if (entry.getDescription() != null) {                                      
-                                desc = Jsoup.parse(entry.getDescription().getValue()).text();
-                        } else {       
-                                desc = null;
-                                LOG.severe("No description for " + feedTitle + " - " + title);
-                        }
-                        
-                        date = entry.getPublishedDate();
-                        if (date == null)
-                                date = entry.getUpdatedDate();
-                        if (date == null) {
-                                LOG.severe("The article " + feedTitle + " - " + title + " does not have a date");
-                                continue;
-                        }                                
+                        a = toArticle(link, cat, entry, feed);
                         
-                        synchronized(articlesByCategory) {
-                                link = entry.getLink().trim();
-
-                                articles = articlesByCategory.get(cat);
-                                exist = false;
-                                if (articles == null) {
-                                        articles = new ArrayList<>();
-                                        articlesByCategory.put(cat, articles);
-                                } else {                                
-                                        for (Article a: articles)
-                                                if (a.link.equals(link)) {
-                                                        LOG.fine("addArticles " + link + " is already present");
-                                                        exist = true;
-                                                }
-                                }
-                                
-                                if (!exist) {
-                                        LOG.fine("add " + cat.getId() + " " + feedTitle + " " + title);
-                                
-                                        articles.add(new Article(link, cat, title, desc, thumbnail, date,
-                                                        feed.getTitle()));
+                        synchronized (articles) {
+                                articles.add(a);
 
-                                        Collections.sort(articles, new Comparator<Article>() {
-                                                @Override
-                                                public int compare(Article o1, Article o2) {
-                                                        return o2.publicationDate.compareTo(o1.publicationDate);
-                                                }
+                                Collections.sort(articles, new Comparator<Article>() {
+                                        @Override
+                                        public int compare(Article o1, Article o2) {
+                                                if (o1.publicationDate == o2.publicationDate)
+                                                        return 0;
+                                                if (o1.publicationDate == null)
+                                                        return 1;
+                                                if (o2.publicationDate == null)
+                                                        return -1;
+                                                return o2.publicationDate.compareTo(o1.publicationDate);
+                                        }
 
-                                        });
-                                } else {
-                                        LOG.finest("addArticles already exist: " + title);
-                                }
+                                });
                         }
                 }          
                 
@@ -202,8 +225,14 @@ public class ArticleProvider {
          */
         public List<Article> getArticles(Category cat)
                         throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+                List<Article> articles;
+                
                 synchronized (articlesByCategory) {
-                        return new ArrayList<>(articlesByCategory.get(cat));
+                        articles = getArticlesForUpdate(cat);
+                }
+                
+                synchronized (articles) {
+                        return new ArrayList<>(articles);
                 }
         }