/**
 *    Copyright 2013 Talend Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.sheepdog.mashmesh.resources;

import com.sheepdog.mashmesh.models.RideRequest;
import com.sheepdog.mashmesh.tasks.SendNotificationTask;
import org.joda.time.DateTime;

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

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

    @POST
    @Produces({MediaType.TEXT_PLAIN})
    public String scheduleNotification(@FormParam("patientEmail") String patientEmail,
                                       @FormParam("appointmentAddress") String appointmentAddress,
                                       @FormParam("appointmentTime") String appointmentTimeRfc339) {
        if (patientEmail == null || patientEmail.isEmpty() ||
                appointmentAddress == null || appointmentAddress.isEmpty() ||
                appointmentTimeRfc339 == null || appointmentTimeRfc339.isEmpty()) {
            // TODO: Actually report errors with bean validation.
            throw new WebApplicationException(400);
        }

        DateTime appointmentTime = DateTime.parse(appointmentTimeRfc339);
        RideRequest request = SendNotificationTask.createRequest(patientEmail, appointmentAddress, appointmentTime);
        SendNotificationTask.scheduleRequest(request);

        return "OK";
    }
}

/**
 *    Copyright 2013 Talend Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.sheepdog.mashmesh.resources;

import com.sheepdog.mashmesh.models.RideRequest;
import com.sheepdog.mashmesh.models.UserProfile;
import com.sheepdog.mashmesh.tasks.SendNotificationTask;
import org.joda.time.DateTime;

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

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

    private void verifyPatientEmail(String patientEmail) {
        UserProfile patientProfile = UserProfile.getByEmail(patientEmail);
        if (patientProfile == null || patientProfile.getType() != UserProfile.UserType.PATIENT) {
            Response response = Response.status(Response.Status.NOT_FOUND)
                    .entity("No such patient: " + patientEmail)
                    .build();
            throw new WebApplicationException(response);
        }
    }

    @POST
    @Produces({MediaType.TEXT_PLAIN})
    public String scheduleNotification(@FormParam("patientEmail") String patientEmail,
                                       @FormParam("appointmentAddress") String appointmentAddress,
                                       @FormParam("appointmentTime") String appointmentTimeRfc339) {
        if (patientEmail == null || patientEmail.isEmpty() ||
                appointmentAddress == null || appointmentAddress.isEmpty() ||
                appointmentTimeRfc339 == null || appointmentTimeRfc339.isEmpty()) {
            // TODO: Actually report errors with bean validation.
            throw new WebApplicationException(400);
        }

        verifyPatientEmail(patientEmail);

        DateTime appointmentTime = DateTime.parse(appointmentTimeRfc339);
        RideRequest request = SendNotificationTask.createRequest(patientEmail, appointmentAddress, appointmentTime);
        SendNotificationTask.scheduleRequest(request);

        return "OK";
    }
}

Verify patient addresses when they are received