blacklisted entities are now in the configuration file
[pnews.git] / war / src / main / java / pnews / servlet / Config.java
1 package pnews.servlet;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.Reader;
6 import java.io.UnsupportedEncodingException;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15
16 import javax.json.Json;
17 import javax.json.JsonArray;
18 import javax.json.JsonObject;
19 import javax.json.JsonString;
20 import javax.json.JsonValue;
21
22 import pnews.Category;
23 import pnews.Feed;
24 import pnews.Language;
25
26 public class Config {
27         private Feed[] feeds;
28         private Category[] categories;
29         private Language[] languages;
30         private final Set<String> blacklistedEntities = new HashSet<>();
31         private static final String CLASS_NAME = Config.class.getName();
32         
33         /**
34          * The key is the language, the value is the default category for this language.
35          */
36         private Map<String, Category> defaultCategories = new HashMap<>();
37         private static final Logger LOG = Logger.getLogger(CLASS_NAME);
38                 
39         private void loadCategories(JsonArray jcats) {
40                 List<Category> cats;
41                 JsonObject jcat;
42                 Category cat;
43                 String id, label, title, language;
44                 
45                 cats = new ArrayList<>(jcats.size());
46                 
47                 for (JsonValue v: jcats) {
48                         jcat = (JsonObject)v;
49                         id = jcat.getString("id");
50                         label = jcat.getString("label");
51                         title = jcat.getString("title");
52                         language = jcat.getString("language");
53                         cat = new Category(id, label, title, language);
54                         cats.add(cat);
55                         if (defaultCategories.get(language) == null)
56                                 defaultCategories.put(language, cat);
57                 }
58                 
59                 categories = cats.toArray(new Category[0]);
60         }
61
62         private void loadLanguages(JsonArray jlangs) {
63                 List<Language> langs;
64                 JsonObject jlang;
65                 String id;
66                 
67                 langs = new ArrayList<>(jlangs.size());
68                 
69                 for (JsonValue v: jlangs) {
70                         jlang = (JsonObject)v;
71                         id = jlang.getString("id");
72                         langs.add(new Language(id));
73                 }
74                 
75                 languages = langs.toArray(new Language[0]);
76         }
77         
78         private Category getCategory(String id) {
79                 for (Category c: categories)
80                         if (c.getId().equals(id))
81                                 return c;
82                 return null;
83         }
84         
85         private void loadEntities(JsonObject jroot) {
86                 JsonObject jentities;
87                 JsonArray jblacklist;
88                 final String METHOD_NAME = "loadEntities";
89                 
90                 jentities = jroot.getJsonObject("entities");
91                 jblacklist = jentities.getJsonArray("blacklist");
92                 
93                 jblacklist.forEach((jv)-> {
94                         JsonString js;
95                         
96                         js = (JsonString)jv;
97                         blacklistedEntities.add(js.getString());
98                 });
99                 
100                 LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " blacklistedEntities=" + blacklistedEntities);
101         }
102         
103         public void loadConfig() throws UnsupportedEncodingException {
104                 Reader r;
105                 JsonObject jfeeds, jroot;
106                 List<Feed> feedList;
107                 
108                 r = null;
109                 try {
110                         r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
111                                                   "UTF-8");
112                         jroot = Json.createReader(r).readObject();
113                 } finally {
114                         if (r != null)
115                                 try { r.close(); } catch (IOException e) { };
116                 }
117                 
118                 loadLanguages(jroot.getJsonArray("languages"));
119                 loadCategories(jroot.getJsonArray("categories"));
120                 
121                 jfeeds = jroot.getJsonObject("feeds");
122                 
123                 feedList = new ArrayList<Feed>(jfeeds.size());
124
125                 jfeeds.forEach((k, v)-> {
126                         JsonObject jf;
127                         String str;
128                         Category cat;
129                         JsonArray jcategories;
130                         
131                         jf = (JsonObject)v;
132                         jcategories = jf.getJsonArray("categories");
133                         str = jcategories.getString(0);
134                         
135                         cat = getCategory(str);
136                         
137                         if (cat != null)
138                                 feedList.add(new Feed(k, cat));
139                         else
140                                 LOG.severe("Missing category: " + str);
141                 });
142                 
143                 feeds = feedList.toArray(new Feed[0]);
144                 
145                 loadEntities(jroot);
146         }
147         
148         public boolean isBlacklistedEntity(String e) {
149                 final String METHOD_NAME = "isBlacklistedEntity";
150                 boolean result;
151                 
152                 LOG.entering(CLASS_NAME, METHOD_NAME, e);
153                 
154                 result = blacklistedEntities.contains(e);
155                 
156                 LOG.exiting(CLASS_NAME, METHOD_NAME, result);
157                 
158                 return result;
159         }
160         
161         public Feed[] getFeeds() {
162                 return feeds;
163         }
164         
165         public Map<Category, List<Feed>> getFeedsByCategory() {
166                 Map<Category, List<Feed>> result;
167                 Feed[] feeds;
168                 List<Feed> catFeeds;
169                 Category cat;
170                 
171                 result = new HashMap<>();
172                 
173                 feeds = getFeeds();
174                 for (Feed f: feeds) {
175                         cat = f.getCategory();
176                 
177                         catFeeds = result.get(cat);
178                         if (catFeeds == null) {
179                                 catFeeds = new ArrayList<Feed>();
180                                 result.put(cat, catFeeds);
181                         }
182                         catFeeds.add(f);
183                 }
184                 
185                 return result;
186         }
187
188         public Category[] getCategories() {
189                 return categories;
190         }
191         
192         public Category getDefaultCategory(Language lang) {
193                 return defaultCategories.get(lang.getId());
194         }
195         
196         public Language[] getLanguages() {
197                 return languages;
198         }
199         
200         public Language getDefaultLanguage() {
201                 return languages[0];
202         }
203         
204         public static void main(String[] args) throws UnsupportedEncodingException {
205                 Config cfg;
206                 Feed[] feeds;           
207                 Category[] cats;
208
209                 cfg = new Config();
210                 cfg.loadConfig();
211                 
212                 cats = cfg.getCategories();
213                 for (Category cat: cats)
214                         System.out.println(cat);
215                 
216                 feeds = cfg.getFeeds();
217                 
218                 System.out.println("Number of feeds: " + feeds.length);
219                 for (Feed f: feeds)
220                         System.out.println(f);
221         }
222 }