read categories information from the configuration
[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.List;
10 import java.util.Map;
11 import java.util.logging.Logger;
12
13 import javax.json.Json;
14 import javax.json.JsonArray;
15 import javax.json.JsonObject;
16 import javax.json.JsonValue;
17
18 import pnews.Category;
19 import pnews.Feed;
20
21 public class Config {
22         private Feed[] feeds;
23         private Category[] categories;
24         private static final Logger LOG = Logger.getLogger(Config.class.getName());
25                 
26         private void loadCategories(JsonArray jcats) {
27                 List<Category> cats;
28                 JsonObject jcat;
29                 String id, label, title, language;
30                 
31                 cats = new ArrayList<>(jcats.size());
32                 
33                 for (JsonValue v: jcats) {
34                         jcat = (JsonObject)v;
35                         id = jcat.getString("id");
36                         label = jcat.getString("label");
37                         title = jcat.getString("title");
38                         language = jcat.getString("language");
39                         cats.add(new Category(id, label, title, language));
40                 }
41                 
42                 categories = cats.toArray(new Category[0]);
43         }
44         
45         private Category getCategory(String id) {
46                 for (Category c: categories)
47                         if (c.getId().equals(id))
48                                 return c;
49                 return null;
50         }
51         
52         public void loadConfig() throws UnsupportedEncodingException {
53                 Reader r;
54                 JsonObject jfeeds, jroot;
55                 List<Feed> feedList;
56                 
57                 r = null;
58                 try {
59                         r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"),
60                                                   "UTF-8");
61                         jroot = Json.createReader(r).readObject();
62                 } finally {
63                         if (r != null)
64                                 try { r.close(); } catch (IOException e) { };
65                 }
66                 
67                 
68                 loadCategories(jroot.getJsonArray("categories"));
69                 
70                 jfeeds = jroot.getJsonObject("feeds");
71                 
72                 feedList = new ArrayList<Feed>(jfeeds.size());
73
74                 jfeeds.forEach((k, v)-> {
75                         JsonObject jf;
76                         String str;
77                         Category cat;
78                         JsonArray jcategories;
79                         
80                         jf = (JsonObject)v;
81                         jcategories = jf.getJsonArray("categories");
82                         str = jcategories.getString(0);
83                         
84                         cat = getCategory(str);
85                         
86                         if (cat != null)
87                                 feedList.add(new Feed(k, cat));
88                         else
89                                 LOG.severe("Missing category: " + str);
90                 });
91                 
92                 feeds = feedList.toArray(new Feed[0]);
93         }
94         
95         public Feed[] getFeeds() {
96                 return feeds;
97         }
98         
99         public Map<Category, List<Feed>> getFeedsByCategory() {
100                 Map<Category, List<Feed>> result;
101                 Feed[] feeds;
102                 List<Feed> catFeeds;
103                 Category cat;
104                 
105                 result = new HashMap<>();
106                 
107                 feeds = getFeeds();
108                 for (Feed f: feeds) {
109                         cat = f.getCategory();
110                 
111                         catFeeds = result.get(cat);
112                         if (catFeeds == null) {
113                                 catFeeds = new ArrayList<Feed>();
114                                 result.put(cat, catFeeds);
115                         }
116                         catFeeds.add(f);
117                 }
118                 
119                 return result;
120         }
121
122         public Category[] getCategories() {
123                 return categories;
124         }
125         
126         public Category getDefaultCategory() {
127                 return categories[0];
128         }
129         
130         public static void main(String[] args) throws UnsupportedEncodingException {
131                 Config cfg;
132                 Feed[] feeds;           
133                 Category[] cats;
134
135                 cfg = new Config();
136                 cfg.loadConfig();
137                 
138                 cats = cfg.getCategories();
139                 for (Category cat: cats)
140                         System.out.println(cat);
141                 
142                 feeds = cfg.getFeeds();
143                 
144                 System.out.println("Number of feeds: " + feeds.length);
145                 for (Feed f: feeds)
146                         System.out.println(f);
147         }
148 }