[700] | 1 | import org.hibernate.FetchMode as FM |
---|
| 2 | |
---|
[247] | 3 | class AssignedGroupService { |
---|
[251] | 4 | |
---|
[247] | 5 | boolean transactional = false |
---|
| 6 | |
---|
[291] | 7 | def authService |
---|
[251] | 8 | |
---|
[247] | 9 | def delete(params) { |
---|
[251] | 10 | AssignedGroup.withTransaction { status -> |
---|
| 11 | def result = [:] |
---|
[247] | 12 | |
---|
[251] | 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 | } |
---|
[247] | 20 | |
---|
[251] | 21 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
[247] | 22 | |
---|
[251] | 23 | if(!result.assignedGroupInstance) |
---|
| 24 | return fail(code:"default.not.found") |
---|
[247] | 25 | |
---|
[291] | 26 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
[251] | 27 | taskModificationType: TaskModificationType.get(10), |
---|
| 28 | task: result.assignedGroupInstance.task) |
---|
[247] | 29 | |
---|
[251] | 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 | |
---|
[247] | 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 | |
---|
[700] | 51 | result.assignedGroupInstance = AssignedGroup.createCriteria().get { |
---|
| 52 | idEq(params.id.toLong()) |
---|
| 53 | fetchMode("personGroup", FM.EAGER) |
---|
| 54 | fetchMode("personGroup.persons", FM.EAGER) |
---|
| 55 | } |
---|
[247] | 56 | |
---|
| 57 | if(!result.assignedGroupInstance) |
---|
| 58 | return fail(code:"default.not.found") |
---|
| 59 | |
---|
[254] | 60 | result.personGroup = result.assignedGroupInstance.personGroup |
---|
[256] | 61 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 62 | |
---|
[247] | 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 | |
---|
[700] | 79 | result.personGroup = PersonGroup.createCriteria().get { |
---|
| 80 | idEq(params.personGroup.id.toLong()) |
---|
| 81 | fetchMode("persons", FM.EAGER) |
---|
| 82 | } |
---|
[254] | 83 | |
---|
| 84 | // Prevent a null property reference. |
---|
| 85 | if(!result.personGroup) |
---|
| 86 | return fail(code:"personGroup.not.found") |
---|
| 87 | |
---|
[256] | 88 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 89 | |
---|
[247] | 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 | |
---|
[291] | 106 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
[251] | 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 | |
---|
[247] | 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 | |
---|
[256] | 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) } |
---|
[254] | 134 | |
---|
[256] | 135 | // Success. |
---|
[247] | 136 | return result |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | def save(params) { |
---|
[251] | 140 | AssignedGroup.withTransaction { status -> |
---|
| 141 | def result = [:] |
---|
[247] | 142 | |
---|
[251] | 143 | def fail = { Map m -> |
---|
[254] | 144 | status.setRollbackOnly() |
---|
[251] | 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 | } |
---|
[247] | 150 | |
---|
[700] | 151 | result.personGroup = PersonGroup.createCriteria().get { |
---|
| 152 | idEq(params.personGroup.id.toLong()) |
---|
| 153 | fetchMode("persons", FM.EAGER) |
---|
| 154 | } |
---|
[247] | 155 | |
---|
[254] | 156 | // Prevent a null property reference. |
---|
| 157 | if(!result.personGroup) |
---|
| 158 | return fail(code:"personGroup.not.found") |
---|
| 159 | |
---|
[700] | 160 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 161 | |
---|
| 162 | result.assignedGroupInstance = new AssignedGroup() |
---|
| 163 | result.assignedGroupInstance.properties = params |
---|
| 164 | |
---|
[251] | 165 | if(result.assignedGroupInstance.hasErrors() || !result.assignedGroupInstance.save()) |
---|
| 166 | return fail(code:"default.create.failure") |
---|
[247] | 167 | |
---|
[254] | 168 | // Record a taskModification for everyone except "system". |
---|
[291] | 169 | if(authService.currentUser.id != 1) { |
---|
| 170 | def taskModification = new TaskModification(person: authService.currentUser, |
---|
[251] | 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 | |
---|
[254] | 178 | // Success. |
---|
[251] | 179 | return result |
---|
| 180 | |
---|
| 181 | } //end withTransaction |
---|
| 182 | } // end save() |
---|
| 183 | |
---|
[254] | 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 | |
---|
[700] | 192 | if(!params.personGroup?.id?.isLong()) |
---|
| 193 | return fail(code:"default.not.found") |
---|
[254] | 194 | |
---|
[700] | 195 | result.personGroup = PersonGroup.createCriteria().get { |
---|
| 196 | idEq(params.personGroup.id.toLong()) |
---|
| 197 | fetchMode("persons", FM.EAGER) |
---|
| 198 | } |
---|
| 199 | |
---|
[254] | 200 | if(!result.personGroup) |
---|
| 201 | return fail(code:"default.not.found") |
---|
| 202 | |
---|
[256] | 203 | result.personsInGroup = result.personGroup.persons.sort { p1, p2 -> p1.firstName.compareToIgnoreCase(p2.firstName) } |
---|
[254] | 204 | |
---|
| 205 | // Success. |
---|
| 206 | return result |
---|
| 207 | } |
---|
| 208 | |
---|
[247] | 209 | } // end class |
---|