package javango.polls;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static javango.contrib.galoot.Helper.renderToResponse;
import org.apache.log4j.Logger;
import com.google.inject.Inject;
import javango.polls.forms.VoteForm;
import javango.contrib.hibernate.DaoException;
import javango.contrib.hibernate.ModelFactory;
import javango.contrib.hibernate.ModelDao;
import javango.polls.model.Choice;
import javango.polls.model.Poll;
import javango.forms.FormFactory;
import javango.http.HttpException;
import javango.http.HttpRequest;
import javango.http.HttpResponse;
import javango.http.HttpResponseRedirect;
import javango.http.SimpleHttpResponse;
public class Views {
private final static Logger log = Logger.getLogger(Views.class);
/**
*
* @param request
* @param response
* @return
*/
public HttpResponse index(HttpRequest request) throws HttpException {
try {
ModelDao<Poll> dao = new ModelDao<Poll>(Poll.class);
List<Poll> latest_poll_list = dao.filter().list();
Map<String, Object> context = new HashMap<String, Object>();
context.put("latest_poll_list", latest_poll_list);
return renderToResponse("src/main/webapp/templates/index.html", context);
} catch (DaoException e) {
throw new HttpException(e);
}
}
public HttpResponse detail(HttpRequest request, Long poll_id) throws HttpException {
try {
Poll p = modelFactory.dao(Poll.class).get(poll_id);
if (p == null) {
return new SimpleHttpResponse("Unable to find poll with id = " + poll_id);
}
VoteForm form = (VoteForm)formFactory.newForm(VoteForm.class);
form.updateChoices(p);
Map<String, Object> context = new HashMap<String, Object>();
context.put("poll", p);
context.put("form", form);
return renderToResponse("src/main/webapp/templates/detail.html", context);
} catch (DaoException e) {
throw new HttpException(e);
}
}
public HttpResponse results(HttpRequest request, Long poll_id) throws HttpException {
try {
Poll p = modelFactory.dao(Poll.class).get(poll_id);
Map<String, Object> context = new HashMap<String, Object>();
context.put("poll", p);
return renderToResponse("src/main/webapp/templates/results.html", context);
} catch (DaoException e) {
throw new HttpException(e);
}
}
public HttpResponse vote(HttpRequest request, Long poll_id) throws HttpException {
try {
Poll p = modelFactory.dao(Poll.class).get(poll_id);
VoteForm form = (VoteForm)formFactory.newForm(VoteForm.class).bind(request.getParameterMap());
form.updateChoices(p);
if (!form.isValid()) {
Map<String, Object> context = new HashMap<String, Object>();
context.put("poll", p);
context.put("error_message", "You didn't select a valid choice");
context.put("form", form);
return renderToResponse("src/main/webapp/templates/detail.html", context);
}
Long choice = (Long)form.getCleanedData().get("choice");
Choice selected_choice = modelFactory.dao(Choice.class).get(choice);
if (selected_choice == null) {
Map<String, Object> context = new HashMap<String, Object>();
context.put("poll", p);
context.put("error_message", "You didn't select a valid choice");
context.put("form", form);
return renderToResponse("src/main/webapp/templates/detail.html", context);
} else {
int votes = selected_choice.getVotes() == null ? 0 : selected_choice.getVotes().intValue();
selected_choice.setVotes(votes + 1);
// due to the way hibernate works this will automatically save when the middleware commits
return new HttpResponseRedirect("/polls/" + poll_id + "/results/");
}
} catch (DaoException e) {
throw new HttpException(e);
}
}
}