aboutsummaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorChristopher Baines <cbaines8@gmail.com>2012-03-24 16:27:15 +0000
committerChristopher Baines <cbaines8@gmail.com>2012-03-24 16:27:15 +0000
commit18fed94860f17501d95a44f2fb0c372a58c0eaa7 (patch)
treeae14622e131fb605fce4959e35653c206a33f85a /src/net
parentdc44d733b56335f56f2fae23acf2c04277a0ce2f (diff)
downloadsouthampton-university-calendar-applet-18fed94860f17501d95a44f2fb0c372a58c0eaa7.tar
southampton-university-calendar-applet-18fed94860f17501d95a44f2fb0c372a58c0eaa7.tar.gz
Properly implemented weeks... :)
Diffstat (limited to 'src/net')
-rw-r--r--src/net/cbaines/suca/CalendarApplet.java2
-rw-r--r--src/net/cbaines/suca/SotonCalendarParser.java107
2 files changed, 90 insertions, 19 deletions
diff --git a/src/net/cbaines/suca/CalendarApplet.java b/src/net/cbaines/suca/CalendarApplet.java
index 329c180..5569285 100644
--- a/src/net/cbaines/suca/CalendarApplet.java
+++ b/src/net/cbaines/suca/CalendarApplet.java
@@ -278,8 +278,6 @@ public class CalendarApplet extends JApplet {
Calendar calendar = builder.build(new StringReader(calendarCSVString));
calendarICALString = calendar.toString();
calendarPreviewTextArea.setText(calendarICALString);
- System.out.println(calendarICALString);
-
createCalendarButton.setEnabled(true);
creationProgress.setIndeterminate(false);
diff --git a/src/net/cbaines/suca/SotonCalendarParser.java b/src/net/cbaines/suca/SotonCalendarParser.java
index a98fd81..7473bf8 100644
--- a/src/net/cbaines/suca/SotonCalendarParser.java
+++ b/src/net/cbaines/suca/SotonCalendarParser.java
@@ -9,6 +9,9 @@ import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Map;
import net.fortuna.ical4j.data.CalendarParser;
import net.fortuna.ical4j.data.ContentHandler;
@@ -35,6 +38,10 @@ public class SotonCalendarParser implements CalendarParser {
private boolean mergeHeaders = false;
+ private Calendar startYear = UniTermDates.getStartOf(UniTermDates.SEMESTER_1,
+ Calendar.getInstance().get(Calendar.YEAR) - 1);
+ private Calendar endYear = UniTermDates.getEndOf(UniTermDates.SEMESTER_2, UniTermDates.YEAR_BEGINING_2019);
+
// private final static String TAG = "SotonCalendarParser";
public void parse(InputStream in, ContentHandler handler) throws IOException, ParserException {
@@ -382,19 +389,19 @@ public class SotonCalendarParser implements CalendarParser {
log("Semester 1, year " + year + " starts on the " + dayOfTheWeek);
int difference2 = 0;
if (dayOfTheWeek == Calendar.MONDAY) {
- difference2 = 8;
- } else if (dayOfTheWeek == Calendar.TUESDAY) {
difference2 = 7;
- } else if (dayOfTheWeek == Calendar.WEDNESDAY) {
+ } else if (dayOfTheWeek == Calendar.TUESDAY) {
difference2 = 6;
- } else if (dayOfTheWeek == Calendar.THURSDAY) {
+ } else if (dayOfTheWeek == Calendar.WEDNESDAY) {
difference2 = 5;
- } else if (dayOfTheWeek == Calendar.FRIDAY) {
+ } else if (dayOfTheWeek == Calendar.THURSDAY) {
difference2 = 4;
- } else if (dayOfTheWeek == Calendar.SATURDAY) {
+ } else if (dayOfTheWeek == Calendar.FRIDAY) {
difference2 = 3;
- } else if (dayOfTheWeek == Calendar.SUNDAY) {
+ } else if (dayOfTheWeek == Calendar.SATURDAY) {
difference2 = 2;
+ } else if (dayOfTheWeek == Calendar.SUNDAY) {
+ difference2 = 1;
} else {
log("Error calculating difference");
}
@@ -403,6 +410,16 @@ public class SotonCalendarParser implements CalendarParser {
int week = 1;
while (semester1StartDate.before(semester1EndDate)) {
+
+ if (semester1StartDate.after(UniTermDates.getEndOf(UniTermDates.AUTUMN_TERM, year))
+ && semester1StartDate.before(UniTermDates
+ .getStartOf(UniTermDates.SPRING_TERM, year))) {
+ semester1StartDate.add(Calendar.WEEK_OF_YEAR, 1);
+ continue;
+ }
+
+ log("Semester 1, Week " + week);
+
handler.startComponent(Component.VEVENT);
// handler.startProperty(Property.UID);
// handler.propertyValue(ug.generateUid().toString());
@@ -420,22 +437,30 @@ public class SotonCalendarParser implements CalendarParser {
// Start time
- logCalendar(" Start time " + "Week " + week + " ", semester1StartDate);
+ logCalendar(" Start time ", semester1StartDate);
handler.startProperty(Property.DTSTART);
handler.parameter("VALUE", "DATE");
- handler.propertyValue(new Date(semester1StartDate.getTime()).toString());
+ // Calendar startCalTemp = ((Calendar) semester1StartDate.clone());
+ // startCalTemp.add(Calendar.DAY_OF_YEAR, 1);
+ // Date startDate = new Date(startCalTemp.getTime());
+ // logDate(" Start date " + "Week " + week + " ", startDate);
+ handler.propertyValue(getDate(semester1StartDate));
handler.endProperty(Property.DTSTART);
semester1StartDate.add(Calendar.WEEK_OF_YEAR, 1);
// End time
- logCalendar(" End time " + "Week " + week + " ", semester1StartDate);
+ logCalendar(" End time ", semester1StartDate);
handler.startProperty(Property.DTEND);
handler.parameter("VALUE", "DATE");
- handler.propertyValue(new Date(semester1StartDate.getTime()).toString());
+ // Calendar endCalTemp = ((Calendar) semester1StartDate.clone());
+ // endCalTemp.add(Calendar.DAY_OF_YEAR, 1);
+ // Date endDate = new Date(endCalTemp.getTime());
+ // logDate(" End date " + "Week " + week + " ", endDate);
+ handler.propertyValue(getDate(semester1StartDate));
handler.endProperty(Property.DTEND);
handler.endComponent(Component.VEVENT);
@@ -449,6 +474,16 @@ public class SotonCalendarParser implements CalendarParser {
week = 1;
while (semester2StartDate.before(semester2EndDate)) {
+
+ if (semester2StartDate.after(UniTermDates.getEndOf(UniTermDates.SPRING_TERM, year))
+ && semester2StartDate.before(UniTermDates
+ .getStartOf(UniTermDates.SUMMER_TERM, year))) {
+ semester2StartDate.add(Calendar.WEEK_OF_YEAR, 1);
+ continue;
+ }
+
+ log("Semester 2, Week " + week);
+
handler.startComponent(Component.VEVENT);
// handler.startProperty(Property.UID);
// handler.propertyValue(ug.generateUid().toString());
@@ -466,22 +501,22 @@ public class SotonCalendarParser implements CalendarParser {
// Start time
- logCalendar(" Start time " + "Week " + week + " ", semester2StartDate);
+ logCalendar(" Start time ", semester2StartDate);
handler.startProperty(Property.DTSTART);
handler.parameter("VALUE", "DATE");
- handler.propertyValue(new Date(semester2StartDate.getTime()).toString());
+ handler.propertyValue(getDate(semester2StartDate));
handler.endProperty(Property.DTSTART);
semester2StartDate.add(Calendar.WEEK_OF_YEAR, 1);
// End time
- logCalendar(" End time " + "Week " + week + " ", semester2StartDate);
+ logCalendar(" End time ", semester2StartDate);
handler.startProperty(Property.DTEND);
handler.parameter("VALUE", "DATE");
- handler.propertyValue(new Date(semester2StartDate.getTime()).toString());
+ handler.propertyValue(getDate(semester2StartDate));
handler.endProperty(Property.DTEND);
handler.endComponent(Component.VEVENT);
@@ -502,7 +537,32 @@ public class SotonCalendarParser implements CalendarParser {
}
}
+ private String getDate(Calendar cal) {
+ StringBuilder builder = new StringBuilder(8);
+
+ builder.append(cal.get(Calendar.YEAR));
+
+ builder.append(cal.get(Calendar.MONTH) + 1);
+ if (builder.length() == 5)
+ builder.insert(4, '0');
+
+ builder.append(cal.get(Calendar.DAY_OF_MONTH));
+ if (builder.length() == 7)
+ builder.insert(6, '0');
+
+ return builder.toString();
+ }
+
private static void logCalendar(String prefix, Calendar cal) {
+ Map<Integer, String> daysOfTheWeek = new HashMap<Integer, String>(7);
+ daysOfTheWeek.put(Calendar.MONDAY, "Monday");
+ daysOfTheWeek.put(Calendar.TUESDAY, "Tuesday");
+ daysOfTheWeek.put(Calendar.WEDNESDAY, "Wednesday");
+ daysOfTheWeek.put(Calendar.THURSDAY, "Thursday");
+ daysOfTheWeek.put(Calendar.FRIDAY, "Friday");
+ daysOfTheWeek.put(Calendar.SATURDAY, "Saturday");
+ daysOfTheWeek.put(Calendar.SUNDAY, "Sunday");
+
String hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
if (hour.length() == 1) {
hour = "0" + hour;
@@ -513,8 +573,21 @@ public class SotonCalendarParser implements CalendarParser {
minute = "0" + minute;
}
- log(prefix + hour + ":" + minute + " " + cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH) + 1)
- + "/" + cal.get(Calendar.YEAR));
+ log(prefix + hour + ":" + minute + " " + daysOfTheWeek.get(cal.get(Calendar.DAY_OF_WEEK)) + " "
+ + cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR));
+ }
+
+ private static void logDate(String prefix, Date date) {
+ Map<Integer, String> daysOfTheWeek = new HashMap<Integer, String>(7);
+ daysOfTheWeek.put(Calendar.MONDAY, "Monday");
+ daysOfTheWeek.put(Calendar.TUESDAY, "Tuesday");
+ daysOfTheWeek.put(Calendar.WEDNESDAY, "Wednesday");
+ daysOfTheWeek.put(Calendar.THURSDAY, "Thursday");
+ daysOfTheWeek.put(Calendar.FRIDAY, "Friday");
+ daysOfTheWeek.put(Calendar.SATURDAY, "Saturday");
+ daysOfTheWeek.put(Calendar.SUNDAY, "Sunday");
+
+ log(prefix + " " + date.toString());
}
private static void log(String string) {