package com.sahuaro.software;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.googlecode.objectify.Key;
import com.sahuaro.captcha.ReCaptchaUtils;
import com.sahuaro.domain.Message;
import com.sahuaro.domain.Prospect;
import com.sahuaro.domain.Suggestion;
import com.sahuaro.interfaces.IMessageRepository;
import com.sahuaro.interfaces.IProspectRepository;
import com.sahuaro.interfaces.ISuggestionRepository;
import com.sahuaro.mail.EmailSender;
import com.sahuaro.repositories.MessageRepository;
import com.sahuaro.repositories.ProspectRepository;
import com.sahuaro.repositories.SuggestionRepository;
@SuppressWarnings("serial")
public class SuggestionServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
// Evita continuar si la validación del captcha no fue exitosa.
if (!ReCaptchaUtils.checkRecaptchaResponse(req)) {
System.out.println("Recaptcha validation failed.");
// TODO: Redirigir o otra página o algo asi.
resp.sendRedirect("/suggestions.jsp");
return;
}
//get all parameters from the post
String firstName = req.getParameter("firstname");
String lastName = req.getParameter("lastname");
String email = req.getParameter("email");
String message = req.getParameter("message");
IProspectRepository prospectRepo = new ProspectRepository();
IMessageRepository messageRepo = new MessageRepository();
ISuggestionRepository suggRepo = new SuggestionRepository();
//creates a prospect
Prospect prospect = new Prospect(firstName, lastName, email);
Key<Prospect> prospKey = prospectRepo.save(prospect);
//creates a contact
Message messageObj = new Message(message, prospKey.getId());
Key<Message> msgKey = messageRepo.save(messageObj);
Suggestion suggestion = new Suggestion(message, msgKey.getId());
Key<Suggestion> suggKey = suggRepo.save(suggestion);
//creates an EmailSender instance
EmailSender<Suggestion> sender = new EmailSender<Suggestion>(suggestion);
sender.sendEmail(email, message, prospect);
resp.sendRedirect("/about.jsp");
}
}