-package de.dhbwloe.campusapp.vorlesungen;
-
-
-import android.util.Log;
-
-import net.fortuna.ical4j.model.Calendar;
-import net.fortuna.ical4j.model.Component;
-import net.fortuna.ical4j.model.Property;
-
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.ListIterator;
-import java.util.Locale;
-
-import de.dhbwloe.campusapp.CampusAppContext;
-import de.dhbwloe.campusapp.network.IscRequestHelper;
-import de.dhbwloe.campusapp.search.SearchIndices;
-
-/**
- * Created by pk910 on 19.01.2016.
- */
-public class VorlesungsplanManager extends IscRequestHelper {
- private static final String[][] PLAN_SOURCES = {
- {"STUV", "https://www.google.com/calendar/ical/asta.dhbw.de_c0g35t6hrh16kr4ankrqg2rdm4%40group.calendar.google.com/public/basic.ics"},
- };
-
- private CampusAppContext AppContext;
- private boolean bRequestRunning = false;
- private boolean bFastSynchronisation = false;
- private String sCourseName;
- private String source[];
- private ArrayList<VorlesungsplanManagerInterface> aCallbackInterfaces = new ArrayList<VorlesungsplanManagerInterface>();
-
- public VorlesungsplanManager(CampusAppContext context, String courseName) {
- AppContext = context;
- sCourseName = courseName;
-
- for(String src[] : PLAN_SOURCES) {
- if(src[0].equalsIgnoreCase(courseName)) {
- source = src;
- break;
- }
- }
- }
-
- public void performFastSynchronisation(VorlesungsplanManagerInterface callback) {
- performSynchronisation(callback, false);
- }
-
- public void performFullSynchronisation(VorlesungsplanManagerInterface callback) {
- performSynchronisation(callback, true);
- }
-
- private void performSynchronisation(VorlesungsplanManagerInterface callback, boolean fullsync) {
- if(sCourseName.length() == 0) {
- callback.onVorlesungsplanUpdateFail("no course name");
- return;
- }
-
- aCallbackInterfaces.add(callback);
- if(bRequestRunning)
- return;
-
- bFastSynchronisation = !fullsync;
- bRequestRunning = true;
- String courseCalendarUrl;
- if(source == null) {
- courseCalendarUrl = "https://webmail.dhbw-loerrach.de/owa/calendar/kal-" + sCourseName + "@dhbw-loerrach.de/Kalender/calendar.ics";
- } else {
- courseCalendarUrl = source[1];
- }
- requestCalenderFromWeb(courseCalendarUrl);
- }
-
- @Override
- protected void onCalendarReceived(Calendar calendar) {
- long timeFrom, timeTo;
- long now = (new Date()).getTime() / 1000;
- if(bFastSynchronisation) {
- timeFrom = now - (86400 * 5);
- timeTo = now + (86400 * 7 * 4);
- } else {
- timeFrom = now - (86400 * 365 * 3);
- timeTo = now + (86400 * 365 * 4);
- }
- CourseEvent[] events = AppContext.getDatabaseManager().getCourseCalendarEvents(sCourseName, timeFrom, timeTo);
- Log.i("VPMSync", "Event count: " + events.length);
- ArrayList<CourseEvent> newEvents = new ArrayList<CourseEvent>();
- ArrayList<SearchIndices> newIndices = new ArrayList<SearchIndices>();
- int lastEventIndex = 0;
- ListIterator cIterator = calendar.getComponents().listIterator();
-
- while (cIterator.hasNext()) {
- Component event = (Component)cIterator.next();
- CourseEvent dbEvent;
- Property prop;
-
- prop = event.getProperty("UID");
- String uid;
- if(prop != null)
- uid = prop.getValue();
- else
- continue;
-
- prop = event.getProperty("SEQUENCE");
- int sequence;
- if(prop != null)
- sequence = Integer.parseInt(prop.getValue());
- else
- sequence = 1;
-
- long startTime = 0, endTime = 0;
- prop = event.getProperty("DTSTART");
- if(prop != null)
- startTime = getTimeFromTimestamp(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: DTSTART not found!");
-
- prop = event.getProperty("DTEND");
- if(prop != null)
- endTime = getTimeFromTimestamp(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: DTEND not found!");
-
- if(!(endTime > timeFrom && startTime < timeTo)) {
- Log.i("VPMSync", "Skip Entry: ("+timeFrom+" - "+timeTo+") Filter: "+startTime+" - "+endTime);
- continue;
- }
-
- dbEvent = null;
- for(int i = lastEventIndex; i < events.length; i++) {
- if(events[i].getUniqueId().equalsIgnoreCase(uid)) {
- dbEvent = events[i];
- break;
- }
- }
- if(dbEvent == null && lastEventIndex > 0) {
- for(int i = 0; i < lastEventIndex; i++) {
- if(events[i].getUniqueId().equalsIgnoreCase(uid)) {
- dbEvent = events[i];
- break;
- }
- }
- }
- if(dbEvent == null) {
- for(CourseEvent cevent : newEvents) {
- if(cevent.getUniqueId().equalsIgnoreCase(uid)) {
- dbEvent = cevent;
- break;
- }
- }
- }
-
- if(dbEvent == null) {
- // new event!
- Log.i("VLPSync", "New Event "+uid);
- dbEvent = new CourseEvent(sCourseName, uid, sequence, true);
- newEvents.add(dbEvent);
- } else {
- Log.i("VLPSync", "Existing Event "+uid+" ("+dbEvent.getSequenceId()+" >= "+sequence+")");
- if(dbEvent.getSequenceId() >= sequence) {
- continue; // skip event
- }
- dbEvent.setSequenceId(sequence);
- }
- // perform update
- prop = event.getProperty("SUMMARY");
- if(prop != null)
- dbEvent.setEventTitle(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: SUMMARY not found!");
-
- dbEvent.setEventFrom(startTime);
- dbEvent.setEventTo(endTime);
-
- prop = event.getProperty("LOCATION");
- if(prop != null)
- dbEvent.setEventLocation(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: LOCATION not found!");
-
- prop = event.getProperty("STATUS");
- if(prop != null)
- dbEvent.setEventStatus(prop.getValue());
- else
- Log.i("VPMSync", "Parse Event: STATUS not found!");
-
- prop = event.getProperty("RRULE");
- if(prop != null)
- dbEvent.setRecurRule(prop.getValue());
- else
- dbEvent.setRecurRule("");
-
- prop = event.getProperty("EXDATE");
- if(prop != null)
- dbEvent.setExcludeDates(prop.getValue());
- else
- dbEvent.setExcludeDates("");
-
- CourseGroup group = CourseGroup.GetCourseGroupByName(AppContext.getDatabaseManager(), sCourseName, dbEvent.getGroupTitle().trim());
- if(group != null)
- dbEvent.setCourseGroup(group);
-
- if(group.isNewGroup(true)) {
- SearchIndices indices = new SearchIndices("Vorlesungsplan#Group"+group.getGroupId(), false);
- indices.setUpdateTime(dbEvent.getEventFrom());
- indices.setTarget("#Vorlesungsplan#groupid=" + group.getGroupId());
- indices.setTitle("Vorlesungsplan " + dbEvent.getCourseName());
- indices.setDescription("Vorlesung " + dbEvent.getEventTitle());
- indices.addKeyWord(dbEvent.getGroupTitle());
- indices.addKeyWord(dbEvent.getEventLocation());
- newIndices.add(indices);
- }
-
- dbEvent.update(AppContext.getDatabaseManager());
- Log.i("VPMSync", "Update Event: "+dbEvent.getUniqueId());
- }
-
- SearchIndices[] newIndicesArr = new SearchIndices[newIndices.size()];
- newIndicesArr = newIndices.toArray(newIndicesArr);
- AppContext.addSearchIndices(newIndicesArr);
-
- for(VorlesungsplanManagerInterface callback : aCallbackInterfaces) {
- callback.onVorlesungsplanUpdateDone();
- }
- aCallbackInterfaces.clear();
- bRequestRunning = false;
- }
-
- private long getTimeFromTimestamp(String timestamp) {
- // 20160425T090000
- // 20160321
- DateFormat df = new SimpleDateFormat("yyyyMMdd'T'kkmmss", Locale.ENGLISH);
- try {
- Date result = df.parse(timestamp);
- return result.getTime()/1000;
- } catch (ParseException e) {
- df = new SimpleDateFormat("yyyyMMdd", Locale.ENGLISH);
- try {
- Date result = df.parse(timestamp);
- return result.getTime()/1000;
- } catch (ParseException e2) {
- Log.i("VPMSync", "Failed parsing: "+timestamp);
- return 0;
- }
- }
- }
-
- @Override
- protected void onCalendarRequestFail(int statusCode, String errorMessage) {
- Log.i("VPM", "Calendar Error: "+statusCode);
- for(VorlesungsplanManagerInterface callback : aCallbackInterfaces) {
- callback.onVorlesungsplanUpdateFail("error "+statusCode+": "+errorMessage);
- }
- aCallbackInterfaces.clear();
- bRequestRunning = false;
- }
-}