1 | class AssetService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def saveCopy(params) { |
---|
6 | Asset.withTransaction { status -> |
---|
7 | def result = [:] |
---|
8 | |
---|
9 | def fail = { Map m -> |
---|
10 | status.setRollbackOnly() |
---|
11 | if(result.assetInstance && m.field) |
---|
12 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
13 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
14 | return result |
---|
15 | } |
---|
16 | |
---|
17 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
18 | if(!result.assetToCopy) |
---|
19 | return fail(code:"default.not.found") |
---|
20 | |
---|
21 | if(!params.copyMethod) |
---|
22 | fail(code:"asset.copy.method.required") |
---|
23 | |
---|
24 | result.assetInstance = new Asset(params) |
---|
25 | |
---|
26 | if(result.assetInstance.hasErrors() || !result.assetInstance.save()) |
---|
27 | return fail(code:"default.create.failure") |
---|
28 | |
---|
29 | def assetSubItemInstance1 |
---|
30 | |
---|
31 | // Copy subItems from level 2 and bellow. |
---|
32 | def copyAssetSubItem = { assetSubItemToCopy, parentItem -> |
---|
33 | def assetSubItemInstance = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
34 | description: assetSubItemToCopy.description, |
---|
35 | parentItem: parentItem) |
---|
36 | |
---|
37 | if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) |
---|
38 | return fail(field:"subItems", code:"default.create.failure") |
---|
39 | |
---|
40 | def i = 0 |
---|
41 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
42 | call(assetSubItem, assetSubItemInstance) |
---|
43 | // Protect against endless recurrsion. |
---|
44 | i++ |
---|
45 | if(i > 100) |
---|
46 | fail(code:"default.create.failure") |
---|
47 | // Stop if an error is flagged. |
---|
48 | if(result.error) |
---|
49 | break |
---|
50 | } |
---|
51 | } //copyAssetSubItem |
---|
52 | |
---|
53 | // Copy the 1st level of subItems. |
---|
54 | def copyAssetSubItem1 = { assetSubItemToCopy -> |
---|
55 | assetSubItemInstance1 = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
56 | description: assetSubItemToCopy.description, |
---|
57 | asset: result.assetInstance) |
---|
58 | |
---|
59 | if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) |
---|
60 | return fail(field:"assetSubItems", code:"default.create.failure") |
---|
61 | |
---|
62 | result.assetInstance.addToAssetSubItems(assetSubItemInstance1) |
---|
63 | |
---|
64 | def i = 0 |
---|
65 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
66 | copyAssetSubItem(assetSubItem, assetSubItemInstance1) |
---|
67 | // Protect against endless recurrsion. |
---|
68 | i++ |
---|
69 | if(i > 100) |
---|
70 | fail(code:"default.create.failure") |
---|
71 | // Stop if an error is flagged. |
---|
72 | if(result.error) |
---|
73 | break |
---|
74 | } |
---|
75 | } //copyAssetSubItem1 |
---|
76 | |
---|
77 | def linkAssetSubItem = { assetSubItemToLink -> |
---|
78 | result.assetInstance.addToAssetSubItems(assetSubItemToLink) |
---|
79 | } |
---|
80 | |
---|
81 | def i = 0 |
---|
82 | for(assetSubItem in result.assetToCopy.assetSubItems) { |
---|
83 | |
---|
84 | if(params.copyMethod == "copy") |
---|
85 | copyAssetSubItem1(assetSubItem) |
---|
86 | else |
---|
87 | linkAssetSubItem(assetSubItem) |
---|
88 | // Protect against endless recurrsion. |
---|
89 | i++ |
---|
90 | if(i > 100) |
---|
91 | fail(code:"default.create.failure") |
---|
92 | // Stop if an error is flagged. |
---|
93 | if(result.error) |
---|
94 | break |
---|
95 | } |
---|
96 | |
---|
97 | // Success or not. |
---|
98 | return result |
---|
99 | |
---|
100 | } //end withTransaction |
---|
101 | } // end saveCopySrvce |
---|
102 | |
---|
103 | } // end class |
---|