1 | |
---|
2 | import org.apache.commons.lang.WordUtils |
---|
3 | |
---|
4 | class AssetService { |
---|
5 | |
---|
6 | boolean transactional = false |
---|
7 | |
---|
8 | def assetSubItemService |
---|
9 | |
---|
10 | /** |
---|
11 | * Determines and returns a possible list of asset sub items. |
---|
12 | * @returns A list of the possible assetSubItems. |
---|
13 | */ |
---|
14 | def possibleAssetSubItems() { |
---|
15 | def criteria = AssetSubItem.createCriteria() |
---|
16 | def possibleAssetSubItems = criteria.list() { |
---|
17 | and { |
---|
18 | eq('isActive', true) |
---|
19 | isNull("parentItem") |
---|
20 | } |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | def delete(params) { |
---|
25 | Asset.withTransaction { status -> |
---|
26 | def result = [:] |
---|
27 | |
---|
28 | def fail = { Map m -> |
---|
29 | status.setRollbackOnly() |
---|
30 | if(result.assetInstance && m.field) |
---|
31 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
32 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
33 | return result |
---|
34 | } |
---|
35 | |
---|
36 | result.assetInstance = Asset.get(params.id) |
---|
37 | |
---|
38 | if(!result.assetInstance) |
---|
39 | return fail(code:"default.not.found") |
---|
40 | |
---|
41 | if(result.assetInstance.maintenanceActions) |
---|
42 | return fail(code:"maintenanceActions.still.associated") |
---|
43 | |
---|
44 | // Remove orphan assetSubItems. |
---|
45 | def assetSubItems = new ArrayList(result.assetInstance.assetSubItems) // avoid ConcurrentModificationException. |
---|
46 | def r |
---|
47 | for(assetSubItem in assetSubItems) { |
---|
48 | result.assetInstance.removeFromAssetSubItems(assetSubItem) |
---|
49 | if(!assetSubItem.assets && !assetSubItem.parentItem) { |
---|
50 | r = assetSubItemService.delete(id: assetSubItem.id) |
---|
51 | if(r.error) { |
---|
52 | log.debug r.error |
---|
53 | fail(code:"asset.subItems.delete.failure") |
---|
54 | break |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | if(result.error) |
---|
60 | return result |
---|
61 | |
---|
62 | // Success. |
---|
63 | // We have handled all the foreign keys so the delete should go forward. |
---|
64 | // Can't flush here due to cascading from Section and Site. |
---|
65 | // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException |
---|
66 | // since that will only happen after leaving the transaction. |
---|
67 | result.assetInstance.delete() |
---|
68 | return result |
---|
69 | |
---|
70 | } // end withTransaction |
---|
71 | } // end delete() |
---|
72 | |
---|
73 | def create(params) { |
---|
74 | def result = [:] |
---|
75 | def fail = { Map m -> |
---|
76 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
77 | return result |
---|
78 | } |
---|
79 | |
---|
80 | result.assetInstance = new Asset() |
---|
81 | result.assetInstance.properties = params |
---|
82 | |
---|
83 | // success |
---|
84 | return result |
---|
85 | } |
---|
86 | |
---|
87 | def copy(params) { |
---|
88 | def result = [:] |
---|
89 | def fail = { Map m -> |
---|
90 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
91 | return result |
---|
92 | } |
---|
93 | |
---|
94 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
95 | |
---|
96 | if(!result.assetToCopy) |
---|
97 | return fail(code: "asset.copy.asset.required") |
---|
98 | |
---|
99 | result.assetInstance = new Asset(name: result.assetToCopy.name, |
---|
100 | description: result.assetToCopy.description, |
---|
101 | comment: result.assetToCopy.comment, |
---|
102 | section: result.assetToCopy.section) |
---|
103 | |
---|
104 | result.assetInstance.properties = params |
---|
105 | |
---|
106 | // success |
---|
107 | return result |
---|
108 | } |
---|
109 | |
---|
110 | def save(params) { |
---|
111 | def result = [:] |
---|
112 | def fail = { Map m -> |
---|
113 | if(result.assetInstance && m.field) |
---|
114 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
115 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
116 | return result |
---|
117 | } |
---|
118 | |
---|
119 | result.assetInstance = new Asset(params) |
---|
120 | |
---|
121 | use(WordUtils) { |
---|
122 | result.assetInstance.name = result.assetInstance.name.capitalize() |
---|
123 | result.assetInstance.description = result.assetInstance.description.capitalize() |
---|
124 | } |
---|
125 | |
---|
126 | if(result.assetInstance.hasErrors() || !result.assetInstance.save(flush: true)) |
---|
127 | return fail(code:"default.create.failure") |
---|
128 | |
---|
129 | // success |
---|
130 | return result |
---|
131 | } |
---|
132 | |
---|
133 | def saveCopy(params) { |
---|
134 | Asset.withTransaction { status -> |
---|
135 | def result = [:] |
---|
136 | |
---|
137 | def fail = { Map m -> |
---|
138 | status.setRollbackOnly() |
---|
139 | if(result.assetInstance && m.field) |
---|
140 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
141 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
142 | return result |
---|
143 | } |
---|
144 | |
---|
145 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
146 | if(!result.assetToCopy) |
---|
147 | return fail(code:"default.not.found") |
---|
148 | |
---|
149 | if(!params.copyMethod) |
---|
150 | fail(code:"asset.copy.method.required") |
---|
151 | |
---|
152 | result.assetInstance = new Asset(params) |
---|
153 | |
---|
154 | if(result.assetInstance.hasErrors() || !result.assetInstance.save()) |
---|
155 | return fail(code:"default.create.failure") |
---|
156 | |
---|
157 | def assetSubItemInstance1 |
---|
158 | |
---|
159 | // Copy subItems from level 2 and bellow. |
---|
160 | def copyAssetSubItem = { assetSubItemToCopy, parentItem -> |
---|
161 | def assetSubItemInstance = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
162 | description: assetSubItemToCopy.description, |
---|
163 | parentItem: parentItem) |
---|
164 | |
---|
165 | if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) |
---|
166 | return fail(field:"subItems", code:"default.create.failure") |
---|
167 | |
---|
168 | def i = 0 |
---|
169 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
170 | call(assetSubItem, assetSubItemInstance) |
---|
171 | // Protect against endless recurrsion. |
---|
172 | i++ |
---|
173 | if(i > 100) |
---|
174 | fail(code:"default.create.failure") |
---|
175 | // Stop if an error is flagged. |
---|
176 | if(result.error) |
---|
177 | break |
---|
178 | } |
---|
179 | } //copyAssetSubItem |
---|
180 | |
---|
181 | // Copy the 1st level of subItems. |
---|
182 | def copyAssetSubItem1 = { assetSubItemToCopy -> |
---|
183 | assetSubItemInstance1 = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
184 | description: assetSubItemToCopy.description, |
---|
185 | asset: result.assetInstance) |
---|
186 | |
---|
187 | if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) |
---|
188 | return fail(field:"assetSubItems", code:"default.create.failure") |
---|
189 | |
---|
190 | result.assetInstance.addToAssetSubItems(assetSubItemInstance1) |
---|
191 | |
---|
192 | def i = 0 |
---|
193 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
194 | copyAssetSubItem(assetSubItem, assetSubItemInstance1) |
---|
195 | // Protect against endless recurrsion. |
---|
196 | i++ |
---|
197 | if(i > 100) |
---|
198 | fail(code:"default.create.failure") |
---|
199 | // Stop if an error is flagged. |
---|
200 | if(result.error) |
---|
201 | break |
---|
202 | } |
---|
203 | } //copyAssetSubItem1 |
---|
204 | |
---|
205 | def linkAssetSubItem = { assetSubItemToLink -> |
---|
206 | result.assetInstance.addToAssetSubItems(assetSubItemToLink) |
---|
207 | } |
---|
208 | |
---|
209 | def i = 0 |
---|
210 | for(assetSubItem in result.assetToCopy.assetSubItems) { |
---|
211 | |
---|
212 | if(params.copyMethod == "copy") |
---|
213 | copyAssetSubItem1(assetSubItem) |
---|
214 | else |
---|
215 | linkAssetSubItem(assetSubItem) |
---|
216 | // Protect against endless recurrsion. |
---|
217 | i++ |
---|
218 | if(i > 100) |
---|
219 | fail(code:"default.create.failure") |
---|
220 | // Stop if an error is flagged. |
---|
221 | if(result.error) |
---|
222 | break |
---|
223 | } |
---|
224 | |
---|
225 | // Success or not. |
---|
226 | return result |
---|
227 | |
---|
228 | } // end withTransaction |
---|
229 | } // end saveCopySrvce |
---|
230 | |
---|
231 | } // end class |
---|