1 | package dbnp.studycapturing |
---|
2 | import dbnp.data.Term |
---|
3 | |
---|
4 | class ProtocolController { |
---|
5 | |
---|
6 | static allowedMethods = [save: "POST", update: "POST", delete: "POST"] |
---|
7 | |
---|
8 | |
---|
9 | def index = { |
---|
10 | redirect(action: "list", params: params) |
---|
11 | } |
---|
12 | |
---|
13 | def list = { |
---|
14 | params.max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
15 | [protocolInstanceList: Protocol.list(params), protocolInstanceTotal: Protocol.count()] |
---|
16 | } |
---|
17 | |
---|
18 | def create = { |
---|
19 | def protocolInstance = new Protocol() |
---|
20 | protocolInstance.properties = params |
---|
21 | return [protocolInstance: protocolInstance] |
---|
22 | } |
---|
23 | |
---|
24 | def save = { |
---|
25 | |
---|
26 | def protocolInstance = new Protocol(params) |
---|
27 | |
---|
28 | if (protocolInstance.save(flush: true)) { |
---|
29 | flash.message = "${message(code: 'default.created.message', args: [message(code: 'protocol.label', default: 'Protocol'), protocolInstance.id])}" |
---|
30 | redirect(action:show, id: protocolInstance.id) |
---|
31 | } |
---|
32 | else { |
---|
33 | render(view: "create", model: [protocolInstance: protocolInstance]) |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | def show = { |
---|
38 | |
---|
39 | println params |
---|
40 | |
---|
41 | def protocolInstance = Protocol.get(params.id) |
---|
42 | if (!protocolInstance) { |
---|
43 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'protocol.label', default: 'Protocol'), params.id])}" |
---|
44 | redirect(action: "list") |
---|
45 | } |
---|
46 | else { [protocolInstance: protocolInstance] } |
---|
47 | } |
---|
48 | |
---|
49 | def edit = { |
---|
50 | def protocolInstance = Protocol.get(params.id) |
---|
51 | if (!protocolInstance) { |
---|
52 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'protocol.label', default: 'Protocol'), params.id])}" |
---|
53 | redirect(action: "list") |
---|
54 | } |
---|
55 | else { |
---|
56 | return [protocolInstance: protocolInstance] |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | def update = { |
---|
61 | def protocolInstance = Protocol.get(params.id) |
---|
62 | if (protocolInstance) { |
---|
63 | if (params.version) { |
---|
64 | def version = params.version.toLong() |
---|
65 | if (protocolInstance.version > version) { |
---|
66 | |
---|
67 | protocolInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'protocol.label', default: 'Protocol')] as Object[], "Another user has updated this Protocol while you were editing") |
---|
68 | render(view: "edit", model: [protocolInstance: protocolInstance]) |
---|
69 | return |
---|
70 | } |
---|
71 | } |
---|
72 | protocolInstance.properties = params |
---|
73 | if (!protocolInstance.hasErrors() && protocolInstance.save(flush: true)) { |
---|
74 | flash.message = "${message(code: 'default.updated.message', args: [message(code: 'protocol.label', default: 'Protocol'), protocolInstance.id])}" |
---|
75 | redirect(action: "show", id: protocolInstance.id) |
---|
76 | } |
---|
77 | else { |
---|
78 | render(view: "edit", model: [protocolInstance: protocolInstance]) |
---|
79 | } |
---|
80 | } |
---|
81 | else { |
---|
82 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'protocol.label', default: 'Protocol'), params.id])}" |
---|
83 | redirect(action: "list") |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | def delete = { |
---|
88 | def protocolInstance = Protocol.get(params.id) |
---|
89 | if (protocolInstance) { |
---|
90 | try { |
---|
91 | protocolInstance.delete(flush: true) |
---|
92 | flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'protocol.label', default: 'Protocol'), params.id])}" |
---|
93 | redirect(action: "list") |
---|
94 | } |
---|
95 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
96 | flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'protocol.label', default: 'Protocol'), params.id])}" |
---|
97 | redirect(action: "show", id: params.id) |
---|
98 | } |
---|
99 | } |
---|
100 | else { |
---|
101 | flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'protocol.label', default: 'Protocol'), params.id])}" |
---|
102 | redirect(action: "list") |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | def createDummies = |
---|
107 | { |
---|
108 | def newInstance = new Protocol() |
---|
109 | newInstance.name = "Hugo (dummy #1)" |
---|
110 | newInstance.reference = Term.find("from Term t") |
---|
111 | |
---|
112 | if( newInstance.save() ) |
---|
113 | { redirect( action:show, id: newInstance.id ) } |
---|
114 | else { chain( action:list ) } |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|