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