Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / vorlesungen / CourseEvent.java
index 71a49cab5487321599a9c01356880b1428fbce39..4973dd52e1fa4226d7848b80a3b7d248c328f949 100644 (file)
@@ -1,24 +1,52 @@
+/* CourseEvent.java
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
 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 +60,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 +78,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 +122,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 +134,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 +189,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 +252,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);
+    }
 }