| 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
|---|
| 2 | |
|---|
| 3 | class TaskDetailedController extends BaseController { |
|---|
| 4 | |
|---|
| 5 | def index = { redirect(action:list,params:params) } |
|---|
| 6 | |
|---|
| 7 | // the delete, save and update actions only accept POST requests |
|---|
| 8 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
|---|
| 9 | |
|---|
| 10 | def list = { |
|---|
| 11 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
|---|
| 12 | [ taskInstanceList: Task.list( params ), taskInstanceTotal: Task.count() ] |
|---|
| 13 | } |
|---|
| 14 | |
|---|
| 15 | def show = { |
|---|
| 16 | def taskInstance = Task.get( params.id ) |
|---|
| 17 | |
|---|
| 18 | if(!taskInstance) { |
|---|
| 19 | flash.message = "Task not found with id ${params.id}" |
|---|
| 20 | redirect(action:list) |
|---|
| 21 | } |
|---|
| 22 | else { return [ taskInstance : taskInstance ] } |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | def delete = { |
|---|
| 26 | def taskInstance = Task.get( params.id ) |
|---|
| 27 | if(taskInstance) { |
|---|
| 28 | try { |
|---|
| 29 | taskInstance.delete() |
|---|
| 30 | flash.message = "Task ${params.id} deleted" |
|---|
| 31 | redirect(action:list) |
|---|
| 32 | } |
|---|
| 33 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
|---|
| 34 | flash.message = "Task ${params.id} could not be deleted" |
|---|
| 35 | redirect(action:show,id:params.id) |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | else { |
|---|
| 39 | flash.message = "Task not found with id ${params.id}" |
|---|
| 40 | redirect(action:list) |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | def edit = { |
|---|
| 45 | def taskInstance = Task.get( params.id ) |
|---|
| 46 | |
|---|
| 47 | if(!taskInstance) { |
|---|
| 48 | flash.message = "Task not found with id ${params.id}" |
|---|
| 49 | redirect(action:list) |
|---|
| 50 | } |
|---|
| 51 | else { |
|---|
| 52 | def criteria = taskInstance.createCriteria() |
|---|
| 53 | def results = criteria { |
|---|
| 54 | and { |
|---|
| 55 | notEqual('id', taskInstance.id) |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | return [ taskInstance : taskInstance, possibleParentList: results ] |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | def update = { |
|---|
| 63 | def taskInstance = Task.get( params.id ) |
|---|
| 64 | if(taskInstance) { |
|---|
| 65 | if(params.version) { |
|---|
| 66 | def version = params.version.toLong() |
|---|
| 67 | if(taskInstance.version > version) { |
|---|
| 68 | |
|---|
| 69 | taskInstance.errors.rejectValue("version", "task.optimistic.locking.failure", "Another user has updated this Task while you were editing.") |
|---|
| 70 | render(view:'edit',model:[taskInstance:taskInstance]) |
|---|
| 71 | return |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | taskInstance.properties = params |
|---|
| 75 | if(!taskInstance.hasErrors() && taskInstance.save()) { |
|---|
| 76 | flash.message = "Task ${params.id} updated" |
|---|
| 77 | redirect(action:show,id:taskInstance.id) |
|---|
| 78 | } |
|---|
| 79 | else { |
|---|
| 80 | render(view:'edit',model:[taskInstance:taskInstance]) |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | else { |
|---|
| 84 | flash.message = "Task not found with id ${params.id}" |
|---|
| 85 | redirect(action:edit,id:params.id) |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | def create = { |
|---|
| 90 | def taskInstance = new Task() |
|---|
| 91 | taskInstance.properties = params |
|---|
| 92 | return ['taskInstance':taskInstance] |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | def save = { |
|---|
| 96 | def taskInstance = new Task(params) |
|---|
| 97 | if(!taskInstance.hasErrors() && taskInstance.save()) { |
|---|
| 98 | flash.message = "Task ${taskInstance.id} created" |
|---|
| 99 | redirect(action:show,id:taskInstance.id) |
|---|
| 100 | } |
|---|
| 101 | else { |
|---|
| 102 | render(view:'create',model:[taskInstance:taskInstance]) |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|