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