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