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