[247] | 1 | class AssignedGroupService { |
---|
[251] | 2 | |
---|
[247] | 3 | boolean transactional = false |
---|
| 4 | |
---|
[291] | 5 | def authService |
---|
[251] | 6 | |
---|
[247] | 7 | def delete(params) { |
---|
[251] | 8 | AssignedGroup.withTransaction { status -> |
---|
| 9 | def result = [:] |
---|
[247] | 10 | |
---|
[251] | 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 | } |
---|
[247] | 18 | |
---|
[251] | 19 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
[247] | 20 | |
---|
[251] | 21 | if(!result.assignedGroupInstance) |
---|
| 22 | return fail(code:"default.not.found") |
---|
[247] | 23 | |
---|
[291] | 24 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
[251] | 25 | taskModificationType: TaskModificationType.get(10), |
---|
| 26 | task: result.assignedGroupInstance.task) |
---|
[247] | 27 | |
---|
[251] | 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 | |
---|
[247] | 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 | |
---|
[254] | 54 | result.personGroup = result.assignedGroupInstance.personGroup |
---|
[256] | 55 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 56 | |
---|
[247] | 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 | |
---|
[254] | 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 | |
---|
[256] | 79 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 80 | |
---|
[247] | 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 | |
---|
[291] | 97 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
[251] | 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 | |
---|
[247] | 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 | |
---|
[256] | 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) } |
---|
[254] | 125 | |
---|
[256] | 126 | // Success. |
---|
[247] | 127 | return result |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | def save(params) { |
---|
[251] | 131 | AssignedGroup.withTransaction { status -> |
---|
| 132 | def result = [:] |
---|
[247] | 133 | |
---|
[251] | 134 | def fail = { Map m -> |
---|
[254] | 135 | status.setRollbackOnly() |
---|
[251] | 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 | } |
---|
[247] | 141 | |
---|
[254] | 142 | result.personGroup = PersonGroup.get(params.personGroup.id) |
---|
[247] | 143 | |
---|
[254] | 144 | // Prevent a null property reference. |
---|
| 145 | if(!result.personGroup) |
---|
| 146 | return fail(code:"personGroup.not.found") |
---|
| 147 | |
---|
[514] | 148 | result.personsInGroup = result.personGroup.persons?.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 149 | |
---|
| 150 | result.assignedGroupInstance = new AssignedGroup() |
---|
| 151 | result.assignedGroupInstance.properties = params |
---|
| 152 | |
---|
[251] | 153 | if(result.assignedGroupInstance.hasErrors() || !result.assignedGroupInstance.save()) |
---|
| 154 | return fail(code:"default.create.failure") |
---|
[247] | 155 | |
---|
[254] | 156 | // Record a taskModification for everyone except "system". |
---|
[291] | 157 | if(authService.currentUser.id != 1) { |
---|
| 158 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
[251] | 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 | |
---|
[254] | 166 | // Success. |
---|
[251] | 167 | return result |
---|
| 168 | |
---|
| 169 | } //end withTransaction |
---|
| 170 | } // end save() |
---|
| 171 | |
---|
[254] | 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 | |
---|
[256] | 185 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 186 | |
---|
| 187 | // Success. |
---|
| 188 | return result |
---|
| 189 | } |
---|
| 190 | |
---|
[247] | 191 | } // end class |
---|