Package javango.polls

Source Code of javango.polls.Views

package javango.polls;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javango.contrib.freemarker.Helper.renderToResponse;

import com.google.inject.Inject;

import javango.polls.forms.VoteForm;

import javango.contrib.compat.Javango;
import javango.db.Manager;
import javango.db.ManagerException;
import javango.polls.model.Choice;
import javango.polls.model.Poll;
import javango.http.HttpException;
import javango.http.HttpRequest;
import javango.http.HttpResponse;
import javango.http.HttpResponseRedirect;

public class Views {
  Javango javango;
 
  @Inject
  public Views(Javango javango) {
    super();
    this.javango = javango;
  }

  /**
   *
   * @param request
   * @param response
   * @return
   */
  public HttpResponse index(HttpRequest request) throws HttpException {
    try {
      Manager<Poll> polls = javango.newManager(Poll.class);
      List<Poll> latest_poll_list = polls.all().list();
     
      Map<String, Object> context = new HashMap<String, Object>();
      context.put("latest_poll_list", latest_poll_list);
     
      return renderToResponse("javango/polls/templates/index.ftl", context);
     
    } catch (ManagerException e) {
      throw new HttpException(e);
    }
  }
 
  public HttpResponse detail(HttpRequest request, Long poll_id) throws HttpException {
    Poll p = javango.getObjectOr404(Poll.class,poll_id);
   
    VoteForm form = javango.newForm(VoteForm.class).setPoll(p);
   
    Map<String, Object> context = new HashMap<String, Object>();
    context.put("poll", p);
    context.put("form", form);
                 
    return renderToResponse("javango/polls/templates/detail.ftl", context);
  }

  public HttpResponse results(HttpRequest request, Long poll_id) throws HttpException {
    Poll p = javango.getObjectOr404(Poll.class, poll_id);
   
    Map<String, Object> context = new HashMap<String, Object>();
    context.put("poll", p);
   
    return renderToResponse("javango/polls/templates/results.ftl", context);

  }
 
  public HttpResponse vote(HttpRequest request, Long poll_id) throws HttpException {
    Poll p = javango.getObjectOr404(Poll.class, poll_id);
   
    VoteForm form = javango.newForm(VoteForm.class).setPoll(p);
    form.bind(request.getParameterMap());     
   
    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("javango/polls/templates/detail.ftl", context);
    }
   
    Choice selected_choice = (Choice)form.getCleanedValue(form.choice);

    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/");
  }
}
TOP

Related Classes of javango.polls.Views

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.