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