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

@Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
class ExtendedAttributeTypeDetailedController extends BaseController {
    
    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)
        [ extendedAttributeTypeInstanceList: ExtendedAttributeType.list( params ), extendedAttributeTypeInstanceTotal: ExtendedAttributeType.count() ]
    }

    def show = {

        // In the case of an actionSubmit button, rewrite action name from 'index'.
        if(params._action_Show)
            params.action='show'

        def extendedAttributeTypeInstance = ExtendedAttributeType.get( params.id )

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

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

    def edit = {

        // In the case of an actionSubmit button, rewrite action name from 'index'.
        if(params._action_Edit)
            params.action='edit'

        def extendedAttributeTypeInstance = ExtendedAttributeType.get( params.id )

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

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

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

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