1 | class AssignedGroupService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def personService |
---|
6 | |
---|
7 | def delete(params) { |
---|
8 | AssignedGroup.withTransaction { status -> |
---|
9 | def result = [:] |
---|
10 | |
---|
11 | def fail = { Map m -> |
---|
12 | status.setRollbackOnly() |
---|
13 | if(result.assignedGroupInstance && m.field) |
---|
14 | result.assignedGroupInstance.errors.rejectValue(m.field, m.code) |
---|
15 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
16 | return result |
---|
17 | } |
---|
18 | |
---|
19 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
20 | |
---|
21 | if(!result.assignedGroupInstance) |
---|
22 | return fail(code:"default.not.found") |
---|
23 | |
---|
24 | def taskModification = new TaskModification(person: personService.currentUser, |
---|
25 | taskModificationType: TaskModificationType.get(10), |
---|
26 | task: result.assignedGroupInstance.task) |
---|
27 | |
---|
28 | if(!taskModification.save()) |
---|
29 | return fail(field:"taskModifications", code:"task.modifications.failedToSave") |
---|
30 | |
---|
31 | try { |
---|
32 | result.assignedGroupInstance.delete(flush:true) |
---|
33 | return result //Success. |
---|
34 | } |
---|
35 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
36 | return fail(code:"default.delete.failure") |
---|
37 | } |
---|
38 | |
---|
39 | } //end withTransaction |
---|
40 | } // end delete() |
---|
41 | |
---|
42 | def edit(params) { |
---|
43 | def result = [:] |
---|
44 | def fail = { Map m -> |
---|
45 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
46 | return result |
---|
47 | } |
---|
48 | |
---|
49 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
50 | |
---|
51 | if(!result.assignedGroupInstance) |
---|
52 | return fail(code:"default.not.found") |
---|
53 | |
---|
54 | result.personGroup = result.assignedGroupInstance.personGroup |
---|
55 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
56 | |
---|
57 | // Success. |
---|
58 | return result |
---|
59 | } |
---|
60 | |
---|
61 | def update(params) { |
---|
62 | AssignedGroup.withTransaction { status -> |
---|
63 | def result = [:] |
---|
64 | |
---|
65 | def fail = { Map m -> |
---|
66 | status.setRollbackOnly() |
---|
67 | if(result.assignedGroupInstance && m.field) |
---|
68 | result.assignedGroupInstance.errors.rejectValue(m.field, m.code) |
---|
69 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
70 | return result |
---|
71 | } |
---|
72 | |
---|
73 | result.personGroup = PersonGroup.get(params.personGroup.id) |
---|
74 | |
---|
75 | // Prevent a null property reference. |
---|
76 | if(!result.personGroup) |
---|
77 | return fail(code:"personGroup.not.found") |
---|
78 | |
---|
79 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
80 | |
---|
81 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
82 | |
---|
83 | if(!result.assignedGroupInstance) |
---|
84 | return fail(code:"default.not.found") |
---|
85 | |
---|
86 | // Optimistic locking check. |
---|
87 | if(params.version) { |
---|
88 | if(result.assignedGroupInstance.version > params.version.toLong()) |
---|
89 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
90 | } |
---|
91 | |
---|
92 | result.assignedGroupInstance.properties = params |
---|
93 | |
---|
94 | if(result.assignedGroupInstance.hasErrors() || !result.assignedGroupInstance.save()) |
---|
95 | return fail(code:"default.update.failure") |
---|
96 | |
---|
97 | def taskModification = new TaskModification(person: personService.currentUser, |
---|
98 | taskModificationType: TaskModificationType.get(10), |
---|
99 | task: result.assignedGroupInstance.task) |
---|
100 | |
---|
101 | if(!taskModification.save()) |
---|
102 | return fail(field:"taskModifications", code:"task.modifications.failedToSave") |
---|
103 | |
---|
104 | // Success. |
---|
105 | return result |
---|
106 | |
---|
107 | } //end withTransaction |
---|
108 | } // end update() |
---|
109 | |
---|
110 | def create(params) { |
---|
111 | def result = [:] |
---|
112 | def fail = { Map m -> |
---|
113 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
114 | return result |
---|
115 | } |
---|
116 | |
---|
117 | if(!params.task?.id) |
---|
118 | fail(code:"assignedGroup.task.not.found") |
---|
119 | |
---|
120 | result.assignedGroupInstance = new AssignedGroup() |
---|
121 | result.assignedGroupInstance.properties = params |
---|
122 | |
---|
123 | result.personGroup = PersonGroup.list().sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }[0] // Get the first result. |
---|
124 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
125 | |
---|
126 | // Success. |
---|
127 | return result |
---|
128 | } |
---|
129 | |
---|
130 | def save(params) { |
---|
131 | AssignedGroup.withTransaction { status -> |
---|
132 | def result = [:] |
---|
133 | |
---|
134 | def fail = { Map m -> |
---|
135 | status.setRollbackOnly() |
---|
136 | if(result.assignedGroupInstance && m.field) |
---|
137 | result.assignedGroupInstance.errors.rejectValue(m.field, m.code) |
---|
138 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
139 | return result |
---|
140 | } |
---|
141 | |
---|
142 | result.personGroup = PersonGroup.get(params.personGroup.id) |
---|
143 | |
---|
144 | // Prevent a null property reference. |
---|
145 | if(!result.personGroup) |
---|
146 | return fail(code:"personGroup.not.found") |
---|
147 | |
---|
148 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
149 | |
---|
150 | result.assignedGroupInstance = new AssignedGroup() |
---|
151 | result.assignedGroupInstance.properties = params |
---|
152 | |
---|
153 | if(result.assignedGroupInstance.hasErrors() || !result.assignedGroupInstance.save()) |
---|
154 | return fail(code:"default.create.failure") |
---|
155 | |
---|
156 | // Record a taskModification for everyone except "system". |
---|
157 | if(personService.currentUser.id != 1) { |
---|
158 | def taskModification = new TaskModification(person: personService.currentUser, |
---|
159 | taskModificationType: TaskModificationType.get(10), |
---|
160 | task: result.assignedGroupInstance.task) |
---|
161 | |
---|
162 | if(!taskModification.save()) |
---|
163 | return fail(field:"taskModifications", code:"task.modifications.failedToSave") |
---|
164 | } |
---|
165 | |
---|
166 | // Success. |
---|
167 | return result |
---|
168 | |
---|
169 | } //end withTransaction |
---|
170 | } // end save() |
---|
171 | |
---|
172 | def personsInGroup(params) { |
---|
173 | def result = [:] |
---|
174 | |
---|
175 | def fail = { Map m -> |
---|
176 | result.error = [ code: m.code, args: ["PersonGroup", params.personGroup?.id] ] |
---|
177 | return result |
---|
178 | } |
---|
179 | |
---|
180 | result.personGroup = PersonGroup.get(params.personGroup?.id) |
---|
181 | |
---|
182 | if(!result.personGroup) |
---|
183 | return fail(code:"default.not.found") |
---|
184 | |
---|
185 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
186 | |
---|
187 | // Success. |
---|
188 | return result |
---|
189 | } |
---|
190 | |
---|
191 | } // end class |
---|