alpha 0.0.1
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / vorlesungen / CourseEvent.java
1 package de.dhbwloe.campusapp.vorlesungen;
2
3 import net.fortuna.ical4j.model.Component;
4
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 import de.dhbwloe.campusapp.database.DatabaseManager;
9
10 /**
11  * Created by pk910 on 20.01.2016.
12  */
13 public class CourseEvent implements Comparable<CourseEvent> {
14     public enum CourseType {
15         COURSETYPE_NORMAL,
16         COURSETYPE_SPECIAL,
17         COURSETYPE_KLAUSUR,
18     }
19
20     private int iEventId;
21     private String sCourseName;
22     private String sUniqueId;
23     private int iSequenceId;
24     private long iEventFrom, iEventTo;
25     private String sEventTitle, sEventTitleShort, sEventTitleAuthor, sEventLocation, sEventStatus;
26     private String sRecurRule, sExcludeDates;
27     private CourseGroup oCourseGroup;
28     private CourseType oCourseType = null;
29
30     private boolean bMustUpdate = false;
31     private boolean bIsNew = false;
32
33     private boolean bIsKlausurPraesi = false;
34
35     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) {
36         iEventId = id;
37         sCourseName = coursename;
38         sUniqueId = uniqueid;
39         iSequenceId = sequenceid;
40         iEventFrom = eventfrom;
41         iEventTo = eventto;
42         sEventTitle = title;
43         sEventLocation = location;
44         sEventStatus = status;
45         sRecurRule = rrule;
46         sExcludeDates = exdates;
47
48         oCourseGroup = group;
49         if(group != null) {
50             group.addCourseEvent(this);
51             try {
52                 oCourseType = CourseType.values()[eventtype];
53             } catch(Exception e) {}
54         }
55
56         ParseEventTitle();
57     }
58
59     public CourseEvent(String coursename, String uniqueid, int sequenceid, boolean isNew) {
60         sCourseName = coursename;
61         sUniqueId = uniqueid;
62         iSequenceId = sequenceid;
63         bIsNew = isNew;
64         bMustUpdate = isNew;
65     }
66
67     public void ParseEventTitle() {
68         Pattern pattern = Pattern.compile("^((Klausur|Tutorium|Pr(ä|ae)sentation)[ :]+)?(.*?)( - ([a-zA-Z., -]+))?$");
69         Matcher m = pattern.matcher(sEventTitle);
70         CourseType oldtype = oCourseType;
71         boolean typeisset = (oCourseType != null);
72         if (m.matches()) {
73             String eventType = m.group(2);
74             if (!typeisset && eventType != null && !eventType.isEmpty()) {
75                 if(eventType.equalsIgnoreCase("Klausur"))
76                     oCourseType = CourseType.COURSETYPE_KLAUSUR;
77                 else if(eventType.equalsIgnoreCase("Präsentation") || eventType.equalsIgnoreCase("Praesentation")) {
78                     oCourseType = CourseType.COURSETYPE_KLAUSUR;
79                     bIsKlausurPraesi = true;
80                 } else if(eventType.equalsIgnoreCase("Tutorium"))
81                     oCourseType = CourseType.COURSETYPE_SPECIAL;
82
83                 typeisset = true;
84             }
85             sEventTitleShort = m.group(4);
86             sEventTitleAuthor = m.group(6);
87         } else {
88             sEventTitleShort = sEventTitle;
89             sEventTitleAuthor = "";
90         }
91         if(!typeisset)
92             oCourseType = CourseType.COURSETYPE_NORMAL;
93
94         if(oldtype != oCourseType)
95             bMustUpdate = true;
96     }
97
98     public void setEventId(int id) {
99         iEventId = id;
100     }
101
102     public int getEventId() {
103         return iEventId;
104     }
105
106     private void resetUpdateFlag() {
107         bIsNew = false;
108         bMustUpdate = false;
109     }
110
111     public void update(DatabaseManager dbm, Component event) {
112         if(!bMustUpdate)
113             return;
114
115         dbm.updateCourseCalendar(this, event);
116         resetUpdateFlag();
117     }
118
119     public CourseGroup getCourseGroup() {
120         return oCourseGroup;
121     }
122
123     public void setCourseType(CourseType type) {
124         oCourseType = type;
125     }
126
127     public CourseType getCourseType() {
128         return oCourseType;
129     }
130
131     public int getCourseTypeId() {
132         for(int i = 0; i < CourseType.values().length; i++) {
133             if(oCourseType == CourseType.values()[i])
134                 return i;
135         }
136         return 0;
137     }
138
139     public void setCourseGroup(CourseGroup group) {
140         group.addCourseEvent(this);
141         oCourseGroup = group;
142     }
143
144     public String getUniqueId() {
145         return sUniqueId;
146     }
147
148     public boolean IsPendingUpdate() {
149         return bMustUpdate;
150     }
151
152     public boolean IsNewEvent() {
153         return bIsNew;
154     }
155
156     public String getEventStatus() {
157         return sEventStatus;
158     }
159
160     public void setEventStatus(String sEventStatus) {
161         this.sEventStatus = sEventStatus;
162         bMustUpdate = true;
163     }
164
165     public String getEventLocation() {
166         return sEventLocation;
167     }
168
169     public void setEventLocation(String sEventLocation) {
170         this.sEventLocation = sEventLocation;
171         bMustUpdate = true;
172     }
173
174     public String getEventTitle() {
175         return sEventTitle;
176     }
177     public String getGroupTitle() {
178         return sEventTitleShort + (sEventTitleAuthor == null ? "" : " - " + sEventTitleAuthor);
179     }
180     public String getEventShortTitle() {
181         return sEventTitleShort;
182     }
183     public String getEventDozent() {
184         return sEventTitleAuthor;
185     }
186
187     public void setEventTitle(String sEventTitle) {
188         this.sEventTitle = sEventTitle;
189         ParseEventTitle();
190         bMustUpdate = true;
191     }
192
193     public long getEventTo() {
194         return iEventTo;
195     }
196
197     public void setEventTo(long iEventTo) {
198         this.iEventTo = iEventTo;
199         bMustUpdate = true;
200     }
201
202     public long getEventFrom() {
203         return iEventFrom;
204     }
205
206     public void setEventFrom(long iEventFrom) {
207         this.iEventFrom = iEventFrom;
208         bMustUpdate = true;
209     }
210
211     public String getCourseName() {
212         return sCourseName;
213     }
214
215     public int getSequenceId() {
216         return iSequenceId;
217     }
218
219     public void setSequenceId(int iSequenceId) {
220         this.iSequenceId = iSequenceId;
221         bMustUpdate = true;
222     }
223
224     public String getRecurRule() {
225         return sRecurRule;
226     }
227
228     public void setRecurRule(String rrule) {
229         this.sRecurRule = rrule;
230         bMustUpdate = true;
231     }
232
233     public String getExcludeDates() {
234         return sExcludeDates;
235     }
236
237     public void setExcludeDates(String exrules) {
238         this.sExcludeDates = exrules;
239         bMustUpdate = true;
240     }
241
242     public boolean getIsKlausurPraesi() {
243         return bIsKlausurPraesi;
244     }
245
246     @Override
247     public int compareTo(CourseEvent another) {
248         return (int)(another.getEventFrom() - iEventFrom);
249     }
250 }