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

@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager'])
class AssetSubItemExtendedAttributeDetailedController extends BaseController {

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

    def index = { redirect(controller: "assetDetailed", action: "search", params:params) }

    def show = {
        def assetSubItemExtendedAttributeInstance = AssetSubItemExtendedAttribute.get( params.id )

        if(!assetSubItemExtendedAttributeInstance) {
            flash.message = "AssetSubItemExtendedAttribute not found with id ${params.id}"
            redirect(controller: "assetDetailed", action: "search")
        }
        else { return [ assetSubItemExtendedAttributeInstance : assetSubItemExtendedAttributeInstance ] }
    }

    def delete = {
        def assetSubItemExtendedAttributeInstance = AssetSubItemExtendedAttribute.get( params.id )
        if(assetSubItemExtendedAttributeInstance) {
            def assetSubItem = assetSubItemExtendedAttributeInstance.assetSubItem
            try {
                assetSubItemExtendedAttributeInstance.delete(flush:true)
                flash.message = "AssetSubItemExtendedAttribute ${params.id} deleted"
                redirect(controller: "assetSubItemDetailed", action: 'show', id: assetSubItem.id)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "AssetSubItemExtendedAttribute ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "AssetSubItemExtendedAttribute not found with id ${params.id}"
            redirect(controller: "assetDetailed", action: "search")
        }
    }

    def edit = {
        def assetSubItemExtendedAttributeInstance = AssetSubItemExtendedAttribute.get( params.id )

        if(!assetSubItemExtendedAttributeInstance) {
            flash.message = "AssetSubItemExtendedAttribute not found with id ${params.id}"
            redirect(controller: "assetDetailed", action: "search")
        }
        else {
            return [ assetSubItemExtendedAttributeInstance : assetSubItemExtendedAttributeInstance ]
        }
    }

    def update = {
        def assetSubItemExtendedAttributeInstance = AssetSubItemExtendedAttribute.get( params.id )
        if(assetSubItemExtendedAttributeInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(assetSubItemExtendedAttributeInstance.version > version) {
                    
                    assetSubItemExtendedAttributeInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
                    render(view:'edit',model:[assetSubItemExtendedAttributeInstance:assetSubItemExtendedAttributeInstance])
                    return
                }
            }
            assetSubItemExtendedAttributeInstance.properties = params
            if(!assetSubItemExtendedAttributeInstance.hasErrors() && assetSubItemExtendedAttributeInstance.save(flush: true)) {
                flash.message = "AssetSubItemExtendedAttribute ${params.id} updated"
                //redirect(action:show,id:assetSubItemExtendedAttributeInstance.id)
                redirect(controller: 'assetSubItemDetailed', action: 'show', id: assetSubItemExtendedAttributeInstance.assetSubItem.id)
            }
            else {
                render(view:'edit',model:[assetSubItemExtendedAttributeInstance:assetSubItemExtendedAttributeInstance])
            }
        }
        else {
            flash.message = "AssetSubItemExtendedAttribute not found with id ${params.id}"
            redirect(controller: "assetDetailed", action: "search")
        }
    }

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

    def save = {
        def assetSubItemExtendedAttributeInstance = new AssetSubItemExtendedAttribute(params)
        if(!assetSubItemExtendedAttributeInstance.hasErrors() && assetSubItemExtendedAttributeInstance.save(flush: true)) {
            flash.message = "AssetSubItemExtendedAttribute ${assetSubItemExtendedAttributeInstance.id} created"
            redirect(controller:'assetSubItemDetailed', action:'edit',id:assetSubItemExtendedAttributeInstance.assetSubItem.id)
        }
        else {
            render(view:'create',model:[assetSubItemExtendedAttributeInstance:assetSubItemExtendedAttributeInstance])
        }
    }
}
