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