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