alpha 0.0.1
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / vorlesungen / CourseEvent.java
index 71a49cab5487321599a9c01356880b1428fbce39..0e5a1cb1717ca17c3700d26ffdf156dfc8fd5151 100644 (file)
@@ -1,24 +1,38 @@
 package de.dhbwloe.campusapp.vorlesungen;
 
+import net.fortuna.ical4j.model.Component;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 import de.dhbwloe.campusapp.database.DatabaseManager;
 
 /**
  * Created by pk910 on 20.01.2016.
  */
-public class CourseEvent {
+public class CourseEvent implements Comparable<CourseEvent> {
+    public enum CourseType {
+        COURSETYPE_NORMAL,
+        COURSETYPE_SPECIAL,
+        COURSETYPE_KLAUSUR,
+    }
+
     private int iEventId;
     private String sCourseName;
     private String sUniqueId;
     private int iSequenceId;
     private long iEventFrom, iEventTo;
-    private String sEventTitle, sEventLocation, sEventStatus;
+    private String sEventTitle, sEventTitleShort, sEventTitleAuthor, sEventLocation, sEventStatus;
     private String sRecurRule, sExcludeDates;
     private CourseGroup oCourseGroup;
+    private CourseType oCourseType = null;
 
     private boolean bMustUpdate = false;
     private boolean bIsNew = false;
 
-    public CourseEvent(int id, String coursename, String uniqueid, int sequenceid, long eventfrom, long eventto, String title, String location, String status, String rrule, String exdates, CourseGroup group) {
+    private boolean bIsKlausurPraesi = false;
+
+    public CourseEvent(int id, String coursename, String uniqueid, int sequenceid, long eventfrom, long eventto, String title, String location, String status, String rrule, String exdates, CourseGroup group, int eventtype) {
         iEventId = id;
         sCourseName = coursename;
         sUniqueId = uniqueid;
@@ -32,8 +46,14 @@ public class CourseEvent {
         sExcludeDates = exdates;
 
         oCourseGroup = group;
-        if(group != null)
+        if(group != null) {
             group.addCourseEvent(this);
+            try {
+                oCourseType = CourseType.values()[eventtype];
+            } catch(Exception e) {}
+        }
+
+        ParseEventTitle();
     }
 
     public CourseEvent(String coursename, String uniqueid, int sequenceid, boolean isNew) {
@@ -44,6 +64,37 @@ public class CourseEvent {
         bMustUpdate = isNew;
     }
 
+    public void ParseEventTitle() {
+        Pattern pattern = Pattern.compile("^((Klausur|Tutorium|Pr(ä|ae)sentation)[ :]+)?(.*?)( - ([a-zA-Z., -]+))?$");
+        Matcher m = pattern.matcher(sEventTitle);
+        CourseType oldtype = oCourseType;
+        boolean typeisset = (oCourseType != null);
+        if (m.matches()) {
+            String eventType = m.group(2);
+            if (!typeisset && eventType != null && !eventType.isEmpty()) {
+                if(eventType.equalsIgnoreCase("Klausur"))
+                    oCourseType = CourseType.COURSETYPE_KLAUSUR;
+                else if(eventType.equalsIgnoreCase("Präsentation") || eventType.equalsIgnoreCase("Praesentation")) {
+                    oCourseType = CourseType.COURSETYPE_KLAUSUR;
+                    bIsKlausurPraesi = true;
+                } else if(eventType.equalsIgnoreCase("Tutorium"))
+                    oCourseType = CourseType.COURSETYPE_SPECIAL;
+
+                typeisset = true;
+            }
+            sEventTitleShort = m.group(4);
+            sEventTitleAuthor = m.group(6);
+        } else {
+            sEventTitleShort = sEventTitle;
+            sEventTitleAuthor = "";
+        }
+        if(!typeisset)
+            oCourseType = CourseType.COURSETYPE_NORMAL;
+
+        if(oldtype != oCourseType)
+            bMustUpdate = true;
+    }
+
     public void setEventId(int id) {
         iEventId = id;
     }
@@ -57,11 +108,11 @@ public class CourseEvent {
         bMustUpdate = false;
     }
 
-    public void update(DatabaseManager dbm) {
+    public void update(DatabaseManager dbm, Component event) {
         if(!bMustUpdate)
             return;
 
-        dbm.updateCourseCalendar(this);
+        dbm.updateCourseCalendar(this, event);
         resetUpdateFlag();
     }
 
@@ -69,6 +120,22 @@ public class CourseEvent {
         return oCourseGroup;
     }
 
+    public void setCourseType(CourseType type) {
+        oCourseType = type;
+    }
+
+    public CourseType getCourseType() {
+        return oCourseType;
+    }
+
+    public int getCourseTypeId() {
+        for(int i = 0; i < CourseType.values().length; i++) {
+            if(oCourseType == CourseType.values()[i])
+                return i;
+        }
+        return 0;
+    }
+
     public void setCourseGroup(CourseGroup group) {
         group.addCourseEvent(this);
         oCourseGroup = group;
@@ -108,11 +175,18 @@ public class CourseEvent {
         return sEventTitle;
     }
     public String getGroupTitle() {
-        return sEventTitle; // maybe cut prof name?
+        return sEventTitleShort + (sEventTitleAuthor == null ? "" : " - " + sEventTitleAuthor);
+    }
+    public String getEventShortTitle() {
+        return sEventTitleShort;
+    }
+    public String getEventDozent() {
+        return sEventTitleAuthor;
     }
 
     public void setEventTitle(String sEventTitle) {
         this.sEventTitle = sEventTitle;
+        ParseEventTitle();
         bMustUpdate = true;
     }
 
@@ -164,4 +238,13 @@ public class CourseEvent {
         this.sExcludeDates = exrules;
         bMustUpdate = true;
     }
+
+    public boolean getIsKlausurPraesi() {
+        return bIsKlausurPraesi;
+    }
+
+    @Override
+    public int compareTo(CourseEvent another) {
+        return (int)(another.getEventFrom() - iEventFrom);
+    }
 }