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

class MaintenanceActionController 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)
        [ maintenanceActionInstanceList: MaintenanceAction.list( params ), maintenanceActionInstanceTotal: MaintenanceAction.count() ]
    }

    def show = {
        def maintenanceActionInstance = MaintenanceAction.get( params.id )

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

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

    def edit = {
        def maintenanceActionInstance = MaintenanceAction.get( params.id )

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

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

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

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