import org.codehaus.groovy.grails.plugins.springsecurity.Secured

class AssetExtendedAttributeController extends BaseAppAdminController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ assetExtendedAttributeInstanceList: AssetExtendedAttribute.list( params ), assetExtendedAttributeInstanceTotal: AssetExtendedAttribute.count() ]
    }

    def show = {
        def assetExtendedAttributeInstance = AssetExtendedAttribute.get( params.id )

        if(!assetExtendedAttributeInstance) {
            flash.message = "AssetExtendedAttribute not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ assetExtendedAttributeInstance : assetExtendedAttributeInstance ] }
    }

    def delete = {
        def assetExtendedAttributeInstance = AssetExtendedAttribute.get( params.id )
        if(assetExtendedAttributeInstance) {
            try {
                assetExtendedAttributeInstance.delete()
                flash.message = "AssetExtendedAttribute ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "AssetExtendedAttribute ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "AssetExtendedAttribute not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def assetExtendedAttributeInstance = AssetExtendedAttribute.get( params.id )

        if(!assetExtendedAttributeInstance) {
            flash.message = "AssetExtendedAttribute not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ assetExtendedAttributeInstance : assetExtendedAttributeInstance ]
        }
    }

    def update = {
        def assetExtendedAttributeInstance = AssetExtendedAttribute.get( params.id )
        if(assetExtendedAttributeInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(assetExtendedAttributeInstance.version > version) {
                    
                    assetExtendedAttributeInstance.errors.rejectValue("version", "assetExtendedAttribute.optimistic.locking.failure", "Another user has updated this AssetExtendedAttribute while you were editing.")
                    render(view:'edit',model:[assetExtendedAttributeInstance:assetExtendedAttributeInstance])
                    return
                }
            }
            assetExtendedAttributeInstance.properties = params
            if(!assetExtendedAttributeInstance.hasErrors() && assetExtendedAttributeInstance.save()) {
                flash.message = "AssetExtendedAttribute ${params.id} updated"
                redirect(action:show,id:assetExtendedAttributeInstance.id)
            }
            else {
                render(view:'edit',model:[assetExtendedAttributeInstance:assetExtendedAttributeInstance])
            }
        }
        else {
            flash.message = "AssetExtendedAttribute not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def assetExtendedAttributeInstance = new AssetExtendedAttribute()
        assetExtendedAttributeInstance.properties = params
        return ['assetExtendedAttributeInstance':assetExtendedAttributeInstance]
    }

    def save = {
        def assetExtendedAttributeInstance = new AssetExtendedAttribute(params)
        if(!assetExtendedAttributeInstance.hasErrors() && assetExtendedAttributeInstance.save()) {
            flash.message = "AssetExtendedAttribute ${assetExtendedAttributeInstance.id} created"
            redirect(action:show,id:assetExtendedAttributeInstance.id)
        }
        else {
            render(view:'create',model:[assetExtendedAttributeInstance:assetExtendedAttributeInstance])
        }
    }
}
