package com.sheepdog.mashmesh.resources;

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.sheepdog.mashmesh.util.CalendarUtils;
import org.joda.time.DateTime;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import java.io.IOException;

import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;

@Path("/eventNotification")
public class SendEventNotificationResource {
    private static final String QUEUE_NAME = "notifications";

    @POST
    @Produces({MediaType.TEXT_PLAIN})
    public String scheduleNotification(@FormParam("patientEmail") String patientEmail,
                                       @FormParam("calendarId") String calendarId,
                                       @FormParam("eventId") String eventId) throws IOException {
        Calendar calendar = CalendarUtils.getCalendar(patientEmail);
        Event event = calendar.events().get(calendarId, eventId).execute();
        String appointmentTime = event.getStart().getDateTime().toStringRfc3339();

        // TODO: Validate the email address before passing off to the queue.

        TaskOptions task = withUrl("/tasks/notification/")
                .method(TaskOptions.Method.GET)
                .param("patientEmail", patientEmail)
                .param("appointmentAddress", event.getLocation())
                .param("appointmentTime", appointmentTime);

        Queue queue = QueueFactory.getQueue(QUEUE_NAME);
        queue.add(task);

        return "OK";
    }
}

@@ -35,16 +35,90 @@
 urces;%0A%0A
+import com.google.api.client.googleapis.json.GoogleJsonResponseException;%0A
 import c
@@ -159,16 +159,16 @@
 lendar;%0A
-
 import c
@@ -372,16 +372,65 @@
 ptions;%0A
+import com.sheepdog.mashmesh.models.UserProfile;%0A
 import c
@@ -558,16 +558,50 @@
 diaType;
+%0Aimport javax.ws.rs.core.Response;
 %0A%0Aimport
@@ -838,16 +838,1392 @@
 ions%22;%0A%0A
+    private void verifyPatientEmail(String patientEmail) %7B%0A        UserProfile patientProfile = UserProfile.getByEmail(patientEmail);%0A        if (patientProfile == null %7C%7C patientProfile.getType() != UserProfile.UserType.PATIENT) %7B%0A            Response response = Response.status(Response.Status.NOT_FOUND)%0A                    .entity(%22No such patient: %22 + patientEmail)%0A                    .build();%0A            throw new WebApplicationException(response);%0A        %7D%0A    %7D%0A%0A    private Event getCalendarEvent(String patientEmail, String calendarId, String eventId) %7B%0A        Calendar calendar = CalendarUtils.getCalendar(patientEmail);%0A        try %7B%0A            return calendar.events().get(calendarId, eventId).execute();%0A        %7D catch (GoogleJsonResponseException e) %7B%0A            if (e.getDetails().getCode() == 404) %7B%0A                String message = String.format(%22No such calendar event: calendarId=%25s, eventId=%25s%22,%0A                        calendarId, eventId);%0A                Response response = Response.status(Response.Status.NOT_FOUND)%0A                        .entity(message)%0A                        .build();%0A                throw new WebApplicationException(response);%0A            %7D else %7B%0A                throw new WebApplicationException(e);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new WebApplicationException(e);%0A        %7D%0A    %7D%0A%0A
     @POS
@@ -2542,53 +2542,26 @@
     
-Calendar calendar = CalendarUtils.getCalendar
+verifyPatientEmail
 (pat
@@ -2594,38 +2594,47 @@
 event = 
-c
+getC
 alendar
-.e
+E
 vent
-s().get(
+(patientEmail, 
 calendar
@@ -2641,34 +2641,24 @@
 Id, eventId)
-.execute()
 ;%0A        St
@@ -2735,86 +2735,8 @@
 );%0A%0A
-        // TODO: Validate the email address before passing off to the queue.%0A%0A
     

Add validation to calendar notification resource