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