blacklisted entities are now in the configuration file
[pnews.git] / war / src / main / java / pnews / servlet / Config.java
index a77eb88..fec3770 100644 (file)
@@ -3,41 +3,124 @@ package pnews.servlet;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
+import javax.json.JsonString;
+import javax.json.JsonValue;
 
 import pnews.Category;
 import pnews.Feed;
+import pnews.Language;
 
 public class Config {
         private Feed[] feeds;
         private Category[] categories;
+        private Language[] languages;
+        private final Set<String> blacklistedEntities = new HashSet<>();
+        private static final String CLASS_NAME = Config.class.getName();
+        
+        /**
+         * The key is the language, the value is the default category for this language.
+         */
+        private Map<String, Category> defaultCategories = new HashMap<>();
+        private static final Logger LOG = Logger.getLogger(CLASS_NAME);
+                
+        private void loadCategories(JsonArray jcats) {
+                List<Category> cats;
+                JsonObject jcat;
+                Category cat;
+                String id, label, title, language;
+                
+                cats = new ArrayList<>(jcats.size());
+                
+                for (JsonValue v: jcats) {
+                        jcat = (JsonObject)v;
+                        id = jcat.getString("id");
+                        label = jcat.getString("label");
+                        title = jcat.getString("title");
+                        language = jcat.getString("language");
+                        cat = new Category(id, label, title, language);
+                        cats.add(cat);
+                        if (defaultCategories.get(language) == null)
+                                defaultCategories.put(language, cat);
+                }
+                
+                categories = cats.toArray(new Category[0]);
+        }
+
+        private void loadLanguages(JsonArray jlangs) {
+                List<Language> langs;
+                JsonObject jlang;
+                String id;
+                
+                langs = new ArrayList<>(jlangs.size());
                 
-        public void loadConfig() {
+                for (JsonValue v: jlangs) {
+                        jlang = (JsonObject)v;
+                        id = jlang.getString("id");
+                        langs.add(new Language(id));
+                }
+                
+                languages = langs.toArray(new Language[0]);
+        }
+        
+        private Category getCategory(String id) {
+                for (Category c: categories)
+                        if (c.getId().equals(id))
+                                return c;
+                return null;
+        }
+        
+        private void loadEntities(JsonObject jroot) {
+                JsonObject jentities;
+                JsonArray jblacklist;
+                final String METHOD_NAME = "loadEntities";
+                
+                jentities = jroot.getJsonObject("entities");
+                jblacklist = jentities.getJsonArray("blacklist");
+                
+                jblacklist.forEach((jv)-> {
+                        JsonString js;
+                        
+                        js = (JsonString)jv;
+                        blacklistedEntities.add(js.getString());
+                });
+                
+                LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " blacklistedEntities=" + blacklistedEntities);
+        }
+        
+        public void loadConfig() throws UnsupportedEncodingException {
                 Reader r;
                 JsonObject jfeeds, jroot;
                 List<Feed> feedList;
-                Map<String, Category> cats;
                 
                 r = null;
                 try {
-                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"));
+                        r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
+                                                  "UTF-8");
                         jroot = Json.createReader(r).readObject();
                 } finally {
                         if (r != null)
                                 try { r.close(); } catch (IOException e) { };
                 }
                 
+                loadLanguages(jroot.getJsonArray("languages"));
+                loadCategories(jroot.getJsonArray("categories"));
+                
                 jfeeds = jroot.getJsonObject("feeds");
                 
                 feedList = new ArrayList<Feed>(jfeeds.size());
-                cats = new HashMap<>();
 
                 jfeeds.forEach((k, v)-> {
                         JsonObject jf;
@@ -49,17 +132,30 @@ public class Config {
                         jcategories = jf.getJsonArray("categories");
                         str = jcategories.getString(0);
                         
-                        cat = cats.get(str);
-                        if (cat == null) {
-                                cat = new Category(str);
-                                cats.put(str, cat);
-                        }
+                        cat = getCategory(str);
                         
-                        feedList.add(new Feed(k, cat));                                           
+                        if (cat != null)
+                                feedList.add(new Feed(k, cat));
+                        else
+                                LOG.severe("Missing category: " + str);
                 });
                 
                 feeds = feedList.toArray(new Feed[0]);
-                categories = cats.values().toArray(new Category[0]);
+                
+                loadEntities(jroot);
+        }
+        
+        public boolean isBlacklistedEntity(String e) {
+                final String METHOD_NAME = "isBlacklistedEntity";
+                boolean result;
+                
+                LOG.entering(CLASS_NAME, METHOD_NAME, e);
+                
+                result = blacklistedEntities.contains(e);
+                
+                LOG.exiting(CLASS_NAME, METHOD_NAME, result);
+                
+                return result;
         }
         
         public Feed[] getFeeds() {
@@ -93,11 +189,19 @@ public class Config {
                 return categories;
         }
         
-        public Category getDefaultCategory() {
-                return categories[0];
+        public Category getDefaultCategory(Language lang) {
+                return defaultCategories.get(lang.getId());
+        }
+        
+        public Language[] getLanguages() {
+                return languages;
+        }
+        
+        public Language getDefaultLanguage() {
+                return languages[0];
         }
         
-        public static void main(String[] args) {
+        public static void main(String[] args) throws UnsupportedEncodingException {
                 Config cfg;
                 Feed[] feeds;           
                 Category[] cats;