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

class SupplierController 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)
        [ supplierInstanceList: Supplier.list( params ), supplierInstanceTotal: Supplier.count() ]
    }

    def 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()
                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 = {
        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", "supplier.optimistic.locking.failure", "Another user has updated this Supplier while you were editing.")
                    render(view:'edit',model:[supplierInstance:supplierInstance])
                    return
                }
            }
            supplierInstance.properties = params
            if(!supplierInstance.hasErrors() && supplierInstance.save()) {
                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:edit,id:params.id)
        }
    }

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

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