1 | <%=packageName ? "package ${packageName}\n\n" : ''%>class ${className}Controller { |
---|
2 | |
---|
3 | static allowedMethods = [save: "POST", update: "POST", delete: "POST"] |
---|
4 | |
---|
5 | def index = { |
---|
6 | redirect(action: "list", params: params) |
---|
7 | } |
---|
8 | |
---|
9 | def list = { |
---|
10 | params.max = Math.min(params.max ? params.int('max') : 10, 100) |
---|
11 | [${propertyName}List: ${className}.list(params), ${propertyName}Total: ${className}.count()] |
---|
12 | } |
---|
13 | |
---|
14 | def create = { |
---|
15 | def ${propertyName} = new ${className}() |
---|
16 | ${propertyName}.properties = params |
---|
17 | return [${propertyName}: ${propertyName}] |
---|
18 | } |
---|
19 | |
---|
20 | def save = { |
---|
21 | def ${propertyName} = new ${className}(params) |
---|
22 | if (${propertyName}.save(flush: true)) { |
---|
23 | flash.message = "\${message(code: 'default.created.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}" |
---|
24 | redirect(action: "show", id: ${propertyName}.id) |
---|
25 | } |
---|
26 | else { |
---|
27 | render(view: "create", model: [${propertyName}: ${propertyName}]) |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | def show = { |
---|
32 | def ${propertyName} = ${className}.get(params.id) |
---|
33 | if (!${propertyName}) { |
---|
34 | flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}" |
---|
35 | redirect(action: "list") |
---|
36 | } |
---|
37 | else { |
---|
38 | [${propertyName}: ${propertyName}] |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | def edit = { |
---|
43 | def ${propertyName} = ${className}.get(params.id) |
---|
44 | if (!${propertyName}) { |
---|
45 | flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}" |
---|
46 | redirect(action: "list") |
---|
47 | } |
---|
48 | else { |
---|
49 | return [${propertyName}: ${propertyName}] |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | def update = { |
---|
54 | def ${propertyName} = ${className}.get(params.id) |
---|
55 | if (${propertyName}) { |
---|
56 | if (params.version) { |
---|
57 | def version = params.version.toLong() |
---|
58 | if (${propertyName}.version > version) { |
---|
59 | <% def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className) %> |
---|
60 | ${propertyName}.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: '${domainClass.propertyName}.label', default: '${className}')] as Object[], "Another user has updated this ${className} while you were editing") |
---|
61 | render(view: "edit", model: [${propertyName}: ${propertyName}]) |
---|
62 | return |
---|
63 | } |
---|
64 | } |
---|
65 | ${propertyName}.properties = params |
---|
66 | if (!${propertyName}.hasErrors() && ${propertyName}.save(flush: true)) { |
---|
67 | flash.message = "\${message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])}" |
---|
68 | redirect(action: "show", id: ${propertyName}.id) |
---|
69 | } |
---|
70 | else { |
---|
71 | render(view: "edit", model: [${propertyName}: ${propertyName}]) |
---|
72 | } |
---|
73 | } |
---|
74 | else { |
---|
75 | flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}" |
---|
76 | redirect(action: "list") |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | def delete = { |
---|
81 | def ${propertyName} = ${className}.get(params.id) |
---|
82 | if (${propertyName}) { |
---|
83 | try { |
---|
84 | ${propertyName}.delete(flush: true) |
---|
85 | flash.message = "\${message(code: 'default.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}" |
---|
86 | redirect(action: "list") |
---|
87 | } |
---|
88 | catch (org.springframework.dao.DataIntegrityViolationException e) { |
---|
89 | flash.message = "\${message(code: 'default.not.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}" |
---|
90 | redirect(action: "show", id: params.id) |
---|
91 | } |
---|
92 | } |
---|
93 | else { |
---|
94 | flash.message = "\${message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])}" |
---|
95 | redirect(action: "list") |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|