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