[247] | 1 | class AssignedGroupService { |
---|
| 2 | boolean transactional = false |
---|
| 3 | |
---|
| 4 | def list(params) { |
---|
| 5 | def result = [:] |
---|
| 6 | def fail = { Map m -> |
---|
| 7 | result.error = [ code: m.code, args: ["AssignedGroup"] ] |
---|
| 8 | return result |
---|
| 9 | } |
---|
| 10 | |
---|
| 11 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
| 12 | result.assignedGroupInstanceList = AssignedGroup.list(params) |
---|
| 13 | result.assignedGroupInstanceTotal = AssignedGroup.count() |
---|
| 14 | |
---|
| 15 | if(!result.assignedGroupInstanceList || !result.assignedGroupInstanceTotal) |
---|
| 16 | return fail(code:"default.list.failure") |
---|
| 17 | |
---|
| 18 | // Success. |
---|
| 19 | return result |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | def show(params) { |
---|
| 23 | def result = [:] |
---|
| 24 | def fail = { Map m -> |
---|
| 25 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
| 26 | return result |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
| 30 | |
---|
| 31 | if(!result.assignedGroupInstance) |
---|
| 32 | return fail(code:"default.not.found") |
---|
| 33 | |
---|
| 34 | // Success. |
---|
| 35 | return result |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | def delete(params) { |
---|
| 39 | def result = [:] |
---|
| 40 | def fail = { Map m -> |
---|
| 41 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
| 42 | return result |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
| 46 | |
---|
| 47 | if(!result.assignedGroupInstance) |
---|
| 48 | return fail(code:"default.not.found") |
---|
| 49 | |
---|
| 50 | try { |
---|
| 51 | result.assignedGroupInstance.delete(flush:true) |
---|
| 52 | return result //Success. |
---|
| 53 | } |
---|
| 54 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
| 55 | return fail(code:"default.delete.failure") |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | def edit(params) { |
---|
| 61 | def result = [:] |
---|
| 62 | def fail = { Map m -> |
---|
| 63 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
| 64 | return result |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
| 68 | |
---|
| 69 | if(!result.assignedGroupInstance) |
---|
| 70 | return fail(code:"default.not.found") |
---|
| 71 | |
---|
| 72 | // Success. |
---|
| 73 | return result |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | def update(params) { |
---|
| 77 | AssignedGroup.withTransaction { status -> |
---|
| 78 | def result = [:] |
---|
| 79 | |
---|
| 80 | def fail = { Map m -> |
---|
| 81 | status.setRollbackOnly() |
---|
| 82 | if(result.assignedGroupInstance && m.field) |
---|
| 83 | result.assignedGroupInstance.errors.rejectValue(m.field, m.code) |
---|
| 84 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
| 85 | return result |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | result.assignedGroupInstance = AssignedGroup.get(params.id) |
---|
| 89 | |
---|
| 90 | if(!result.assignedGroupInstance) |
---|
| 91 | return fail(code:"default.not.found") |
---|
| 92 | |
---|
| 93 | // Optimistic locking check. |
---|
| 94 | if(params.version) { |
---|
| 95 | if(result.assignedGroupInstance.version > params.version.toLong()) |
---|
| 96 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | result.assignedGroupInstance.properties = params |
---|
| 100 | |
---|
| 101 | if(result.assignedGroupInstance.hasErrors() || !result.assignedGroupInstance.save()) |
---|
| 102 | return fail(code:"default.update.failure") |
---|
| 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 | // success |
---|
| 124 | return result |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | def save(params) { |
---|
| 128 | def result = [:] |
---|
| 129 | def fail = { Map m -> |
---|
| 130 | if(result.assignedGroupInstance && m.field) |
---|
| 131 | result.assignedGroupInstance.errors.rejectValue(m.field, m.code) |
---|
| 132 | result.error = [ code: m.code, args: ["AssignedGroup", params.id] ] |
---|
| 133 | return result |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | result.assignedGroupInstance = new AssignedGroup(params) |
---|
| 137 | |
---|
| 138 | if(result.assignedGroupInstance.hasErrors() || !result.assignedGroupInstance.save(flush: true)) |
---|
| 139 | return fail(code:"default.create.failure") |
---|
| 140 | |
---|
| 141 | // success |
---|
| 142 | return result |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | } // end class |
---|