[278] | 1 | class AssetService { |
---|
| 2 | |
---|
| 3 | boolean transactional = false |
---|
| 4 | |
---|
[284] | 5 | def create(params) { |
---|
| 6 | def result = [:] |
---|
| 7 | def fail = { Map m -> |
---|
| 8 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
| 9 | return result |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | result.assetInstance = new Asset() |
---|
| 13 | result.assetInstance.properties = params |
---|
| 14 | |
---|
| 15 | // success |
---|
| 16 | return result |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | def copy(params) { |
---|
| 20 | def result = [:] |
---|
| 21 | def fail = { Map m -> |
---|
| 22 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
| 23 | return result |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
| 27 | |
---|
| 28 | if(!result.assetToCopy) |
---|
| 29 | return fail(code: "asset.copy.asset.required") |
---|
| 30 | |
---|
| 31 | result.assetInstance = new Asset(name: result.assetToCopy.name, |
---|
| 32 | description: result.assetToCopy.description, |
---|
| 33 | section: result.assetToCopy.section) |
---|
| 34 | |
---|
| 35 | result.assetInstance.properties = params |
---|
| 36 | |
---|
| 37 | // success |
---|
| 38 | return result |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | def save(params) { |
---|
| 42 | def result = [:] |
---|
| 43 | def fail = { Map m -> |
---|
| 44 | if(result.assetInstance && m.field) |
---|
| 45 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
| 46 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
| 47 | return result |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | result.assetInstance = new Asset(params) |
---|
| 51 | |
---|
| 52 | if(result.assetInstance.hasErrors() || !result.assetInstance.save(flush: true)) |
---|
| 53 | return fail(code:"default.create.failure") |
---|
| 54 | |
---|
| 55 | // success |
---|
| 56 | return result |
---|
| 57 | } |
---|
| 58 | |
---|
[278] | 59 | def saveCopy(params) { |
---|
| 60 | Asset.withTransaction { status -> |
---|
| 61 | def result = [:] |
---|
| 62 | |
---|
| 63 | def fail = { Map m -> |
---|
| 64 | status.setRollbackOnly() |
---|
| 65 | if(result.assetInstance && m.field) |
---|
| 66 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
| 67 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
| 68 | return result |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
| 72 | if(!result.assetToCopy) |
---|
| 73 | return fail(code:"default.not.found") |
---|
| 74 | |
---|
| 75 | if(!params.copyMethod) |
---|
| 76 | fail(code:"asset.copy.method.required") |
---|
| 77 | |
---|
| 78 | result.assetInstance = new Asset(params) |
---|
| 79 | |
---|
| 80 | if(result.assetInstance.hasErrors() || !result.assetInstance.save()) |
---|
| 81 | return fail(code:"default.create.failure") |
---|
| 82 | |
---|
| 83 | def assetSubItemInstance1 |
---|
| 84 | |
---|
| 85 | // Copy subItems from level 2 and bellow. |
---|
| 86 | def copyAssetSubItem = { assetSubItemToCopy, parentItem -> |
---|
| 87 | def assetSubItemInstance = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
| 88 | description: assetSubItemToCopy.description, |
---|
| 89 | parentItem: parentItem) |
---|
| 90 | |
---|
| 91 | if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) |
---|
| 92 | return fail(field:"subItems", code:"default.create.failure") |
---|
| 93 | |
---|
| 94 | def i = 0 |
---|
| 95 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
| 96 | call(assetSubItem, assetSubItemInstance) |
---|
| 97 | // Protect against endless recurrsion. |
---|
| 98 | i++ |
---|
| 99 | if(i > 100) |
---|
| 100 | fail(code:"default.create.failure") |
---|
| 101 | // Stop if an error is flagged. |
---|
| 102 | if(result.error) |
---|
| 103 | break |
---|
| 104 | } |
---|
| 105 | } //copyAssetSubItem |
---|
| 106 | |
---|
| 107 | // Copy the 1st level of subItems. |
---|
| 108 | def copyAssetSubItem1 = { assetSubItemToCopy -> |
---|
| 109 | assetSubItemInstance1 = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
| 110 | description: assetSubItemToCopy.description, |
---|
| 111 | asset: result.assetInstance) |
---|
| 112 | |
---|
| 113 | if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) |
---|
| 114 | return fail(field:"assetSubItems", code:"default.create.failure") |
---|
| 115 | |
---|
| 116 | result.assetInstance.addToAssetSubItems(assetSubItemInstance1) |
---|
| 117 | |
---|
| 118 | def i = 0 |
---|
| 119 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
| 120 | copyAssetSubItem(assetSubItem, assetSubItemInstance1) |
---|
| 121 | // Protect against endless recurrsion. |
---|
| 122 | i++ |
---|
| 123 | if(i > 100) |
---|
| 124 | fail(code:"default.create.failure") |
---|
| 125 | // Stop if an error is flagged. |
---|
| 126 | if(result.error) |
---|
| 127 | break |
---|
| 128 | } |
---|
| 129 | } //copyAssetSubItem1 |
---|
| 130 | |
---|
| 131 | def linkAssetSubItem = { assetSubItemToLink -> |
---|
| 132 | result.assetInstance.addToAssetSubItems(assetSubItemToLink) |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | def i = 0 |
---|
| 136 | for(assetSubItem in result.assetToCopy.assetSubItems) { |
---|
| 137 | |
---|
| 138 | if(params.copyMethod == "copy") |
---|
| 139 | copyAssetSubItem1(assetSubItem) |
---|
| 140 | else |
---|
| 141 | linkAssetSubItem(assetSubItem) |
---|
| 142 | // Protect against endless recurrsion. |
---|
| 143 | i++ |
---|
| 144 | if(i > 100) |
---|
| 145 | fail(code:"default.create.failure") |
---|
| 146 | // Stop if an error is flagged. |
---|
| 147 | if(result.error) |
---|
| 148 | break |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // Success or not. |
---|
| 152 | return result |
---|
| 153 | |
---|
| 154 | } //end withTransaction |
---|
| 155 | } // end saveCopySrvce |
---|
| 156 | |
---|
| 157 | } // end class |
---|