1 | /** |
---|
2 | * Provides a service class for the InventoryItem domain class. |
---|
3 | */ |
---|
4 | class InventoryItemService { |
---|
5 | |
---|
6 | boolean transactional = false |
---|
7 | |
---|
8 | /** |
---|
9 | * Prepare the data for the show view. |
---|
10 | * The result can be used to easily construct the model for the show view. |
---|
11 | * @param params The incoming params as normally passed to the show view |
---|
12 | * primarily including the id of the inventoryItem. |
---|
13 | * @returns A map containing result.error, if any error, otherwise result.inventoryItemInstance. |
---|
14 | */ |
---|
15 | def show(params) { |
---|
16 | def result = [:] |
---|
17 | def fail = { Map m -> |
---|
18 | result.error = [ code: m.code, args: ["InventoryItem", params.id] ] |
---|
19 | return result |
---|
20 | } |
---|
21 | |
---|
22 | result.inventoryItemInstance = InventoryItem.get( params.id ) |
---|
23 | |
---|
24 | if(!result.inventoryItemInstance) |
---|
25 | return fail(code:"default.not.found") |
---|
26 | |
---|
27 | result.showTab = [:] |
---|
28 | switch (params.showTab) { |
---|
29 | case "showDetailTab": |
---|
30 | result.showTab.detail = new String("true") |
---|
31 | break |
---|
32 | case "showMovementTab": |
---|
33 | result.showTab.movement = new String("true") |
---|
34 | break |
---|
35 | default: |
---|
36 | result.showTab.inventory = new String("true") |
---|
37 | } |
---|
38 | |
---|
39 | def p = [:] |
---|
40 | p.max = result.inventoryMovementListMax = 10 |
---|
41 | p.order = "desc" |
---|
42 | p.sort = "id" |
---|
43 | result.inventoryMovementList = InventoryMovement.findAllByInventoryItem(result.inventoryItemInstance, p) |
---|
44 | result.inventoryMovementListTotal = InventoryMovement.countByInventoryItem(result.inventoryItemInstance) |
---|
45 | |
---|
46 | // Success. |
---|
47 | return result |
---|
48 | |
---|
49 | } // end show() |
---|
50 | |
---|
51 | def delete(params) { |
---|
52 | def result = [:] |
---|
53 | def fail = { Map m -> |
---|
54 | result.error = [ code: m.code, args: ["InventoryItem", params.id] ] |
---|
55 | return result |
---|
56 | } |
---|
57 | |
---|
58 | result.inventoryItemInstance = InventoryItem.get(params.id) |
---|
59 | |
---|
60 | if(!result.inventoryItemInstance) |
---|
61 | return fail(code:"default.not.found") |
---|
62 | |
---|
63 | try { |
---|
64 | result.inventoryItemInstance.delete(flush:true) |
---|
65 | return result //Success. |
---|
66 | } |
---|
67 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
68 | return fail(code:"default.delete.failure") |
---|
69 | } |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | def edit(params) { |
---|
74 | def result = [:] |
---|
75 | def fail = { Map m -> |
---|
76 | result.error = [ code: m.code, args: ["InventoryItem", params.id] ] |
---|
77 | return result |
---|
78 | } |
---|
79 | |
---|
80 | result.inventoryItemInstance = InventoryItem.get(params.id) |
---|
81 | |
---|
82 | if(!result.inventoryItemInstance) |
---|
83 | return fail(code:"default.not.found") |
---|
84 | |
---|
85 | // Success. |
---|
86 | return result |
---|
87 | } |
---|
88 | |
---|
89 | def update(params) { |
---|
90 | InventoryItem.withTransaction { status -> |
---|
91 | def result = [:] |
---|
92 | |
---|
93 | def fail = { Map m -> |
---|
94 | status.setRollbackOnly() |
---|
95 | if(result.inventoryItemInstance && m.field) |
---|
96 | result.inventoryItemInstance.errors.rejectValue(m.field, m.code) |
---|
97 | result.error = [ code: m.code, args: ["InventoryItem", params.id] ] |
---|
98 | return result |
---|
99 | } |
---|
100 | |
---|
101 | result.inventoryItemInstance = InventoryItem.get(params.id) |
---|
102 | |
---|
103 | if(!result.inventoryItemInstance) |
---|
104 | return fail(code:"default.not.found") |
---|
105 | |
---|
106 | // Optimistic locking check. |
---|
107 | if(params.version) { |
---|
108 | if(result.inventoryItemInstance.version > params.version.toLong()) |
---|
109 | return fail(field:"version", code:"default.optimistic.locking.failure") |
---|
110 | } |
---|
111 | |
---|
112 | result.inventoryItemInstance.properties = params |
---|
113 | |
---|
114 | if(result.inventoryItemInstance.hasErrors() || !result.inventoryItemInstance.save()) |
---|
115 | return fail(code:"default.update.failure") |
---|
116 | |
---|
117 | // Success. |
---|
118 | return result |
---|
119 | |
---|
120 | } //end withTransaction |
---|
121 | } // end update() |
---|
122 | |
---|
123 | def create(params) { |
---|
124 | def result = [:] |
---|
125 | def fail = { Map m -> |
---|
126 | result.error = [ code: m.code, args: ["InventoryItem", params.id] ] |
---|
127 | return result |
---|
128 | } |
---|
129 | |
---|
130 | result.inventoryItemInstance = new InventoryItem() |
---|
131 | result.inventoryItemInstance.properties = params |
---|
132 | |
---|
133 | // success |
---|
134 | return result |
---|
135 | } |
---|
136 | |
---|
137 | def save(params) { |
---|
138 | InventoryItem.withTransaction { status -> |
---|
139 | def result = [:] |
---|
140 | |
---|
141 | def fail = { Map m -> |
---|
142 | status.setRollbackOnly() |
---|
143 | if(result.inventoryItemInstance && m.field) |
---|
144 | result.inventoryItemInstance.errors.rejectValue(m.field, m.code) |
---|
145 | result.error = [ code: m.code, args: ["InventoryItem", params.id] ] |
---|
146 | return result |
---|
147 | } |
---|
148 | |
---|
149 | result.inventoryItemInstance = new InventoryItem(params) |
---|
150 | |
---|
151 | if(result.inventoryItemInstance.hasErrors() || !result.inventoryItemInstance.save()) |
---|
152 | return fail(code:"default.create.failure") |
---|
153 | |
---|
154 | // success |
---|
155 | return result |
---|
156 | |
---|
157 | } //end withTransaction |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | } // end class |
---|