[274] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
[298] | 3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
[274] | 4 | class AssetSubItemDetailedController extends BaseController { |
---|
[285] | 5 | |
---|
| 6 | def assetSubItemService |
---|
| 7 | |
---|
[274] | 8 | // the delete, save and update actions only accept POST requests |
---|
| 9 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 10 | |
---|
[298] | 11 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
| 12 | def index = { redirect(controller: "assetDetailed", params:params) } |
---|
| 13 | |
---|
| 14 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[274] | 15 | def show = { |
---|
| 16 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 17 | |
---|
| 18 | if(!assetSubItemInstance) { |
---|
| 19 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
| 20 | redirect(controller: "assetDetailed", action: "search") |
---|
| 21 | } |
---|
| 22 | else { return [ assetSubItemInstance : assetSubItemInstance ] } |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | def delete = { |
---|
| 26 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 27 | if(assetSubItemInstance) { |
---|
| 28 | try { |
---|
| 29 | assetSubItemInstance.delete(flush:true) |
---|
| 30 | flash.message = "AssetSubItem ${params.id} deleted" |
---|
| 31 | redirect(controller: "assetDetailed", action: "search") |
---|
| 32 | } |
---|
| 33 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
[285] | 34 | flash.errorMessage = "AssetSubItem ${params.id} could not be deleted" |
---|
[274] | 35 | redirect(action:show,id:params.id) |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | else { |
---|
| 39 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
| 40 | redirect(controller: "assetDetailed", action: "search") |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | def edit = { |
---|
| 45 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 46 | |
---|
| 47 | if(!assetSubItemInstance) { |
---|
| 48 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
| 49 | redirect(controller: "assetDetailed", action: "search") |
---|
| 50 | } |
---|
| 51 | else { |
---|
| 52 | return [ assetSubItemInstance : assetSubItemInstance ] |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | def update = { |
---|
| 57 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 58 | if(assetSubItemInstance) { |
---|
| 59 | if(params.version) { |
---|
| 60 | def version = params.version.toLong() |
---|
| 61 | if(assetSubItemInstance.version > version) { |
---|
| 62 | |
---|
| 63 | assetSubItemInstance.errors.rejectValue("version", "assetSubItem.optimistic.locking.failure", "Another user has updated this AssetSubItem while you were editing.") |
---|
| 64 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 65 | return |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | assetSubItemInstance.properties = params |
---|
| 70 | |
---|
[276] | 71 | // if(!assetSubItemInstance.asset && !assetSubItemInstance.parentItem) { |
---|
| 72 | // flash.message = "Either an asset or parentItem is required." |
---|
| 73 | // render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 74 | // return |
---|
| 75 | // } |
---|
| 76 | // |
---|
| 77 | // if(assetSubItemInstance.asset && assetSubItemInstance.parentItem) { |
---|
| 78 | // flash.message = "Both an asset and parentItem may not be specified at the same time." |
---|
| 79 | // render(view:'edit',model:[parentItem:parentItem]) |
---|
| 80 | // return |
---|
| 81 | // } |
---|
[274] | 82 | |
---|
| 83 | if(!assetSubItemInstance.hasErrors() && assetSubItemInstance.save(flush: true)) { |
---|
| 84 | flash.message = "AssetSubItem ${params.id} updated" |
---|
| 85 | |
---|
[276] | 86 | if(assetSubItemInstance.assets) |
---|
| 87 | redirect(controller: "assetDetailed", action:"show", id: assetSubItemInstance.assets.toArray()[0].id) |
---|
[274] | 88 | else { |
---|
| 89 | //Get the top parentItem. |
---|
| 90 | def parentItem = assetSubItemInstance.parentItem |
---|
| 91 | def i = 0 |
---|
| 92 | while(parentItem?.parentItem) { |
---|
| 93 | parentItem = parentItem.parentItem |
---|
| 94 | // Protect against infinite recurrsion. |
---|
| 95 | i++ |
---|
| 96 | if(i > 100) |
---|
| 97 | break |
---|
| 98 | } |
---|
| 99 | |
---|
[276] | 100 | if(parentItem?.assets) |
---|
| 101 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
[274] | 102 | else |
---|
| 103 | redirect(action:"show", id: assetSubItemInstance.id) // End stop if all else fails. |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | else { |
---|
| 107 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | else { |
---|
| 111 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
| 112 | redirect(controller: "assetDetailed", action: "search") |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | def create = { |
---|
[285] | 117 | def result = assetSubItemService.create(params) |
---|
| 118 | |
---|
| 119 | if(!result.error) |
---|
| 120 | return [assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance] |
---|
| 121 | |
---|
| 122 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
| 123 | redirect(controller: 'assetDetailed', action: 'search') |
---|
[274] | 124 | } |
---|
| 125 | |
---|
| 126 | def save = { |
---|
[285] | 127 | def result = assetSubItemService.save(params) |
---|
[274] | 128 | |
---|
[285] | 129 | if(!result.error) { |
---|
| 130 | // Success. |
---|
| 131 | flash.message = g.message(code: "default.create.success", args: ["AssetSubItem", result.assetSubItemInstance.name]) |
---|
[274] | 132 | |
---|
[285] | 133 | /// @todo: This just grabs the first parent in an unsorted list and needs improving. |
---|
| 134 | if(result.assetSubItemInstance.assets) |
---|
| 135 | redirect(controller: "assetDetailed", action:"show", id: result.assetSubItemInstance.assets.toArray()[0].id) |
---|
[274] | 136 | else { |
---|
| 137 | //Get the top parentItem. |
---|
[285] | 138 | def parentItem = result.assetSubItemInstance.parentItem |
---|
[274] | 139 | def i = 0 |
---|
| 140 | while(parentItem?.parentItem) { |
---|
| 141 | parentItem = parentItem.parentItem |
---|
| 142 | // Protect against infinite recurrsion. |
---|
| 143 | i++ |
---|
| 144 | if(i > 100) |
---|
| 145 | break |
---|
| 146 | } |
---|
| 147 | |
---|
[276] | 148 | if(parentItem?.assets) |
---|
| 149 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
[274] | 150 | else |
---|
[285] | 151 | redirect(action:"show", id: result.assetSubItemInstance.id) // End stop if all else fails. |
---|
[274] | 152 | } |
---|
[285] | 153 | return |
---|
[274] | 154 | } |
---|
| 155 | |
---|
[285] | 156 | if(result.error.code == "assetSubItem.asset.not.found") |
---|
| 157 | flash.errorMessage = g.message(code: "default.create.success") |
---|
| 158 | |
---|
| 159 | render(view:'create', model:[assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance]) |
---|
| 160 | } |
---|
| 161 | |
---|
[274] | 162 | } // end class |
---|