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