package app.controllers; import java.util.List; import javax.persistence.EntityManager; import app.models.<%= class_name %>; import br.com.caelum.vraptor.Delete; import br.com.caelum.vraptor.Get; import br.com.caelum.vraptor.Path; import br.com.caelum.vraptor.Post; import br.com.caelum.vraptor.Put; import br.com.caelum.vraptor.Resource; import br.com.caelum.vraptor.Result; import br.com.caelum.vraptor.Validator; @Resource public class <%= controller_class_name %> { private final Result result; private final EntityManager entityManager; private final Validator validator; public <%= controller_class_name %>(Result result, EntityManager entityManager, Validator validator) { this.entityManager = entityManager; this.result = result; this.validator = validator; } @Get @Path("<%= path %>") public List<<%= class_name %>> index() { return entityManager.createQuery("from <%= class_name %>").getResultList(); } @Post @Path("<%= path %>") public void create(<%= class_name %> <%= @model %>) { validator.validate(<%= @model %>); validator.onErrorForwardTo(this).form(<%= @model %>); entityManager.persist(<%= @model %>); result.redirectTo(this).index(); } @Get @Path("<%= "#{path}/new" %>") public void new<%= class_name %>() { result.forwardTo(this).form(new <%= class_name %>()); } @Put @Path("<%= path %>") public void update(<%= class_name %> <%= @model %>) { validator.validate(<%= @model %>); validator.onErrorForwardTo(this).form(<%= @model %>); entityManager.merge(<%= @model %>); result.redirectTo(this).index(); } @Get @Path("<%= "#{path}/{#{@model}.id}/edit" %>") public void edit(<%= @model.capitalize %> <%= @model %>) { result.forwardTo(this).form(entityManager.find(<%= @model.capitalize %>.class, <%= @model %>.getId())); } @Get @Path("<%= "#{path}/{#{@model}.id}/show" %>") public <%= class_name %> show(<%= @model.capitalize %> <%= @model %>) { return entityManager.find(<%= @model.capitalize %>.class, <%= @model %>.getId()); } @Delete @Path("<%= path %>") public void destroy(<%= class_name %> <%= @model %>) { entityManager.remove(entityManager.find(<%= class_name %>.class, <%= @model %>.getId())); result.redirectTo(this).index(); } public <%= class_name %> form(<%= class_name %> <%= @model %>) { return <%= @model %>; } }