[274] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[336] | 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
[274] | 3 | |
---|
[298] | 4 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager']) |
---|
[274] | 5 | class AssetSubItemDetailedController extends BaseController { |
---|
[285] | 6 | |
---|
[336] | 7 | def filterService |
---|
| 8 | def exportService |
---|
[285] | 9 | def assetSubItemService |
---|
| 10 | |
---|
[274] | 11 | // the delete, save and update actions only accept POST requests |
---|
| 12 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 13 | |
---|
[298] | 14 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[342] | 15 | def index = { redirect(action:search,params:params) } |
---|
[298] | 16 | |
---|
| 17 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[336] | 18 | def search = { |
---|
| 19 | |
---|
| 20 | if(session.assetSearchParamsMax) |
---|
| 21 | params.max = session.assetSearchParamsMax |
---|
| 22 | |
---|
| 23 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
| 24 | |
---|
| 25 | def assetSubItemInstanceList = [] |
---|
| 26 | def assetSubItemInstanceTotal |
---|
| 27 | def filterParams = [:] |
---|
| 28 | |
---|
| 29 | // Quick Search: |
---|
| 30 | if(!params.filter) { |
---|
| 31 | assetSubItemInstanceList = AssetSubItem.list( params ) |
---|
| 32 | assetSubItemInstanceTotal = AssetSubItem.count() |
---|
| 33 | filterParams.quickSearch = params.quickSearch |
---|
| 34 | } |
---|
| 35 | else { |
---|
| 36 | // filterPane: |
---|
| 37 | assetSubItemInstanceList = filterService.filter( params, AssetSubItem ) |
---|
| 38 | assetSubItemInstanceTotal = filterService.count( params, AssetSubItem ) |
---|
| 39 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // export plugin: |
---|
| 43 | if(params?.format && params.format != "html") { |
---|
| 44 | |
---|
| 45 | def dateFmt = { date -> |
---|
| 46 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | String title = "AssetSubItem List." |
---|
| 50 | |
---|
| 51 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 52 | response.setHeader("Content-disposition", "attachment; filename=assets.${params.extension}") |
---|
| 53 | List fields = ["name", |
---|
| 54 | "description"] |
---|
| 55 | Map labels = ["name": "AssetSubItem", |
---|
| 56 | "description": "Description"] |
---|
| 57 | |
---|
| 58 | Map formatters = [:] |
---|
| 59 | Map parameters = [title: title, separator: ","] |
---|
| 60 | |
---|
| 61 | exportService.export(params.format, |
---|
| 62 | response.outputStream, |
---|
| 63 | assetSubItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 64 | fields, |
---|
| 65 | labels, |
---|
| 66 | formatters, |
---|
| 67 | parameters) |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // Add some basic params to filterParams. |
---|
| 71 | filterParams.max = params.max |
---|
| 72 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 73 | filterParams.sort = params.sort ?: "id" |
---|
| 74 | filterParams.order = params.order ?: "desc" |
---|
| 75 | |
---|
| 76 | return[ assetSubItemInstanceList: assetSubItemInstanceList, |
---|
| 77 | assetSubItemInstanceTotal: assetSubItemInstanceTotal, |
---|
| 78 | filterParams: filterParams ] |
---|
| 79 | |
---|
| 80 | } // end search() |
---|
| 81 | |
---|
| 82 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager', 'ROLE_AssetUser']) |
---|
[274] | 83 | def show = { |
---|
| 84 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 85 | |
---|
| 86 | if(!assetSubItemInstance) { |
---|
| 87 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
[342] | 88 | redirect(action: "search") |
---|
[274] | 89 | } |
---|
| 90 | else { return [ assetSubItemInstance : assetSubItemInstance ] } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | def delete = { |
---|
[347] | 94 | def result = assetSubItemService.delete(params) |
---|
| 95 | |
---|
| 96 | if(!result.error) { |
---|
| 97 | flash.message = g.message(code: "default.delete.success", args: ["AssetSubItem", params.id]) |
---|
| 98 | redirect(action: search) |
---|
| 99 | return |
---|
[274] | 100 | } |
---|
[347] | 101 | |
---|
| 102 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 103 | |
---|
| 104 | if(result.error.code == "default.not.found") { |
---|
| 105 | redirect(action: search) |
---|
| 106 | return |
---|
[274] | 107 | } |
---|
[347] | 108 | |
---|
| 109 | redirect(action:show, id: params.id) |
---|
[274] | 110 | } |
---|
| 111 | |
---|
| 112 | def edit = { |
---|
| 113 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 114 | |
---|
| 115 | if(!assetSubItemInstance) { |
---|
| 116 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
[342] | 117 | redirect(action: "search") |
---|
[274] | 118 | } |
---|
| 119 | else { |
---|
| 120 | return [ assetSubItemInstance : assetSubItemInstance ] |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | def update = { |
---|
| 125 | def assetSubItemInstance = AssetSubItem.get( params.id ) |
---|
| 126 | if(assetSubItemInstance) { |
---|
| 127 | if(params.version) { |
---|
| 128 | def version = params.version.toLong() |
---|
| 129 | if(assetSubItemInstance.version > version) { |
---|
| 130 | |
---|
| 131 | assetSubItemInstance.errors.rejectValue("version", "assetSubItem.optimistic.locking.failure", "Another user has updated this AssetSubItem while you were editing.") |
---|
| 132 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 133 | return |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | assetSubItemInstance.properties = params |
---|
| 138 | |
---|
[276] | 139 | // if(!assetSubItemInstance.asset && !assetSubItemInstance.parentItem) { |
---|
| 140 | // flash.message = "Either an asset or parentItem is required." |
---|
| 141 | // render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 142 | // return |
---|
| 143 | // } |
---|
| 144 | // |
---|
| 145 | // if(assetSubItemInstance.asset && assetSubItemInstance.parentItem) { |
---|
| 146 | // flash.message = "Both an asset and parentItem may not be specified at the same time." |
---|
| 147 | // render(view:'edit',model:[parentItem:parentItem]) |
---|
| 148 | // return |
---|
| 149 | // } |
---|
[274] | 150 | |
---|
| 151 | if(!assetSubItemInstance.hasErrors() && assetSubItemInstance.save(flush: true)) { |
---|
| 152 | flash.message = "AssetSubItem ${params.id} updated" |
---|
| 153 | |
---|
[276] | 154 | if(assetSubItemInstance.assets) |
---|
| 155 | redirect(controller: "assetDetailed", action:"show", id: assetSubItemInstance.assets.toArray()[0].id) |
---|
[274] | 156 | else { |
---|
| 157 | //Get the top parentItem. |
---|
| 158 | def parentItem = assetSubItemInstance.parentItem |
---|
| 159 | def i = 0 |
---|
| 160 | while(parentItem?.parentItem) { |
---|
| 161 | parentItem = parentItem.parentItem |
---|
| 162 | // Protect against infinite recurrsion. |
---|
| 163 | i++ |
---|
| 164 | if(i > 100) |
---|
| 165 | break |
---|
| 166 | } |
---|
| 167 | |
---|
[276] | 168 | if(parentItem?.assets) |
---|
| 169 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
[274] | 170 | else |
---|
| 171 | redirect(action:"show", id: assetSubItemInstance.id) // End stop if all else fails. |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | else { |
---|
| 175 | render(view:'edit',model:[assetSubItemInstance:assetSubItemInstance]) |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | else { |
---|
| 179 | flash.message = "AssetSubItem not found with id ${params.id}" |
---|
[342] | 180 | redirect(action: "search") |
---|
[274] | 181 | } |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | def create = { |
---|
[285] | 185 | def result = assetSubItemService.create(params) |
---|
| 186 | |
---|
| 187 | if(!result.error) |
---|
| 188 | return [assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance] |
---|
| 189 | |
---|
| 190 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
| 191 | redirect(controller: 'assetDetailed', action: 'search') |
---|
[274] | 192 | } |
---|
| 193 | |
---|
| 194 | def save = { |
---|
[285] | 195 | def result = assetSubItemService.save(params) |
---|
[274] | 196 | |
---|
[285] | 197 | if(!result.error) { |
---|
| 198 | // Success. |
---|
| 199 | flash.message = g.message(code: "default.create.success", args: ["AssetSubItem", result.assetSubItemInstance.name]) |
---|
[274] | 200 | |
---|
[285] | 201 | /// @todo: This just grabs the first parent in an unsorted list and needs improving. |
---|
| 202 | if(result.assetSubItemInstance.assets) |
---|
| 203 | redirect(controller: "assetDetailed", action:"show", id: result.assetSubItemInstance.assets.toArray()[0].id) |
---|
[274] | 204 | else { |
---|
| 205 | //Get the top parentItem. |
---|
[285] | 206 | def parentItem = result.assetSubItemInstance.parentItem |
---|
[274] | 207 | def i = 0 |
---|
| 208 | while(parentItem?.parentItem) { |
---|
| 209 | parentItem = parentItem.parentItem |
---|
| 210 | // Protect against infinite recurrsion. |
---|
| 211 | i++ |
---|
| 212 | if(i > 100) |
---|
| 213 | break |
---|
| 214 | } |
---|
| 215 | |
---|
[276] | 216 | if(parentItem?.assets) |
---|
| 217 | redirect(controller: "assetDetailed", action:"show", id: parentItem.assets.toArray()[0].id) |
---|
[274] | 218 | else |
---|
[285] | 219 | redirect(action:"show", id: result.assetSubItemInstance.id) // End stop if all else fails. |
---|
[274] | 220 | } |
---|
[285] | 221 | return |
---|
[274] | 222 | } |
---|
| 223 | |
---|
[285] | 224 | if(result.error.code == "assetSubItem.asset.not.found") |
---|
| 225 | flash.errorMessage = g.message(code: "default.create.success") |
---|
| 226 | |
---|
| 227 | render(view:'create', model:[assetSubItemInstance: result.assetSubItemInstance, assetInstance: result.assetInstance]) |
---|
| 228 | } |
---|
| 229 | |
---|
[274] | 230 | } // end class |
---|