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

@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager'])
class TaskGroupDetailedController 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)
        [ taskGroupInstanceList: TaskGroup.list( params ), taskGroupInstanceTotal: TaskGroup.count() ]
    }

    def show = {

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

        def taskGroupInstance = TaskGroup.get( params.id )

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

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

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

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

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

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