Changeset 284


Ignore:
Timestamp:
Jan 21, 2010, 11:25:46 AM (10 years ago)
Author:
gav
Message:

Move asset copy and create functions into AssetService.
Repair create links in AssetTreeTagLib.

Location:
trunk/grails-app
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/AssetDetailedController.groovy

    r283 r284  
    234234
    235235    def create = {
    236         def assetInstance = new Asset()
    237         assetInstance.properties = params
    238         return ['assetInstance':assetInstance]
     236        def result = assetService.create(params)
     237
     238        if(!result.error)
     239            return [assetInstance: result.assetInstance]
     240
     241        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
     242        redirect(action: search)
    239243    }
    240244
    241245    def copy = {
    242         def assetToCopy = Asset.get(params.asset.id)
    243 
    244         if(!assetToCopy) {
    245             flash.message = "Please select an asset to copy."
    246             redirect(action: list)
    247             return
    248         }
    249 
    250         def assetInstance = new Asset(name: assetToCopy.name,
    251                                                             description: assetToCopy.description,
    252                                                             section: assetToCopy.section)
    253         assetInstance.properties = params
    254         return ['assetInstance':assetInstance, assetToCopy: assetToCopy]
     246        def result = assetService.copy(params)
     247
     248        if(!result.error)
     249            return [assetInstance: result.assetInstance, assetToCopy: result.assetToCopy]
     250
     251        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
     252        redirect(action: search)
    255253    }
    256254
    257255    def save = {
    258         def assetInstance = new Asset(params)
    259         if(!assetInstance.hasErrors() && assetInstance.save(flush: true)) {
    260             flash.message = "Asset ${assetInstance.id} created"
    261             redirect(action:show,id:assetInstance.id)
    262         }
    263         else {
    264             render(view:'create',model:[assetInstance:assetInstance])
    265         }
    266     }
    267 
    268     def saveCopy = {
    269         def result = assetService.saveCopy(params)
     256        def result = assetService.save(params)
    270257
    271258        if(!result.error) {
     
    275262        }
    276263
     264        render(view:'create', model:[assetInstance: result.assetInstance])
     265    }
     266
     267
     268    def saveCopy = {
     269        def result = assetService.saveCopy(params)
     270
     271        if(!result.error) {
     272            flash.message = g.message(code: "default.create.success", args: ["Asset", result.assetInstance.id])
     273            redirect(action:show, id: result.assetInstance.id)
     274            return
     275        }
     276
    277277        if(result.error.code == "default.not.found") {
    278278            flash.message = g.message(code: result.error.code, args: ["Asset", params.assetToCopy?.id])
  • trunk/grails-app/i18n/messages.properties

    r278 r284  
    99asset.copy.method.help=Link creates a new asset and links it to EXISTING sub items. \
    1010    While copy creates a new asset and new sub items.
     11asset.copy.asset.required=Please select an asset to copy.
    1112
    1213sub.task.create.confirm=Immediately create and save a new sub task?
  • trunk/grails-app/services/AssetService.groovy

    r278 r284  
    22
    33    boolean transactional = false
     4
     5    def create(params) {
     6        def result = [:]
     7        def fail = { Map m ->
     8            result.error = [ code: m.code, args: ["Asset", params.id] ]
     9            return result
     10        }
     11
     12        result.assetInstance = new Asset()
     13        result.assetInstance.properties = params
     14
     15        // success
     16        return result
     17    }
     18
     19    def copy(params) {
     20        def result = [:]
     21        def fail = { Map m ->
     22            result.error = [ code: m.code, args: ["Asset", params.id] ]
     23            return result
     24        }
     25
     26        result.assetToCopy = Asset.get(params.assetToCopy?.id)
     27
     28        if(!result.assetToCopy)
     29            return fail(code: "asset.copy.asset.required")
     30
     31        result.assetInstance = new Asset(name: result.assetToCopy.name,
     32                                                            description: result.assetToCopy.description,
     33                                                            section: result.assetToCopy.section)
     34
     35        result.assetInstance.properties = params
     36
     37        // success
     38        return result
     39    }
     40
     41    def save(params) {
     42        def result = [:]
     43        def fail = { Map m ->
     44            if(result.assetInstance && m.field)
     45                result.assetInstance.errors.rejectValue(m.field, m.code)
     46            result.error = [ code: m.code, args: ["Asset", params.id] ]
     47            return result
     48        }
     49
     50        result.assetInstance = new Asset(params)
     51
     52        if(result.assetInstance.hasErrors() || !result.assetInstance.save(flush: true))
     53            return fail(code:"default.create.failure")
     54
     55        // success
     56        return result
     57    }
    458
    559    def saveCopy(params) {
  • trunk/grails-app/taglib/AssetTreeTagLib.groovy

    r282 r284  
    8181                                                            img(src: addImg(), alt: 'Add', title: 'Add Sub Item')
    8282                                                        }
     83                                                        a(href: assetCopyLink(asset.id)) {
     84                                                            img(src: copyImg(), alt: 'Add', title: 'Copy Asset')
     85                                                        }
    8386                                                    } // li
    8487
     
    240243    }
    241244
    242     def sectionCreateLink(id) {
    243         createLink(controller: 'sectionDetailed', action: 'create', params: ['id': id] ).toString()
     245    def sectionCreateLink(siteId) {
     246        createLink(controller: 'sectionDetailed', action: 'create', params: ['site.id': siteId] ).toString()
    244247    }
    245248
     
    252255    }
    253256
    254     def assetCreateLink(id) {
    255         createLink(controller: 'assetDetailed', action: 'create', params: ['id': id] ).toString()
     257    def assetCreateLink(sectionId) {
     258        createLink(controller: 'assetDetailed', action: 'create', params: ['section.id': sectionId] ).toString()
    256259    }
    257260
     
    265268
    266269    def assetCopyLink(id) {
    267         createLink(controller: 'assetDetailed', action: 'copy', params: ['asset.id': id] ).toString()
     270        createLink(controller: 'assetDetailed', action: 'copy', params: ['assetToCopy.id': id] ).toString()
    268271    }
    269272
  • trunk/grails-app/views/assetDetailed/overview.gsp

    r278 r284  
    1414        </div>
    1515        <div class="body">
    16             <g:if test="${flash.message}">
    17             <div class="message">${flash.message}</div>
    18             </g:if>
     16            <g:render template="/shared/messages" />
    1917
    2018            <div class="dialog">
  • trunk/grails-app/views/assetDetailed/search.gsp

    r271 r284  
    1515        </div>
    1616        <div class="body">
    17             <g:if test="${flash.message}">
    18             <div class="message">${flash.message}</div>
    19             </g:if>
     17            <g:render template="/shared/messages" />
     18
    2019            <filterpane:currentCriteria domainBean="Asset"
    2120                                    action="search"
  • trunk/grails-app/views/shared/_assetTree.gsp

    r278 r284  
    55        <img src="${resource(dir:'images/skin',file:'database_add.png')}" alt="Add" title="Add Sub Item"/>
    66    </g:link>
    7     <g:link params="['asset.id':assetInstance?.id]" action="copy">
     7    <g:link params="['assetToCopy.id':assetInstance?.id]" action="copy">
    88        <img src="${resource(dir:'images/skin',file:'page_copy.png')}" alt="Copy" title="Copy Asset"/>
    99    </g:link>
Note: See TracChangeset for help on using the changeset viewer.