Grundaufbau der App
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / news / NewsManager.java
1 package de.dhbwloe.campusapp.news;
2
3 import android.util.Base64;
4 import android.util.Log;
5
6 import java.io.UnsupportedEncodingException;
7 import java.net.URLEncoder;
8 import java.text.DateFormat;
9 import java.text.ParseException;
10 import java.text.SimpleDateFormat;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.List;
14 import java.util.Locale;
15
16 import de.dhbwloe.campusapp.CampusAppContext;
17 import de.dhbwloe.campusapp.mensaplan.MensaplanManagerInterface;
18 import de.dhbwloe.campusapp.network.XmlEntry;
19 import de.dhbwloe.campusapp.network.XmlRequestHelper;
20 import de.dhbwloe.campusapp.search.SearchIndices;
21
22 /**
23  * Created by pk910 on 22.01.2016.
24  */
25 public class NewsManager extends XmlRequestHelper {
26     private static final String[][] NEWS_SOURCES = {
27         {"DHBW", "http://www.dhbw-loerrach.de/index.php?id=59&type=100"},
28         {"STUV", "http://stuv-loerrach.de/feed"},
29     };
30
31
32     private CampusAppContext AppContext;
33     private boolean bRequestRunning = false;
34     private ArrayList<NewsManagerInterface> aCallbackInterfaces = new ArrayList<NewsManagerInterface>();
35     private String[] source;
36
37     public NewsManager(CampusAppContext context, String source) {
38         AppContext = context;
39
40         for(String[] src : NEWS_SOURCES) {
41             if(src[0].equalsIgnoreCase(source)) {
42                 this.source = src;
43             }
44         }
45     }
46
47     public void performSynchronisation(NewsManagerInterface callback) {
48         aCallbackInterfaces.add(callback);
49         if(bRequestRunning || this.source == null)
50             return;
51
52         bRequestRunning = true;
53         requestXmlFromWeb(this.source[1], "channel", "item");
54     }
55
56     private long getTimeFromDateString(String timestamp) {
57         // Thu, 08 Oct 2015 08:00:34 +0000
58         if(timestamp == null)
59             return 0;
60         DateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
61         try {
62             Date result =  df.parse(timestamp);
63             return result.getTime()/1000;
64         } catch (ParseException e) {
65             return 0;
66         }
67     }
68
69     @Override
70     protected void onXmlReceived(List<XmlEntry> entries) {
71         ArrayList<SearchIndices> newIndices = new ArrayList<SearchIndices>();
72
73         Log.i("NMSync", "Received News XML");
74         for(XmlEntry entry : entries) {
75             Log.i("NMSync", "Parse News");
76             XmlEntry centry;
77
78             centry = XmlEntry.FindXmlEntryByName(entry, "pubDate");
79             long time = (centry == null ? 0 : getTimeFromDateString(centry.getValue()));
80             if(time == 0)
81                 continue;
82
83             centry = XmlEntry.FindXmlEntryByName(entry, "link");
84             String link = (centry == null ? null : centry.getValue());
85             centry = XmlEntry.FindXmlEntryByName(entry, "uuid");
86             String uuid = (centry == null ? null : centry.getValue());
87
88             String uniqueid;
89             if(uuid == null) {
90                 if(link == null)
91                     continue;
92                 byte[] data = new byte[0];
93                 try {
94                     data = link.getBytes("UTF-8");
95                 } catch (UnsupportedEncodingException e) {
96                     continue;
97                 }
98                 uniqueid = Base64.encodeToString(data, Base64.DEFAULT);
99             } else {
100                 byte[] data = new byte[0];
101                 try {
102                     data = uuid.getBytes("UTF-8");
103                 } catch (UnsupportedEncodingException e) {
104                     continue;
105                 }
106                 uniqueid = Base64.encodeToString(data, Base64.DEFAULT);
107             }
108
109             centry = XmlEntry.FindXmlEntryByName(entry, "title");
110             String title = (centry == null ? null : centry.getValue());
111
112             centry = XmlEntry.FindXmlEntryByName(entry, "description");
113             String summary = (centry == null ? null : centry.getValue());
114
115             centry = XmlEntry.FindXmlEntryByName(entry, "content");
116             String content = (centry == null ? null : centry.getValue());
117
118             centry = XmlEntry.FindXmlEntryByName(entry, "creator");
119             String creator = (centry == null ? null : centry.getValue());
120             if(creator != null) {
121                 if(content != null)
122                     content = creator + ": " + content;
123                 else
124                     content = creator;
125             }
126
127             StringBuilder categories = new StringBuilder();
128             XmlEntry[] centries = XmlEntry.FindXmlEntriesByName(entry, "category");
129             for(XmlEntry centry2 : centries) {
130                 if(categories.length() > 0)
131                     categories.append(",");
132                 categories.append(centry2.getValue());
133             }
134
135             NewsItem newsitem = new NewsItem(0, source[0], time, uniqueid, 0, title, summary, content, link, categories.toString());
136             newsitem.calculateChkSum();
137
138             AppContext.getDatabaseManager().updateNewsItem(newsitem);
139             if(newsitem.getIsNew(true)) {
140                 SearchIndices indices = new SearchIndices("NewsItem#"+newsitem.getId(), false);
141                 indices.setTarget("#News#shownews=" + newsitem.getId());
142                 indices.setTitle(newsitem.getSource() + " News (" + newsitem.getFormatedDate() + ")");
143                 indices.setDescription(newsitem.getTitle() + ": " + newsitem.getSummary());
144                 indices.setUpdateTime(newsitem.getTime());
145                 indices.addKeyWord(newsitem.getTitle());
146                 indices.addKeyWord(newsitem.getSummary());
147                 indices.addKeyWord(newsitem.getContent());
148                 newIndices.add(indices);
149             }
150         }
151
152         SearchIndices[] newIndicesArr = new SearchIndices[newIndices.size()];
153         newIndicesArr = newIndices.toArray(newIndicesArr);
154         AppContext.addSearchIndices(newIndicesArr);
155
156         for(NewsManagerInterface callback : aCallbackInterfaces) {
157             callback.onNewsUpdateDone();
158         }
159         aCallbackInterfaces.clear();
160         bRequestRunning = false;
161     }
162
163     @Override
164     protected void onXmlRequestFail(int statusCode, String errorMessage) {
165         for(NewsManagerInterface callback : aCallbackInterfaces) {
166             callback.onNewsUpdateFail("error " + statusCode + ": " + errorMessage);
167         }
168         aCallbackInterfaces.clear();
169         bRequestRunning = false;
170     }
171 }