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