Permalink
Browse files

Updated tests, semi-working insertion of absences and events

  • Loading branch information...
1 parent df8ca36 commit e29c8c68a8b41afd8838767c595b3433a71cf419 @danstiner committed May 5, 2012
@@ -1,5 +1,7 @@
package edu.iastate.music.marching.attendance;
+import java.util.TimeZone;
+
public class App {
private static final String TITLE = "ISUCF'V'MB Attendance";
@@ -18,4 +20,9 @@ public static String getHashedMobilePassword() {
return null;
}
+ public static TimeZone getTimeZone() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
}
@@ -1,28 +1,82 @@
package edu.iastate.music.marching.attendance.controllers;
import java.util.Date;
+import java.util.List;
+import com.google.code.twig.ObjectDatastore;
+
+import edu.iastate.music.marching.attendance.model.Absence;
+import edu.iastate.music.marching.attendance.model.Event;
+import edu.iastate.music.marching.attendance.model.ModelFactory;
import edu.iastate.music.marching.attendance.model.User;
public class AbsenceController extends AbstractController {
+ private DataTrain train;
+
public AbsenceController(DataTrain dataTrain) {
- super(dataTrain);
+ this.train = dataTrain;
}
- public void createOrUpdateTardy(User student, Date time) {
- // TODO Auto-generated method stub
+ public Absence createOrUpdateTardy(User student, Date time) {
+ if(student == null)
+ throw new IllegalArgumentException("Tried to create absence for null user");
+
+ Absence absence = ModelFactory.newAbsence(Absence.Type.Tardy, student);
+ absence.setDatetime(time);
+
+ // Associate with event
+ List<Event> events = train.getEventsController().get(time);
+
+ if (events.size() == 1)
+ absence.setEvent(events.get(0));
+ // else the absence is orphaned
+
+ ObjectDatastore od = this.train.getDataStore();
+ od.storeOrUpdate(absence);
+ return absence;
}
- public void createOrUpdateAbsence(User student, Date start, Date end) {
- // TODO Auto-generated method stub
+ public Absence createOrUpdateAbsence(User student, Date start, Date end) {
+
+ if(student == null)
+ throw new IllegalArgumentException("Tried to create absence for null user");
+ Absence absence = ModelFactory.newAbsence(Absence.Type.Tardy, student);
+ absence.setStart(start);
+ absence.setEnd(end);
+
+ // Associate with event
+ List<Event> events = train.getEventsController().get(start, end);
+
+ if (events.size() == 1)
+ absence.setEvent(events.get(0));
+ // else the absence is orphaned
+
+ ObjectDatastore od = this.train.getDataStore();
+ od.storeOrUpdate(absence);
+ return absence;
}
- public void createOrUpdateEarlyCheckout(User student, Date time) {
- // TODO Auto-generated method stub
+ public Absence createOrUpdateEarlyCheckout(User student, Date time) {
+
+ if(student == null)
+ throw new IllegalArgumentException("Tried to create absence for null user");
+ Absence absence = ModelFactory.newAbsence(Absence.Type.EarlyCheckOut, student);
+ absence.setDatetime(time);
+
+ // Associate with event
+ List<Event> events = train.getEventsController().get(time);
+
+ if (events.size() == 1)
+ absence.setEvent(events.get(0));
+ // else the absence is orphaned
+
+ ObjectDatastore od = this.train.getDataStore();
+ od.storeOrUpdate(absence);
+ return absence;
}
}
@@ -32,7 +32,7 @@ public static User createStudent(String netID, int univID,
UserController uc = DataTrain.getAndStartTrain().getUsersController();
User u = uc.createStudent(netID, univID, sanitizedFirstName,
- sanitizedLastName);
+ sanitizedLastName, 0, null); // TODO
return u;
@@ -15,7 +15,7 @@ public static DataTrain getAndStartTrain()
/** Never need to create an instance of this, only call static methods */
private DataTrain() {
- datastore = new AnnotationObjectDatastore(false);
+ datastore = new AnnotationObjectDatastore();
}
public AbsenceController getAbscencesController() {
@@ -1,8 +1,13 @@
package edu.iastate.music.marching.attendance.controllers;
+import java.util.ArrayList;
+import java.util.Calendar;
import java.util.Date;
+import java.util.Iterator;
import java.util.List;
+import com.google.appengine.api.datastore.Query.FilterOperator;
+import com.google.code.twig.FindCommand.RootFindCommand;
import com.google.code.twig.ObjectDatastore;
import edu.iastate.music.marching.attendance.model.Event;
@@ -17,28 +22,95 @@ public EventController(DataTrain dataTrain) {
this.train = dataTrain;
}
-// public boolean create(Type type, Date start, Date end) {
-// Event event = ModelFactory.newEvent(type, start, end);
-// ObjectDatastore od = this.train.getDataStore();
-// od.store(event);
-// return true;
-// }
+ // public boolean create(Type type, Date start, Date end) {
+ // Event event = ModelFactory.newEvent(type, start, end);
+ // ObjectDatastore od = this.train.getDataStore();
+ // od.store(event);
+ // return true;
+ // }
public Event createOrUpdate(Type type, Date start, Date end) {
Event event = ModelFactory.newEvent(type, start, end);
+
+ // Check that start and end are on the same day
+ // TODO
+
+ event.setDate(datetimeToJustDate(start));
+
ObjectDatastore od = this.train.getDataStore();
od.storeOrUpdate(event);
return event;
}
-
+
public void delete(Event event) {
ObjectDatastore od = this.train.getDataStore();
od.delete(event);
- //od.delete() returns void, so we don't really have anything to return here, either
+ // od.delete() returns void, so we don't really have anything to return
+ // here, either
}
-
+
public List<Event> readAll() {
- return this.train.getDataStore().find().type(Event.class).returnAll().now();
+ return this.train.getDataStore().find().type(Event.class).returnAll()
+ .now();
+ }
+
+ /**
+ * Gets a list of events which are happening during given time
+ *
+ * @param time
+ * @param end
+ * @return
+ */
+ public List<Event> get(Date time) {
+
+ // Build date for the day of the event
+
+
+ // Due to limitations of the data store, we cannot filter on the start and end fields,
+ // so match date and them manually filter results
+ RootFindCommand<Event> find = this.train.getDataStore().find()
+ .type(Event.class);
+ find.addFilter(Event.FIELD_DATE, FilterOperator.EQUAL,
+ datetimeToJustDate(time));
+ Iterator<Event> iter = find.now();
+
+ List<Event> matchingEvents = new ArrayList<Event>();
+ while(iter.hasNext())
+ {
+ Event e = iter.next();
+ if(e.getStart().compareTo(time) <= 0 && e.getEnd().compareTo(time) >= 0)
+ matchingEvents.add(e);
+ }
+
+ return matchingEvents;
+ }
+
+ /**
+ * Gets a list of events which start and end exactly at the given times
+ *
+ * @param start
+ * @param end
+ * @return
+ */
+ public List<Event> get(Date start, Date end) {
+ RootFindCommand<Event> find = this.train.getDataStore().find()
+ .type(Event.class);
+
+ find.addFilter(Event.FIELD_START, FilterOperator.EQUAL, start);
+ find.addFilter(Event.FIELD_END, FilterOperator.EQUAL, end);
+ return find.returnAll().now();
+ }
+
+ private Date datetimeToJustDate(Date datetime)
+ {
+ Calendar ctime = Calendar.getInstance();
+ ctime.setTime(datetime);
+
+ Calendar cdate = Calendar.getInstance();
+ cdate.setTimeInMillis(0);
+ cdate.set(ctime.get(Calendar.YEAR), ctime.get(Calendar.MONTH), ctime.get(Calendar.DATE));
+
+ return cdate.getTime();
}
}
@@ -1,6 +1,9 @@
package edu.iastate.music.marching.attendance.controllers;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Calendar;
import java.util.Date;
import java.util.List;
@@ -12,8 +15,13 @@
public class MobileDataController {
- private static final String NEWLINE = ",";
+ private static final String NEWLINE = "&newline&";
private static final String SEPARATOR = "&split&";
+ private static final SimpleDateFormat MOBILE_DATETIME_FORMAT = new SimpleDateFormat(
+ "yyyy-MM-dd HHmm");
+ private static final Object EVENT_TYPE_REHERSAL = null;
+ private static final Object EVENT_TYPE_PERFORMANCE = null;
+
private DataTrain train;
MobileDataController(DataTrain dataTrain) {
@@ -59,6 +67,7 @@ public String getClassList() {
sb.append(NEWLINE);
}
+
return sb.toString();
}
@@ -91,15 +100,31 @@ public boolean pushMobileData(String data) throws IllegalArgumentException {
for (String s : eventLines) {
// TODO, this is all really bullshit to mock it up.
String[] event = s.split(SEPARATOR);
- String strType = event[1];
- String startTime = event[5];
+ String strType = event[1].toLowerCase().trim();
String strDate = event[4];
+ String startTime = event[5];
String endTime = event[6];
- Date start = new Date();
- Date end = new Date();
- Event.Type type2 = Event.Type.Performance;
- Event newEvent = ec.createOrUpdate(type2, start, end);
+ Date start;
+ try {
+ start = MOBILE_DATETIME_FORMAT.parse(strDate + " " + startTime);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(
+ "Unable to parse event start datetime from: " + s, e);
+ }
+
+ Date end;
+ try {
+ end = MOBILE_DATETIME_FORMAT.parse(strDate + " " + endTime);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(
+ "Unable to parse event end datetime from: " + s, e);
+ }
+
+ Event.Type type = Event.Type.valueOf(strType.substring(0, 1)
+ .toUpperCase() + strType.substring(1));
+
+ Event newEvent = ec.createOrUpdate(type, start, end);
if (newEvent == null) {
// do something TODO
@@ -111,8 +136,20 @@ public boolean pushMobileData(String data) throws IllegalArgumentException {
String[] parts = s.split(SEPARATOR);
if (s.contains("tardy")) {
- Date time = null;
- String netid = null;
+ String firstName = parts[1];
+ String lastName = parts[2];
+ String netid = parts[3];
+ String strDate = parts[4];
+ String strTime = parts[5];
+
+ Date time;
+ try {
+ time = MOBILE_DATETIME_FORMAT
+ .parse(strDate + " " + strTime);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(
+ "Unable to parse start datetime in:" + s, e);
+ }
User student = uc.get(netid);
@@ -126,19 +163,43 @@ public boolean pushMobileData(String data) throws IllegalArgumentException {
String strStartTime = parts[5];
String strEndTime = parts[6];
- Date start = null;
- Date end = null;
+ Date start;
+ try {
+ start = MOBILE_DATETIME_FORMAT.parse(strDate + " "
+ + strStartTime);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(
+ "Unable to parse start datetime in:" + s, e);
+ }
+ Date end;
+ try {
+ end = MOBILE_DATETIME_FORMAT.parse(strDate + " "
+ + strEndTime);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(
+ "Unable to parse end datetime in:" + s, e);
+ }
User student = uc.get(netid);
-
+
// TODO check for valid student
ac.createOrUpdateAbsence(student, start, end);
} else if (s.toLowerCase().contains("earlycheckout")) {
-
- String netid = null;
-
- Date time = null;
+ String firstName = parts[1];
+ String lastName = parts[2];
+ String netid = parts[3];
+ String strDate = parts[4];
+ String strTime = parts[5];
+
+ Date time;
+ try {
+ time = MOBILE_DATETIME_FORMAT
+ .parse(strDate + " " + strTime);
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(
+ "Unable to parse start datetime in:" + s, e);
+ }
User student = uc.get(netid);
Oops, something went wrong.

0 comments on commit e29c8c6

Please sign in to comment.