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

@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager'])
class SupplierDetailedController 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)
        [ supplierInstanceList: Supplier.list( params ), supplierInstanceTotal: Supplier.count() ]
    }

    def show = {

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

        def supplierInstance = Supplier.get( params.id )

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

    def delete = {
        def supplierInstance = Supplier.get( params.id )
        if(supplierInstance) {
            try {
                supplierInstance.delete(flush:true)
                flash.message = "Supplier ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Supplier ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Supplier 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 supplierInstance = Supplier.get( params.id )

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

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

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

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