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